【问题标题】:Why does my CSS layout keeps breaking when i resize my screen?当我调整屏幕大小时,为什么我的 CSS 布局会不断中断?
【发布时间】:2020-04-25 14:34:06
【问题描述】:
每次练习CSS布局时,每次调整浏览器窗口大小时,站点右侧的屏幕总是被截断,所以会出现一个空白区域,背景为白色。有人可以帮我找出问题所在,这样我就可以永久避免这种情况再次发生吗?这是HTML 代码:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
color: #fff;
}
header {
background: url(http://www.moirmedia.co.uk/wp-content/uploads/2016/05/Backgroundlow.jpg) no-repeat center center/cover;
background-position: inherit;
height: 165vh;
}
nav {
background: black;
padding: 1.5rem 1rem;
display: flex;
justify-content: space-between;
align-items: center;
}
nav h2 {
text-transform: uppercase;
font-size: 2.5rem;
padding-right: 2rem;
}
nav ul {
display: flex;
}
nav ul li {
list-style: none;
position: relative;
right: 5rem;
padding: 0 4rem;
font-size: 1.2rem;
}
nav ul li a {
text-decoration: none;
}
.hero-text {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
height: 100%;
}
.hero-text h1 {
font-size: 4rem;
padding: 1rem;
}
.hero-text p {
font-size: 1.3rem;
text-align: justify;
}
<header>
<nav>
<h2 class="logo">Logo</h2>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
<div class="hero-text">
<h1>Banner Text Heading</h1>
<p>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum <br> Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum <br> Lorem ipsum</p>
</div>
</header>
【问题讨论】:
标签:
html
css
responsive-design
flexbox
【解决方案1】:
从此替换您的 css 代码
nav ul {
display: flex;
}
nav ul li {
list-style: none;
position: relative;
right: 5rem;
padding: 0 4rem;
font-size: 1.2rem;
}
换一个
nav ul {
display: flex;
flex-wrap: wrap;
}
nav ul li {
list-style: none;
position: relative;
padding: 0 2rem;
font-size: 1.2rem;
}
【解决方案2】:
您在nav ul li 上设置的padding 和right 样式将内容推送到父容器之外。使用media queries 添加一些响应式断点将解决您的问题,并让您更好地控制不同屏幕分辨率下的布局。 MDN 有一篇文章Beginner's guide to media queries 提供有关媒体查询的更多信息。
以下是在您的代码中使用媒体查询的示例。请注意每个断点如何指定一个 padding-right 值,您可能需要对其进行调整,以使您的内容符合预期。微调可以是一个过程。我使用来自Typical Device Breakpoints 的断点作为一个很好的起点。
* {
margin: 0;
padding: 0;
box-sizing: border-box;
color: #fff;
}
header {
background: url(http://www.moirmedia.co.uk/wp-content/uploads/2016/05/Backgroundlow.jpg) no-repeat center center/cover;
background-position: inherit;
height: 165vh;
width: 100%;
}
nav {
background: black;
padding: 1.5rem 1rem;
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
}
nav h2 {
text-transform: uppercase;
font-size: 2.5rem;
padding-right: 2rem;
}
nav ul {
display: flex;
}
nav ul li {
list-style: none;
position: relative;
padding: 0 0.5rem;
font-size: 1.2rem;
}
nav ul li a {
text-decoration: none;
}
.hero-text {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
height: 100%;
}
.hero-text h1 {
font-size: 4rem;
padding: 1rem;
}
.hero-text p {
font-size: 1.3rem;
text-align: justify;
}
/* Extra small devices (phones, 600px and down) */
@media only screen and (max-width: 600px) {
nav ul li {
padding-right: 0.5rem;
}
}
/* Small devices (portrait tablets and large phones, 600px and up) */
@media only screen and (min-width: 600px) {
nav ul li {
padding-right: 0.8rem;
}
}
/* Medium devices (landscape tablets, 768px and up) */
@media only screen and (min-width: 768px) {
nav ul li {
padding-right: 1.8rem;
}
}
/* Large devices (laptops/desktops, 992px and up) */
@media only screen and (min-width: 992px) {
nav ul li {
padding-right: 3rem;
}
}
/* Extra large devices (large laptops and desktops, 1200px and up) */
@media only screen and (min-width: 1200px) {
nav ul li {
right: 5rem;
padding-right: 4rem;
}
}
<header>
<nav>
<h2 class="logo">Logo</h2>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
<div class="hero-text">
<h1>Banner Text Heading</h1>
<p>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum <br> Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum <br> Lorem ipsum</p>
</div>
</header>
这是一种替代方法,它使用flexbox 属性(例如justify-content)并将填充放在a 元素上以增加触摸目标的大小,从而提高您网站的可访问性。我建议使用此选项,因为它管理的代码更少,方法更现代,并且可以实现类似的结果。
* {
margin: 0;
padding: 0;
box-sizing: border-box;
color: #fff;
}
header {
background: url(http://www.moirmedia.co.uk/wp-content/uploads/2016/05/Backgroundlow.jpg) no-repeat center center/cover;
background-position: inherit;
height: 165vh;
width: 100%;
}
nav {
background: black;
padding: 1.5rem 1rem;
display: flex;
justify-content: space-between;
align-items: center;
width: 100%;
}
nav h2 {
text-transform: uppercase;
font-size: 2.5rem;
padding-right: 2rem;
}
nav ul {
display: flex;
flex: 1;
justify-content: flex-end;
}
nav ul li {
list-style: none;
position: relative;
font-size: 1.2rem;
}
nav ul li a {
text-decoration: none;
padding: 1rem 0.5rem;
}
nav ul li a:hover {
text-decoration: underline;
}
.hero-text {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
height: 100%;
}
.hero-text h1 {
font-size: 4rem;
padding: 1rem;
}
.hero-text p {
font-size: 1.3rem;
text-align: justify;
}
/* Medium devices (landscape tablets, 768px and up) */
@media only screen and (min-width: 768px) {
nav ul {
justify-content: space-evenly;
}
nav ul li a {
padding: 1rem;
}
}
<header>
<nav>
<h2 class="logo">Logo</h2>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">Services</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
<div class="hero-text">
<h1>Banner Text Heading</h1>
<p>Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum <br> Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum <br> Lorem ipsum</p>
</div>
</header>
这两个选项仍然无法将您的所有nav 项目放在移动设备等小屏幕上的一行中。我建议您考虑替代navigation patterns,例如可折叠菜单,甚至只是将您的内容用flex-wrap: wrap; 包装为@Yudiz_Webdesign noted in their answer。