要使HTML元素居中,可以使用CSS left: 50%;
。但是,这样会将元素相对于整个窗口居中。
我有一个元素,它是<div>
元素的子元素,并且我想将该子元素与父<div>
元素居中对齐,而不是整个窗口。
我不希望容器<div>
的所有内容都居中,只有一个特定的子元素需要这样做。
要使HTML元素居中,可以使用CSS left: 50%;
。但是,这样会将元素相对于整个窗口居中。
我有一个元素,它是<div>
元素的子元素,并且我想将该子元素与父<div>
元素居中对齐,而不是整个窗口。
我不希望容器<div>
的所有内容都居中,只有一个特定的子元素需要这样做。
将text-align:center;
设置到父级div中,将margin:auto;
设置到子级div中。
#parent {
text-align:center;
background-color:blue;
height:400px;
width:600px;
}
.block {
height:100px;
width:200px;
text-align:left;
}
.center {
margin:auto;
background-color:green;
}
.left {
margin:auto auto auto 0;
background-color:red;
}
.right {
margin:auto 0 auto auto;
background-color:yellow;
}
<div id="parent">
<div id="child1" class="block center">
a block to align center and with text aligned left
</div>
<div id="child2" class="block left">
a block to align left and with text aligned left
</div>
<div id="child3" class="block right">
a block to align right and with text aligned left
</div>
</div>
这是一个很好的资源,可以让大部分内容居中显示。
http://howtocenterincss.com/
使用CSS3的flex boxes非常简单。
.flex-container{
display: -webkit-box; /* OLD - iOS 6-, Safari 3.1-6, BB7 */
display: -ms-flexbox; /* TWEENER - IE 10 */
display: -webkit-flex; /* NEW - Safari 6.1+. iOS 7.1+, BB10 */
display: flex; /* NEW, Spec - Firefox, Chrome, Opera */
justify-content: center;
align-items: center;
width: 400px;
height: 200px;
background-color: #3498db;
}
.inner-element{
width: 100px;
height: 100px;
background-color: #f1c40f;
}
<div class="flex-container">
<div class="inner-element"></div>
</div>
看起来我在回答时没有注意到原帖的编辑。上述代码将使所有内部元素居中(它们之间不会重叠),但是原帖想要特定元素居中,而不是其他内部元素。因此@Warface的第二种方法更为适合,但仍需要垂直居中:
.flex-container{
position: relative;
/* Other styling stuff */
width: 400px;
height: 200px;
background-color: #3498db;
}
.inner-element{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
/* or 3d alternative if you will add animations (smoother transitions) */
transform: translate3d(-50%,-50%,0);
/* Other styling stuff */
width: 100px;
height: 100px;
background-color: #f1c40f;
}
<div class="flex-container">
<p>Other inner elements like this follows the normal flow.</p>
<div class="inner-element"></div>
</div>
CSS
body{
text-align:center;
}
.divWrapper{
width:960px //Change it the to width of the parent you want
margin: 0 auto;
text-align:left;
}
超文本标记语言
<div class="divWrapper">Tada!!</div>
这将使div居中
2016年 - HTML5 + CSS3 方法
CSS
div#relative{
position:relative;
}
div#thisDiv{
position:absolute;
left:50%;
transform: translateX(-50%);
-webkit-transform: translateX(-50%);
}
HTML
<div id="relative">
<div id="thisDiv">Bla bla bla</div>
</div>
修改了一下
<div class="d-flex justify-content-center">
// the elements you want to center
</div>
这将适用于内部元素数量的任何情况。
仅使特定的子元素居中:
.parent {
height: 100px;
background-color: gray;
position: relative;
}
.child {
background-color: white;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 20px;
height:20px;
margin: auto;
}
<div class="parent">
<span class="child">hi</span>
</div>
或者,你也可以使用flex,但这会使所有子元素都居中
.parent {
height: 100px;
background-color: gray;
display: flex;
justify-content: center;
align-items: center;
}
.child {
background-color: white;
}
<div class="parent">
<span class="child">hi</span>
</div>
在父级 div
上加上 text-align:center;
就可以解决问题。
#element { /* this is the child div */
margin-left:auto;
margin-right:auto;
/* Add remaining styling here */
}
或者您可以将父级div设置为text-align:center;
,并将子div设置为text-align:left;
。
left:50%;
仅在将div设置为position:absolute;
时根据整个页面居中。如果您将div设置为left:50%;
,它应该相对于父div的宽度进行居中。对于固定宽度,请执行以下操作:
#parent {
width:500px;
}
#child {
left:50%;
margin-left:-100px;
width:200px;
}
只需给父级div添加 position: relative
属性。
display:grid;
- 两个指令 (display/place-items) 都应用于父容器。同时请注意,父容器需要一个高度才能垂直居中。*(与 flexbox 解决方案非常相似(也应用于父容器):* display:flex; justify-content:center; align-items:center;
) - cssyphus.container{
display:flex;
color:blue;
background-color: yellow;
justify-content:center;
}
<div class="container">
<div>Element</div>
</div>
.container{
display:flex;
color:blue;
height:100px;
background-color: yellow;
justify-content:center;
align-items:center;
}
<div class="container">
<div>Element</div>
</div>
<i>
元素上似乎无法使用text-align
属性。但是给它一个类可以解决问题。 - nclsvhdisplay: table-cell
或flexbox
来实现此功能。请查看我添加的链接。 - pasine