Home Web Front-end CSS Tutorial How to set div vertically centered in css

How to set div vertically centered in css

Apr 29, 2021 am 11:09 AM
css Center vertically

How to set the vertical centering of a div in css: 1. Set its actual height height to be equal to the height of the row [line-height]; 2. Set the padding so that the upper and lower padding values ​​are the same.

How to set div vertically centered in css

The operating environment of this tutorial: windows7 system, css3 version, DELL G3 computer.

How to set div vertical centering in css:

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 to be equal to the height line-height of the row.

For example:

div {  
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;}
  div {
    height:25px;
    line-height:25px;
    border:1px solid #FF0099;
    background-color:#FFCCFF;
  }
  </style>
</head>
<body>
  <div>现在我们要使这段文字垂直居中显示!</div>
</body>
</html>
Copy after login

2. Vertical centering of multiple lines of text of unknown height

If the height of a piece of content is variable, then we can use the method mentioned in the previous section The last method used to achieve horizontal centering is to set the padding so that the padding values ​​​​for the upper and lower sides 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:

div {  
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 the application of 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;}
    div {
    padding:25px;
    border:1px solid #FF0099;
    background-color:#FFCCFF;
    width:760px;
  }
  </style>
</head>
<body>
  <div><br>    <pre class="brush:php;toolbar:false">现在我们要使这段文字垂直居中显示!
    

  
Copy after login

Recommended related tutorials: CSS video tutorial

The above is the detailed content of How to set div vertically centered 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Is Debian Strings compatible with multiple browsers Is Debian Strings compatible with multiple browsers Apr 02, 2025 am 08:30 AM

"DebianStrings" is not a standard term, and its specific meaning is still unclear. This article cannot directly comment on its browser compatibility. However, if "DebianStrings" refers to a web application running on a Debian system, its browser compatibility depends on the technical architecture of the application itself. Most modern web applications are committed to cross-browser compatibility. This relies on following web standards and using well-compatible front-end technologies (such as HTML, CSS, JavaScript) and back-end technologies (such as PHP, Python, Node.js, etc.). To ensure that the application is compatible with multiple browsers, developers often need to conduct cross-browser testing and use responsiveness

How to convert XML to PDF on Android phone? How to convert XML to PDF on Android phone? Apr 02, 2025 pm 09:51 PM

Converting XML to PDF directly on Android phones cannot be achieved through the built-in features. You need to save the country through the following steps: convert XML data to formats recognized by the PDF generator (such as text or HTML); convert HTML to PDF using HTML generation libraries such as Flying Saucer.

How to solve the problem of style loss after Django project is deployed to Pagoda panel? How to solve the problem of style loss after Django project is deployed to Pagoda panel? Apr 01, 2025 pm 09:09 PM

Detailed explanation of style loss after Django project is deployed to pagoda panel. After deploying Django project to pagoda panel, you may encounter style loss problem. This...

How to display hidden lines in xml How to display hidden lines in xml Apr 02, 2025 pm 11:45 PM

There are two common ways to hide rows in XML: Use the display property in CSS to set to none Use XSLT to skip hidden rows via conditional copying

How to display the content of the interface with xml How to display the content of the interface with xml Apr 02, 2025 pm 11:48 PM

XML is widely used to build and manage user interfaces. It defines and displays the interface content through the following steps: Define interface elements: XML uses tags to define interface elements and their properties. Building a hierarchy: XML organizes interface elements according to hierarchical relationships to form a tree structure. Using Stylesheets: Developers use stylesheet languages ​​such as CSS or XSL to specify the visual appearance and behavior of elements. Rendering process: A browser or application uses an XML parser and stylesheet to parse an XML file and render interface elements to make it visible on the screen.

If you convert XML to PDF on your mobile phone, will the format be messy after conversion? If you convert XML to PDF on your mobile phone, will the format be messy after conversion? Apr 02, 2025 pm 10:21 PM

When converting XML to PDF on mobile phone, whether the format is chaotic depends on: 1. The quality of the conversion tool; 2. XML structure and content; 3. Style sheet writing. Specifically, poor conversion tools, messy XML structures, or wrong XSLT code can lead to malformation.

How to implement a tight transition animation in React using react-transition-group? How to implement a tight transition animation in React using react-transition-group? Apr 04, 2025 pm 11:27 PM

Using react-transition-group in React to achieve confusion about closely following transition animations. In React projects, many developers will choose to use react-transition-group library to...

How to quickly build a foreground page in a React Vite project using AI tools? How to quickly build a foreground page in a React Vite project using AI tools? Apr 04, 2025 pm 01:45 PM

How to quickly build a front-end page in back-end development? As a backend developer with three or four years of experience, he has mastered the basic JavaScript, CSS and HTML...

See all articles