search
HomeWeb Front-endHTML TutorialCSS之各种居中_html/css_WEB-ITnose

转自个人博客

本博客讨论居中情况设定为总宽度不定,内容宽度不定的情况。(改变大小时,仍然居中)。

特别说明:在元素设置position:absolute;来设置居中效果时,除去博客下介绍的css3方法外,还可以使用负的margin来居中,这样解决了兼容性的问题,但是只适用于宽高已知的情况(因为负的偏移量为元素宽高的一半)。宽高改变时,不再是居中效果。

在这些布局中的子元素,因为其属性设置,都默认为内容宽度。

本博客所有居中的例子,只讨论css的实现,html代码统一如下:

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

1. 水平居中

1.1 inline-block配合text-align

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

优点:兼容性非常好,只需要添加只需要在子元素的css中添加*display:inline和*zoom:1就可兼容到IE6、7;缺点:内部文字也会水平居中,需消除影响。

1.2 table配合margin

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

优点:设置特别简单,只需对子元素进行设置,支持IE8+,需支持IE6,7时,可更换子元素为表格结构。

1.3 abasolute配合transform

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

优点:居中元素不对其他元素产生影响。缺点:CSS3新属性支持IE9+,低版本浏览器不支持。

2. 垂直居中

2.1 table-cell配合vertical-align

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

优点:设置简单,只需对父元素进行设置,兼容到IE8+,需兼容地版本浏览器时,可更换div为表格结构。

2.2 absolute配合tranform

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

优点:居中元素不对其他元素产生影响。缺点:CSS3新属性支持IE9+,低版本浏览器不支持。

3. 水平+垂直居中

3.1 inline-block配合text-align加上table-cell配合vertical-align

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

优点:综合前两中方法,兼容性好!支持IE8+,低版本浏览器也好兼容。缺点:设置较为复杂。

3.2 absolute配合transform

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

优点:居中元素不对其他元素产生影响。缺点:CSS3新属性支持IE9+,低版本浏览器不支持。

4. 全能的flex

css3新增布局属性,布局简单,强大,性能略差,只支持IE10+,在移动端使用较多。

4.1 水平居中

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

4.2 垂直居中

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

4.3 水平垂直居中

.parent{    display: flex;    justify-content: center;    align-items: center;}/*如同flex布局的第一部分一样这里也可以对子元素进行下面的设置:同时删除上面的除去display外的其他属性*//*  .child{        margin:auto;    } */
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
Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

How to efficiently add stroke effects to PNG images on web pages?How to efficiently add stroke effects to PNG images on web pages?Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

How do I use HTML5 form validation attributes to validate user input?How do I use HTML5 form validation attributes to validate user input?Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What is the purpose of the <datalist> element?What is the purpose of the <datalist> element?Mar 21, 2025 pm 12:33 PM

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <progress> element?What is the purpose of the <progress> element?Mar 21, 2025 pm 12:34 PM

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

What are the best practices for cross-browser compatibility in HTML5?What are the best practices for cross-browser compatibility in HTML5?Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <meter> element?What is the purpose of the <meter> element?Mar 21, 2025 pm 12:35 PM

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

What is the purpose of the <iframe> tag? What are the security considerations when using it?What is the purpose of the <iframe> tag? What are the security considerations when using it?Mar 20, 2025 pm 06:05 PM

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

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

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

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

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.