css style can achieve horizontal centering, so how to set css horizontal centering? This article will share with you the implementation method of css setting horizontal centering.
In css, you can set the horizontal centering of inline elements and the horizontal centering of block-level elements. Students who don’t understand inline elements and block-level elements can refer to What is the definition of block-level elements in css? What are the css block-level elements? What are the and css inline elements? The difference between css block-level elements and inline elementsIn these two articles, let’s look directly at the two horizontal centering settings in css.
First, let’s take a look at css to set the horizontal centering of inline elements.
The first type: CSS inline elements are horizontally centered:
Syntax: div{text-align:center} /*Inline elements within DIV will be horizontally centered* /
Simple explanation: Set the text-align:center style for the parent element.
<div style="text-align: center"> <span>行内元素水平居中</span> </div>
Detailed explanation:
1.
2.
Text
At this time, it makes sense to center the two words "Text" horizontally, because p is a block-level element, its width occupies one line, and text It only occupies a width of two characters. At this time, set text-align:center for the p element; then the two words "text" will be horizontally centered in one line.
3.
After reading the horizontal centering of css inline elements, let’s talk about the horizontal centering of css block-level elements. The horizontal centering of css block-level elements is divided into the horizontal centering of fixed-width block-level elements. and variable-width block-level elements are horizontally centered. Let’s look at them separately below.
The second type: CSS fixed-width block-level elements are horizontally centered: they are both block-level elements and fixed-width elements.
Set the left and right margin values of the block-level elements that need to be horizontally centered to auto;
For example:
<div> <div style="width: 100px;height: 100px;background-color: green;margin: 0 auto"> </div></div>
css variable width block Level elements are horizontally centered: block-level elements with variable widths
For example:
1. Add block-level elements to the