如何在div中将绝对定位的元素居中?

CSS

小宇宙神无

2020-03-13

我需要在窗口中心放置一个div(带有position:absolute;)元素。但是我这样做有问题,因为宽度未知

我试过了 但是由于宽度是响应性的,因此需要对其进行调整。

.center {
  left: 50%;
  bottom:5px;
}

有任何想法吗?

第1565篇《如何在div中将绝对定位的元素居中?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

19个回答
Green达蒙 2020.03.13

A simple approach that worked for me to horizontally center a block of unknown width:

<div id="wrapper">
  <div id="block"></div>
</div>

#wrapper { position: absolute; width: 100%; text-align: center; }
#block { display: inline-block; }

A text-align property may be added to the #block ruleset to align its content independently of the alignment of the block.

This worked on recent versions of Firefox, Chrome, IE, Edge and Safari.

JinJinGreen伽罗 2020.03.13

You can place the image in a div and add a div id and have the CSS for that div have a text-align:center

HTML:

<div id="intro_img">

    <img src="???" alt="???">

</div>

CSS :

#intro_img {
    text-align:center;
}
达蒙阿飞斯丁 2020.03.13
.center {
  position: absolute
  left: 50%;
  bottom: 5px;
}

.center:before {
    content: '';
    display: inline-block;
    margin-left: -50%;
}
伽罗理查德 2020.03.13

HTML:

<div class="wrapper">
    <div class="inner">
        content
    </div>
</div>

CSS:

.wrapper {
    position: relative;

    width: 200px;
    height: 200px;

    background: #ddd;
}

.inner {
    position: absolute;
    top: 0; bottom: 0;
    left: 0; right: 0;
    margin: auto;

    width: 100px;
    height: 100px;

    background: #ccc;
}

This and more examples here

神无L 2020.03.13

This solution works if the element has width and height

.wrapper {
  width: 300px;
  height: 200px;
  background-color: tomato;
  position: relative;
}

.content {
  width: 100px;
  height: 100px;
  background-color: deepskyblue;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
}
<div class="wrapper">
  <div class="content"></div>
</div>

樱LEY神无 2020.03.13
#container
{
  position: relative;
  width: 100%;
  float:left
}
#container .item
{
  width: 50%;
  position: absolute;
  margin: auto;
  left: 0;
  right: 0;
}
GOL蛋蛋 2020.03.13

sass/compass version of Responsive Solution above:

#content {
  position: absolute;
  left: 50%;
  top: 50%;
  @include vendor(transform, translate(-50%, -50%));
}
Me无敌小哥 2020.03.13

My preferred centering method:

position: absolute;
margin: auto;
width: x%
  • absolute block element positioning
  • margin auto
  • same left/right, top/bottom

JSFiddle here

别坑我路易 2020.03.13

This worked for me :

<div class="container><p>My text</p></div>

.container{
position: absolute;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
}
小卤蛋樱 2020.03.13

Flex can be used to center absolute positioned div.

display:flex;
align-items:center;
justify-content:center;

.relative {
  width: 275px;
  height: 200px;
  background: royalblue;
  color: white;
  margin: auto;
  position: relative;
}

.absolute-block {
  position: absolute;
  height: 36px;
  background: orange;
  padding: 0px 10px;
  bottom: -5%;
  border: 1px solid black;
}

.center-text {
  display: flex;
  justify-content: center;
  align-items: center;
  box-shadow: 1px 2px 10px 2px rgba(0, 0, 0, 0.3);
}
<div class="relative center-text">
  Relative Block
  <div class="absolute-block center-text">Absolute Block</div>
</div>

A猪猪 2020.03.13

** RESPONSIVE SOLUTION **

Assuming the element in the div, is another div...

this solution works fine

<div class="container">
  <div class="center"></div>
</div>

the container can be any size(must be position relative)

.container {
position: relative;/*important*/
width: 200px;/*any width*/
height: 200px;/*any height*/
background: red;
}

The element(div) can also be any size(must be smaller than container)

.center {
position: absolute;/*important*/
top: 50%;/*position Y halfway in*/
left: 50%;/*position X halfway in*/
transform: translate(-50%,-50%);/*move it halfway back(x,y)*/
width: 100px;/*any width*/
height: 100px;/*any height*/
background: blue;
}

The result will look like this. Run the code snippet

.container {
	position: relative;
	width: 200px;
	height: 200px;
	background: red;
}

.center {
	position: absolute;
	top: 50%;
	left: 50%;
	transform: translate(-50%,-50%);
	width: 100px;
	height: 100px;
	background: blue;
}
<div class="container">
	<div class="center"></div>
</div>

I found it very helpfull

Gil前端 2020.03.13

This is a mix of other answers, which worked for us:

.el {
   position: absolute;
   top: 50%;
   margin: auto;
   transform: translate(-50%, -50%);
}
西里猴子 2020.03.13

Works on any random unknown width of the absolute positioned element you want to have in the centre of your container element.

Demo

<div class="container">
  <div class="box">
    <img src="https://picsum.photos/200/300/?random" alt="">
  </div>
</div>

.container {
  position: relative;
  width: 100%;
}

.box {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
}
神无乐 2020.03.13

If you need to center horizontally and vertically too:

left: 50%;
top: 50%;
transform: translate(-50%, -50%);
神无宝儿达蒙 2020.03.13

Searching for an solution I got answers above and could make content centered with Matthias Weiler answer but using text-align.

#content{
  position:absolute;
  left:0;
  right:0;
  text-align: center;
}

Worked with chrome and Firefox.

Harry小卤蛋 2020.03.13

this work for vertical and horizontal

  #myContent{
        position: absolute;
        left: 0;
        right: 0;
        top:0;
        bottom:0;
        margin: auto;
   }

and if you want make element center of parent, set position of parent relative

 #parentElement{
      position:relative
  }

edit:

  • for vertical center align set height to your element. thanks to @Raul

  • if you want make element center of parent, set position of parent to relative

Gil村村 2020.03.13

Absolute Centre

HTML :

<div class="parent">
  <div class="child">
    <!-- content -->
  </div>
</div>

CSS :

.parent {
  position: relative;
}

.child {
  position: absolute;

  top: 0;
  right: 0;
  bottom: 0;
  left: 0;

  margin: auto;
}

Demo: http://jsbin.com/rexuk/2/

Tested in Google Chrome, Firefox, and IE8

Hope this helps :)

阿飞猿 2020.03.13

Really nice post.. Just wanted to add if someone wants to do it with single div tag then here the way out:

Taking width as 900px.

#styleName {
    position: absolute;
    left: 50%;
    width: 900px;
    margin-left: -450px;
}

In this case one should know the width beforehand.

凯理查德 2020.03.13

<body>
  <div style="position: absolute; left: 50%;">
    <div style="position: relative; left: -50%; border: dotted red 1px;">
      I am some centered shrink-to-fit content! <br />
      tum te tum
    </div>
  </div>
</body>

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android