What are the CSS layouts? Common CSS layout methods (with code)

不言
Release: 2018-08-07 17:23:25
Original
3932 people have browsed it

What are the css layouts? CSS layout can make the page look more beautiful and tidy. The following article summarizes several common layout methods in CSS, let us take a look in detail.

Horizontal centered layout

1. Margin fixed width

Demo
Copy after login

Running results :

What are the CSS layouts? Common CSS layout methods (with code)

Everyone must have seen it on the front end. The fixed width is horizontally centered. We can also use the following to achieve variable width

2. table margin

Demo
Copy after login

Run result:

What are the CSS layouts? Common CSS layout methods (with code)

display: The table is similar to the block element in performance, but the width is the width of the content.

No need to set the parent element style (supports IE 8 and above) compatible with IE 8. The following versions need to be adjusted to

3, inline-block text-align

Demo
Copy after login

Good compatibility (even compatible with IE 6 and IE 7)

4. absolute margin-left

Demo
Copy after login

Fixed width

Compared with using transform, it has better compatibility

5. Absolute transform

Demo
Copy after login

Absolute positioning is out of the document flow and will not affect subsequent elements The layout is affected.

transform is a CSS3 property and has compatibility issues

6, flex justify-content

Demo
Copy after login

You only need to set the parent node attribute, no need to set the child Element

flex has compatibility issues

Vertical centering:

1, table-cell vertical-align

Demo
Copy after login

Good compatibility (versions below IE 8 need to adjust the page structure to table

2, absolute transform

Powerful absolute for Of course, this small problem is also very simple

Demo
Copy after login

Absolute positioning is out of the document flow and will not affect the layout of subsequent elements. But if the absolutely positioned element is the only element, the parent element will also lose its height.

Transform is a CSS3 attribute and has compatibility issues
Horizontal centering, which can also be achieved with margin-top, the principle is horizontal centering

3. flex align-items

If absolute is powerful, then flex is just smiling, because he is the strongest...but it has compatibility issues

Demo
Copy after login

Horizontal and vertical centering :

1. Absolute transform

Demo
Copy after login

Absolute positioning is out of the document flow and will not affect the layout of subsequent elements.

transform is a CSS3 attribute and has compatibility issues

2. inline-block text-align table-cell vertical-align

Demo
Copy after login

Good compatibility

3. flex justify-content align-items

Demo
Copy after login

You only need to set the parent node attributes, no need to set the child elements

Painful compatibility issues

One column has a fixed width and one column is adaptive

1. float margin

left

right

right

Copy after login

IE 6 will have a 3-pixel BUG. The solution can be to add margin-left:-3px to .left. Of course, there are also solutions to solve this small bug as follows:

left

right

right

Copy after login

This method There will be no 3-pixel BUG in IE 6, but .left cannot be selected, and .left {position: relative} needs to be set to increase the level. Note that this method adds unnecessary structure to the HTML text.
Arrogant programmers should give up browsers that are too low version

2. float overflow

left

right

right

Copy after login

Setting overflow: hidden will trigger BFC mode (Block Formatting Context) block-level format context. What is BFC? In layman's terms, no matter what you do inside BFC, the outside world will not be affected. This method has a simple style but does not support IE 6

3. The display characteristics of table

left

right

right

Copy after login

table are the cell width of each column and must be equal to the table width. table-layout: fixed can speed up rendering and also set layout priority. Margin cannot be set in table-cell, but the spacing can be set through padding

4, flex

left

right

right

Copy after login
Copy after login

Low version browser compatibility issues

Performance The problem is that it is only suitable for small-scale layout
After we learn the layout of one column with fixed width and one column with adaptive layout, we can also easily implement multiple columns with fixed width, one column with adaptive multiple columns with variable width and one column with adaptive layout. We will not explain them one by one here. , you can try it yourself, or you can consolidate the

etc. distribution patternyou learned earlier:

1, float

1

2

3

4

Copy after login

This method is perfectly compatible with IE8 and above versions

2, flex

1

2

3

4

Copy after login

is powerful and simple, but has compatibility issues

3. table

1

2

3

4

Copy after login

Contour layout

##1、table

left

right

right

Copy after login

2, flex

left

right

right

Copy after login
Copy after login

注意这里实际上使用了 align-items: stretch,flex 默认的 align-items 的值为 stretch

3、float

left

right

right

Copy after login

此方法为伪等高(只有背景显示高度相等),左右真实的高度其实不相等。 兼容性较好。

到此,我们了解常见的布局解决方案,这些只是参考,一样的布局实现方式多种多样。主要就使用position、flex 、table(从很久很久以前起,我们就抛弃了table布局页面,但display: table;是异常强大)、float等属性目前flex兼容性较差。

相关文章推荐:

常见css水平自适应布局_html/css_WEB-ITnose

DIV+CSS布局中常见的10大错误_html/css_WEB-ITnose

CSS常用布局实现方法_html/css_WEB-ITnose

table The characteristics of each column are equal width and each row are equal height, which can be used to solve this requirement

The above is the detailed content of What are the CSS layouts? Common CSS layout methods (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!