【发布时间】:2014-08-18 10:50:25
【问题描述】:
我有一个响应式页面,我需要在其中插入不同高度的各种内容:
上半部分是带有渐变叠加(红色)的图像。
在它下面是一个颜色相同(蓝色)的 div,所以看起来这两个 div 之间的过渡很平滑。
问题是内容(黄色)需要下推蓝色 div,具体取决于黄色 div 内有多少内容,但黄色 div 需要position: absolute 才能显示在它所在的位置。
这在纯 CSS 中可行吗?还是我需要 javascript 来计算蓝色 div 的高度?
HTML
<div class="pr">
<img src="http://www.cphrecmedia.dk/musikdk/stage/demobilleder/detartistchannel2.jpg">
<div id="campaignreleases"></div>
<img id="camlogo" src="http://www.cphrecmedia.dk/musikdk/stage/css/lyttesession.svgz" alt="MusikDK logo">
</div>
<div id="camrel">
<img src="http://www.cphrecmedia.dk/musikdk/stage/demobilleder/detlyttealbum.jpg" alt="ALBUMNAME">
<p>Below I need to add whatever I'll need. So basically it needs to push down the "camrel" div, but all this content also needs to be positioned just below the logo (inside the "pr" div)</p>
</div>
CSS
body {
background: #ddd
}
.pr {
position: relative;
width: 800px
}
.pr img {
width: 100%
}
#campaignreleases {
position: absolute;
top:0;
bottom:0;
left:0;
right:0;
background: -moz-linear-gradient(top, rgba(26, 188, 156, 0.2) 0%, rgba(26, 188, 156, 1) 100%);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, rgba(26, 188, 156, 0.2)), color-stop(100%, rgba(26, 188, 156, 1)));
}
#camlogo {
position: absolute;
-webkit-transform: translateX(-50%);
left: 50%;
top:15%;
opacity: 1 !important;
max-width: 500px
}
#camrel {
background: #1ABC9C;
text-align: center;
overflow: auto;
}
#camrel img {
width: 250px;
.roundedcorners;
}
【问题讨论】:
-
Go'dag Morten,在 oprette en jsfiddle 的 være så venlig。请设置一个 jsfiddle,以便我们可以看到您的代码。塔克。
-
是红色和蓝色的div只是为了放在后台吗?如果是这样,为什么不在正文上使用 CSS 背景?
-
@JezD:给你:jsfiddle.net/Lhj65aam/1
-
@misterManSam:几乎是对的。蓝色 div 的高度需要灵活,因为黄色 div 内的内容可以改变高度。我不确定将它用作 css-backgrounds 会如何改变问题?
-
取决于您想要实现的目标,但这就是 CSS 背景的用途!类似this example