In the standard document flow, for a block element that does not have a width set, when it has content or a height is set, its own width is 100% of the width of the parent element. At this time, set the horizontal margin value for the block element. Will change the visual width of the element. But the height cannot be changed, because the height of the block element is fixed or equal to the height of its content and will not stretch.
This feature is useful in bootstrap layout.
<p class="wrap"> 父元素padding: 0 10px; <p class="yellow">此block元素没有设置margin值</p> <p class="red">此block元素设置margin-left:-10px;margin-right:-10px; 拉伸了元素宽度</p></p>
* {margin:0; padding:0;}.wrap { width: 400px; height: 400px; margin: 50px auto; padding: 0 10px; border: 1px solid #ccc; }.red { height: 100px; background-color: red; margin: 0 -10px; }.yellow { height: 100px; background-color: yellow; }
<p class="wrap"> 父元素position: relative; <p class="yellow">绝对定位,并且设置top:0; bottom:0; 垂直拉伸元素,通过设置margin来改变可视高度</p></p>
* {margin:0; padding:0;}.wrap { position: relative; width: 400px; height: 400px; margin: 50px auto; border: 1px solid #ccc; }.yellow { position: absolute; top: 0; bottom: 0; width: 100px; margin: 50px 0; background-color: yellow; }
##1. Percentage The margin of the value is the same as padding. For elements that are normally in the standard document flow, when the margin value of the element is a percentage value, its actual value is equal to the width of the parent element * percentage;
<p class="wrap"> 父元素宽度400px; <p class="yellow">margin-top:10%; 实际的margin-top=400px*10%=40px</p></p>
* {margin:0; padding:0;}.wrap { width: 400px; height: 400px; margin: 50px auto; border: 1px solid #ccc; }.yellow { width: 200px; height: 200px; margin-top: 10%; background-color: yellow; }
Margin overlap usually occurs between sibling elements and between parent and child elements. If you don’t pay attention to it, margin Overlap may often cause us some minor troubles. Here is a summary of the situations where margin overlap occurs.
When two elements have margin overlap, their actual values are:
When two margin values When both are positive, the actual value = the larger of the two values
When the two margin values are one positive and one negative, the actual value = the addition of the two values
When both margin values are negative, the actual value = the larger absolute value of the two
When will margin overlap occur?
2>Between the parent element and the first/last child element
Set padding-top/bottom
Set attributes such as overflow:hidden/auto to implement the auto value of BFC
3.margin
It feels a bit confusing. For example, there is a block element with a fixed width. We want it to be displayed on the right side. The most commonly used method is to use the float attribute, but it can also be achieved with margin-left: auto;:
<p class="wrap"> <p class="red">margin-left: auto;</p></p>
* {margin:0; padding:0;}.wrap { position: relative; width: 400px; height: 400px; margin: 50px auto; border: 1px solid #ccc; }.red { width: 100px; height: 100px; background-color: red; margin-left: auto; }
Ordinary fixed-width block elements, the horizontal direction setting is valid;
Absolutely positioned elements, when paired left/right, top are set When /bottom or both are set, fixed width or fixed height is valid;
#When the parent element is display: flex;, the child element margin value is auto
For more CSS attribute margin related articles, please pay attention to the PHP Chinese website!