Home Web Front-end HTML Tutorial How to vertically center a div in css

How to vertically center a div in css

Nov 27, 2017 am 09:54 AM
css center

If it is a div with a fixed height and width that is vertically centered on the web page, it must be very simple. I believe everyone can easily write it. But if it is a div with a fixed height and width, how to vertically center it? So how do these divs get vertically centered? In this article, I will summarize

Fixed height and width div vertically centered

enter image description here
Copy after login

As shown above, fixed height and width is very simple, the writing is as follows:

 position: absolute;
 left: 50%;
 top: 50%;
width:200px;
height:100px;
margin-left:-100px;
margin-top:-50px;
Copy after login

No fixed height and width How to vertically center a div

Method 1:

Use a "ghost" pseudo-element (invisible pseudo-element) and inline-block / vertical-align It can be done Centered, very clever. However, this method requires that the element to be centered is inline-block, which is not a truly universal solution.

html is as follows:

<div style="height: 300px;">
    <div>
        <h1>haorooms案例题目</h1>
        <p>haorooms案例内容,haorooms案例内容haorooms案例内容haorooms案例内容haorooms案例内容haorooms案例内容haorooms案例内容haorooms案例内容haorooms案例内容</p>
    </div>
</div>
Copy after login

css is as follows:

/* This parent can be any width and height */
.block {
  text-align: center;
}
/* The ghost, nudged to maintain perfect centering */
.block:before {
  content: &#39;&#39;;
  display: inline-block;
  height: 100%;
  vertical-align: middle;
  margin-right: -0.25em; /* Adjusts for spacing */
}
/* The element to be centered, can
   also be of any width and height */ 
.centered {
  display: inline-block;
  vertical-align: middle;
  width: 50%;
}
Copy after login

Method 2:

You can use the table layout method, but this method also has limitations!

The writing method is as follows:

<table style="width: 100%;">
  <tr>
     <td style="text-align: center; vertical-align: middle;">
          Unknown stuff to be centered.
     </td>
  </tr>
</table>
Copy after login

Since table writing is more time-consuming, you can also use div instead of table. The writing method is as follows:

html:

<div>
   <div>
       Unknown stuff to be centered.
   </div>
</div>
css:
.something-semantic {
   display: table;
   width: 100%;
}
.something-else-semantic {
   display: table-cell;
   text-align: center;
   vertical-align: middle;
}
Copy after login

Method 3 , The ultimate solution:

The above two methods may have their limitations. The third method I introduced is a more mature method of vertical centering that is not a fixed height and width div! But the method is written in CSS3. For children who want to be compatible with IE8, it is recommended to use the above method!

The method is similar to our fixed height and width, but without margin we use translate()

The demo is as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>haorooms不固定高度div写法</title>
    <style>
.center {
  position: fixed;
  top: 50%;
  left: 50%;
  background-color: #000;
  width:50%;
  height: 50%;
-webkit-transform: translateX(-50%) translateY(-50%);
}
    </style>
</head>
<body>
    <div></div>
</body>
</html>
Copy after login

My css above is only for the webkit kernel Browser, other kernel browsers are written as follows:

-webkit-transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-ms-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
Copy after login

Regarding the writing method of each browser, you can read my previous article: http://www.haorooms.com/post/css_common

Some pop-up layer styles can also be centered using this method

position: fixed;
top: 50%;
left: 50%;
width: 50%;
max-width: 630px;
min-width: 320px;
height: auto;
z-index: 2000;
visibility: hidden;
-webkit-backface-visibility: hidden;
-moz-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-transform: translateX(-50%) translateY(-50%);
-moz-transform: translateX(-50%) translateY(-50%);
-ms-transform: translateX(-50%) translateY(-50%);
transform: translateX(-50%) translateY(-50%);
Copy after login

css3 variable width, high level, and vertical centering

As long as three sentences are needed, you can achieve variable width, high level, and vertical centering.

justify-content:center;//子元素水平居中
align-items:center;//子元素垂直居中
display:-webkit-flex;
Copy after login

Add the above three sentences to the parent element to achieve horizontal and vertical centering of the child elements.

Use margin:auto for vertical centering

The margin value is set to auto, which allows us to allocate the remaining space! We know that block-level elements are set to margin:0 auto; they can be displayed left, right, and centered! Is there any way to make the margin be centered on the top, bottom, left, and right after setting it to margin:auto? By centering the top, bottom, left and right, we can achieve our vertical centering!

The answer is yes, as long as we allow enough space at the top and bottom, we can let margin auto allocate the top and bottom space.

We can use positioning to give the margin enough space up, down, left, and right! Then you can use margin:auto to achieve vertical centering!

The implementation of html is as follows: (make a simple vertical pop-up box)

<div>
    <div></div>
</div>
Copy after login

css code is as follows, it is very simple and has good compatibility. It supports IE8+

.father{position:fixed;width:100%;height:100%;top;0;left:0;background-color:rgba(0,0,0,.7);}
.son{position: absolute;top:0;left:0;bottom:0;right:0;width:50%;height:50%;margin:auto;background-color:red;}
Copy after login

This way Vertical centering can be achieved. Isn’t it very simple? For more exciting content, please pay attention to other related articles on the php Chinese website!


Related reading:

How to realize the pop-up effect of prompt text in CSS3

CSS3 How to realize typing animation in CSS3

How to realize loading animation effect in CSS3

The above is the detailed content of How to vertically center a div in css. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

See all articles