html - 如何在列表元素中获取 2 个 div 元素以使用页面上的可用宽度?

标签 html css width html-lists

我希望我的“设置”div 使用页面上的可用宽度。有时是 1000px,有时是 780px,有时是 600px。 我尝试将宽度设置为 100%,但这导致 div 被放在下一行,而不是我想要的:在“标题”div 旁边。 因此,在下面的示例中,带有“性别”的 div 始终为 180 像素宽,但设置 div(值为“男性”)应采用页面上的可用宽度。

我该怎么做?

<style>
.profilesettings {
    padding-left:0px;
    display:inline-block;
}
.profilesettings > li {
    width:100%;
}

.profilesettings li.header {
    border-bottom:1px solid #888;
    margin-bottom:10px;
    margin-top:10px;
    font-weight:bold;
    font-size:14px;
    color:#901C51;
}

.profilesettings div.title {
width:180px;
    float:left;
}

.profilesettings div.title h3 {
    font-weight:normal;
    font-size:12px;
}

.profilesettings div.setting {
width:780px;
    float:left;
    padding-bottom:6px;
}
</style>


<ul class="profilesettings">
    <li class="header"><h2>Details</h2></li>
    <li>
        <div class="title">Gender</div>
        <div class="setting">
            Male
        </div>
    </li>
</ul>   

最佳答案

Demo Fiddle

首先,确保 ul 的宽度为 100%,然后从 .profilesettings div.setting 中移除 float 和宽度,并简单地给它 overflow:隐藏;

html,body, ul{
    width:100%; /* <--- ensure the list is the max width of the page */
    margin:0;
    padding:0;
}
.profilesettings {
    padding-left:0px;
    display:inline-block;
}
.profilesettings > li {
    width:100%;
}
.profilesettings li.header {
    border-bottom:1px solid #888;
    margin-bottom:10px;
    margin-top:10px;
    font-weight:bold;
    font-size:14px;
    color:#901C51;
}
.profilesettings div.title {
    width:180px;
    float:left;
}
.profilesettings div.title h3 {
    font-weight:normal;
    font-size:12px;
}
.profilesettings div.setting {
    overflow:hidden; /* <-- remove float and width and add overflow to force div to take up remaining space */
    padding-bottom:6px;
}

关于html - 如何在列表元素中获取 2 个 div 元素以使用页面上的可用宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23497565/

相关文章:

html - 没有填充的棋盘 HTML 列表 <li>

javascript - HTML/JS 行尾标点符号错误地左对齐

html - 使位置固定不可滚动

css - 我们可以将 Mat-card 与 Anchor Tag 一起使用吗?

C# WPF 窗口宽度、MaxWidth、MinWidth 被忽略

javascript - 如何将左侧位置的 div 放置在屏幕边缘的另一个 div 和右侧位置?

html - 将滤镜渐变应用于单元格时,IE9 中不显示单元格边框

javascript - 如何在html文件中包含外部babel js(es 6)文件?

php - 根据背景图像的主色(在文本区域中)更改字体颜色

WPF 给出不正确的大小值