HTML : Text after empty bold tag also displayed in bold formatting

Sambhaji picture Sambhaji · Sep 16, 2011 · Viewed 12.7k times · Source

I am trying to understand why browser behaves like this.

I have following text in html document.

<html>
<body>
This is Sample Text. <B/>Text after empty bold tag.
</body>
</html>

If I view this document in browser, it is displayed as shown below.

This is Sample Text. Text after empty bold tag.

Why is this happening? Afterall, I did not mark any text as bold.

Answer

Nachshon Schwartz picture Nachshon Schwartz · Sep 16, 2011

You need to write valid html code for this to work correctly with opening and closing tags. So you would need to write an opening tag (<B>) before the text you want in bold and a closing tag () after the text.

<html>
    <body>
        <B>This is Sample Text.</B>Text after empty bold tag.
    </body>
</html>

Notice that in your example you are using <B/> which is neither a valid opening or closing tag but which may be interpreted as an opening tag in certain browsers.