Get the App
SLTechnology News&Howtos  ›  Development  › 

The method of centering div and div content

Shulou Source: shulou.com Published: 2022-06-01 07:49:15 09月24日 Update

This article introduces the relevant knowledge of "div centralization and div content centralization". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First, div himself is in the middle:

Apply margin:0 auto

The concave and convex is 0, which dominates the self-adaptive css pattern.

In order to keep the div level in the middle, you can't set the equipment float in addition to configuring css margin:0 auto, otherwise it will cause div to the left (set float:left) and div to the right (configure float:right).

1. Div structure centered example code

Div Organization Center instance CSS5#jz {margin:0 auto; width:320px; height:100px; border:1px solid # F00} configure margin:0 auto compatible readers to center div horizontally

2. Screenshot

Screenshot of div box leveling centered

The above example is the use of margin:0 auto to make div compatible with each major reader level in the middle.

2. The content in the div is centered:

The model is divided equally into the middle of the div model and the vertical center of the div model.

Div content horizontal center compared to the construction of div center, and div form center comparison is simple, only need to configure a form center css (text-align:center), form vertical center (line-height).

1. The formal level of div is in the middle. CSS:

Text-align:center

No matter whether it is div as usual, or set up equipment for it, the CSS implementation is in the middle of the mode level in the corresponding response image.

2. Vertical center line height attribute of div content:

Line-height

To center vertically in the form of only one line within the div, the div mode can be vertically centered by usually communicating between the height (height) and the line-height (line height) of the div configuration.

3. Example code of horizontal and vertical centering of div content

Div centered instances CSS5#juzhong {width:320px;height:100px; line-height:100px; text-align:center; border:1px solid # F00} text-align and line-height configurations are centered horizontally and vertically

4. Screenshot

An example of vertical centering layout between div formal leveling and div mode

From the image above, we can see that the div content is in the middle between the two conditions, but the equipment is not set margin:0 auto and the div is not centered.

You can copy the above code again under the example, and try to achieve the center of the DIV layout code to achieve the center of DIV.

This is the end of the content of "div centralization and div content centralization". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Content example form pattern level configuration level code screenshot arrangement method layout more knowledge reader practical important learned next one line Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Information macOS Shulou Tech Info Docker