[英]Absolute positioning within relative positioning issue
I'm a complete novice at absolute positioning, and i can't work out why the H2 text in the div is being horizontally centered... I'm only trying to center it vertically. 我是一个绝对定位的新手,我不知道为什么div中的H2文本水平居中...我只想垂直居中。
HTML HTML
<div class="column1">
<div class="alignCenter">
<img style="float:left;width:40%;min-width:300px;max-width:400px;" src="http://dummyimage.com/500x500/000/fff.png&text=SampleImage" alt="">
<h2 style="float:left;width:60%;max-width:850px";>Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla!Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! Bla! </h2><br style="clear:both">
</div>
</div>
CSS CSS
.alignCenter{
position:relative;
width:70%;
margin: 0 auto;
}
.alignCenter h2{
position:absolute;
top:30%;
}
It looks to me like your div
is constraining the h2
--that is, because you have the width: 70%;
在我看来,您的div
限制了h2
也就是说,您的width: 70%;
, your div
is only taking up 70% of the screen. ,您的div
仅占据屏幕的70%。 Yet the entire div
itself is centered, due to the margin: 0 auto;
但是由于margin: 0 auto;
,整个div
本身居中margin: 0 auto;
property you have applied. 您申请的属性。 Therefore, both the image and the text are appearing at the left-most boundary of the div
. 因此,图像和文本都出现在div
的最左边界。
The reason the first image isn't at the right like the second one is because you haven't applied the float: right;
第一张图片不像第二张图片在右侧的原因是,因为您尚未应用float: right;
property to it, instead you have it at float: left;
属性,而是将其放置在float: left;
. 。
If you want the image to be offset from the text, you will have to give it its own position within the div
. 如果要使图像与文本偏移,则必须在div
为其赋予自己的位置。 Otherwise, make the div
width wider, then apply the position properties to the image and the h2
respectively. 否则,使div
宽度更宽,然后分别将位置属性应用于图像和h2
。
See this fiddle: http://jsfiddle.net/kX4bh/2/ . 看到这个小提琴: http : //jsfiddle.net/kX4bh/2/ 。 Notice how I made the width of the div
to be the full screen, and offset the image by using the left: 20px;
请注意,我是如何使div
的宽度成为全屏的,并使用left: 20px;
偏移图像left: 20px;
style. 样式。
I'm not sure what specifically you are trying to accomplish so I can't be any more specific in my answer then this. 我不确定您要具体完成什么工作,所以我的回答不能再具体了。 If you clarify what you need then I can help you more. 如果您明确需要什么,我可以为您提供更多帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.