保持可扩展div的纵横比

时间:2014-03-28 06:49:17

标签: html css twitter-bootstrap responsive-design aspect-ratio

我正在使用bootstrap2进行设计

<div class="row">
    <div class="span-4">
    </div>
    <div class="span-8">
        <div id="expandable" style="width:100%">
         here I want to change the height of this div according to the width size.
        </div>
    </div>
</div>

当我拖动浏览器时,它会改变'span-8'div的大小。

我想保持身高4:3的宽度。

例如,当宽度为400时,我想保持高度300。 当宽度为200时,高度应为150

有可能吗?

1 个答案:

答案 0 :(得分:2)

我已经回答了这个问题here。 重点是使用虚拟div和绝对定位的div来制作具有固定宽高比的响应元素。此解决方案仅使用CSS。

要使其适应您的情况,您可以这样做:

<强> DEMO

HTML:

<div class="row">
    <div class="span-4"></div>
    <div class="span-8">
        <div class="expandable">
            here I want to change the height of this div according to the width size.
        </div>
        <div class="dummy"></div>
    </div>
</div>

CSS:

.span-8 {
    position: relative;
}
.dummy {
    padding-top: 75%;
}
.expandable {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background:gold;
}
相关问题