CSS收缩元素而不会导致跳转到下一行

时间:2013-04-19 15:58:16

标签: html css responsive-design

我想缩小红色元素,并在正确的位置使用固定宽度的蓝色元素。

查看示例:http://jsfiddle.net/YUTFc/

<div class="wrapper">
   <div class="red">1</div>
   <div class="blue">2</div>
</div>

.wrapper{
    max-width: 300px;
    height: 100px;
    margin: 0 auto;
}

.red{
    height: 100%;
    width: 80%; 
    background-color: red;
    float: left;
}

.blue{
    height: 100%;
    width: 60px;
    background-color: blue;
    float: right;
}

调整窗口大小。

我该怎么办?谢谢。

2 个答案:

答案 0 :(得分:1)

试试这个:

.wrapper {
    max-width:300px;
    height:100px;
    margin:0 auto;
    position:relative;
}
.red, .blue {
    position:absolute;
    top: 0; bottom: 0;
}
.red {
    background-color:red;
    left:0; right:60px;
}
.blue {
    background-color:blue;
    width:60px; right:0;
}

Updated Fiddle

答案 1 :(得分:0)

如果您的内容大小不可预测,通过CSS将元素转换为表格元素将是您最好的选择。

http://jsfiddle.net/YUTFc/2/

.wrapper{
    display: table;
    max-width: 300px;
    height: 100px;
    margin: 0 auto;
}

.red{
    height: 100%;
    width: 80%; 
    background-color: red;
    display: table-cell;
}

.blue{
    height: 100%;
    width: 60px;
    background-color: blue;
    display: table-cell;
}
相关问题