Home Web Front-end CSS Tutorial Share 7 techniques for achieving CSS vertical centering (with code)

Share 7 techniques for achieving CSS vertical centering (with code)

Sep 15, 2018 pm 04:18 PM

The demand for vertical centering of CSS on web pages has never stopped, and its difficulty has never been easy. After research by every developer, it is said that there are nearly ten vertical centering techniques for CSS. But it is still little known. Some companies even regard CSS vertical centering skills as interview questions. Its importance is evident. Today, I will take you to learn about the various ways of CSS vertical centering.

1. Line-height

Applicable scenarios: vertical centering techniques for single-line text

This method should be the most well-known, and is common in single-line text The most common way to apply text is objects such as buttons, or elements such as drop-down boxes and navigation. The principle of this method is that after setting the line height of a single line of text, the text will be located in the vertical middle of the line height. Using this principle, the vertical centering requirement can be easily achieved.

<div class="content">Lorem ipsam.</div>
.content{
  width: 400px;
  background: #ccc;
  line-height:100px;
  margin: auto;
}
Copy after login

2. Line-height inline-block

Applicable scenarios: Vertical centering techniques for multiple objects

Since you can use the first If you can achieve vertical centering of row elements in this way, of course there is no reason why you can't do multiple rows~ But you need to treat multiple elements or multi-row elements as one row element, so we must wrap this data in one more layer. And set it to inline-block, and use inline-block in the outer object of the inline-block object to replace the height setting, so that you can achieve the purpose of vertical centering, so that your data includes the title It can also be vertically centered normally including the content.

<div class="box box2">
  <div class="content">
    立马来看Amos实际完成的    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
      CSS3精美相册效果    </a>
    效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
</div>
h2{
  text-align: center;
}
.box{
  width: 500px;
  border: 1px solid #f00;
  margin: auto;
  line-height: 200px;
  text-align: center;
}
.box2 .content{
  display: inline-block;
  height: auto;
  line-height:1;
  width: 400px;
  background: #ccc;
}
Copy after login

3.:before inline-block

Applicable scenarios: CSS vertical centering techniques for multiple objects

:before pseudo-class element The writing method with the inline-block attribute should be a very traditional vertical centering technique. The advantage of this method is that the child element can be centered without the need to set a special height. We will use the :before pseudo-class element to set it to 100% high inline -block, and then set the child elements that need to be centered to the inline-block property, you can use vertical-align:middle to achieve the purpose of vertical centering. This method was actually a great vertical centering solution in the past. , only the small flaw of 4-5px space between inline-block elements needs to be specially dealt with, but it is also very practical.

<h2>3.:before + inline-block</h2>
<div class="box box3">
  <div class="content">
    立马来看Amos实际完成的    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
      CSS3精美相册效果    </a>
    效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
</div>
h2{
  text-align: center;
}
.box{
  width: 500px;
  height: 250px;
  border: 1px solid #f00;
  margin: auto;
  text-align: center;
}
.box::before{
  content:&#39;&#39;;
  display: inline-block;
  height: 100%;
  width: 0;
  vertical-align: middle;
}
.box .content{
  width: 400px;
  background: #ccc;
  display: inline-block;
  vertical-align: middle;
}
Copy after login

4. Absolute margin negative value

Applicable scenarios: vertical centering skills for multi-line text

Who said absolute positioning is less use? Amos believes that there is no problem of using less and more. The key point is whether you use it properly. In this example, absolute positioning will set top:50% to capture 50% of the space height, and then set the margin-top of the centered element. Set the height to negative half, so that the element can be centered. Is this method a centering method that has been passed down for many years?

<h2>4.absolute + margin 負值</h2>
<div class="box box4">
  <div class="content">
    立马来看Amos实际完成的    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
      CSS3精美相册效果    </a>
    效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
</div>
h2{
  text-align: center;
}
.box{
  width: 500px;
  height: 250px;
  border: 1px solid #f00;
  margin: auto;
  position: relative;
}
.box4 .content{
  width: 400px;
  background: #ccc;
  height: 70px;
  position: absolute;
  top:50%;
  left: 50%;
  margin-left: -200px;
  margin-top: -35px;
}
Copy after login

5. Absolute margin auto

Applicable scenarios: Vertical centering techniques for multi-line text

Another absolute positioning vertical centering Solution, this method is a bit special. When the element is set to absolute positioning, it is assumed that it cannot capture the overall available space range, so margin:auto will fail, but when you set top:0;bottom:0; At this time, the absolutely positioned element has captured the available space. At this time, your margin:auto will take effect (magic). If your absolutely positioned element needs to be horizontally centered on the parent layer, you can also set left: 0;right:0; to allow the absolutely positioned element to obtain the available space, and then set margin-left and margin-right to auto to center it. But the disadvantage of this method is that your positioned element must have a fixed width and height (percentage also counts) in order to be properly centered.

<h2>5.absolute + translate(-50%, -50%)</h2>
<div class="box box5">
  <div class="content">
    立马来看Amos实际完成的    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
      CSS3精美相册效果    </a>
    效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
</div>
h2{
  text-align: center;
}
.box{
  width: 500px;
  height: 250px;
  border: 1px solid #f00;
  margin: auto;
  position: relative;
}
.content{
  width: 400px;
  background: #ccc;
  height: 70px;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
}
Copy after login

6. Display: table-cell

Applicable scenarios: Vertical centering skills for multi-line text

I think this trick Older developers should have seen it. Of course, it is the first time for a new developer like me to see it. The principle of this trick is to use the CSS display property to set the div to a table cell, so that Use the vertical-align attribute that supports storage cell alignment to vertically center information

<h2>19.display: table-cell</h2>
<div class="box box19">
  <div class="content">
    立马来看Amos实际完成的    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
      CSS3精美相册效果    </a>
    效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
</div>
h2{
  text-align: center;
}
.box{
  width: 500px;
  height: 250px;
  border: 1px solid #f00;
  margin: auto;
    text-align: center;
    display: table-cell;
  vertical-align: middle;
}
.content{
  width: 400px;
  background: #ccc;
  margin: auto;
}
Copy after login

7. padding

Applicable scenarios: vertical centering of multi-line text Tips

What! This is also considered a vertical centering technique. Even my grandma knows this method, right? Yes, this is indeed a vertical centering method. It cannot be denied that this method is really too simple, so that it is a bit The developers think that this method cannot be regarded as a vertical centering technique, but you can't refute the fact that my data is indeed vertically centered. Well, just think of me as a hard concave. You are right, okay.

<h2>22.padding</h2>
<div class="box box22">
  <div class="content">
    立马来看Amos实际完成的    <a href="http://csscoke.com/2015/07/31/nth-child_rwd_album/">
      CSS3精美相册效果    </a>
    效果吧!別忘了拖拉一下窗口看看 RWD 效果喔!  </div>
</div>
h2{
  text-align: center;
}
.box{
  width: 500px;
  border: 1px solid #f00;
  margin: auto;
  height: auto;
  padding: 50px 0;
}
.content{
  width: 400px;
  background: #ccc;
  margin: auto;
}
Copy after login

Summary: The above introduces 7 CSS vertical centering techniques to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the PHP Chinese website!

The above is the detailed content of Share 7 techniques for achieving CSS vertical centering (with code). 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 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 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 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 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-

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.

See all articles