I use this in visual studio but the compiler claimed that h1
cannot be nested in legend
element, but browser can render it anyway so i am confused
The legend element of the fieldset is not designed to allow child elements unless they are considered "phrasing content". While most browsers will not complain (thus making it valid in pratice) you would probably be better off using a style to set the look/feel of your legend element as that is what css is for, elements should be used for logically grouping/identifing content not styling it.
Legend: http://www.w3.org/TR/html5/forms.html#the-legend-element
Phrasing Content: http://www.w3.org/TR/html5/content-models.html#phrasing-content