Home Web Front-end CSS Tutorial CSS Tutorial: Multiple Methods of Vertically Centering divs

CSS Tutorial: Multiple Methods of Vertically Centering divs

Mar 13, 2017 pm 05:50 PM
css div Center vertically

This article mainly introduces in detail the various methods of vertical centering of p in the CSS tutorial, including the method of vertically centering multi-line text. Interested friends can refer to it

is talking about When it comes to this question, someone may ask if there is no vertical-align attribute in CSS to set vertical centering? Even if some browsers don't support it, I only need to do a little CSS Hack technology! So I have to say a few words here. There is indeed a vertical-align attribute in CSS, but it only takes effect for elements with valign attributes in (X)HTML elements, such as < in table elements. ;td>, , , etc., and elements like

, do not have valign attributes, so using vertical-align will not work on them.

1. Single-line vertical centering

If there is only one line of text in a container, it is relatively simple to center it. We only need to set its actual height height can be equal to the heightline-height of the row.

For example:

p {      
        height:25px;      
        line-height:25px;      
        overflow:hidden;      
 }
Copy after login

This code is very simple. The overflow:hidden setting is used later to prevent the content from exceeding the container or automatically wrapping lines, so that the vertical centering effect cannot be achieved.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> 单行文字实现垂直居中 </title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <style type="text/css">
 body { font-size:12px;font-family:tahoma;}   
 p {   
  height:25px;   
  line-height:25px;   
  border:1px solid #FF0099;   
  background-color:#FFCCFF;   
 }   
  </style>
 </head>
 <body>
  <p>现在我们要使这段文字垂直居中显示!</p>
 </body>
</html>
Copy after login


2. Vertical centering of multi-line text of unknown height

If a piece of content, its height is adjustable Then we can use the last method used to achieve horizontal centering mentioned in the previous section, which is to set Padding so that the upper and lower padding values ​​are the same. Again, this is a way of "looking" vertical centering, it's just a way of making the text completely fill the

. You can use code similar to the following:

p {      
 padding:25px;      
}
Copy after login

The advantage of this method is that it can run on any browser, and the code is very simple, but the prerequisite for this method is that the height of the container must be scalable of.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> 多行文字实现垂直居中 </title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <style type="text/css">
 body { font-size:12px;font-family:tahoma;}   
 p {   
  padding:25px;   
  border:1px solid #FF0099;   
  background-color:#FFCCFF;   
  width:760px;   
 }   
  </style>
 </head>
 <body>
  <p><pre class="brush:php;toolbar:false">现在我们要使这段文字垂直居中显示!   
   p {   
  padding:25px;   
  border:1px solid #FF0099;   
  background-color:#FFCCFF;   
 }   

Copy after login

3. Centering multi-line text with fixed height

At the beginning of this article, we have said that the vertical-align attribute in CSS will only It works on (X)HTML tag with valign attribute, but there is also a display attribute in CSS that can simulate

, so we can use this attribute to make < ;p> Simulate
and you can use vertical-align. Note that when using display:table and display:table-cell, the former must be set on the parent element, and the latter must be set on the child element, so we need to add another

element for the text that needs to be positioned:

p#wrap {      
    height:400px;      
 display:table;      
}      
p#content {      
  vertical-align:middle;      
    display:table-cell;      
   border:1px solid #FF0099;      
 background-color:#FFCCFF;      
 width:760px;      
}
Copy after login
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> 多行文字实现垂直居中 </title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <style type="text/css">
 body { font-size:12px;font-family:tahoma;}   
 p#wrap {   
  height:400px;   
  display:table;   
 }   
 p#content {   
  vertical-align:middle;   
  display:table-cell;   
  border:1px solid #FF0099;   
  background-color:#FFCCFF;   
  width:760px;   
 }   
  </style>
 </head>
 <body>
 <p id="wrap">
  <p id="content"><pre class="brush:php;toolbar:false">现在我们要使这段文字垂直居中显示! Webjx.Com    
 p#wrap {   
  height:400px;   
  display:table;   
 }   
 p#content {   
  vertical-align:middle;   
  display:table-cell;   
  border:1px solid #FF0099;   
  background-color:#FFCCFF;   
  width:760px;   
 }   

Copy after login


This method should be ideal, but unfortunately Internet Explorer 6 does not correctly understand display:table and display:table-cell, so this method is used in It is invalid in Internet Explorer 6 and below. Well, that’s depressing! But we have other ways


4. Solution in Internet Explorer

In Internet Explorer 6 and below , there are flaws in the calculation of height. After positioning the parent element in Internet Explorer 6, if the percentage calculation is performed on the child element, the calculation basis seems to be inherited (if the positioning value is an absolute value, there is no such problem, but using the percentage calculation basis will It is no longer the height of the element, but the positioning height inherited from the parent element). For example, we have the following (X)HTML code snippet:

