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

How to vertically center a div in css

php中世界最好的语言
Release: 2017-11-27 09:54:42
Original
1652 people have browsed it

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!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template