Home > Web Front-end > CSS Tutorial > How to set horizontal centering in css? Two ways to set horizontal centering in CSS

How to set horizontal centering in css? Two ways to set horizontal centering in CSS

不言
Release: 2018-09-10 13:59:55
Original
7425 people have browsed it

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>
Copy after login

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.