Home > Web Front-end > CSS Tutorial > How to Horizontally Center a Div with a Minimum Width Using CSS?

How to Horizontally Center a Div with a Minimum Width Using CSS?

Linda Hamilton
Release: 2024-12-21 01:23:10
Original
165 people have browsed it

How to Horizontally Center a Div with a Minimum Width Using CSS?

Horizontal Centering of a <div> with Minimum Width Using CSS

Encapsulating a <div> element within another <div> allows for precise horizontal centering. The outer <div> determines the width constraints, while the inner <div> contains the desired content.

For a <div> of variable width, the following CSS can achieve horizontal centering while maintaining a minimum width:

#wrapper {
  background-color: green; /* for visualization purposes */
  text-align: center;
}
#yourdiv {
  background-color: red; /* for visualization purposes */
  display: inline-block;
}
Copy after login
<div>
Copy after login

The outer <div> with the id="wrapper" establishes a background color for visualization and centers the text inside it using text-align: center. The inner <div> with id="yourdiv" is assigned a background color for clarity. It utilizes display: inline-block to render as an inline element while still retaining the ability to adjust its width.

The above is the detailed content of How to Horizontally Center a Div with a Minimum Width Using CSS?. 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