Get the App
SLTechnology News&Howtos  ›  Development  › 

The method of transferring and folding up and down margin in CSS

Shulou Source: shulou.com Published: 2022-06-01 05:56:16 10月03日 Update

This article introduces the relevant knowledge of "the transmission and folding method of upper and lower margin in CSS". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1.margin-top transfer

Why does the upper margin pass?

If the top line of the block-level element overlaps the top line of the parent element, the margin-top value of the block-level element is passed to the parent element.

Sample code: set margin-top: 20px; for the inner box.

.reference {

Width: 100px

Height: 100px

Background-color: # f00

Color: # fff

}

.box {

Width: 200px

Height: 200px

Background-color: # 0f0

}

.inner {

Width: 100px

Height: 100px

Background-color: # 00f

Margin-top: 20px

}

Reference box

The value of margin-top for inner is passed to box.

2.margin-bottom transfer

Why does the lower margin pass occur?

If the bottom line of the block-level element overlaps the bottom line of the parent element, and the height of the parent element is auto, then the margin-bottom value of the block-level element is passed to the parent element.

Sample code: set margin-bottom: 20pxscape for the inner box and height: auto; for the parent element.

.box {

Width: 200px

Height: auto; / * sets the height of the parent element to auto, or no height. The default is auto * /

Background-color: # 0f0

}

.inner {

Width: 100px

Height: 100px

Background-color: # 00f

Margin-bottom: 20px

Color: # fff

}

.reference {

Width: 100px

Height: 100px

Background-color: # f00

Color: # fff

}

Inner

Reference box

The value of margin-bottom for inner is passed to box.

This is the end of the introduction of "the method of passing and folding up and down margin in CSS". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Elements boxes height up and down methods code content bottom more knowledge examples top reference practical learned and then above dilemma actual situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux vpn MariaDB Docker NVidia