CSS - position absolute & document flow

↘锁芯ラ 提交于 2019-12-19 09:25:32

问题


Yes, I know doesn't work with position absolute, but is there a way to display elements "below" (after in code) not behind them?

Example:

<img src="image.jpg" style="width: 500px; height: 400px; position: absolute; top: 0;" /> 
<h2 style="padding: 15px" >This text is behind not below the image</h2>

Is there a way of displaying the h2 below the image excepting positioning it absolutely too?

Example:

http://jsfiddle.net/fDGHU/1/

(yes, I have to use absolute in my case, and dynamic margined content below, and I'm lost :D)


回答1:


The only way I was able to do what you are asking is setting the top property of h2, aka positioning the text after the image. Fiddle.

PS: position:block doesn't exist. Only absolute, relative, static, and fixed.




回答2:


For h2:

specify a top margin equal to the height of your image.

eg.

img {
    position: absolute;
    top: 0;
}

h2 {
    margin-top: 400px;
    padding: 40px;
}



回答3:


Simple , just remove position absolute . (tested) If an object is not defined it will automatically go to the right of its neighbour or below




回答4:


How about wrapping the image and the title in an absolute block? This solution puts the title after the image because h2 is a block by default and your content is still absolutely positionned.

<style type="text/css">
.wrapper{
    position: absolute;
    top: 0;
}
h2 {
    padding: 40px;
}
</style>

<div class="wrapper">
    <img src="image_url" alt="image!" />
    <h2>Am I invisible? (not!)</h2>
</div>


来源:https://stackoverflow.com/questions/5009798/css-position-absolute-document-flow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!