[英]CSS: Can't get text in relative-positioned div to center align?
The text in my TextWrapper
div won't center align. 我的
TextWrapper
div中的文本不会居中对齐。 text-align: center
worked when I didn't use position: relative
and : absolute
on them, but they are necessary for my jQuery code. 当我不对它们使用
position: relative
和: absolute
时, text-align: center
起作用了,但是它们对于我的jQuery代码而言是必需的。 How do I get the text to center align in the div? 如何使文本在div中居中对齐?
Absolute positioning removes the default behavior of block elements to fill the width of their parent element. 绝对定位将删除块元素的默认行为,以填充其父元素的宽度。 Either add
width: 100%
or left: 0; right: 0;
添加
width: 100%
或left: 0; right: 0;
left: 0; right: 0;
to the absolutely positioned divs. 到绝对定位的div。
$(function(){ $("#TextWrapper div:gt(0)").hide(); setInterval(function(){ var current = $('#TextWrapper div:visible'); var next = current.next().length ? current.next() : $('#TextWrapper div:eq(0)'); current.fadeOut(500); next.fadeIn(500); }, 1000); });
.Border { border: 1px solid black; display: inline-flex; height: 110px; } #ImgAndText { text-align: center; } .Img { width: 75px; height:75px; } #TextWrapper { font-family: Helvetica; font-size: 13px; padding: 5px 5px 5px 5px; position: relative; text-align: center; } #TextWrapper div { position: absolute; text-align: center; left: 0; right: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <div class="Border"> <div id="ImgAndText"> <img src="#" class="Img"> <div id="TextWrapper"> <div>Text</div> <div>Other</div> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.