How can I place two headings next to each other using CSS?
Asked Answered
P

3

11
<h5>Category</h5><h6>auto</h6>

places Category and auto on separate lines, like this:

Category

auto

How can I place them both on the same line, like this?

Category auto

Pascoe answered 18/8, 2009 at 14:39 Comment(0)
C
29

h(n) elements are 'block' elements, which means they will grow to take all available horizontal space. This also means they will push anything "right" of them down to the next line.

One easy way to accomplish this is to set their display to inline:

<style>
    h5, h6 {display:inline;}
</style>

Note that inline-block is not supported in all browsers.

You can also float block elements, but that can become a sticky issue as floating can be fairly complex. Stick with inline for cases like this.

Correll answered 18/8, 2009 at 14:42 Comment(2)
Can you tell which browsers are not supporting the inline-block display mode?Rochkind
@Rochkind IE only supports it for elements which are natively inline; FF2 does not support it at all.Correll
S
7
<h5 style="display:inline-block;">Category</h5>
<h6 style="display:inline-block;">auto</h6>
Shiv answered 18/8, 2009 at 14:40 Comment(4)
inline-block is not supported in all browsers.Correll
This is possible, however using inline styles is not a recommended way of working. Your solution is not wrong, but I think it is better to show a much nicer way of working by using stylesheets or a style definition on top of the document.Rochkind
for IE, 'inline-block' is restricted to <span> <strong> <em>Xmas
@ertjan: I agree. It was just a minimalistic version to communicate the idea.Shiv
R
4

You must change the display mode of the elements. H tags are rendered as BLOCK elements by default. To override this behavior add the following style definitions to your website or CSS

h5,h6 { display: inline; } 

You can also decide to let them "float" next to each other you can do that via:

h5,h6 { float: left; } 

Please note that floating only works on block elements (so using both styles will perform no float because inline elements cannot float).

Rochkind answered 18/8, 2009 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.