CSS layout-negative margin-margin

Source: Internet
Author: User

The negative margin (negative margin) in CSS is a common technique in layout, so long as it is used properly, it often has unexpected effects. Many special CSS layout methods rely on negative margins, so mastering its usage is a must for the front-end students. This article is very basic, the veteran can skip over.

The effect and effect of negative margin in normal document flow

Those elements that are not out of the document flow (meaning that not floating elements are not absolute positioning, fixed positioning elements, etc.), whose position in the page is the change of the follower's document flow. Look at the following picture:

Negative margins affect these document flow-controlled elements by offsetting their position in the document flow, but this offset is different from relative positioning, and by relative positioning the offset will still hold the space it originally occupies and will not allow other elements of the document flow to take advantage. An element that is offset by a negative margin discards the space occupied before the offset, so that other elements in the document flow behind it will "flow" to fill the space. Let's illustrate it by example. Now we have a negative margin margin:-10px in the block element, the inline element, and the Inline-block element. See what happens:

As we can see, the black-gray block elements seem to be embedded in the bounds of the browser window to the left and the top 10px, and then the text below the block element has climbed to it, and the element to the left covers a word in front of it, and the text behind it is partly covered in it, The change of position of Inline-block is also obvious.

Well, I believe it is clear that the negative margin seems to reduce the size of the element in the document flow, but in fact, its size does not change, but the document flow in the calculation of the position of the element, the negative margin will be considered to reduce the size of the element, because the location has changed. It's just a very figurative metaphor to help you understand. Also note that the document flow can only be the back of the flow ahead, that is, the document flow can only flow to the left or upward, cannot move down or to the right.

So, as long as everything is determined by the flow of documents, negative margins can work.

For example, a block element that does not have a height set, its height is automatic, in particular it is determined by the final position of the document flow inside it. Suppose that there is a child element in the document flow, height is 100px, then the height of the parent element is equal to the height of the child element 100px, if the child elements continue to increase, then the parent element will also be increased. However, if the child element is set to a negative margin-bottom, such as -20px, because the negative margin will affect the document flow, the height of the document flow is from the top of the parent element to the bottom of the child element, and now the child element has a margin-bottom:-20px; It is equivalent to the document flow to back up 20px, so that the entire document flow height is reduced by 20px, then the height of the parent element will also be reduced 20px. In ie8+ and those standard browsers, this also requires the parent element to have a Overflow:hidden property, because the parent element's height is changed, but the height of the child element is not changed, so the child element needs to be out of hiding, but even if the Overflow:hidden is not set, The height of the parent element is also smaller, except when the child element's height exceeds the parent element. It is not required in IE6, but it needs to trigger its Haslayout property. So the above-mentioned multi-column high-level layout is the use of this principle to achieve.

In a word, in a document flow, the final boundary of an element is determined by margin, and margin is negative when it is equal to the boundary of the element, and the document flow is just this boundary, not the actual size.

The influence of the left and right negative margins on the width of the element

Negative margins not only affect the position of the element in the document flow, but also increase the width of the element!

The premise of this effect is that the element does not set the Width property (of course width:auto is possible).

For example, the black-gray part is a block element, it does not set the width. It is wrapped in a parent element with a width of 400px and centered horizontally.

Now set a margin-right:-100px for this element;

We see that its width does grow 100px; and then it sets a margin-left:-100px;

We see it getting wider.

The negative margin changes the width of the element, which is really confusing, and if the negative margin changes the position of the element in the flow of the document or is well understood, then changing the width of the phenomenon is actually quite incredible.

What's the use of the goods? Let me give you an example.

How do you want to create a couple of elements in the black box that are sorted in order, with some spacing between them? , of course, the simplest and easiest way is to use floating. Do we have to float the child elements in the black box to the left and then set a suitable margin-right? However, because the width of the outer black box is fixed, that is, the width of the four sub-elements and the width of the three-column interval, so the sub-elements near the right edge should not have a positive margin-right, otherwise this line can only accommodate three sub-elements. Some people say it's not simple, add a class to the child elements near the right border, and set the Margin-right to 0. This is certainly possible, but if these child elements are dynamically output through a loop in a template, then you have to determine which child elements are close to the right boundary at the time of the loop, and if so, add a class. Would that be a bit of a hassle? So the solution is to increase the width of the parent container of the child element, so that it can accommodate four child elements in a row and a width of four columns, and then the outermost black box of the container will be set to a overflow:hidden. It says that the negative left and right margin can increase the width of the element, so it is possible to set a proper negative margin-right for the parent container of the child element. Of course you can also set the width of the parent container of the element directly in the CSS, just to illustrate that the negative margin is also a method. Look at the complete code:

12345678910111213141516171819 <style>body,ul,li{ padding:0; margin:0;}ul,li{ list-style:none;}.container{ height:210px; width:460px; border:5px solid #000;}ul{ height:210px; overflow:hidden; margin-right:-20px;}/*一个负的margin-right,相当于把ul的宽度增加了20px*/li{ height:100px; width:100px; background:#09F; float:left; margin-right:20px; margin-bottom:10px;}</style><divclass="container">    <ul>        <li>子元素1</li>        <li>子元素2</li>        <li>子元素3</li>        <li>子元素4</li>        <li>子元素5</li>        <li>子元素6</li>        <li>子元素7</li>        <li>子元素8</li>    </ul></div>

Influence of negative margins on floating elements

The effect of negative margins on floating elements and negative margins on elements in the document flow is virtually the same. The position of the element in the document flow is determined by the direction of the document flow, and the floating element can also be seen as having a "floating flow", although the floating stream can be either left or right.

For example, there are three left-floating elements, with a width of 100px:

Now set them all to a margin-right:-50px; And then it will look like this:

We see that the elements that follow are stacked onto the previous element.

Then look at the following figure:

We narrowed the browser down and then the element 3 fell because it was not wide enough. We set a margin-left:-80px for element 3; see what happens.

Then we see that element 3 goes up, and it also covers part 2 of the element. Continue with element 3 set to margin-left:-100px

Element 3 is then completely covered by element 2, when element 3 is set to: margin-left:-200px:

We see element 3 continue to move to the left and overwrite element 1.

Now it must be clear to everyone that the negative margin affects the position of the floating element. So what the holy Grail layout, the double-wing layout, or whatever it sounds like, is all done using this principle. Is that an element is written in the back, but it can be in front of the browser when it is displayed by a negative margin. We can talk about this later.

Influence of negative margins on absolute positioning elements

The top, right, bottom, and left equivalents of an absolutely positioned element define the distance from the boundary of the element itself to the nearest positioned ancestor, which is the boundary of the margin definition, so if margin is positive, then its boundary is outward, If margin is negative, then its boundary is inward. With this, there is a classic way to center with absolute positioning:

Look at the effect:

However, the disadvantage of this method is that you must know the height and width of the element to be centered.

Summarize:

1. An element that is offset by a negative margin, discards the space occupied before the offset, and the other elements flow into the control that complements the offset, while relative positioning position:relative does not discard the space occupied before the offset.

2. In the document flow, the final boundary of the element is determined by margin, and the document flow can only flow left or upward, and cannot be moved down or to the right.

3. You can use the margin to adjust the width of the element, center it, and so on.

(GO) CSS layout-negative margin-margin

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.