[Frontend] css stepping on the pit: the outer margin of the empty element does not take effect, and the inner margin takes effect

Margins on empty elements do not take effect

The height, border, and padding of an empty element are all 0, and the top and bottom margins are 20px, so the height of the box model of this empty element is 0

html:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>文档标题</title>
</head>
<body>
  <div class='a'>1</div>
  <div class='b'>2</div>
</body>
</html>

css:

.a {
    
    
  margin:20px;
  background-color:red;
 }
.b {
    
    
  margin:20px;
  background-color:green;
  }

Effect: the content takes effect

insert image description here
No content will not take effect .
insert image description here

The padding of empty elements takes effect

The height, border, and outer margin of an empty element are all 0, and the upper and lower inner margins are 20px, so the height of the box model of this empty element is 40px

css:

.a {
    
    
  padding:20px;
  background-color:red;
 }
.b {
    
    
  padding:20px;
  background-color:green;
  }

insert image description here

reference

Page layout model
online html tool

Guess you like

Origin blog.csdn.net/karshey/article/details/130529595
Recommended