如何将卡片div集中在网格上
我正在尝试创建一个HTML布局,该布局由两个列组成。在右列中,我想显示一个符合卡,在其旁边的每一侧是人字形图标。我正在使用SCSS来设置我的布局,并且有些关闭:卡片容器似乎并没有集中,实际上似乎它似乎不适合其容器内。
我一直在努力弄清楚为什么会发生这种情况,而无法提出解决方案。感谢任何帮助!
HTML:
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="./test.scss" rel="stylesheet">
<title>Document</title>
</head>
<body>
<div class="container">
<div class="left-column"></div>
<div class="right-column">
<span class="material-icons chevron-left">chevron_left</span>
<div class="card"></div>
<span class="material-icons chevron-right">chevron_right</span>
</div>
</div>
</body>
SCSS:
.container {
display: grid;
grid-template-columns: 35fr 65fr;
.right-column {
display: grid;
grid-template-columns: 15fr 70fr 15fr;
height: 95vh;
span {
height: 50px;
margin: 20px;
font-size: 50px;
cursor: default;
display: flex;
align-items: center;
&:hover {
font-size: 60px;
}
&.chevron-left {
justify-content: center;
}
&.chevron-right {
justify-content: center;
}
}
.card {
width: 100%;
height: 100%;
margin: 50px;
border: 3px solid black;
border-radius: 8px;
box-sizing: border-box;
overflow: auto;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,原因,您的卡在您的右列的盒子外面,是因为它的高度为100%的父级,而且它具有边距,从而将其向外推出并溢出其列,应该是包含在内。为防止它,请使用
calc()
函数,例如:高度:calc(100%-100px)
andwidth:calc(100%-100px)< /代码>考虑
上设置
保证金:50px
表示每个轴总计100px。其次,要垂直将所有内容集中在右列中,您需要在
align-items
。第三,要水平中心您的卡,请在
上设置
josify-self:center
.card更新:以来,您的评论指出了您只需要顶部和底部边距,您就应该设置
宽度:卡上的100%
,一切都会按预期工作 - 您也无需水平将卡中心居中,因此无需正当:卡中的中心
在您的卡上。我在下面编辑了代码片段以说明这一点。First of all, the reason, your card is outside your the box of your right column, is because it has a height of 100% of its parent PLUS it has a margin, thereby pushing it outwards and overflowing its column it is supposed to be contained within. To prevent it, use
calc()
function, for example:height: calc(100%- 100px)
andwidth: calc(100% - 100px)
consideringmargin: 50px
means a total of 100px in each axis.Second, to center everything vertically inside your right column, you need to set
align-items: center
on your.right-column
.Third, to center your card horizontally as well, set
justify-self: center
on your.card
Update: Since, your comment pointed out that you only need top and bottom margins, you should set
width: 100%
on your card and everything will work as expected - also you would not need to center your card horizontally then, thereby no need forjustify-self: center
on your card. I edited the code snippet below to account for this.