Table of Contents
1. Treat the container as a table unit
2. Fixed-height elements should be vertically centered
3. Fixed-height elements are vertically centered
4. Vertically centered elements with variable height
5. Single-line text is vertically centered
6. The container has a fixed or variable height, and multi-line text is vertically centered
7. Vertical-align implements vertical centering of inline elements
Home Web Front-end HTML Tutorial Summary of CSS vertical centering methods_html/css_WEB-ITnose

Summary of CSS vertical centering methods_html/css_WEB-ITnose

Jun 24, 2016 am 11:48 AM

1. Treat the container as a table unit

<div class="middle-demo1">    <button>按钮</button></div>
.middle-demo1{ display: table-cell; height: 100px; //可以动态改变高度,这里只是演示 vertical-align: middle; border: 1px solid #666; }

Browser support:
http://caniuse.com /#search=table-cell

2. Fixed-height elements should be vertically centered

<div class="middle-demo2">    <div class="g-box"></div></div>
.middle-demo2{ position: relative; width: 200px; height: 200px; border: 1px solid #666; }.g-box{ position: absolute; top: 50%; width: 100px; height: 100px; margin-top: -50px; background-color: deepskyblue; }或者.g-box{ position: absolute; top: 50%; width: 100px; height: 100px; -webkit-transform: translateY(-50px); -moz-transform: translateY(-50px); -ms-transform: translateY(-50px); -o-transform: translateY(-50px); transform: translateY(-50px); background-color: deepskyblue; }

Note: should be vertically centered The element (g-box) must be of fixed height (except for js dynamic calculation settings).
Browser support for transform http://caniuse.com/#search=transform

3. Fixed-height elements are vertically centered

<div class="middle-demo3">    <div class="g-box2"></div>    <div class="g-box3"></div></div>

g-box3 is an element that needs to be vertically centered

.middel-demo3{ position: relative; width: 200px; height: 200px; border: 1px solid #666; }.g-box2{ height: 50%; margin-bottom: -50px; }.g-box3{ height: 100px; background-color: deepskyblue; }

Note: The element (g-box3) that needs to be vertically centered must be a fixed height.

4. Vertically centered elements with variable height

<div class="middle-demo4">    <div class="g-box4"></div></div>
.middle-demo4{ position: relative; width: 200px; height: 200px; border: 1px solid #666; }.g-box4{ position: absolute; top: 0; bottom: 0; margin-top: auto; margin-bottom: auto; width: 100px; height: 100px; background-color: deepskyblue; }

Explanation: Elements to be vertically centered (g-box4) can be The height is not fixed. The browser supports IE8 and other browsers have better support

5. Single-line text is vertically centered

<div class="middle-demo5">    <span>单行文本</span></div>
.middle-demo5{ height: 100px; line-height: 100px; border: 1px solid #666; }

Instructions : is invalid for internal block elements

6. The container has a fixed or variable height, and multi-line text is vertically centered

<div class="middle-demo6">    <p>单行文本</p>    <p>多行文本</p></div>
.middle-demo6{ display: table-cell; vertical-align: middle; height: 200px; //容器可以不定高,这里只是演示 border: 1px solid #666; }

7. Vertical-align implements vertical centering of inline elements

<div class="middle-demo7">    <span class="g-fix"></span>    <button class="g-box7">行内元素</button></div>
.middle-demo7{ position: relative; width: 200px; height: 200px; border: 1px solid #666; }.g-fix{ display: inline-block; width:0; height: 100%; vertical-align: middle; }

Instructions: Browser support http://caniuse.com/#search= inline-block

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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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

PHP Tutorial
1596
276
How to create an unordered list in HTML? How to create an unordered list in HTML? Jul 30, 2025 am 04:50 AM

To create an HTML unordered list, you need to use a tag to define a list container. Each list item is wrapped with a tag, and the browser will automatically add bullets; 1. Create a list with a tag; 2. Each list item is defined with a tag; 3. The browser automatically generates default dot symbols; 4. Sublists can be implemented through nesting; 5. Use the list-style-type attribute of CSS to modify the symbol style, such as disc, circle, square, or none; use these tags correctly to generate a standard unordered list.

How to embed a PDF document in HTML? How to embed a PDF document in HTML? Aug 01, 2025 am 06:52 AM

Using tags is the easiest and recommended method. The syntax is suitable for modern browsers to embed PDF directly; 2. Using tags can provide better control and backup content support, syntax is, and provides download links in tags as backup solutions when they are not supported; 3. It can be embedded through Google DocsViewer, but it is not recommended to use widely due to privacy and performance issues; 4. In order to improve the user experience, appropriate heights should be set, responsive sizes (such as height: 80vh) and PDF download links should be provided so that users can download and view them themselves.

How to add an icon to your website title tab in HTML How to add an icon to your website title tab in HTML Aug 07, 2025 pm 11:30 PM

To add an icon to the website title bar, you need to link a favicon file in part of the HTML. The specific steps are as follows: 1. Prepare a 16x16 or 32x32 pixel icon file. It is recommended to use favicon.ico to name it and place it in the website root directory, or use modern formats such as PNG and SVG; 2. Add link tags to HTML, such as PNG or SVG formats, adjust the type attribute accordingly; 3. Optionally add high-resolution icons for mobile devices, such as AppleTouchIcon, and specify different sizes through the sizes attribute; 4. Follow best practices, place the icon in the root directory to ensure automatic detection, clear the browser cache after update, and check the correctness of the file path.

Using HTML `input` Types for User Data Using HTML `input` Types for User Data Aug 03, 2025 am 11:07 AM

Choosing the right HTMLinput type can improve data accuracy, enhance user experience, and improve usability. 1. Select the corresponding input types according to the data type, such as text, email, tel, number and date, which can automatically checksum and adapt to the keyboard; 2. Use HTML5 to add new types such as url, color, range and search, which can provide a more intuitive interaction method; 3. Use placeholder and required attributes to improve the efficiency and accuracy of form filling, but it should be noted that placeholder cannot replace label.

How to create a search input field in an HTML form How to create a search input field in an HTML form Aug 02, 2025 pm 04:44 PM

Usetheelementwithinatagtocreateasemanticsearchfield.2.Includeaforaccessibility,settheform'sactionandmethod="get"attributestosenddatatoasearchendpointwithashareableURL.3.Addname="q"todefinethequeryparameter,useplaceholdertoguideuse

Why is my HTML image not showing up? Why is my HTML image not showing up? Aug 16, 2025 am 10:08 AM

First, check whether the src attribute path is correct, and ensure that the relative or absolute path matches the HTML file location; 2. Verify whether the file name and extension are spelled correctly and case-sensitive; 3. Confirm that the image file actually exists in the specified directory; 4. Use appropriate alt attributes and ensure that the image format is .jpg, .png, .gif or .webp widely supported by the browser; 5. Troubleshoot browser cache issues, try to force refresh or directly access the image URL; 6. Check server permission settings to ensure that the file can be read and not blocked; 7. Verify that the img tag syntax is correct, including the correct quotes and attribute order, and finally troubleshoot 404 errors or syntax problems through the browser developer tool to ensure that the image is displayed normally.

How to use the HTML abbr tag for abbreviations How to use the HTML abbr tag for abbreviations Aug 05, 2025 pm 12:54 PM

Using HTML tags can improve the accessibility and clarity of content; 1. Mark abbreviations or acronyms with abbreviations; 2. Add title attributes to unusual abbreviations to provide a complete explanation; 3. Use when the document first appears, avoiding duplicate annotations; 4. You can customize the style through CSS, and the default browser usually displays dotted underscores; 5. It helps screen reader users understand terms and enhance user experience.

How to add an icon to a button in HTML How to add an icon to a button in HTML Aug 07, 2025 pm 11:09 PM

Using FontAwesome can quickly add icons by introducing CDN and adding icon classes to buttons, such as Like; 2. Using labels to embed custom icons in buttons, the correct path and size must be specified; 3. Embed SVG code directly to achieve high-resolution icons and keep them consistent with the text color; 4. Spacing should be added through CSS and aria-label should be added to the icon buttons to improve accessibility; in summary, FontAwesome is most suitable for standard icons, pictures are suitable for custom designs, while SVG provides the best scaling and control, and methods should be selected according to project needs. FontAwesome is usually recommended.

See all articles