gpt4 book ai didi

CSS 网格对齐元素大小不同

转载 作者:太空宇宙 更新时间:2023-11-04 05:49:05 26 4
gpt4 key购买 nike

我想使用 CSS 网格重新创建与下面第二张图片相同的设计。我遇到了对齐问题,当然,由于两个跨度,输入没有按应有的方式对齐。我几乎已经掌握了 CSS 网格,但我无法弄清楚。有没有办法用网格来做,或者有其他解决方案吗?

Render in react Adobe xd design

 <div className="grade-card">
<div className="grade-card-header">
<span className="title">{this.props.title}</span>
</div>
<div className="grade-card-body">
<div className="grade-input">
<span>I</span>
<input type="text"/>
<span>1x</span>
</div>
<div className="grade-input">
<span>I</span>
<input type="text"/>
<span>Min</span>
</div>
<div className="grade-input">
<span>I</span>
<input type="text"/>
<span>1x</span>
</div>
<div className="grade-input">
<span>I</span>
<input type="text"/>
</div>
<div className="grade-input">
<span>I</span>
<input type="text"/>
</div>
<div className="grade-input">
<input type="text"/>
</div>
</div>
</div>
.grade-card{
width: 349px;
height: 384px;
background-color: white;
border-radius: 28px;

.grade-card-header{
display: flex;
align-items: center;
justify-content: center;
height: 20%;
border-radius: 28px 28px 0 0;
background-color: #1089FF;

.title{

color: white;
font-size: 1.5em;
}
}

.grade-card-body{
height: 80%;
cursor: pointer;
display: grid;
grid-template-columns: repeat(2, 174.5px);
grid-template-rows: repeat(3, 102.4px);
justify-items: center;//horizontally
align-items: center;//vertically

input{
outline: none;
width: 74px;
height: 51px;
border: 1px solid #707070;
border-radius: 6px;
font-size: 1.3em;
text-align: center;
}

.grade-input:last-child{
input{
width: 117px;
height: 69px;
}
}
}
}

最佳答案

这与@Brice 的回答略有不同,因为在他的版本中:

  1. 您需要针对标签文本的每个变体进行调整
  2. 如果字体呈现不同,它就会中断(缩放、平台之间的差异等)。

这个版本解决了,但是用绝对定位来逃避流的思路是一样的。

/*********** begin changes **********/

.grade-input {
position: relative;
}

.label {
position: absolute;
top: 50%;
}

.label-left {
transform: translate(calc(-100% - 6px), -50%);
}

.label-right {
transform: translate(6px, -50%);
}

/*********** end changes **********/


.grade-card {
width: 349px;
height: 384px;
background-color: white;
border-radius: 28px;
}

.grade-card .grade-card-header {
display: flex;
align-items: center;
justify-content: center;
height: 20%;
border-radius: 28px 28px 0 0;
background-color: #1089ff;
}

.grade-card .grade-card-header .title {
color: white;
font-size: 1.5em;
}

.grade-card .grade-card-body {
height: 80%;
cursor: pointer;
display: grid;
grid-template-columns: repeat(2, 174.5px);
grid-template-rows: repeat(3, 102.4px);
justify-items: center;
align-items: center;
}

.grade-card .grade-card-body input {
outline: none;
width: 74px;
height: 51px;
border: 1px solid #707070;
border-radius: 6px;
font-size: 1.3em;
text-align: center;
}

.grade-card .grade-card-body .grade-input:last-child input {
width: 117px;
height: 69px;
}
<div class="grade-card">
<div class="grade-card-header">
<span class="title">{this.props.title}</span>
</div>

<div class="grade-card-body">
<div class="grade-input">
<span class="label label-left">I</span>
<input type="text"/>
<span class="label label-right">1x</span>
</div>

<div class="grade-input">
<span class="label label-left">I</span>
<input type="text"/>
<span class="label label-right">Min</span>
</div>

<div class="grade-input">
<span class="label label-left">I</span>
<input type="text"/>
<span class="label label-right">1x</span>
</div>

<div class="grade-input">
<span class="label label-left">I</span>
<input type="text"/>
</div>

<div class="grade-input">
<span class="label label-left">I</span>
<input type="text"/>
</div>
<div class="grade-input">
<input type="text"/>
</div>
</div>
</div>

关于CSS 网格对齐元素大小不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58440839/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com