為什么網站不能正確適應較小的設備?仍然有一個水平滾動條。在設計視圖中輸入時,移動設備不會顯示我想要的內容。我試圖調整所有內容的大小,但它仍然不起作用。
它可能是某處的邊距或填充嗎?或者專案部分太大?還是與媒體查詢有關?
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-rows: 110px auto 1fr 470px auto auto auto auto auto;
gap: 0px 0px;
grid-template-areas:
"header header header header"
"hero hero hero hero"
"about about about about"
"text text text text"
"project1 project1 project1 project1"
"project2 project2 project2 project2"
"project3 project3 project3 project3"
"project4 project4 project4 project4"
"footer footer footer footer";
}
@media (max-width: 1200px) {
.grid-container {
grid-template-columns: 1fr;
grid-template-rows: auto;
grid-template-areas:
"header"
"hero"
"about"
"text"
"project1"
"project2"
"project3"
"project4"
"footer";
}
}
@media only screen and (max-width: 1200px) {
section.content-container {
margin-left: 10px;
margin-right: 10px;
}
}
@media only screen and (max-width: 1200px) {
footer.content-container {
margin-left: 10px;
margin-right: 10px;
}
}
.header {
grid-area: header;
}
.navbar {
position: absolute;
display: inline-block;
top: 50px;
right: 60px;
font-family: 'Khula', sans-serif;
line-height: 38px;
font-weight: 400;
font-size: 16px;
}
.nav-link {
margin: 18px;
color: #222222;
text-decoration: none;
}
.name {
position: absolute;
display: inline-block;
top: 50px;
left: 60px;
font-family: 'Kanit', sans-serif;
line-height: 30px;
font-weight: 500;
font-size: 28px;
color: #222222;
text-decoration: none;
}
.hero {
grid-area: hero;
padding-top: 90px;
padding-bottom: 250px;
text-align: center;
}
.hero-h1 {
font-family: 'Khula', sans-serif;
font-weight: 600;
font-size: 100px;
color: #bbbbbb;
line-height: 100px;
letter-spacing: -1px;
}
.hero-h1-color {
color: #222222;
}
.container {
max-width: 1240px;
}
.about {
grid-area: about;
background-color: #f5f5f5;
height: 700px;
margin-left: 100px;
margin-right: 100px;
margin-top: 38px;
}
.home-h2-text {
font-family: 'Khula', sans-serif;
letter-spacing: -1px;
padding: 90px 100px 80px;
font-size: 70px;
color: #222222;
line-height: 70px;
}
.home-h3-text {
font-family: 'Khula', sans-serif;
letter-spacing: -1px;
font-size: 35px;
color: #222222;
padding: 90px 100px 80px;
font-weight: 400;
}
.text {
grid-area: text;
height: 500px;
margin-left: 100px;
margin-right: 100px;
font-family: 'Khula', sans-serif;
letter-spacing: -1px;
text-align: center;
font-size: 40px;
color: #bbbbbb;
padding-top: 138px;
line-height: 60px;
}
.text-h3-color {
color: #222222;
}
.project1 {
grid-area: project1;
background-color: #fdf0f2;
height: 700px;
margin-left: 100px;
margin-right: 100px;
margin-top: 38px;
}
.project2 {
grid-area: project2;
background-color: #f8f7ff;
height: 700px;
margin-left: 100px;
margin-right: 100px;
margin-top: 38px;
}
.project3 {
grid-area: project3;
background-color: #fdf0f2;
height: 700px;
margin-left: 100px;
margin-right: 100px;
margin-top: 38px;
}
.project4 {
grid-area: project4;
background-color: #eff5fd;
height: 700px;
margin-left: 100px;
margin-right: 100px;
margin-top: 38px;
}
.footer {
grid-area: footer;
margin-top: 38px;
margin-left: 100px;
margin-right: 100px;
height: 700px;
margin-bottom: 100px;
}
.footer-text {
padding: 200px 100px;
font-family: 'Khula', sans-serif;
font-weight: 600;
font-size: 80px;
color: #bbbbbb;
line-height: 80px;
letter-spacing: -1px;
}
.footer-contact {
font-family: 'Khula', sans-serif;
font-weight: 600;
font-size: 35px;
color: #222222;
line-height: 50px;
letter-spacing: -1px;
text-decoration: none;
padding-top: 0px;
margin-left: 60px;
margin-right: 60px;
}
.footer-text-color {
color: #222222;
}
ul {
list-style-type: none;
text-decoration: none;
}
<div class="grid-container">
<header class="header">
<div><a href=index.html class="name">Web Developer</a></div>
<nav class="navbar">
<a href=about.html class="nav-link">About</a>
<a href=projects.html class="nav-link">Projects</a>
<a href=contact.html class="nav-link">Contact</a>
</nav>
</header>
<div class="hero">
<h1 class="hero-h1">ABC <br /><span class="hero-h1-color">front end developer</span></h1>
</div>
<section class="content-container about">
<h2 class="home-h2-text">Nice to <br />meet you</h2>
<h3 class="home-h3-text">About Me</h3>
</section>
<div class="content-container text">
<h3>ABCDEFG <br /><span class="text-h3-color">ABCDEFG.</span></h3>
</div>
<section class="content-container project1">
<h2 class="home-h2-text">Project 1</h2>
<h3 class="home-h3-text">About Me</h3>
</section>
<section class="content-container project2">
<h2 class="home-h2-text">Project 2</h2>
<h3 class="home-h3-text">About Me</h3>
</section>
<section class="content-container project3">
<h2 class="home-h2-text">Project 3</h2>
<h3 class="home-h3-text">About Me</h3>
</section>
<section class="content-container project4">
<h2 class="home-h2-text">Project 4</h2>
<h3 class="home-h3-text">About Me</h3>
</section>
<footer class="content-container footer">
<div class="footer-text">Let's <br> <span class="footer-text-color">Connect</span></div>
<ul>
<li><a href=about.html class="footer-contact">Email</a></li>
<li><a href=projects.html class="footer-contact">LinkedIn</a></li>
<li><a href=contact.html class="footer-contact">GitHub</a></li>
</ul>
</footer>
</div>
uj5u.com熱心網友回復:
您可以將代碼嵌套在我所做的 flex 容器中。例如,我稱它為main-wrapper。然后我向該類添加了一些 flex-box 樣式并應用overflow: hidden;使其不滾動。然后我添加了一些示例媒體查詢以在調整大小時對齊導航組件。它們標記在 CSS 的底部,標記為/* additions */“隨意更改”,但這應該會讓您走上正確的軌道。
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-rows: 110px auto 1fr 470px auto auto auto auto auto;
gap: 0px 0px;
grid-template-areas:
"header header header header"
"hero hero hero hero"
"about about about about"
"text text text text"
"project1 project1 project1 project1"
"project2 project2 project2 project2"
"project3 project3 project3 project3"
"project4 project4 project4 project4"
"footer footer footer footer";
justify-items: center;
}
@media (max-width: 1200px) {
.grid-container {
grid-template-columns: 1fr;
grid-template-rows: auto;
grid-template-areas:
"header"
"hero"
"about"
"text"
"project1"
"project2"
"project3"
"project4"
"footer";
}
}
@media only screen and (max-width: 1200px) {
section.content-container {
margin-left: 10px;
margin-right: 10px;
width: 90vw;
}
}
@media only screen and (max-width: 1200px) {
footer.content-container {
margin-left: 10px;
margin-right: 10px;
}
}
.header {
grid-area: header;
}
.navbar {
position: absolute;
display: inline-block;
top: 50px;
right: 60px;
font-family: 'Khula', sans-serif;
line-height: 38px;
font-weight: 400;
font-size: 16px;
}
.nav-link {
margin: 18px;
color: #222222;
text-decoration: none;
}
.name {
position: absolute;
display: inline-block;
top: 50px;
left: 60px;
font-family: 'Kanit', sans-serif;
line-height: 30px;
font-weight: 500;
font-size: 28px;
color: #222222;
text-decoration: none;
}
.hero {
grid-area: hero;
padding-top: 90px;
padding-bottom: 250px;
text-align: center;
}
.hero-h1 {
font-family: 'Khula', sans-serif;
font-weight: 600;
font-size: 100px;
color: #bbbbbb;
line-height: 100px;
letter-spacing: -1px;
}
.hero-h1-color {
color: #222222;
}
.container {
max-width: 1240px;
}
.about {
grid-area: about;
background-color: #f5f5f5;
height: 700px;
margin-left: 100px;
margin-right: 100px;
margin-top: 38px;
width: 80vw;
}
.home-h2-text {
font-family: 'Khula', sans-serif;
letter-spacing: -1px;
padding: 90px 100px 80px;
font-size: 70px;
color: #222222;
line-height: 70px;
}
.home-h3-text {
font-family: 'Khula', sans-serif;
letter-spacing: -1px;
font-size: 35px;
color: #222222;
padding: 90px 100px 80px;
font-weight: 400;
}
.text {
grid-area: text;
height: 500px;
margin-left: 100px;
margin-right: 100px;
font-family: 'Khula', sans-serif;
letter-spacing: -1px;
text-align: center;
font-size: 40px;
color: #bbbbbb;
padding-top: 138px;
line-height: 60px;
}
.text-h3-color {
color: #222222;
}
.project1 {
grid-area: project1;
background-color: #fdf0f2;
height: 700px;
margin-left: 100px;
margin-right: 100px;
margin-top: 38px;
}
.project2 {
grid-area: project2;
background-color: #f8f7ff;
height: 700px;
margin-left: 100px;
margin-right: 100px;
margin-top: 38px;
}
.project3 {
grid-area: project3;
background-color: #fdf0f2;
height: 700px;
margin-left: 100px;
margin-right: 100px;
margin-top: 38px;
}
.project4 {
grid-area: project4;
background-color: #eff5fd;
height: 700px;
margin-left: 100px;
margin-right: 100px;
margin-top: 38px;
}
.footer {
grid-area: footer;
margin-top: 38px;
margin-left: 100px;
margin-right: 100px;
height: 700px;
margin-bottom: 100px;
}
.footer-text {
padding: 200px 100px;
font-family: 'Khula', sans-serif;
font-weight: 600;
font-size: 80px;
color: #bbbbbb;
line-height: 80px;
letter-spacing: -1px;
}
.footer-contact {
font-family: 'Khula', sans-serif;
font-weight: 600;
font-size: 35px;
color: #222222;
line-height: 50px;
letter-spacing: -1px;
text-decoration: none;
padding-top: 0px;
margin-left: 60px;
margin-right: 60px;
}
.footer-text-color {
color: #222222;
}
ul {
list-style-type: none;
text-decoration: none;
}
/* additions */
.main-wrapper {
display: flex;
justify-content: center;
overflow-x: hidden;
max-width: 100%;
height: 100%;
}
@media only screen and (max-width: 650px) {
.logo > .name {
position: absolute;
top: 10px;
left: 35%;
}
.logo {
width: 100%;
}
.navbar {
position: relative;
display: flex;
justify-content: center;
align-items: center;
align-content: center;
right: 0;
}
.content-container .about {
width: 100vw;
}
body {
margin: 0;
}
h2 {
white-space: nowrap;
}
.footer-text {
align-items: center;
}
}
.content-container {
width: 80vw;
}
.footer-text {
display: flex;
justify-content: center;
flex-direction: column;
}
<div class="main-wrapper">
<div class="grid-container">
<header class="header">
<div class="logo"><a href=index.html class="name">Web Developer</a></div>
<nav class="navbar">
<a href=about.html class="nav-link">About</a>
<a href=projects.html class="nav-link">Projects</a>
<a href=contact.html class="nav-link">Contact</a>
</nav>
</header>
<div class="hero">
<h1 class="hero-h1">ABC <br /><span class="hero-h1-color">front end developer</span></h1>
</div>
<section class="content-container about">
<h2 class="home-h2-text">Nice to <br />meet you</h2>
<h3 class="home-h3-text">About Me</h3>
</section>
<div class="content-container text">
<h3>ABCDEFG <br /><span class="text-h3-color">ABCDEFG.</span></h3>
</div>
<section class="content-container project1">
<h2 class="home-h2-text">Project 1</h2>
<h3 class="home-h3-text">About Me</h3>
</section>
<section class="content-container project2">
<h2 class="home-h2-text">Project 2</h2>
<h3 class="home-h3-text">About Me</h3>
</section>
<section class="content-container project3">
<h2 class="home-h2-text">Project 3</h2>
<h3 class="home-h3-text">About Me</h3>
</section>
<section class="content-container project4">
<h2 class="home-h2-text">Project 4</h2>
<h3 class="home-h3-text">About Me</h3>
</section>
<footer class="content-container footer">
<div class="footer-text">Let's <br> <span class="footer-text-color">Connect</span></div>
<ul>
<li><a href=about.html class="footer-contact">Email</a></li>
<li><a href=projects.html class="footer-contact">LinkedIn</a></li>
<li><a href=contact.html class="footer-contact">GitHub</a></li>
</ul>
</footer>
</div>
</div>
編輯:防止帶有初始滾動的水平滾動。我保留了main-wrapper我添加的div 來添加樣式。您可以使用max-width: 100%;和overflow-x: hidden;來避免水平滾動。
EDIT 2設定width: 80vw;為與以前相同的寬度。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/390296.html