<p id="wrap">     
 <p id="subwrap">     
   <p id="content">     
 </p>     
</p>   
</p>
Copy after login

If we perform absolute positioning on subwrap, then content will also inherit this attribute, although it will not It will be displayed immediately on the page, but if you position the content relatively, the 100% ratio you use will no longer be the original height of the content. For example, we set the position of subwrap to 40%. If we want the top edge of the content to coincide with the wrap, we must set top:-80%; then, if we When setting top:50% of subwrap, we must use 100% to return the content to its original position, but what if we also set the content to 50%? Then it's exactly vertically centered. So we can use this method to achieve vertical centering in Internet Explorer 6:


p#wrap {      
    border:1px solid #FF0099;      
 background-color:#FFCCFF;      
 width:760px;      
  height:400px;      
 position:relative;      
}      
p#subwrap {      
  position:absolute;      
    border:1px solid #000;      
    top:50%;      
}      
p#content {      
    border:1px solid #000;      
    position:relative;      
    top:-50%;      
}
Copy after login


当然,这段代码只能在Internet Exlporer 6等计算存在问题的浏览器中才会有作用。(不过我不解,我查阅了很多文章,不知道是因为出处相同还是什么原因,似乎很多人都不愿意去解释Internet Exlporer 6中这这个Bug的原理,我也只是了解了一点皮毛,还要再研究)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> 多行文字实现垂直居中 </title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <style type="text/css">
 body { font-size:12px;font-family:tahoma;}   
 p#wrap {   
  border:1px solid #FF0099;   
  background-color:#FFCCFF;   
  width:760px;   
  height:400px;   
  position:relative;   
 }   
 p#subwrap {   
  position:absolute;   
  top:50%;   
 }   
 p#content {   
  position:relative;   
  top:-50%;   
 }   
  </style>
 </head>
 <body>
 <p id="wrap">
  <p id="subwrap">
   <p id="content"><pre class="brush:php;toolbar:false">现在我们要使这段文字垂直居中显示!   
 p#wrap {   
  border:1px solid #FF0099;   
  background-color:#FFCCFF;   
  width:760px;   
  height:500px;   
  position:relative;   
 }   
 p#subwrap {   
  position:absolute;   
  border:1px solid #000;   
  top:50%;   
 }   
 p#content {   
  border:1px solid #000;   
  position:relative;   
  top:-50%;   
 }

Copy after login

五、完美的解决方案


那么我们综合上面两种方法就可以得到一个完美的解决方案,不过这要用到CSS hack的知识。对于如果使用CSS Hack来区分浏览器,你可

以参考这篇“简单CSS hack:区分IE6、IE7、IE8、Firefox、Opera”:

p#wrap {      
    display:table;      
    border:1px solid #FF0099;      
 background-color:#FFCCFF;      
 width:760px;      
  height:400px;      
 _position:relative;      
   overflow:hidden;      
}      
p#subwrap {      
    vertical-align:middle;      
    display:table-cell;      
   _position:absolute;      
   _top:50%;      
}      
p#content {      
   _position:relative;      
   _top:-50%;      
}
Copy after login


至此,一个完美的居中方案就产生了。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> 多行文字实现垂直居中 </title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <style type="text/css">
 body { font-size:12px;font-family:tahoma;}   
 p#wrap {   
  display:table;   
  border:1px solid #FF0099;   
  background-color:#FFCCFF;   
  width:760px;   
  height:400px;   
  _position:relative;   
  overflow:hidden;   
 }   
 p#subwrap {   
  vertical-align:middle;   
  display:table-cell;   
  _position:absolute;   
  _top:50%;   
 }   
 p#content {    
  _position:relative;   
  _top:-50%;   
 }   
  </style>
 </head>
 <body>
 <p id="wrap">
  <p id="subwrap">
   <p id="content"><pre class="brush:php;toolbar:false">现在我们要使这段文字垂直居中显示!   
 p#wrap {   
  border:1px solid #FF0099;   
  background-color:#FFCCFF;   
  width:760px;   
  height:500px;   
  position:relative;   
 }   
 p#subwrap {   
  position:absolute;   
  border:1px solid #000;   
  top:50%;   
 }   
 p#content {   
  border:1px solid #000;   
  position:relative;   
  top:-50%;   
 }

Copy after login


以上就是本文的全部内容,希望对大家的学习有所帮助。

The above is the detailed content of CSS Tutorial: Multiple Methods of Vertically Centering divs. 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)

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.

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 do vertical centering of bootstrap How to do vertical centering of bootstrap Apr 07, 2025 pm 03:21 PM

Use Bootstrap to implement vertical centering: flexbox method: Use the d-flex, justify-content-center, and align-items-center classes to place elements in the flexbox container. align-items-center class method: For browsers that do not support flexbox, use the align-items-center class, provided that the parent element has a defined height.

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.

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 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 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

See all articles