Detailed explanation of 5 ideas for achieving simultaneous horizontal and vertical centering with CSS

高洛峰
Release: 2017-03-13 17:44:07
Original
1233 people have browsed it

The following editor will bring you a brief analysis of 5 ideas for achieving horizontal and vertical centering with CSS. The editor thinks it’s pretty good. Now I’ll share it with you and give it a reference. Let’s follow the editor and take a look.

Horizontal centering and vertical centering have been introduced separately. This article will introduce horizontal and vertical centering at the same time. 5 ideas

Idea 1:text-align+line-heightachieve horizontal and vertical centering of a single line of text

Copy after login

XML/HTML CodeCopy content to clipboard


  1. <pclass="test"style="background-color: lightblue;width: 200px;">Test text p>


# Idea 2:text-align +vertical-align

【1】Set text-align and vertical-align on the parent element, set the parent element to the table-cell element, and set the child element to the inline-block element

[Note] If it is compatible with IE7-browser, change the structure to

display:inline;zoom:1; to achieve inline- The effect of block

测试文字

Copy after login



test

Copy after login


##Three ideas:margin+ vertical-align If you want to set vertical-align in the parent element, it must be set to the table-cell element; if you want margin:0 auto to achieve horizontally centered block element content to expand the width, it must be set to table element. The table element can be nested inside the tabel-cell element, just like a cell can nest a table

[Note] If it is compatible with IE7-browser, the structure needs to be changed to ;structure< p>
>




Use absolute【1】Use thebox model

based on the offset being a certain value.

测试文字

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login


【2】Use the offset attribute of the absolutely positioned element and the own offset of the translate()
function

[Note]IE9-browser does not support

Copy after login

测试文字

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login


【3】When the width and height of the child element are known Below, you can use the negative value of margin to achieve the horizontal and vertical centering effect

Copy after login

测试文字

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login



##Idea 5:

Use flex [Note] IE9-browser does not support[1] Use margin:auto

Copy after login




The above is the detailed content of Detailed explanation of 5 ideas for achieving simultaneous horizontal and vertical centering with CSS. 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!
structure to achieve the effect of table-cell; use
Copy after login
[2] If the child element is an image, table-cell can not be used, but its parent element uses row height instead of height, and the font size is set to 0 . The child element itself sets vertical-align:middle
Copy after login

Copy after login

测试文字

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
## Idea 4:feature of the absolutely positioned element, and set margin:auto attributeto reach the level The effect of vertical centeringrrree[2] on scalable projects Use main axis alignment justify-content and cross axis alignment align-items on the scalable container

测试文字

Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
A brief analysis of the above article The five ideas for achieving simultaneous horizontal and vertical centering with CSS are all the content shared by the editor. I hope it can give you a reference, and I hope you will support the PHP Chinese website.