Detailed explanation of 5 ideas for achieving simultaneous horizontal and vertical centering with CSS
高洛峰
Release: 2017-03-13 17:44:07
Original
1291 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-height achieve horizontal and vertical centering of a single line of text
【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
structure to achieve the effect of table-cell; use
[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
##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
【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
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.
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!
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