这是我第一次使用网格布局。我在弄清楚如何在第三行上换行以及它现在在第一行上的显示方式时遇到了一些麻烦。
这是现在的样子:
我很想达到这个结果:
这是当前的 HTML:
<ul class="grid">
<li class="grid__item">
<h3>Week 1</h3>
<h4><a href="#">Course Welcome and Orientation</a></h4>
</li>
<li class="grid__item">
<h3>Week 2</h3>
<h4><a href="#">Landscapes of Early Cultures</a></h4>
</li>
<li class="grid__item">
<h3>Week 3</h3>
<h4><a href="#">The Enlightened Landscape: Western Europe in the 15th and 16th Centuries</a></h4>
</li>
</ul>
这是随附的 CSS:
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
grid-gap: 10px;
padding: 0px 0 50px 0;
justify-content: start;
margin: 0;
list-style: none;
}
.grid__item {
background: #2774ae;
border: 1px solid #ccc;
padding: 10px 0 10px 0;
margin: 0 !important;
border-radius: 5px;
position: relative;
transition: all 0.2s ease;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.25);
min-height: 190px;
&:hover,
&:focus-within {
background-color: #005587 !important;
}
a {
position: relative;
z-index: 1;
display: inline-block;
color: #ffffff !important;
text-decoration: none !important;
text-decoration-skip-ink: auto;
}
h4 {
font-size: 1.05rem !important;
font-weight: 700;
letter-spacing: 1px;
color: #ffffff !important;
margin: 1rem 1.5rem !important;
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
display: -webkit-box;
-webkit-line-clamp: 3;
max-width: 190px;
-webkit-box-orient: vertical;
}
h4 a {
position: static;
display: inline-block !important;
color: #ffffff !important;
text-decoration: none !important;
text-decoration-skip-ink: auto;
}
h4 a:hover,
h4 a:focus {
color: #ffffff !important;
background-color: transparent !important;
}
h4 a:before {
content: "";
display: block;
position: absolute;
z-index: 0;
width: 100%;
height: 100%;
top: 0;
left: 0;
transition: background-color 0.1s ease-out;
background-color: transparent; }
h3 {
font-weight: 500;
font-size: 1.05rem !important;
line-height: 1;
color: #ffffff !important;
text-align: left !important;
margin: 1.5rem 1.5rem 0.25rem 1.5rem !important;
}
h3:after {
display: block;
content: '';
width: 55px;
height: 4px;
background: #FFD100;
margin: 1rem 0 0.7rem 0;
border-radius: 99px;
}
ul {
display: flex;
margin: 0 0 6px 0 !important;
}
ul li {
margin-right: 10px !important;
}
}
我已经用尽了很多选择。不幸的是,我就是不知道如何让它发挥作用。
从
white-space: nowrap
类中删除 .h4
css 属性:
h4 {
font-size: 1.05rem !important;
font-weight: 700;
letter-spacing: 1px;
color: #ffffff !important;
margin: 1rem 1.5rem !important;
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
display: -webkit-box;
-webkit-line-clamp: 3;
max-width: 190px;
-webkit-box-orient: vertical;
}
与
h4 {
font-size: 1.05rem !important;
font-weight: 700;
letter-spacing: 1px;
color: #ffffff !important;
margin: 1rem 1.5rem !important;
text-overflow: ellipsis;
overflow: hidden;
display: -webkit-box;
-webkit-line-clamp: 3;
max-width: 190px;
-webkit-box-orient: vertical;
}