我试图以200为中心框200我尝试过使用left:50% top:50%
等,但这是某种程度上不工作。箱子的位置不居中
我创建了一个小提琴重现我的问题:https://jsfiddle.net/8k9o9Lvv/2/
我也试图从中央顶部的文本以及与text-align:center
这也是行不通的。
任何想法为什么这不起作用?
HTML
<div id ="container">
<div class="slider-text">
<h2>Test</h2>
</div>
</div>
CSS
#container{
width:100%;
}
.slider-text {
text-align:center;
position:relative;
height:200px;
width: 200px;
border-left:1px solid red;
border-right:1px solid red;
border-top:1px solid red;
border-bottom:1px solid red;
top:50%;
left:50%;
right:50%;
}
您正在使用'position:relative'和'top,bottom,left,right'。更改为'位置:绝对'并给予'容器位置:相对' –
请参阅:https://jsfiddle.net/zainabr1/8k9o9Lvv/6/ –