CSS背景矩形无法铺满设备全屏问题求助
Hero背景矩形无法铺满屏幕宽度的解决方案
问题原因分析
.background类设置了width: max-content,该属性会让元素宽度仅适配内部内容,无法拉伸至屏幕宽度- 父容器
section#hero的全局flex布局(justify-content: center)会让.background居中显示,进一步限制其宽度扩展 - 全局变量
--body-bg的值ffffff缺少前缀#,导致页面背景色未生效 - 描述文本
.desc使用了与背景色相同的var(--secondary-blue),导致文字无法看清
修复步骤
1. 调整核心布局样式
- 移除
.background的width: max-content,改为width: 100%确保铺满屏幕 - 删除全局
section标签的flex样式,避免干扰Hero区域布局 - 给
.background添加min-height,确保背景覆盖Hero区域全部高度
2. 修正变量与文本可读性
- 给
--body-bg添加#,改为#ffffff - 将
.desc的文字颜色改为var(--secondary-gray),提升文字辨识度
3. 修复布局细节错误
- 修正CSS中嵌套错误的
.header样式 - 调整
#hero .container的justify-content属性,优化桌面端内容分布
修改后的完整代码
CSS代码
@charset "utf-8"; /* CSS Document */ @import url('https://fonts.googleapis.com/css2?family=Playfair+Display:wght@500&display=swap'); /*Variable*/ :root { --primary-color: #0d081a; --secondary-blue: #eaf0f9; --secondary-gray: #48463b; --body-bg: #ffffff; /* 修复:添加# */ } /*Global Styles*/ * { padding: 0; margin: 0; box-sizing: border-box; } html { font-family: Arial, Helvetica, sans-serif; font-size: 10px; scroll-behavior: smooth; } body { background-color: var(--body-bg); font-family: Arial, Helvetica, sans-serif; line-height: 1.6; margin: 0; min-height: 100vh; } img { width: 100%; height: 100%; } a { color: black; text-decoration: none; } /* 移除全局section的flex布局,避免影响Hero */ .background { border: 0; padding: 0; width: 100%; /* 修复:替换max-content为100% */ background-color: var(--secondary-blue); min-height: calc(100vh - 10rem); /* 确保背景覆盖Hero剩余高度 */ display: flex; align-items: center; } .container { width: 90%; height: 100%; margin: 0 auto; } .primary-btn { display: inline-block; background-color: var(--primary-color); color: white; font-size: 1.8rem; padding: 0.6em 1.6em; border-radius: 50px; } .buttons { margin-top: 3rem; display: flex; align-items: center; justify-content: flex-start; gap: 2rem; } /*nav*/ #navigation { position: fixed; right: 50px; height: auto; top: 20px; z-index: 100; } /*hero section*/ #hero { height: 100vh; width: 100%; padding-top: 10rem; } #hero .container { display: flex; align-items: center; justify-content: space-between; /* 优化:替换center为space-between */ gap: 2rem; } #hero .container .left { flex: 6; } #hero .container .right { flex: 8; } #hero .left .subheading { font-size: 1.8rem; font-weight: 500; text-transform: uppercase; color: var(--primary-color); } #hero .left .heading { font-size: 6rem; font-family: 'Times New Roman', Times, serif; font-weight: 600; } #hero .left .desc { margin-top: 2.5rem; max-width: 400px; font-size: 1.5rem; /* 修复:去掉空格 */ color: var(--secondary-gray); /* 修复:替换为灰色提升可读性 */ } #hero .right { text-align: right; } #hero .right img { width: 100%; max-width: 450px; height: 600px; object-fit: cover; object-position: 50% 30%; border-radius: 12px; } @media only screen and (max-width: 768px) { html { font-size: 9px; } #hero .container { flex-direction: column-reverse; } #hero .container .right { text-align: center; flex: 1; margin-bottom: 2rem; } #hero .container .left { text-align: center; padding-right: 0; flex: 1; height: fit-content; } #hero .left .buttons { justify-content: center; } #hero .left .heading { font-size: 4rem; margin: 0 auto; } #hero .container .left .desc { margin: 0 auto; margin-top: 2rem; } #hero .right img { width: 100%; height: 400px; } } @media only screen and (max-width: 1200px) { #hero .container .right { flex: 6; } } /* End Hero Section*/ /*Navbar*/ ul { margin: 0; padding: 0; list-style: none; } a { text-decoration: none; } .main-nav { margin-top: 5px; } .logo a, .main-nav a { padding: 10px 15px; text-transform: uppercase; text-align: center; display: block; } .main-nav a { color: #34495e; font-size: 1.5em; } .main-nav a:hover { color: #718daa; } .header { padding-top: 1em; padding-bottom: 1em; } /* Nav Media Queries*/ @media (min-width: 769px) { .header, .main-nav { display: flex; } .header { flex-direction: column; align-items: flex-end; } /* 修复:拆分嵌套错误的样式 */ .header.inner { width: 80%; margin: 0 auto; max-width: 1150px; } } @media (min-width: 1025px) { .header { flex-direction: row; justify-content: flex-end; } }
HTML代码
<body> <!--nav section--> <header class="nav"> <ul class="main-nav"> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Portfolio</a></li> <li><a href="#">Contact</a></li> </ul> </header> <!--end nav section--> <!--hero section--> <section id="hero"> <div class="background"> <div class="container"> <div class="left"> <p class="subheading"></p> <h2 class="heading"> <div class="wrapper"><span>Hello, nice </span></div> <div class="wrapper"><span>to"meet" you!</span></div> </h2> <p class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> <div class="buttons"> <a href="#" class="primary-btn">Contact Me</a> </div> </div> <div class="right"> <img src="/linkedinheadshot.png" alt="picture"> </div> </div> </div> </section> <!--End hero section--> </body>
内容的提问来源于stack exchange,提问作者user4986569
相关产品推荐
相关产品推荐

