Forum Moderators: not2easy
I'm trying to position an image to the left of some text and have the whole area shaded with a background colour. Fairly trivial using tables, but having tried endless things I just can't get it working with CSS. The version below works except for the background colour not extending the full height of the image. Please help!
*************** HTML *****************
<div id="prodRecommends">
<h1>You might also be interested in...</h1>
<img src="images/thumbnail.jpg" width="100" height="100">
<h1>This is the title</h1>
<h1>This is the price</h1>
<div class="extra">This is extra info</div>
</div>
*************** CSS *****************
#prodRecommends {
background-color: #e8f1dc;
font: 0.8em/1.5em arial, helvetica, sans-serif, verdana;
}
#prodRecommends h1 {
font-size: 1.3em;
font-weight: bolder;
color: #003300;
margin: 0;
}
#prodRecommends img {
float: left;
border: 1px solid black;
}
[w3.org...]
Since a float is not in the flow
Hence the image is not part of the main div anymore, I think. (Though it starts off aligned to the top of the div.)
Also you may want to reconsider adding the clear:left to the the extra div, it means nothing can float to the left of it so it will start a new line below the image div.
<edit>
I think you also need to declare a position and width for floated elements.
</edit>
Try this:
<style>
#prodRecommends {
background-color: #e8f1dc;
font: 0.8em/1.5em arial, helvetica, sans-serif, verdana;
width: 100%;
height: 150px;
}
#prodRecommends h1 {
font-size: 1.3em;
font-weight: bolder;
color: #003300;
margin: 0;
}
#prodRecommends img {
float: left;
width:100px;
height:100px;
margin: 6px;
border: 1px solid black;
}
</style>
and this in the html:
<div id="prodRecommends">
<h1>You might also be interested in...</h1>
<img src="images/thumbnail.jpg" alt="Description of product">
<h2>This is the title</h2>
<h2>This is the price</h2>
<p>This is extra info</p>
</div>
On many websites images are generated dynamically and it is impossible to put a general width value into the css.
To me, it sounds like you would like the containing div around your text and image to stretch all the way down. A floating element does not stretch its containing div, that is why you need to clear the float before you close the containing div.
I dont really see the point in specifying a height on the containing div, since it prevents the div to grow dynamically in height when f.e a user resizes the text.
If you put clear:left in your .extra style, the markup that are inside the .extra div will come below the clear. To solve the problem, you should simply add another div like so:
<div id="prodRecommends">
<h1>You might also be interested in...</h1>
<img src="images/thumbnail.jpg" width="100" height="100">
<h1>This is the title</h1>
<h1>This is the price</h1>
<div class="extra">This is extra info</div>
<div style="clear: left;"></div>
</div>
I hope this makes sence.