html - 父级中的两个 div(左、右),右边固定宽度,左边填充可用空间,都在同一行。 (没有位置相对/绝对)

标签 html css stylesheet

<分区>

我遇到了棘手的问题,我需要在一行中放置两个 div(leftright),right 必须已修复宽度,但 left 必须填充可用空间,换句话说:left div 必须有 100% - X 像素,right div 应该是 X 像素.

重点:没有位置相对/绝对 hack。

有什么办法可以达到这个效果。我尝试了很多方法,但都没有成功。

这里是 jsfiddle

标记

<html>
<head>
    <title></title>
</head>
<body>
    <style>
        .container {
            /* container don't matter */
            width: 500px;
            background-color: bisque;
            height: 50px;
        }

            .container .left {
               /* display: inline-block; */ 
                margin-right: 50px;
                background-color: burlywood;
                height: 50px;
            }

            .container .right {
                float: right;
                background-color: chartreuse;
                width: 50px;
                height: 50px;
            }
    </style>
    <div class="container">
        <div class="left">
            fill free space (100% - right)
        </div>
        <div class="right">
            fixed width
        </div>
    </div>
</body>
</html>

最佳答案

你可以这样做:

JSFiddle - DEMO

CSS:

.container {
    width: 500px;
    background-color: bisque;
    height: 50px;
    display: table;
}
.container .left {
    background-color: burlywood;
    height: 50px;
    display: table-cell;
    width: 100%;
}
.container .right {
    background-color: chartreuse;
    width: 50px;
    height: 50px;
    display: inline-block;
    vertical-align: text-top;
}

关于html - 父级中的两个 div(左、右),右边固定宽度,左边填充可用空间,都在同一行。 (没有位置相对/绝对),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25836638/

相关文章:

javascript - 如何制作多模态(使用纯 css)以定位唯一 ID?

c# - 如何在 .NET (C#) 中获取 UserControl 的 HTML 输出?

css - Overflow-x 不适用于平板电脑和移动设备

css - 删除 Bootstrap 工具提示箭头上方的灰色边框

asp.net - 如何检测鼠标左键按下时的移动

javascript - 为什么我无法在 Bootstrap 导航中使用下拉菜单?

javascript - 基于下拉的onclick按钮href

jquery - 使用 jQuery 制作背景 slideDown 的更好方法?

css - 使用 :focus to style outer div?

javascript - AngularJS 指令 : Storing Scripts and Stylesheets