search
HomeWeb Front-endHTML Tutorial《Web开发中让盒子居中的几种方法》_html/css_WEB-ITnose

一、记录下几种盒子居中的方法:

1.0、margin固定宽高居中;

2.0、负margin居中;

3.0、绝对定位居中;

4.0、table-cell居中;

5.0、flex居中;

6.0、transform居中;

7.0、不确定宽高居中(绝对定位百分数);

8.0、button居中。

 

二、代码演示(html使用同一个Demo):

html Demo:

<body>    <div id="container">        <div id="box"></div>    </div></body>

 

1.0、margin固定宽高居中(演示)

这种定位方法纯粹是靠宽高和margin拼出来的,不是很灵活。

CSS:

#container {    width: 600px;    height: 500px;    border: 1px solid #000;    margin: auto;}#box {    width: 200px;    height: 200px;    margin: 150px 200px;    background-color: #0ff;}

 

2.0、负margin居中(演示)

利用负的margin来进行居中,需要知道固定宽高,限制比较大。

CSS:

#container {    position: relative;    width: 600px;    height: 500px;    border: 1px solid #000;    margin: auto;}#box {    position: absolute;    width: 200px;    height: 200px;    left: 50%;    top: 50%;    margin: -100px -100px;    background-color: #0ff;}

 

3.0、绝对定位居中(演示)

利用绝对定位居中,非常常用的一种方法。

CSS:

#container {    position: relative;    width: 600px;    height: 500px;    border: 1px solid #000;    margin: auto;}#box {    position: absolute;    width: 200px;    height: 200px;    left: 0;    top: 0;    right: 0;    bottom: 0;    margin: auto;    background-color: #0ff;}

 

4.0、table-cell居中(演示)

利用table-cell来控制垂直居中。

CSS:

#container {    display: table-cell;    width: 600px;    height: 500px;    vertical-align: middle;    border: 1px solid #000;}#box {    width: 200px;    height: 200px;    margin: 0 auto;    background-color: #0ff;}

 

5.0、flex居中(演示)

CSS3中引入的新布局方式,比较好用。缺点:IE9以及IE9一下不兼容。

CSS:

#container {    display: -webkit-flex;    display: flex;    -webkit-align-items: center;            align-items: center;    -webkit-justify-content: center;            justify-content: center;    width: 600px;    height: 500px;    border: 1px solid #000;    margin: auto;}#box {    width: 200px;    height: 200px;    background-color: #0ff;} 

 

6.0、transform居中(演示)

这种方法灵活运用CSS中transform属性,较为新奇。缺点是IE9下不兼容。

CSS:

#container {    position: relative;    width: 600px;    height: 600px;    border: 1px solid #000;    margin: auto;}#box {    position: relative;    top: 50%;    left: 50%;    width: 200px;    height: 200px;    transform: translate(-50%, -50%);    -webkit-transform: translate(-50%, -50%);    -ms-transform: translate(-50%, -50%);    -moz-transform: translate(-50%, -50%);    background-color: #0ff;}

 

7.0、不确定宽高居中(绝对定位百分数)(演示)

这种不确定宽高的居中,较为灵活。只需要保证left和right的百分数一样就可以实现水平居中,保证top和bottom的百分数一样就可以实现垂直居中。

CSS:

#container {    position: relative;    width: 600px;    height: 500px;    border: 1px solid #000;    margin: auto;}#box {    position: absolute;    left: 30%;    right: 30%;    top: 25%;    bottom: 25%;    background-color: #0ff;}

 

8.0、button居中(演示)

利用button做外容器,里边的块元素会自动垂直居中,只需要控制一下水平居中就可以达到效果。

HTML:

<button>    <div></div></button>

CSS:

button {    width: 600px;    height: 500px;    border: 1px solid #000;}div {    width: 200px;    height: 200px;    margin: 0 auto;    background-color: #0ff;}

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
HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 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.

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尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version