Home > Web Front-end > CSS Tutorial > How to Center a DIV Horizontally and Vertically Without Clipping Content?

How to Center a DIV Horizontally and Vertically Without Clipping Content?

Susan Sarandon
Release: 2024-12-19 00:34:12
Original
852 people have browsed it

How to Center a DIV Horizontally and Vertically Without Clipping Content?

Centering a DIV Horizontally and Vertically Without Cutting Content

When looking to vertically and horizontally center a DIV, it's crucial to ensure that the content remains intact even if the window becomes smaller than the content itself.

One common approach involves absolute positioning and negative margins. However, this method can lead to content being cut off when the window size decreases.

For modern browsers, a better solution is available using Flexbox:

HTML

<div>
Copy after login

CSS

.content {
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}
Copy after login

This approach utilizes transforms to translate the content without affecting its size. As a result, the content remains centered regardless of the window dimensions.

The above is the detailed content of How to Center a DIV Horizontally and Vertically Without Clipping Content?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template