CSS:<div>元素中的center元素

Ran*_*lue 155 css

为了使HTML元素居中,我可以使用CSS left: 50%;.然而,这使元素相对于整个窗口居中.

我有一个元素,它是一个元素的子<div>元素,我想让孩子相对于这个父元素居中<div>,而不是整个窗口.

我不希望容器<div>所有内容集中在一起,只是一个特定的孩子.

我怎样才能做到这一点?

pas*_*ine 242

设置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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

这是一个很好的资源来集中任何东西.
http://howtocenterincss.com/

  • text-align用于居中任何类型的元素. (10认同)
  • 嗯...我不希望整个父div有中心文本.虽然这解决了这个问题,但却创造了另一个问题! (3认同)
  • 你可以设置text-align:left; 到子元素,使文本正确对齐. (3认同)

Ger*_*hes 44

实际上这对于CSS3 flex box来说非常简单.

.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;
}
Run Code Online (Sandbox Code Playgroud)
<div class="flex-container">
  <div class="inner-element"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

更新:

在我写这个答案的时候,我似乎没有读过OP编辑.上面的代码将以所有内部元素为中心(它们之间没有重叠),但OP只需要一个特定元素居中,而不是其他内部元素.所以@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;
}
Run Code Online (Sandbox Code Playgroud)
<div class="flex-container">
  <p>Other inner elements like this follows the normal flow.</p>
  <div class="inner-element"></div>
</div>
Run Code Online (Sandbox Code Playgroud)


War*_*ace 10

CSS

  body{
    text-align:center;
    }
    .divWrapper{
    width:960px //Change it the to width of the parent you want
    margin: 0 auto;
    text-align:left;
    }
Run Code Online (Sandbox Code Playgroud)

HTML

<div class="divWrapper">Tada!!</div>
Run Code Online (Sandbox Code Playgroud)

这应该以div为中心

2016年 - HTML5 + CSS3方法

CSS

div#relative{
  position:relative;
}

div#thisDiv{
  position:absolute;
  left:50%;
  transform: translateX(-50%);
  -webkit-transform: translateX(-50%);
}
Run Code Online (Sandbox Code Playgroud)

HTML

<div id="relative">
  <div id="thisDiv">Bla bla bla</div>
</div>
Run Code Online (Sandbox Code Playgroud)

Fiddledlidle

https://jsfiddle.net/1z7m83dx/


Kev*_*sox 6

text-align:center; 在父 div 上应该可以解决问题

  • 这不是我想要对齐的_text_,而是整个元素(例如按钮)。 (2认同)

Aya*_*yan 6

仅将特定孩子居中:

.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;

}
Run Code Online (Sandbox Code Playgroud)
<div class="parent">
   <span class="child">hi</span>
</div>  
Run Code Online (Sandbox Code Playgroud)

或者,您也可以使用 flex,但这会使所有孩子居中

.parent {
  height: 100px;
  background-color: gray;
  display: flex;
  justify-content: center;
  align-items: center;
}

.child {
  background-color: white;  
}
Run Code Online (Sandbox Code Playgroud)
<div class="parent">
   <span class="child">hi</span>
</div>
Run Code Online (Sandbox Code Playgroud)


小智 6

您可以像这样使用 bootstrap flex 类名:

<div class="d-flex justify-content-center">
    // the elements you want to center
</div>
Run Code Online (Sandbox Code Playgroud)

即使内部有多个元素,这也将起作用。