Reputation: 4330
I have a web page like this.
<html>
<head></head>
<body>
<div style="background:blue">
<h3 style="background:green">Hello world.</h3>
</div>
</body>
</html>
When I analyze the output in chrome, it seems that the h3 tag is taking more space than the div tag. I want the div tag to completely include the h3 tag, and the background color of div to be shown in the entire area. Any idea how to do this?
Upvotes: 2
Views: 1841
Reputation: 2437
<h3 style="background:green;margin:0;">Hello world.</h3>
By default h3 has a margin associated with it. So you have to add a margin:0 to the h3 tag.
Upvotes: 2
Reputation: 8981
Like this
CSS
.div1{
background-color:blue;
float:left;
}
h3{
margin:0;
padding:0;
background:green;
}
Upvotes: 3
Reputation: 1003
Most simple solution I see, add overflow: hidden;
to the enclosing div.
Upvotes: 2
Reputation: 4867
Taken a css reset? This set all to default values.
http://meyerweb.com/eric/tools/css/reset/
Upvotes: 2
Reputation: 11303
The reason this is happening is that some elements have browser styling by default, that is why you should always use a css reset:
if you float the div it will wrap around the element, and set the margin of the h3 to 0.
<div style="background:blue;float:left;">
<h3 style="background:green;margin:0;">Hello world.</h3>
</div>
For the div to take the entire screen's size remove the float.
<div style="background:blue;">
<h3 style="background:green;margin:0;">Hello world.</h3>
</div>
Upvotes: 3
Reputation: 14428
Set a font-size
and line-height
on the h3
like so:
h3 {
font-size: 16px;
line-height: 1em; }
Upvotes: 2