【发布时间】:2017-05-29 00:37:29
【问题描述】:
我在div标签内的网页中间有一个文本,实际上它还没有在中间,但我默认需要它在中间。那里有一个按钮,当你点击时,中间的文字会改变。问题是,当我更改文本时,它不再居中。我想要的是,无论其文本内容如何,文本都应始终位于中心。
$("#myButton").on('click', function() {
$('#throbber_status_info').html('TEXXXXXXXXXXXXXT');
});
#throbber_status_info {
position: absolute;
color: black;
top: 53%;
left: 50%;
margin: 0px auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="myButton">
click me
</button>
<div id="throbber_status_info" style="">Text</div>
【问题讨论】:
-
你真的需要绝对定位吗?
-
是的,我需要绝对定位
-
@Reymark 请务必查看链接的重复项 - 接受的答案概述了将绝对定位的可变宽度元素居中所需的确切 CSS 样式。
-
知道了@Serlite,下次我会检查它。谢谢
标签: javascript jquery html css