search
HomeWeb Front-endCSS TutorialSummary of various centered layout methods using CSS

Summary of various centered layout methods using CSS

Mar 10, 2017 am 10:52 AM
css layout method

This article summarizes various centering layout methods using CSS. Interested friends can refer to

This article discusses the situation where the centering situation is set to a variable total width and a variable content width. (Still centered when resized).

Special note: When setting position:absolute; on an element to set the centering effect, in addition to the css3 method introduced in the blog, you can also use negative margin to center. This solves the compatibility problem, but only Suitable for situations where the width and height are known (because a negative offset is half the width and height of the element). When the width and height change, there is no longer a centered effect.

Child elements in these layouts default to the content width because of their attribute settings.

All the centering examples in this article only discuss the implementation of css. The html code is unified as follows:

<p class="parent">   
    <p class="child">demo</p>   
</p>

1. Horizontal centering

Summary of various centered layout methods using CSS

1.1 inline-block with text-align

.parent{   
    text-align: center;   
}   
.child{   
    display: inline-block;   
}

##Advantages:The compatibility is very good. You only need to add *display:inline and *zoom:1 in the css of the child element to be compatible with IE6 and 7. Disadvantage: the internal text will also be horizontally centered, so the impact needs to be eliminated.

1.2 table with margin

.child{   
    display:table;   
    margin: 0 auto;   
}

Advantages: The setting is very simple, just set Set the sub-element, support IE8+, need to support IE6, 7, you can replace the sub-element into a table structure.

1.3 abasolute with transform

.parent{   
    position:relative;   
}   
.child{   
    position:absolute;   
    left:50%;   
    transform: translateX(-50%);   
}

Advantages: The centered element does not affect other elements . Disadvantages: CSS3 new attributes support IE9+, but lower version browsers do not support it.

2. Vertical centering

Summary of various centered layout methods using CSS

##2.1 table-cell with vertical-align

.parent{   
    display: table-cell;   
    vertical-align:middle;   
}

Advantages:

Easy to set up, just set the parent element, compatible with IE8+, when you need to be compatible with local browsers, you can replace p with a table structure.

2.2 absolute with transform

.parent{   
    position:relative;   
}   
.child{   
    position:absolute;   
    top: 50%;   
    transform: translateY(-50%);   
}

Advantages:

The centered element does not affect other elements . Disadvantages: CSS3 new attributes support IE9+, but lower version browsers do not support it. 3. Horizontal + vertical centering

Summary of various centered layout methods using CSS

3.1 inline-block with text-align plus table-cell with vertical-align

.parent{   
    display: table-cell;   
    vertical-align:middle;   
    text-align:center;   
}   
.child{   
    display: inline-block;   
}

Advantages:

Integrating the first two methods, the compatibility is good! Supports IE8+, and is also compatible with lower version browsers. Disadvantages: The setup is more complicated.

3.2 absolute with transform

.parent{   
    position: relative;   
}   
.child{   
    position: absolute;   
    left: 50%;   
    top: 50%;   
    transform: translate(-50%,-50%);   
}

Advantages:

The centered element does not affect other elements . Disadvantages: CSS3 new attributes support IE9+, but lower version browsers do not support it.

4. Almighty flex

css3 adds new layout attributes. The layout is simple and powerful, but the performance is slightly worse. It only supports IE10+ and is mostly used on mobile terminals.

4.1 Horizontal centering

/*当父元素设置display: flex;时,子元素为flex-item,默认为内容宽度。*/  
.parent{   
    display: flex;   
    justify-content: center;   
}   
/* 在设置子元素为margin: 0 auto;时,可删除父元素的justify-content: center;同样可以达到居中效果*/  
/*  .child{
        margin: 0 auto;   
    }*/

4.2 Vertical centering

.parent{   
    display: flex;   
    align-items: center;   
}

4.3 Horizontal and vertical centering

parent{       
    display: flex;       
    justify-content: center;       
    align-items: center;       
}       
/*如同flex布局的第一部分一样这里也可以对子元素进行下面的设置:同时删除上面的除去display外的其他属性*/      
/*  .child{   
        margin:auto;     
    } */

The above is the entire content of this article, I hope it will be helpful to Everyone’s learning helps.

The above is the detailed content of Summary of various centered layout methods using CSS. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Where should 'Subscribe to Podcast' link to?Where should 'Subscribe to Podcast' link to?Apr 16, 2025 pm 12:04 PM

For a while, iTunes was the big dog in podcasting, so if you linked "Subscribe to Podcast" to like:

Browser Engine DiversityBrowser Engine DiversityApr 16, 2025 pm 12:02 PM

We lost Opera when they went Chrome in 2013. Same deal with Edge when it also went Chrome earlier this year. Mike Taylor called these changes a "Decreasingly

UX Considerations for Web SharingUX Considerations for Web SharingApr 16, 2025 am 11:59 AM

From trashy clickbait sites to the most august of publications, share buttons have long been ubiquitous across the web. And yet it is arguable that these

Weekly Platform News: Apple Deploys Web Components, Progressive HTML Rendering, Self-Hosting Critical ResourcesWeekly Platform News: Apple Deploys Web Components, Progressive HTML Rendering, Self-Hosting Critical ResourcesApr 16, 2025 am 11:55 AM

In this week's roundup, Apple gets into web components, how Instagram is insta-loading scripts, and some food for thought for self-hosting critical resources.

Git Pathspecs and How to Use ThemGit Pathspecs and How to Use ThemApr 16, 2025 am 11:53 AM

When I was looking through the documentation of git commands, I noticed that many of them had an option for . I initially thought that this was just a

A Color Picker for Product ImagesA Color Picker for Product ImagesApr 16, 2025 am 11:49 AM

Sounds kind of like a hard problem doesn't it? We often don't have product shots in thousands of colors, such that we can flip out the with . Nor do we

A Dark Mode Toggle with React and ThemeProviderA Dark Mode Toggle with React and ThemeProviderApr 16, 2025 am 11:46 AM

I like when websites have a dark mode option. Dark mode makes web pages easier for me to read and helps my eyes feel more relaxed. Many websites, including

Some Hands-On with the HTML Dialog ElementSome Hands-On with the HTML Dialog ElementApr 16, 2025 am 11:33 AM

This is me looking at the HTML element for the first time. I've been aware of it for a while, but haven't taken it for a spin yet. It has some pretty cool and

See all articles

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool