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

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

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

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

有任何想法吗?


#1楼

真的很好

width900px

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

在这种情况下,应该事先知道width


#2楼

我想补充@bobince的答案:

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

改进:///这使得水平滚动条在居中的div中不带有大元素。

<body>
    <div style="width:100%; position: absolute; overflow:hidden;">
        <div style="position:fixed; 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>
    </div>
</body>

#3楼

在寻找解决方案时,我在上面得到了答案,并且可以使内容以Matthias Weiler的答案为中心,但使用text-align。

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

使用chrome和Firefox。


#4楼

据我所知,这是不可能实现的未知宽度。

您可以-如果在您的情况下可行-绝对定位宽度和高度为100%的不可见元素,并使用margin:自动并可能垂直对齐在其中居中。 否则,您将需要Javascript来做到这一点。


#5楼

这是一个有用的jQuery插件来执行此操作。 在这里找到。 我认为单纯使用CSS不可能

/**
 * @author: Suissa
 * @name: Absolute Center
 * @date: 2007-10-09
 */
jQuery.fn.center = function() {
    return this.each(function(){
            var el = $(this);
            var h = el.height();
            var w = el.width();
            var w_box = $(window).width();
            var h_box = $(window).height(); 
            var w_total = (w_box - w)/2; //400
            var h_total = (h_box - h)/2;
            var css = {"position": 'absolute', "left": w_total+"px", "top":
h_total+"px"};
            el.css(css)
    });
};

#6楼

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


#7楼

我想出了一个技巧,可以使DIV精确地浮动在页面的中央。 当然非常丑陋,但可以使用

点和短划线

<div style="border: 5 dashed red;position:fixed;top:0;bottom:0;left:0;right:0;padding:5">
    <table style="position:fixed;" width="100%" height="100%">
        <tr>
            <td style="width:50%"></td>
            <td style="text-align:center">
                <div style="width:200;border: 5 dashed green;padding:10">
                    Perfectly Centered Content
                </div>
            </td>
            <td style="width:50%"></td>
        </tr>
    </table>
</div>

清洁器

哇,五年过去了,不是吗?

<div style="position:fixed;top:0px;bottom:0px;left:0px;right:0px;padding:5px">
<table style="position:fixed" width="100%" height="100%">
    <tr>
        <td style="width:50%"></td>
        <td style="text-align:center">
            <div style="padding:10px">
                <img src="Happy.PM.png">
                <h2>Stays in the Middle</h2>
            </div>
        </td>
        <td style="width:50%"></td>
    </tr>
</table>


#8楼

您可以将图像放置在div中并添加div ID,并使该div的CSS具有text-align:center

HTML:

<div id="intro_img">

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

</div>

CSS:

#intro_img {
    text-align:center;
}

#9楼

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

这和更多的例子在这里


#10楼

绝对中心

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

演示: http //jsbin.com/rexuk/2/

在Google Chrome,Firefox和IE8中测试

希望这可以帮助 :)


#11楼

我首选的居中方法:

position: absolute;
margin: auto;
width: x%
  • 绝对块元素定位
  • 保证金自动
  • 相同的左/右,上/下

JSFiddle 在这里


#12楼

响应式解决方案

如果您不需要支持IE8及更低版本,这通常是响应式设计或未知尺寸的好解决方案

.centered-axis-x {
    position: absolute;
    left: 50%;
    transform: translate(-50%, 0);
}

 .outer { position: relative; /* or absolute */ /* unnecessary styling properties */ margin: 5%; width: 80%; height: 500px; border: 1px solid red; } .inner { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); /* unnecessary styling properties */ max-width: 50%; text-align: center; border: 1px solid blue; } 
 <div class="outer"> <div class="inner">I'm always centered<br/>doesn't matter how much text, height or width i have.<br/>The dimensions or my parent are irrelevant as well</div> </div> 

这是一个JS小提琴

提示是left: 50%相对于父级,而translate转换相对于元素的宽/高。

这样,您将拥有一个完美居中的元素,并且在子元素和父元素上都具有灵活的宽度。 奖励:即使孩子比父母大,这也能起作用。

您也可以以此将它垂直居中(同样,父母和孩子的宽度和高度可以完全灵活(和/或未知)):

.centered-axis-xy {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
}

请记住,您可能还需要transform供应商前缀。 例如-webkit-transform: translate(-50%,-50%);


#13楼

#container
{
  position: relative;
  width: 100%;
  float:left
}
#container .item
{
  width: 50%;
  position: absolute;
  margin: auto;
  left: 0;
  right: 0;
}

#14楼

的HTML

<div id='parent'>
  <div id='centered-child'></div>
</div>

的CSS

#parent {
  position: relative;
}
#centered-child {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto auto;
}

http://jsfiddle.net/f51rptfy/


#15楼

这项工作垂直和水平

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

如果要使元素以父级为中心,请设置父级相对位置

 #parentElement{
      position:relative
  }

编辑:

  • 对于垂直居中,将设置的高度与元素对齐。 感谢@Raul

  • 如果要使元素的父级居中,请将父级的位置设置为相对


#16楼

我知道我已经提供了答案,并且以前的答案以及给出的其他答案也很好用。 但是我过去曾经使用过它,它在某些浏览器和某些情况下效果更好。 所以我想id也要给出这个答案。 我没有“编辑”以前的答案并添加它,因为我觉得这是一个完全独立的答案,而我提供的这两个答案无关。

HTML:

<div id='parent'>
  <div id='child'></div>
</div>

CSS:

#parent {
  display: table;
}
#child {
  display: table-cell;
  vertical-align: middle;
}

#17楼

响应式解决方案的sass / compass版本:

#content {
  position: absolute;
  left: 50%;
  top: 50%;
  @include vendor(transform, translate(-50%, -50%));
}

#18楼

这为我工作:

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

.container{
position: absolute;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
}

#19楼

 #content { margin:0 auto; display:table; float:none;} 
 <body> <div> <div id="content"> I'm the content </div> </div> </body> 


#20楼

我知道这个问题已经有了一些答案,但是我还没有找到一个可以在几乎所有类中都可行并且很优雅的解决方案,因此,在调整一堆之后,这是我的看法:

 .container { position: relative; } .container .cat-link { position: absolute; left: 50%; top: 50%; transform: translate3d(-50%,-50%,0); z-index: 100; text-transform: uppercase; /* Forces CSS to treat this as text, not a texture, so no more blurry bugs */ background-color: white; } .color-block { height: 250px; width: 100%; background-color: green; } 
 <div class="container"> <a class="cat-link" href="">Category</a> <div class="color-block"></div> </div> 

这就是说给我一个top: 50% ,一个left: 50% ,然后在X / Y轴上都将(转换空间)转换为-50%值,在某种意义上是“创建镜像空间”。

因此,这将在div的所有4个点上创建相等的空间,该点始终是一个方框(具有4个边)。

这将:

  1. 无需知道父母的身高/宽度即可工作。
  2. 响应迅速。
  3. 在X或Y轴上加工。 或两者皆有,例如我的示例。
  4. 我无法提出无法解决的情况。

#21楼

一个对我有用的简单方法是将一个未知宽度的块水平居中:

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

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

可以将文本对齐属性添加到#block规则集中,以独立于块的对齐方式对齐其内容。

这适用于最新版本的Firefox,Chrome,IE,Edge和Safari。


#22楼

您可以在容器元素的中心想要在绝对定位元素的任意未知宽度上工作。

演示版

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

#23楼

如果元素具有widthheight则此解决方案有效

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


#24楼

这个问题的解决方案不适用于我的情况。。我正在为一些图像添加标题,并且我解决了这个问题

top: 0;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;

display: flex;
align-items: center;

 figure { position: relative; width: 325px; display: block } figcaption{ position: absolute; background: #FFF; width: 120px; padding: 20px; -webkit-box-shadow: 0 0 30px grey; box-shadow: 0 0 30px grey; border-radius: 3px; display: block; top: 0; left: 0; right: 0; margin-left: auto; margin-right: auto; display: flex; align-items: center; } 
 <figure> <img src="https://picsum.photos/325/600"> <figcaption> But as much </figcaption> </figure> 


#25楼

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

.center:before {
    content: '';
    display: inline-block;
    margin-left: -50%;
}

#26楼

Flex可用于将绝对定位的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> 


#27楼

如果您也需要水平和垂直居中:

left: 50%;
top: 50%;
transform: translate(-50%, -50%);

#28楼

您还可以创建中间件div#centered框,该框的absoluteleftright属性居中,没有width属性,然后将主内容div设置为其子级,并带有display:inline-block框,并以text-align:center为其设置text-align:center其中间件父盒

 #container{ position: relative; width: 300px; height: 300px; border: solid 1px blue; color:#dddddd; } #centered{ position: absolute; text-align: center; margin: auto; top: 20px; left:0; right:0; border: dotted 1px red; padding: 10px 0px; } #centered>div{ border: solid 1px red; display:inline-block; color:black; } 
 <div id="container"> hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world hello world <div id="centered"> <div> hello world <br/> I don't know my width<br/> but I'm still absolute! </div> </div> </div> 


#29楼

这是对我们有用的其他答案的组合:

.el {
   position: absolute;
   top: 50%;
   margin: auto;
   transform: translate(-50%, -50%);
}

#30楼

这对我有用:

 #content { position: absolute; left: 0; right: 0; margin-left: auto; margin-right: auto; width: 100px; /* Need a specific value to work */ } 
 <body> <div> <div id="content"> I'm the content </div> </div> </body> 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值