Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Block element's background image goes behind float element

I have a slight problem with a background image on a block element that is preceded by a floating element.

I float an image to the left, followed by a H1. As expected, the H1 (which is a blocl-level element) flows behind the image, but it's contents (the actual title) appear to the right of the image.

Unfortunately, the background-image I'm using on the H1 has to be aligned to the left, and thus appears behind the actual img, because unlike the contents this is not pushed by the floating behaviour.

Example:

http://jsfiddle.net/WwuqG/

(I set the second title to clear: left to show what it should look like).

One solution is to set the left-margin of the title to a little more than the floating image's width, but that would require me to know it's width beforehand.

Another option is adding the title's icon in an element inside the h1, but that's not semantically correct.

Is there a better css-only solution that doesn't require additional elements?

like image 563
Stephan Muller Avatar asked Dec 05 '25 16:12

Stephan Muller


2 Answers

add overflow:hidden to the h1

new fiddle

like image 150
clairesuzy Avatar answered Dec 08 '25 06:12

clairesuzy


I'm slightly confused.

If I do what you suggested:

set the left-margin of the title to a little more than the floating image's width

It looks like this: http://jsfiddle.net/WwuqG/1/

My confusion comes from the fact that your problem seems to be.. really simple to fix.

  • Also add float: left to the <h1>: http://jsfiddle.net/WwuqG/2/

This works with whatever width image: http://jsfiddle.net/WwuqG/3/

Is that it, or have I misunderstood?

like image 29
thirtydot Avatar answered Dec 08 '25 07:12

thirtydot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!