vertically stacked divs have space between them (f

2019-06-27 21:48发布

a little css problem that i cannot quite find on SO - although I assume it has been asked before, apologies.

So, here is the html:

<html>
    <body style="color:white">
        <div class="a" style="width: 70%; background: blue;"><p>helloes helloes helloes</p></div>
        <div class="b" style="width: 70%; background: pink;"><p>talk talk talk</p></div>
        <div class="a" style="width: 70%; background: blue;"><p>yay! yay! yay!</p></div>
    </body>
</html>

lovely.

If i open this in ff, i get three vertically stacked divs - but with space in between them! This is not what i wanted! Drama-rama!

ie renders this as i'd expect, which raises some alarm bells.

ie is 9, ff is 11

cheers, andrew!

UPDATE a lot of mentioning the "p" tag - why/how is the p tag affecting anything? Isn't it wrapped by the div, and the div has the background color applied? Shouldn't, in fact, the div just be internally bigger, but with no space between adjacent divs?

UPDATE:

So i tried this html instead:

<html style="margin:0px; padding:0px;">

which didn't fix the issue, and also this:

<body style="color: white; margin:0px; padding:0px;">

which also didn't fix the issue - shouldn't the css be inherited by the "p" tag in both cases? Interestingly, i also examined the resultant css with firebug, and the p tags all have a margin and padding of 0...

ideas?

UPDATE: a lot of responses asking me to set padding to 0. This doesn't work. Any more answers stating that and i'll down vote 'em.

UPDATE: the question is really specific about using inline css. I don't actually care for inline css myself, but why is everybody providing css stylesheets for their answer?

UPDATE: somebody mentioned -webkit, and while i'm not using a google chrome at all, it is an interesting idea. I cannot see any ff related extra css that might be causing this problem, anybody have any ideas?

5条回答
甜甜的少女心
2楼-- · 2019-06-27 22:09

This is because of the auto-generated margin of a <p> element.

Firefox (and others) do this differently than IE. You can "reset" this simply by doing a
p{margin: 0} in your css.

You can do the same for all elements at once (which I recommend) by simply adding
* { margin: 0; padding: 0;} in your css.

Small tip: Install a browser extension to inspect the behavior of your elements such as Firebug.

查看更多
爷、活的狠高调
3楼-- · 2019-06-27 22:15

Your <p> tags have vertical margins. Vertical margins in CSS collapse, so that child margins can sometimes apply to parents. See http://www.w3.org/TR/CSS21/box.html#collapsing-margins

查看更多
一夜七次
4楼-- · 2019-06-27 22:21

Basically the P tags are by default taking margin. Add css

p{margin:0px; padding:0px;}
查看更多
做个烂人
5楼-- · 2019-06-27 22:23

I resolved this be specifying a CSS 'line-height' I just set it to the same as the font size and then I got consistent DIV spacing across all browsers.

查看更多
Melony?
6楼-- · 2019-06-27 22:30

I tried it with Chrome and saw the same behavior. After looking at the underlying CSS (F12), Chrome is applying the following two lines to the <p> tag:

-webkit-margin-before: 1em;
-webkit-margin-after: 1em;

If I add the following to the css the blank lines go away:

-webkit-margin-before: 0px;
-webkit-margin-after: 0px;

Hope that helps!

查看更多
登录 后发表回答