13

I want to do something like this:

<p>This is a <h2>text</h2> paragraph.</p>

I disabled margin and padding for the h2 but it still breaks the line before and after the h2 tag. How can I use the h2 tag in the middle of a text and make it look like if it was a normal word, just like < b > does?

The doctype of my html document is "XHTML 1.0 Transitional"

Thomas Mueller
  • 48,905
  • 14
  • 116
  • 132
heresma
  • 293
  • 2
  • 4
  • 14

7 Answers7

30

It is not valid to have a h2 inside of a p.

Aside from that, and to answer your question, a h2 is a block level element. Making it an inline level element will cause it to behave similarly to how you describe the b tag acting.

p h2{display:inline}

As I said above though, the HTML you've given is invalid.

Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
18

It's not appropriate to use a tag that means "heading" within body text. The <h..> tags are logical tags; their use imparts meaning to the enclosed text -- namely, that the text is a section heading.

Although you could use the display: inline attribute, consider using a more appropriate tag, or even a <span> tag.

Barry Brown
  • 20,233
  • 15
  • 69
  • 105
  • What if you place it at the beginning of the paragraph as a subheading? Would it still be better to use a tag? Example:

    Chapter 1

    Part I.

    text text text text text
    – digbyterrell Mar 27 '14 at 13:18
  • @AlexWilliams I know this was a while ago, but I think the proper use would be `

    Chapter 1

    Part I

    text...

    ` in this case.
    – eritbh Mar 24 '15 at 21:30
  • You can't use `display: inline`. The h2 element will implicitly terminate the p element. – Quentin Apr 20 '15 at 15:00
3

Don't, the whole point of is that it's a header. Headers are on their own line. Instead, use CSS. Say text and then in a CSS file, choose a font size.

codersarepeople
  • 1,971
  • 3
  • 20
  • 25
  • I’m gonna be a bit to close on the words here, but the h stands for headline, not header. Header is something different and headlines are in wider use (not just as a header). Anyway, don’t take this comment to serious … One could say every headline is a header for something. But when I hear header I’m thinking about a website header, more of a layout element. – Kissaki Jan 13 '11 at 01:44
  • Yeah sorry, you're technically right. – codersarepeople Jan 13 '11 at 01:48
  • 4
    @Kissaki: [From the horse's mouth](http://www.w3.org/TR/html401/struct/global.html#h-7.5.5), they're called headings. I'm even more pedantic! :P – BoltClock Jan 13 '11 at 01:50
1

Despite of the wrong use of the <h2> tag inside a paragraph, we can style <h2> to keep him into a paragraph. I tested one of the above css trick in Firefox and Chrome as follow:

HTML code <p>One paragraph with <h2>title text</h2> in the middle</p>

CSS trick p h2{display:inline}

BUT it doesn't get the result I expect. The browser truncate the paragraph right before the initial h2 tag. Look at DOM from Firebug:

enter image description here

Therefore the CSS trick p h2{display:inline} doesn't work properly because CSS rule is not true, i.e. the <h2> tag is not inside the <p> tag. It looks like this:

enter image description here

Adding CSS trick only for <h2> tag h2{display:inline} is not the definitive solution. It will look like this:

enter image description here

The final work-around is:

HTML code <p class="inline-object">One paragraph with <h2 class="inline-object">title text</h2> in the middle</p>

CSS trick .inline-object" {display:inline}

This will look as we expect:

enter image description here

If you are trying to mask <h2> title text as ordinary text, just add two more rules to the class .inline-object" {display:inline;font-size: inherit;font-weight: normal;}

Delmo
  • 2,188
  • 2
  • 20
  • 29
1

You’ll have to make it

display:inline;

h2 is a block element.

The h2 tag marks a headline, which is per definition not part of a text. So what you’re doing is probably not the best way. Consider doing it differently.

Kissaki
  • 8,810
  • 5
  • 40
  • 42
0

Firefox cutting out my P, because of H2 inside it. And then creating P for

(empty paragraph). And its good. Because its auto-generated, and for cleaning out P tags i need to write tens of PHP code.
Vova Popov
  • 1,053
  • 11
  • 11
0

Create a class like .myH2 (not the best name) with same code of h2 and use a span in p like

<p>this is <span class="myH2">myH2</span>.</p>
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Henrique Gonçalves
  • 1,572
  • 3
  • 16
  • 27