tags:

views:

44

answers:

3

I have an H3 heading that I'd like to style as having a particular background color, but without having the element's background take up the full width of the parent element. Seeing as H3 is by default a block element, my style would need to change the element to an inline-block element, or just an inline inline element like so:

h3 {
    background-color: #333;
    color: white;
    display: inline-block;
}

This will work fine, but only if it is immediately followed by a block element. I do not want to change the markup just to cater for this style, so I was wondering if there is a way to cause any adjacent element, irrespective of how it displays, to start on the next line?

Assume I can use CSS3.

+3  A: 
display:block;
width:auto;

This will make the width as small as possible (not filling the whole parent element) and make other elements appear below.

fredley
*Other than making it a block element. At least read the title.
Alexander
Sorry, I read 'without' as 'with', my bad. I've updated answer, still think `block` is the correct way to do this.
fredley
Actually, this is what was asked for. The OP wanted something that works in the layout as a block element (it lives by itself vertically) but restricts the background area to the width of the content. The question asked for a "no block" solution because the OP didn't know that a block element could act that way.
Stan Rogers
@Stan how about a vote then? :-P
fredley
This doesn't work for me in Chrome. The h3 is still as wide as the container.
Litso
Good point -- write it in the "n00bism" column.
Stan Rogers
@Stan - that is very true. @fredley - Good suggestion, however setting an auto width on a block element simply tells it that it is ok to obey its default behaviour, which is to fill the width of the parent element. I tested it in Chrome anyway, which confirmed this.
Nathan Ridley
I guess then that this would best be solved by modifying your html, making h3 `inline` and adding a line break after it, there isn't a pure css, reliable solution.
fredley
+1  A: 

How often does it happen that the element after the <h3> is an inline element? (Usually after a header there should be like a <p>, <ul> or other block elements, although this totally depends on your html. Is it predictable? Is it an option to just turn every element that directly follows a <h3> into a block element?

h3 ~ * { display: block }

The only other way I know to have a block-element not take up all the space is floating it, but this leaves another problem.

Litso
The element after the H3 could easily be an inline-block element, or it could be floated left, which would sit it on the same line as the H3. Also, by forcing the adjacent element to be a block element, you are overriding how it interacts with its own following siblings, which may have unintended consequences.
Nathan Ridley
+3  A: 

try this:

h3:after {
  content: ".";
  display: block;
  height: 0;
  clear: both;
  visibility: hidden;
}
PeterWong
+1 for inventiveness, but doesn't work for me in Chrome
Litso
`:after` has very patchy support
fredley
Actually I think this sort of thing is quite common, particularly with float clearing. It works for me in Chrome. @Litso - you sure you did it right?
Nathan Ridley