gpt4 book ai didi

html - 让一个 div 响应地占据网页宽度的其余部分

转载 作者:太空宇宙 更新时间:2023-11-04 10:07:12 26 4
gpt4 key购买 nike

我正在学习响应式网页设计,但遇到了一个小问题。我有一个 <aside>和一个 <section>并排。我希望 aside 的大小相同,而 section 自动占据其余的宽度。这可能吗?

这是代码。

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<head>

<body>
<aside id="sidebar">text</aside>

<section id="main">main section</section>
</body>
</html>

这是CSS。

*{
margin: 0px;
padding: 0px;
}

#main{
display: inline-block;
vertical-align: top;
background-color: red;

margin: -4px;
}

aside{
display: inline-block;
vertical-align: top;
width: 200px;
height: 100%;
margin: -4px;
background-color: blue;
}

这是 jsfiddle。 https://jsfiddle.net/ehftnp3u/

最佳答案

使用flex

#container .row {
display: flex;
}
#container aside {
width: 150px;
}
#container section {
flex: 1;
}
#main{
background-color: red;
}
<div id="container">
<div class="row">
<aside>
<span>sidebar</span>
</aside>
<section id="main">
<span>main section</span>
</section>
</div>
</div>


对于旧浏览器支持,您可以使用 display: tablefloat

示例使用 float

#container .row:after {     /* clear fix for your float  */
content: '';
display: table;
clear:both;
}
#container aside {
float: left;
width: 150px;
}
#container section {
margin-left: 150px;
}
#main{
background-color: red;
}
<div id="container">
<div class="row">
<aside>
<span>sidebar</span>
</aside>
<section id="main">
<span>main section</span>
</section>
</div>
</div>

关于html - 让一个 div 响应地占据网页宽度的其余部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37903401/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com