分类分类
更新时间:2026-05-11 03:30:24作者:yezheng
本文介绍了详解左右宽度固定中间自适应html布局解决方案,分享给大家,具体如下:
a.使用浮动布局
html结构如下
<div class="box">
<div class="left">left</div>
<div class="right">right</div>
<div class="center">center</div>
</div>
//此处注意要先渲染左、右浮动的元素才到中间的元素。元素浮动后剩余兄弟块级元素会占满父元素的宽度
<style>
.box{
height:200px;
}
.left{
float:left;
width:300px;
}
.right{
float:right;
width:300px;
}
</style>
b.使用固定定位
html结构如下
<div class="box">
<div class="left">left</div>
<div class="right">right</div>
<div class="center">center</div>
</div>
//和浮动布局同理,先渲染左右元素,使其定位在父元素的左右两端,剩余的中间元素占满父元素剩余宽度。
<style>
.box{
position: relative;
}
.left{
position: absolute;
width: 100px;
left: 0;
}
.right{
width:100px;
position: absolute;
right: 0;
}
.center{
margin: 0 100px;
background: red;
}
</style>
c.表格布局
将父元素display:table,子元素display:table-cell,会将它变为行内块。
这种布局方式的优点是兼容性好。
<div class="box">
<div class="left">
left
</div>
<div class="center">
center
</div>
<div class="right">
right
</div>
</div>
<style>
.box{
display: table;
width: 100%;
}
.left{
display: table-cell;
width: 100px;
left: 0;
}
.right{
width:100px;
display: table-cell;
}
.center{
width: 100%;
background: red;
}
</style>
d.弹性布局
父元素display:flex子元素会全部并列在一排。
子元素中flex:n的宽度会将父元素的宽度/n
如flex:1,宽度就等于父元素高度。
弹性布局的缺点是兼容性不高,目前IE浏览器无法使用弹性布局
<div class="box">
<div class="left">
left
</div>
<div class="center">
center
</div>
<div class="right">
right
</div>
</div>
<style>
.box{
display: flex;
width: 100%;
}
.left{
width: 100px;
left: 0;
}
.right{
width:100px;
}
.center{
flex:1;
}
</style>
e.网格布局
父元素display:grid;
grid-templatecolumns:100px auto 100px;
依次为第一个子元素宽100px 第二个自适应 第三个100px;
网格布局的优点是极为简便,直接通过父元素样式决定,缺点是兼容性不高。
<div class="box">
<div class="left">
left
</div>
<div class="center">
center
</div>
<div class="right">
right
</div>
</div>
<style>
.box{
display: grid;
grid-template-columns: 100px auto 100px;
width: 100%;
}
</style>
以上就是本文的全部内容,希望对大家的学习有所帮助
相关
修真情缘角色扮演303.33 MBv1.0.02026-05-10
下载我的小独角兽女孩休闲益智380.89 MBv1.0.232026-05-10
下载超级种田男孩手机版经营养成1.31Gv1.0.92026-05-10
下载不必要的实验冒险游戏64.9 MBv1.1.592026-05-10
下载军师联盟策略游戏163.2 MBv0.0.12026-05-10
下载翻天喜地凑大钱手机版休闲益智138.72 MBv1.0.92026-05-10
下载东离剑游纪手游动作射击1.6Gv1.4.22026-05-10
下载天猫养车商家版App学习办公74.95 MBv0.6.12026-05-10
下载掌上命运方舟App手游辅助137.48 MBv1.9.12026-05-10
下载退役军人服务APP生活服务67.16 MBv1.2.52026-05-10
下载屯漫漫画APP资讯阅读23.06 MBv2.0.32026-05-10
下载蛋仔派对蛋壳App手游辅助185.73 MBv0.0.22026-05-10
下载









