此框通过将其左右边距宽度设置为“自动”来水平居中。这是使用CSS完成水平居中的首选方法,并且在大多数支持CSS 2的浏览器中运行良好。不幸的是,Internet Explorer 5/Windows不响应此方法-该浏览器的缺点,而不是技术。
有一个简单的解决方法。(在你反击这个词引起的恶心时停顿一下。)准备好了吗?Internet Explorer 5/Windows错误地将CSS“text-的对齐”属性应用于块级元素。为包含块级元素(通常是BODY元素)声明“text-的对齐:中心”水平居中Internet Explorer 5/Windows中的框。
此解决方法有一个副作用:继承了CSS“文本对齐”属性,将内联内容居中。通常需要为居中框显式设置“文本对齐”属性,以抵消Internet Explorer 5/Windows解决方法的效果。相关的CSS如下。
.box {display: flex;flex-wrap: wrap; /* Optional. only if you want the items to wrap */justify-content: center; /* For horizontal alignment */align-items: center; /* For vertical alignment */}
.box {display: flex;flex-wrap: wrap;/* Optional. only if you want the items to wrap */justify-content: center;/* For horizontal alignment */align-items: center;/* For vertical alignment */}* {margin: 0;padding: 0;}html,body {height: 100%;}.box {height: 200px;display: flex;flex-wrap: wrap;justify-content: center;align-items: center;border: 2px solid tomato;}.box div {margin: 0 10px;width: 100px;}.item1 {height: 50px;background: pink;}.item2 {background: brown;height: 100px;}.item3 {height: 150px;background: orange;}
#container {display: table;width: <width of your container>;height: <height of your container>;}
#inner {width: <width of your center div>;display: table-cell;margin: 0 auto;text-align: center;vertical-align: middle;}
Chris Coyier在他的博客上写了一个关于“以未知为中心”的优秀的帖子。这是多个解决方案的综述。我发布了一个没有在这个问题中发布的。它比Flexbox解决方案有更多的浏览器支持,你没有使用display: table;,这可能会破坏其他东西。
/* This parent can be any width and height */.outer {text-align: center;}
/* The ghost, nudged to maintain perfect centering */.outer:before {content: '.';display: inline-block;height: 100%;vertical-align: middle;width: 0;overflow: hidden;}
/* The element to be centered, canalso be of any width and height */.inner {display: inline-block;vertical-align: middle;width: 300px;}
请注意,您必须使用跨度(和inline-block),在现代浏览器+Internet Explorer 8中进行了测试。超文本标记语言:
<h1>Center dynamic box using only css test</h1><div class="container"><div class="center"><div class="center-container"><span class="dyn-box"><div class="dyn-head">This is a head</div><div class="dyn-body">This is a body<br />Content<br />Content<br />Content<br />Content<br /></div></span></div></div></div>
div#outer {height: 120px;background-color: red;}
div#inner {width: 50%;height: 100%;background-color: green;margin: 0 auto;text-align: center; /* For text alignment to center horizontally. */line-height: 120px; /* For text alignment to center vertically. */}
body {font: medium sans-serif;}
.outer {overflow: hidden;background-color: papayawhip;}
.center-helper {display: inline-block;position: relative;left: 50%;background-color: burlywood;}
.inner {display: inline-block;position: relative;left: -50%;background-color: wheat;}
<div class="outer"><div class="center-helper"><div class="inner"><h1>A div with no defined width</h1><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br>Duis condimentum sem non turpis consectetur blandit.<br>Donec dictum risus id orci ornare tempor.<br>Proin pharetra augue a lorem elementum molestie.<br>Nunc nec justo sit amet nisi tempor viverra sit amet a ipsum.</p></div></div></div>
最简单的方法之一就是使用 display: flex。外部的 div 只需要有显示伸缩,而内部需要 margin: 0 auto使其水平居中。
要垂直居中并在另一个 div 中居中一个 div,请查看。下面的内部阶级
.wrapper {
display: flex;
/* Adding whatever height & width we want */
height: 300px;
width: 300px;
/* Just so you can see it is centered */
background: peachpuff;
}
.inner {
/* center horizontally */
margin: 0 auto;
/* center vertically */
/* margin: auto 0; */
/* center */
/* margin: 0 auto; */
}
<div class="wrapper">
<div class="inner">
I am horizontally!
</div>
</div>
#inner {
width: 100%;
max-width: 65px; /* To adapt to screen width. It can be whatever you want. */
left: 65px; /* This has to be approximately the same as the max-width. */
}
<h1>Flexbox Center Horizontally and Vertically Center Align an Element</h1>
<h2>justify-content: center; align-items: center;</h2>
<div class="wrapper">
<div class="parentClass">
<div>c</div>
</div>
</div>
<h1>The text-align Property</h1>
<div class="a">
<h2>text-align: center:</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper diam at erat pulvinar, at pulvinar felis blandit. Vestibulum volutpat tellus diam, consequat gravida libero rhoncus ut.</p>
</div>
<div class="b">
<h2>text-align: left:</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper diam at erat pulvinar, at pulvinar felis blandit. Vestibulum volutpat tellus diam, consequat gravida libero rhoncus ut.</p>
</div>
<div class="c">
<h2>text-align: right:</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper diam at erat pulvinar, at pulvinar felis blandit. Vestibulum volutpat tellus diam, consequat gravida libero rhoncus ut.</p>
</div>
<div class="d">
<h2>text-align: justify:</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam semper diam at erat pulvinar, at pulvinar felis blandit. Vestibulum volutpat tellus diam, consequat gravida libero rhoncus ut.</p>
</div>