我不知道为什么我的标题和正文溢出了480px*859px的浏览器窗口。
它在其他一些设备上是平衡的,但在这个特定的分辨率下不平衡。我认为问题是因为我为 h1 和 body 标签分配了宽度,为什么我这样做是因为我想根据我给出的设计每行实现相同的单词,或者让我说我希望文本对齐完全匹配,但事实并非如此。
我试图使我的代码文本与文本对齐属性完全匹配,但这不起作用,然后我为我的标签分配了一个宽度,然后它可以工作,但无法在所有设备上正确显示
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
html {
font-size: 15px;
}
/*Card Styles*/
body {
background-color: hsl(233, 47%, 7%);
font-family: 'Inter', sans-serif;
font-weight: 400;
}
main {
width: 80%;
max-width: 900px;
margin: 1.5em;
background-color: hsl(244, 38%, 16%);
display: grid;
grid-template-columns: 1fr 1fr;
margin: 8.5em auto;
border-radius: 6px;
}
.first {
background-image: url('images/image-header-desktop.jpg');
background-color: hsl(277, 64%, 61%);
background-blend-mode: multiply;
order: 1;
}
.second {
padding: 3em;
}
h1 {
color: hsl(0, 0%, 100%);
width: 350px;
}
h1>span {
color: hsl(277, 64%, 61%);
}
p {
color: hsla(0, 0%, 100%, 0.75);
margin-top: 1em;
width: 400px;
font-family: 'Lexend Deca', sans-serif;
font-weight: 400;
line-height: 1.4;
}
dl {
margin-top: 2em;
display: flex;
justify-content: space-between;
}
dl dt {
color: hsl(0, 0%, 100%);
font-weight: 700;
font-size: 1.25rem;
}
dl dd {
font-variant-caps: all-petite-caps;
color: hsla(0, 0%, 100%, 0.6);
}
/*media query styles */
@media(max-width:480px) {
body {
min-height: 100vh;
padding: 2em 1em;
}
main {
width: 100%;
grid-template-rows: 250px 1fr;
/*height property cannot be used to adjust height of grid items only grid template rows*/
grid-template-columns: auto;
margin: 3em auto;
overflow: hidden;
}
.first {
background-image: url('images/image-header-mobile.jpg');
background-color: hsl(277, 64%, 61%);
background-blend-mode: multiply;
background-size: cover;
order: -1;
}
.second {
padding: 1.5em;
display: grid;
grid-template-rows: 1fr 1fr 2fr;
}
h1 {
width: 300px;
text-align: center;
margin: 0 auto;
}
p {
width: 245px;
text-align: center;
line-height: 1.6;
margin: 0 auto;
}
dl {
flex-direction: column;
gap: 1.5em;
justify-content: center;
align-items: center;
}
dl div {
width: 71px;
/*giving all flex items equal width to make them all align*/
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- displays site properly based on user's device -->
<link rel="icon" type="image/png" sizes="32x32" href="./images/favicon-32x32.png">
<link rel="stylesheet" href="styles.css">
<!--fonts styles-->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&family=Lexend+Deca&display=swap" rel="stylesheet">
<title>Frontend Mentor | Stats preview card component</title>
</head>
<body>
<main class="container">
<div class="first">
<!--container for background image-->
</div>
<div class="second">
<h1>Get <span>insights</span> that help your business grow.</h1>
<p>Discover the benefits of data analytics and make better decisions regarding revenue, customer experience, and overall efficiency.</p>
<div class="stats">
<dl>
<div>
<dt>10k+</dt>
<dd>companies</dd>
</div>
<div>
<dt>314</dt>
<dd>templates</dd>
</div>
<div>
<dt>12M+</dt>
<dd>queries</dd>
</div>
</dl>
</div>
</div>
</main>
</body>
</html>
您在 PX 中使用宽度,因此您的问题正在生成,如果您需要像这样使用 PX,请使用除宽度之外的最大宽度
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
html {
font-size: 15px;
}
/*Card Styles*/
body {
background-color: hsl(233, 47%, 7%);
font-family: 'Inter', sans-serif;
font-weight: 400;
}
main {
width: 80%;
max-width: 900px;
margin: 1.5em;
background-color: hsl(244, 38%, 16%);
display: grid;
grid-template-columns: 1fr 1fr;
margin: 8.5em auto;
border-radius: 6px;
}
.first {
background-image: url('images/image-header-desktop.jpg');
background-color: hsl(277, 64%, 61%);
background-blend-mode: multiply;
order: 1;
}
.second {
padding: 3em;
}
h1 {
color: hsl(0, 0%, 100%);
max-width: 350px;
width:100%;
}
h1>span {
color: hsl(277, 64%, 61%);
}
p {
color: hsla(0, 0%, 100%, 0.75);
margin-top: 1em;
max-width: 400px;
width:100%;
font-family: 'Lexend Deca', sans-serif;
font-weight: 400;
line-height: 1.4;
}
dl {
margin-top: 2em;
display: flex;
justify-content: space-between;
}
dl dt {
color: hsl(0, 0%, 100%);
font-weight: 700;
font-size: 1.25rem;
}
dl dd {
font-variant-caps: all-petite-caps;
color: hsla(0, 0%, 100%, 0.6);
}
/*media query styles */
@media(max-width:480px) {
body {
min-height: 100vh;
padding: 2em 1em;
}
main {
width: 100%;
grid-template-rows: 250px 1fr;
/*height property cannot be used to adjust height of grid items only grid template rows*/
grid-template-columns: auto;
margin: 3em auto;
overflow: hidden;
}
.first {
background-image: url('images/image-header-mobile.jpg');
background-color: hsl(277, 64%, 61%);
background-blend-mode: multiply;
background-size: cover;
order: -1;
}
.second {
padding: 1.5em;
display: grid;
grid-template-rows: 1fr 1fr 2fr;
}
h1 {
max-width: 300px;
width:100%;
text-align: center;
margin: 0 auto;
}
p {
max-width: 245px;
width:100%;
text-align: center;
line-height: 1.6;
margin: 0 auto;
}
dl {
flex-direction: column;
gap: 1.5em;
justify-content: center;
align-items: center;
}
dl div {
width: 71px;
/*giving all flex items equal width to make them all align*/
}
}
<main class="container">
<div class="first">
<!--container for background image-->
</div>
<div class="second">
<h1>Get <span>insights</span> that help your business grow.</h1>
<p>Discover the benefits of data analytics and make better decisions regarding revenue, customer experience, and overall efficiency.</p>
<div class="stats">
<dl>
<div>
<dt>10k+</dt>
<dd>companies</dd>
</div>
<div>
<dt>314</dt>
<dd>templates</dd>
</div>
<div>
<dt>12M+</dt>
<dd>queries</dd>
</div>
</dl>
</div>
</div>
</main>