我试图在CSS中制作卡片组件,我认为这很简单,但是我在CSS网格属性中苦苦挣扎。另外,在这种情况下,我不能使用flexbox,原因不值得解释:)
布局非常简单:左侧是图片,右侧是一些文字。
<div class="card">
<figure card="card__img">
<img src="https://via.placeholder.com/600" width="600" height="600">
</figure>
<p class="card__txt">Lorem ipsum tempus fugit.</p>
</div>
这是棘手的部分:图像是可选的,并且在显示时,其宽度不应超过50%。
当我为图片添加最大宽度时,如果图片不存在,则文本不会展开。
这是我目前所拥有的:
.card {
display: grid;
column-gap: 1em;
grid-template-columns: minmax(0, 50%) 1fr;
}
我试图弄乱width grid-template-columns
,但是我对它还不熟悉。
您可以在此Codepen上看到它的运行情况:https ://codepen.io/tcharlss/pen/gVvOjY
这是代码片段:
/* Layout stuff I'm strugling width*/
.card {
display: grid;
column-gap: 1em;
grid-template-columns: minmax(0, 50%) 1fr;
}
/* Decoration & miscellaneous */
body { font-family: sans; }
.container {
display: grid;
column-gap: 1em;
grid-template-columns: repeat(auto-fill, 35em);
}
.container + .container {
margin-top: 2em;
}
.explication {
color: hsl(230, 100%, 45%);
font-family: monospace;
font-style: italic;
}
.card {
border: 1px solid hsl(230, 100%, 45%);
min-height: 18em;
}
.card__txt {
background-color: hsl(0, 0%, 90%);
margin: 0;
}
figure {
margin: 0;
}
img {
max-width: 100%;
height: auto;
}
<div class="container">
<div class="col">
<p class="explication">If an image is present, it can't be wider then 50%</p>
<div class="card">
<figure card="card__img">
<img src="https://via.placeholder.com/600" width="600" height="600">
</figure>
<p class="card__txt">Lorem ipsum tempus fugit. Aut rerum nostrum qui adipisci est aut. Est error omnis sit velit.</p>
</div>
</div>
<div class="col">
<p class="explication">Without image, the text should expand to fill all the available space</p>
<div class="card">
<p class="card__txt">Lorem ipsum tempus fugit. Aut rerum nostrum qui adipisci est aut. Est error omnis sit velit.</p>
</div>
</div>
</div>
我最容易想到的一种方法是,以.card__text
一种方式设置通用元素的样式,然后使用+
组合器以不同的方式设置跟随.card__img
元素的元素的样式:
/* here we use Grid layout, setting two columns
each of 1fr (using the repeat() function): */
.card {
display: grid;
grid-gap: 1em;
grid-template-columns: repeat(2, 1fr);
}
/* setting the default presentation of the
.card__txt elements; positioning them
in the first column, spanning two
columns: */
.card__txt {
grid-column: 1 / span 2;
}
/* separately styling the .card__txt elements
that follow a .card__img element; here we
place them in the second column: */
.card__img + .card__txt {
grid-column: 2;
}
/* Decoration & miscellaneous */
body {
font-family: sans;
}
.container {
display: grid;
column-gap: 1em;
grid-template-columns: repeat(auto-fill, 35em);
}
.container+.container {
margin-top: 2em;
}
.explication {
color: hsl(230, 100%, 45%);
font-family: monospace;
font-style: italic;
}
.card {
border: 1px solid hsl(230, 100%, 45%);
min-height: 18em;
}
.card__txt {
background-color: hsl(0, 0%, 90%);
margin: 0;
}
figure {
margin: 0;
}
img {
max-width: 100%;
height: auto;
}
/* changes */
.card {
display: grid;
grid-gap: 1em;
grid-template-columns: repeat(2, 1fr);
}
.card__txt {
grid-column: 1 / span 2;
}
.card__img+.card__txt {
grid-column: 2;
}
<div class="container">
<div class="col">
<p class="explication">If an image is present, it can't be wider then 50%</p>
<div class="card">
<!-- Note the correction, in your posted code the following
was written:
<figure card="card__img">
I corrected 'card' to 'class' -->
<figure class="card__img">
<img src="https://via.placeholder.com/600" width="600" height="600">
</figure>
<p class="card__txt">Lorem ipsum tempus fugit. Aut rerum nostrum qui adipisci est aut. Est error omnis sit velit.</p>
</div>
</div>
<div class="col">
<p class="explication">Without image, the text should expand to fill all the available space</p>
<div class="card">
<p class="card__txt">Lorem ipsum tempus fugit. Aut rerum nostrum qui adipisci est aut. Est error omnis sit velit.</p>
</div>
</div>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句