如何去除屏幕左右空白,让图片与文本铺满屏幕宽度?
解决屏幕两侧空白,让内容铺满全屏的方案
你的问题根源很明确:.container类设置了padding-left: 8%和padding-right: 8%,这就是屏幕两侧空白的来源。之前调整flex-basis、宽度百分比没用,是因为最外层容器的内边距限制了所有子元素的可用宽度。
按照以下步骤修改即可:
1. 移除容器的左右内边距
修改.container的CSS,去掉左右padding:
.container { width: 100%; min-height: 100vh; /* 移除左右内边距 */ /* padding-left: 8%; padding-right: 8%; */ box-sizing: border-box; overflow: hidden; }
2. 调整文本区域的左侧间距
原来的.col-1有margin-left: 50px,现在容器没有内边距了,这个margin可以保留(避免文本贴左边缘),或者改成百分比更适配不同屏幕:
.col-1 { flex-basis: 30%; position: relative; /* 保留固定间距,或者改成2%之类的百分比 */ margin-left: 50px; }
3. 调整导航栏的元素间距
导航栏里的logo和菜单会贴边,给.navbar加一点左右内边距:
.navbar { width: 100%; display: flex; align-items: center; /* 给导航栏加内边距,避免元素贴边 */ padding: 0 20px; box-sizing: border-box; }
4. 修复响应式布局的图片宽度
在移动端媒体查询里,.col-2 .controller的宽度是77%,改成100%让图片铺满:
@media only screen and (max-width:700px) { /* ... 其他代码不变 ... */ .col-2 .controller { width: 100%; } /* 同时给移动端的.col-1去掉或减小margin,避免太贴边 */ .col-1 { margin-left: 20px; } }
修改后的完整CSS代码
* { margin: 0; padding: 0; font-family: sans-serif; } .container { width: 100%; min-height: 100vh; box-sizing: border-box; overflow: hidden; } .navbar { width: 100%; display: flex; align-items: center; padding: 0 20px; box-sizing: border-box; } .logo { width: 50px; cursor: pointer; margin: 30px 0; } .menu-icon { width: 25px; cursor: pointer; display: none; } nav { flex: 1; text-align: right; } nav ul li { list-style: none; display: inline-block; margin-right: 30px; margin-top: 0; padding-top: 0; } nav ul li a { text-decoration: none; color: #000; font-size: 14px; margin-top: 0; padding-top: 0; } nav ul li a:hover { color: gray; } .row { display: flex; margin: 0; width: 100%; align-items: center; padding: 0; } .col-1 { flex-basis: 30%; position: relative; margin-left: 50px; } .col-1 h2 { font-size: 54px; } .col-1 h3 { font-size: 30px; color: #707070; font-weight: 100; margin: 20px 0 10px } .col-1 p { font-size: 16px; color: #b7b7b7; font-weight: 100; } button { width: 140px; border: 0; padding: 12px 10px; outline: none; background: white; border: #000 solid 2px; cursor: pointer; } button:hover { background-color: brown; } .col-1:after { content: ""; width: 10px; height: 57%; background-color: black; position: absolute; left: -40px; top: 8px; } .col-2 { flex-basis: 70%; display: flex; } .col-2 .controller { height: 530px; width: 100%; } .social-links img { height: 13px; margin: 20px; cursor: pointer; } .social-links { text-align: center; } @media only screen and (max-width:700px) { nav ul { width: 100%; background: white; position: absolute; top: 75px; right: 0; z-index: 2; } nav ul li { display: block; margin-top: 10px; margin-bottom: 10px; } nav ul li a { color: #000; } .menu-icon { display: block; } #menuList { overflow: hidden; transition: 0.5s; } .row { flex-direction: column-reverse; margin: 50px 0; } .col-2 { flex-basis: 100%; margin-bottom: 50px; } .col-2 .controller { width: 100%; } .col-1 { flex-basis: 100%; margin-left: 20px; } .col-1 h2 { font-size: 35px; } .col-1 h3 { font-size: 15px; } }
这样修改后,图片和文本就能铺满整个屏幕宽度,同时保留了导航栏和文本区域的合理间距,不会出现内容贴边的问题。
内容的提问来源于stack exchange,提问作者Samarth Bagga
相关产品推荐
相关产品推荐

