为什么我的DIV在展示网格上展开以覆盖所有可用区域?
我试图将2个按钮插入2个divs。 我的CS中的所有DIVS都具有分配的显示网格。
我的问题是,即使我不告诉他们的宽度为100%或宽度为100VW,为什么要使用类纽扣和按钮右侧的DIVS扩展以占用所有可用空间?
对于所有其他Div,我确实指定为100VW,但不是最后一个,那么为什么它们会继续扩展。
如果我放置显示:内部块,它有效,但是我不明白为什么我必须首先指定这一点?
这是我的代码:
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
div {
display: grid;
border: dotted 1px #ff0;
}
body {
background-color: #000;
color: #fff;
font-family: Montserrat, sans-serif;
background-image: url(/../../media/index0.jpg);
background-repeat: no-repeat;
background-color: #000;
background-size: cover;
}
.div-container {
border: solid 2px red;
height: 100vh;
width: 100vw;
}
.div-top-row {
border: solid 2px pink;
height: 65vh;
width: 100vw;
}
.div-bottom-row {
border: dotted 3px green;
height: 30vh;
width: 100vw;
grid-auto-flow: column;
}
.copyrights {
height: 5vh;
width: 100vw;
justify-items: center;
align-items: center;
font-size: 11px;
}
.btn {
transition: all 0.3s ease-in;
box-shadow: unset;
}
.btn-gradient {
border: none;
border-radius: 0.25rem;
color: #fff;
display: inline-block;
font-size: 0.875rem;
font-weight: 600;
line-height: 1em;
padding: 0.875rem 1rem;
text-decoration: none;
transition: all 0.15s;
background: linear-gradient(45deg, #1e92e6, #60fbd0 75%, #1c6599 150%);
background-size: 300% 100%;
}
.btn-blank-blue {
background: 0 0;
border: 1px solid #1e92e6;
color: #1e92e6;
}
.btn:hover {
border-color: transparent;
background: linear-gradient(45deg, #1e92e6, #60fbd0 75%, #1c6599 150%);
background-size: 300% 100%;
background-position: 50% 0;
color: #22242f;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link rel="stylesheet" href="/../../css/index.css" />
<link href="https://fonts.googleapis.com/css2?family=Montserrat&display=swap" rel="stylesheet" />
<link rel="icon" type="image/x-icon" href="/../../media/global0.ico" />
<title>Home</title>
</head>
<body>
<div class="div-container">
<div class="div-top-row"></div>
<div class="div-bottom-row">
<div class="div-button-left">
<button class="btn btn-gradient btn-blank-blue" onclick='window.open("https://www.gitbook.com/")'>Read Docs</button>
</div>
<div class="div-button-right">
<button class="btn btn-gradient" onclick='location.href="dashboard.html"'>Enter App</button>
</div>
</div>
<div class="copyrights">
<p>© 2022 New Company Sample LLC - All rights reserved.</p>
</div>
</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
特殊性似乎存在问题。
您本质上回答了自己的问题。
display:block;
有效,因为这是默认的display
div
's。您有div {display:grid;}
更改默认的display:block;
on on ondiv的
。显示:网格;
将尝试使用所有可用的空间。因此,为什么您的按钮被拉伸。我要么将网格
仅在适用的div
's上。或者,您可以使用 :不 伪级类似:这将排除
div
在中定义的:不是
这些样式的伪类。There seems to be an issue with Specificity.
You essentially answered your own question.
display: block;
works because that is the defaultdisplay
fordiv
's. You havediv { display: grid;}
which changes the defaultdisplay: block;
on all of yourdiv's
.display: grid;
will try to use all available space. Hence, why your buttons are stretched. I would either put thegrid
on only applicablediv
's. Or, you could use the:not
pseudo-class like so:This will exclude the
div
's defined in the:not
pseudo-class of those styles.