Impact of border property on top margin

2020-02-13 05:12发布

Refer to the following code:

body {
  height: 500px;
  width: 80%;
  margin-top: 0;
  margin-bottom: 0;
  margin-left: auto;
  margin-right: auto;
  padding: 0;
  background-color: lightgray;
}
.header {
  width: 80%;
  height: 100px;
  margin-left: auto;
  margin-right: auto;
  background-color: yellow;
  /* border: solid 1px black; */
}
<div class="header">
  <ul>
    <li><a href="index.html">Dashboard</a></li>
  </ul>
</div>

When I remove border: solid 1px black from .header I get some space above div element. But when I add the border property I get the perfect results (that is space removed). What should be impact of border property on this space?

标签: html css margin
3条回答
一夜七次
2楼-- · 2020-02-13 05:50

Problem is your ul tag is not clear

body{
  height: 500px;
  width: 80%;
  margin-top: 0;
  margin-bottom: 0;  
  margin-left: auto;
  margin-right: auto;
  padding: 0;
  background-color: lightgray; 
}

.header {
  width: 80%;
  height: 100px;
  margin-left: auto;
  margin-right: auto; 

  background-color: yellow; 
  /*border: solid 1px black;*/  
}
ul{float: left;}
<!DOCTYPE html> 
<html>
  <head>   
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    <link href="css/style.css" type="text/css" rel="stylesheet"/>
  </head>
  <body>
    <div class="header">
      <ul>
    <li><a href="index.html">Dashboard</a></li>
      </ul>
    </div>
  </body>
</html> 

查看更多
放我归山
3楼-- · 2020-02-13 06:05

This is due to margin collapsing (MDN, W3.org).

In your example the header element contains an unordered list which, by default, has a top and bottom margin. Because of border collapsing, its margin is transferred to the header which in turn is transferred to the body. As a result, the body is pushed down while the header and list aligns with the top of body.

top: margin of ul, bottom: margin of body

Adding a border is one method to prevent margin collapsing (see W3 specs). If you want to avoid adding a border, use the other methods such as assign overflow: hidden to the header.

body {
  height: 500px;
  width: 80%;
  margin-top: 0;
  margin-bottom: 0;
  margin-left: auto;
  margin-right: auto;
  padding: 0;
  background-color: lightgray;
}
.header {
  width: 80%;
  height: 100px;
  margin-left: auto;
  margin-right: auto;
  background-color: yellow;
  /*border: solid 1px black;  */
  overflow: hidden;
}
<div class="header">
  <ul>
    <li><a href="index.html">Dashboard</a></li>
  </ul>
</div>

查看更多
一纸荒年 Trace。
4楼-- · 2020-02-13 06:09

Borders should not affect margins (edit: except here because of margin collapsing, see @salman-a's answer. My bad. But you should still use a CSS reset).

I think you need some sort of CSS reset

One I find usefull for quick testing is the simplest of all (but not recommended to use in other cases, beacause it is way to simple):

*{padding:0;margin:0}

This will clear all browser padding/margin.

I think the problem here is you did not reset your header and ul margins, and the browser adds them (browser do that in order to display stuff nicely, even if there is no CSS).

查看更多
登录 后发表回答