Home  >  Article  >  Web Front-end  >  Magical use of negative margin in css layout and other implementations

Magical use of negative margin in css layout and other implementations

高洛峰
高洛峰Original
2017-02-16 13:32:121151browse

I believe everyone has encountered such a need in project development. X (X>1) blocks should be placed in one row and the spacing between adjacent blocks should be the same.


Magical use of negative margin in css layout and other implementations

It probably looks like the above. Here are several ways to implement it.

1. Negative margin method

Set the width and margin of the element to fill the width of the parent, and then set the margin-left of the parent to be blank The width of negative white space


CSS CodeCopy content to clipboard

   
 

1.关于负margin的实现,由于margin是基于父级计算的,会有一定的偏差,但是用于移动端上,误差可以忽略不计

  • Magical use of negative margin in css layout and other implementations
  • Magical use of negative margin in css layout and other implementations
  • Magical use of negative margin in css layout and other implementations


The above error is caused by the calculation of the margin percentage of ul and li based on different elements. However, on the mobile terminal, due to the limited range of the window, the difference is very small. On PC, px is generally used. , so it can be ignored. (There are more ways below)

2. For the implementation of major websites, fill in the elements and use box-sizing. It requires ie8 and above to support


CSS CodeCopy content to the clipboard

   

2.各大网站的实现,在元素内部进行填充,使用box-sizing,需要ie8及以上才支持

  • Magical use of negative margin in css layout and other implementations
  • Magical use of negative margin in css layout and other implementations
  • Magical use of negative margin in css layout and other implementations

This implementation has not been found yet What are the shortcomings? The code is simple and easy to understand (recommended)

3. The implementation of the flexible box model flex requires compatibility processing (old box + new box)


CSS CodeCopy content to clipboard

   

3.弹性盒模型flex的实现,需要做兼容处理(旧盒子+新盒子),仅为演示,没做兼容处理

  • Magical use of negative margin in css layout and other implementations
  • Magical use of negative margin in css layout and other implementations
  • Magical use of negative margin in css layout and other implementations

How can we do without flex in this situation? , the flexible box model should be specially designed to handle this situation, but there are old and new box models, and each browser implements them differently. Therefore, in general, the attributes of both sets of box models need to be added. (Just do it if you like, the effect is great)

4.classname implementation

Add a separate class to the elements that need special processing, and then do the corresponding deal with. It can be processed in the background or front-end (backend processing is recommended)


CSS CodeCopy content to the clipboard

   

4.classname实现

  • Magical use of negative margin in css layout and other implementations
  • Magical use of negative margin in css layout and other implementations
  • Magical use of negative margin in css layout and other implementations

5.css selector implementation

:first-child :first-type-of :nth-child() There is no technology in these implementations Difficulty, you can check the css document and pay attention to the compatibility.


##CSS CodeCopy the content to the clipboard

   

5.css选择器实现(注意ie兼容性)

  • Magical use of negative margin in css layout and other implementations
  • Magical use of negative margin in css layout and other implementations
  • Magical use of negative margin in css layout and other implementations

Paste all the DEMO


I almost forgot that there is another situation when X=2, set the width, float left on the left and float right on the right.

In fact, when X=3, there is another way to deal with it. The left and right elements float left and right respectively, and the middle element is set to be absolutely positioned and centered relative to the parent.

Note that due to the indivisibility, it cannot be calculated as perfectly as box-sizing, but reasonable application is no problem at all in the project.

The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.

Please pay attention to the PHP Chinese website for more wonderful uses of negative margins in css layout and other implementation-related articles!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn