900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > 前端css实现左侧盒子宽度固定 右侧宽度自适应布局

前端css实现左侧盒子宽度固定 右侧宽度自适应布局

时间:2021-01-17 07:22:19

相关推荐

前端css实现左侧盒子宽度固定 右侧宽度自适应布局

效果图

方法1:通过calc方法做宽度自适应

设置width: calc(100% - 100px);

(1)这里减100px是示例中有left类名的div宽度,具体减多少px根据情况来

(2)减号两侧的空格不能少,不然识别不了

<!DOCTYPE html><html lang="en"><head><style>*{margin: 0;padding: 0;}.main{height: 300px;background: #ccc;}/* 左侧div */.left{/* 宽度固定100px */width: 100px;height: 100px;background: #b8e5ff;float: left;}/* 右侧div */.right{/* 100% - 100px(左侧盒子的宽度) */width: calc(100% - 100px);height: 100px;background: #d1ffd6;float: left;}</style></head><body><div class="main"><div class="left">Left</div><div class="right">Right</div></div></body></html>

方法2:通过弹性盒flex做自适应

父级容器设置display: flex;需要自适应宽度的元素设置flex: 1;

<!DOCTYPE html><html lang="en"><head><style>*{margin: 0;padding: 0;}.main{height: 300px;background: #ccc;display: flex;}/* 左侧div */.left{/* 宽度固定100px */width: 100px;height: 100px;background: #b8e5ff;float: left;}/* 右侧div */.right{flex: 1;height: 100px;background: #d1ffd6;float: left;}</style></head><body><div class="main"><div class="left">Left</div><div class="right">Right</div></div></body></html>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。