Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize horizontal and Vertical centering in css

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

This article mainly introduces css how to achieve horizontal vertical center, the article introduces in great detail, has a certain reference value, interested friends must read it!

Suppose this scenario is given now:

DEMO

The length of the content in the class='child' div is not certain, and now you need to implement the center of the div.

Horizontal and vertical center

This only needs to be a combination of the first few, there are three common methods.

1 inline-block + table-cell.child {display:inline-block; text-align:left;} .parent {text-align:center; display:table-cell; vertical-align:middle;} / * child element text is inherited and centered, so write on it center to the left * / 2 absolute + transform.parent {position:relative;} .child {position:absolute; left:50%; top:50% / * the reference is the parent container * / transform:translate (- 50% justify-content.parent 50%); / * the reference of the percentage is itself * /} 3 flex + align-items + justify-content.parent {display:flex; justify-content:center; align-items:center;}. These are all the contents of the article "how to achieve horizontal and vertical center in css". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Level content reference article value element interest scene container guy partner common text method more percentage percentage knowledge industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Tech Info Shulou Information MariaDB Redmi