简体   繁体   English

如何居中绝对位置的div?

[英]How to center a div which position is absolute?

I've got the following code: 我有以下代码:

HTML: HTML:

<div class="label">Middle</div>
<div id='sliderdiv'>
    <input type="range" value="50" min="0" max="100" />
</div>

CSS: CSS:

 .label {
     position: absolute;
     margin-top: 13px;
     margin-left: auto;
     margin-right: auto;
     z-index: 3;
 }
 input.ui-slider-input {
     display: none !important;
 }
 #sliderdiv .ui-slider-track {
     margin-left: 15px;
 }

But I can't center my text div, is there a solution for this problem or is it not possible with css? 但是我无法将我的文本div居中,是否有解决此问题的方法,或者css无法解决?

I could calculate it dynamically with javascript for sure, but I'd like to avoid that. 我可以肯定地使用javascript动态地计算它,但是我想避免这种情况。

Here is the live code for you: http://jsfiddle.net/VmwQM/ 这是为您提供的实时代码: http : //jsfiddle.net/VmwQM/

Edit: 编辑:

This is not what I want: 这不是我想要的:

.label {
         position: absolute;
         margin-top: 13px;
         margin-left: 50%;
         z-index: 3;
     }

Because I want my text middle at 50% and not my text to start at 50%. 因为我希望文本的中间位置为50%,而不是文本的起始位置为50%。

Do you want something like this: 您想要这样的东西吗?

 body {
     text-align: center;
 }
 .label {
   width: 100%;
 }

And your text will be inside the slider: 您的文本将在滑块内:

EXAMPLE

You can try this: 您可以尝试以下方法:

Fiddle here 在这里摆弄

.label {
 position:absolute;
 top:11px;
 width:100%;
 z-index: 3;
 text-align:center;
}
input.ui-slider-input {
 display: none !important;
}
#sliderdiv .ui-slider-track {
 margin-left: 15px;
}

Good Luck.. 祝好运..

You cannot center elements using margin:auto that are absolutely positioned. 您不能使用margin:auto绝对定位的元素居中。 The display-attribute of them is automatically set to inline . 它们的显示属性自动设置为inline

Why margin:auto doesn't work? 为什么margin:auto不起作用?

jQuery-Solution can be: jQuery-Solution可以是:

var left = $(window).width() / 2 - $('.label').width() / 2;
$('.label').css('left',left+'px');

The margin: 0 auto won't work for absolutely positioned element. margin: 0 auto不适用于绝对定位的元素。 Just set the left or right property to 50%: 只需将leftright属性设置为50%:

.label {
  position: absolute;
  margin-top: 13px;
  left: 50%;
  z-index: 3;
}

And don't forget that absolute positioning is calculated with respect to the parent. 并且不要忘记绝对位置是相对于父级计算的。 You may have other markup around beyond that example that would affect what position: absolute does when applied to .label 除了该示例之外,您可能还会有其他标记,这些标记会影响position: absolute应用于.label

use: 采用:

  left: 50%;
  margin-left: -25px;

Working Fiddle 工作小提琴

HTML 的HTML

<div class="slide-holder">
    <div id='sliderdiv'>
        <input type="range" value="50" min="0" max="100" />
    </div>
</div>

CSS 的CSS

.slide-holder {
    text-align: center;
}
#sliderdiv {
    display: inline-block;
}

You can center it provided you give a width value: 只要提供宽度值,就可以居中:

.label {
    position: absolute;
    margin: 13px auto auto;
    width: 3rem;
    right: 0; left: 0;
}

More info on centering absolute elements with just css in this smashing magazine article 在此热门杂志文章中,更多关于仅用CSS居中绝对元素的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM