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

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

Barbara Streisand
Release: 2024-12-16 01:19:10
Original
882 people have browsed it

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

Vertical and Horizontal DIV Centering Without Content Trimming


Your concern regarding centering a DIV both vertically and horizontally without cutting off content is a common one. While the traditional approach using absolute positioning and negative margins may work, it can lead to content being cut off if the window size becomes smaller than the DIV content.


Fortunately, for modern browsers, there are more advanced options:


Flexbox


HTML:


<div>

CSS:


.content {<br>  position: absolute;<br>  left: 50%;<br>  top: 50%;<br>  -webkit-transform: translate(-50%, -50%);<br>  transform: translate(-50%, -50%);<br>}<br>

This solution utilizes the transform property to translate the DIV content by 50% in both directions, ensuring that its center aligns with the center of the window.


Note: Flexbox is not widely supported in older browsers, so it's important to consider browser compatibility when implementing this solution.

The above is the detailed content of How to Vertically and Horizontally Center a DIV Without Content Trimming?. 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