在特定情况下使用 CSS 网格布局
Use CSS Grid layout in a specific case
我可以通过许多其他方式获得我想要的结果,但我想了解是否可以在保留网格系统的同时修复布局。
先检查结果,以便更好地理解
$(document).ready(function() {
$('#opt1').on('click', function() {
$('.item').css('grid-template-columns', 'minmax(110px, 1fr) auto');
});
$('#opt2').on('click', function() {
$('.item').css('grid-template-columns', 'minmax(110px, 1fr) max-content');
});
$('#opt3').on('click', function() {
$('.item').css('grid-template-columns', 'auto 1fr');
});
});
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 25px 25px;
margin-top: 25px;
}
.item {
position: relative;
display: grid;
grid-template-columns: minmax(110px, 1fr) auto;
background-color: #2f3138;
color: #ffffff;
padding: 15px;
}
.description {
display: grid;
grid-template-rows: auto 25px;
}
.price {
width: 100%;
font-size: 22px;
line-height: 25px;
text-align: right;
}
/* styles not important */
.item::before {
position: absolute;
font-family: monospace;
font-size: 18px;
font-weight: bold;
color: #ffffff;
border-radius: 50%;
background-color: #000000;
width: 25px;
height: 25px;
line-height: 25px;
text-align: center;
border: solid 5px #2f3138;
top: -8px;
right: -8px;
}
.item:nth-child(1)::before {
content: "1";
}
.item:nth-child(2)::before {
content: "2";
}
.item:nth-child(3)::before {
content: "3";
}
.item:nth-child(4)::before {
content: "4";
}
input[type="button"] {
background-color: #000000;
border: solid 2px #2f3138;
color: #ffffff;
outline: none;
margin: 5px 15px 5px 0px;
padding: 5px 15px 8px 15px;
font-family: monospace;
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="opt1" type="button" value="Original: minmax(110px, 1fr) auto" />
<input id="opt2" type="button" value="option 2: minmax(110px, 1fr) max-content" />
<input id="opt3" type="button" value="option 3: auto 1fr" />
<div class="container">
<div class="item">
<img src="https://placeimg.com/100/120/any" />
<div class="description">
<span>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sit amet mauris semper, interdum lacus a, fermentum risus.
</span>
<div class="price">€ 3.33</div>
</div>
</div>
<div class="item">
<div class="description">
<span>Short Text</span>
<div class="price">€ 3.33</div>
</div>
</div>
<div class="item">
<img src="https://placeimg.com/100/121/any" />
<div class="description">
<span>Short Text</span>
<div class="price">€ 3.33</div>
</div>
</div>
<div class="item">
<div class="description">
<span>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sit amet mauris semper, interdum lacus a, fermentum risus.
</span>
<div class="price">€ 3.33</div>
</div>
</div>
</div>
布局表示项目列表。 HTML 是在服务器端生成的,我可以有一个带有图像的项目和一个没有图像的项目。我的目标是让所有项目的文本始终左对齐,价格始终位于右侧。
关键是 grid-template-columns: minmax (110px, 1fr) auto;
文本将 auto
带图像,而 1fr
不带图像。它总是占据它需要的space,但是在第3项中文本很短,space它需要的很少,我希望它像第1项一样扩展。
javascript 部分展示了一些仍然产生非最佳结果的尝试。
我希望我说的很清楚。
谢谢
依靠隐式网格创建。为文本和价格定义模板,然后将为图像创建一个额外的列,如果有:
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 25px;
margin-top: 25px;
}
.item {
position: relative;
display: grid;
grid-template-columns: 1fr;
grid-auto-columns: 110px; /* this is the width of the extra column */
background-color: #2f3138;
color: #ffffff;
padding: 15px;
}
img {
grid-column-end:-2; /* create an implicit column at the beginning */
}
.description {
display: grid;
grid-template-rows: auto 25px;
}
.price {
font-size: 22px;
line-height: 25px;
text-align: right;
}
/* styles not important */
.item::before {
position: absolute;
font-family: monospace;
font-size: 18px;
font-weight: bold;
color: #ffffff;
border-radius: 50%;
background-color: #000000;
width: 25px;
height: 25px;
line-height: 25px;
text-align: center;
border: solid 5px #2f3138;
top: -8px;
right: -8px;
}
.item:nth-child(1)::before {
content: "1";
}
.item:nth-child(2)::before {
content: "2";
}
.item:nth-child(3)::before {
content: "3";
}
.item:nth-child(4)::before {
content: "4";
}
input[type="button"] {
background-color: #000000;
border: solid 2px #2f3138;
color: #ffffff;
outline: none;
margin: 5px 15px 5px 0px;
padding: 5px 15px 8px 15px;
font-family: monospace;
cursor: pointer;
}
<div class="container">
<div class="item">
<img src="https://placeimg.com/100/120/any" />
<div class="description">
<span>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sit amet mauris semper, interdum lacus a, fermentum risus.
</span>
<div class="price">€ 3.33</div>
</div>
</div>
<div class="item">
<div class="description">
<span>Short Text</span>
<div class="price">€ 3.33</div>
</div>
</div>
<div class="item">
<img src="https://placeimg.com/100/121/any" />
<div class="description">
<span>Short Text</span>
<div class="price">€ 3.33</div>
</div>
</div>
<div class="item">
<div class="description">
<span>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sit amet mauris semper, interdum lacus a, fermentum risus.
</span>
<div class="price">€ 3.33</div>
</div>
</div>
</div>
我可以通过许多其他方式获得我想要的结果,但我想了解是否可以在保留网格系统的同时修复布局。
先检查结果,以便更好地理解
$(document).ready(function() {
$('#opt1').on('click', function() {
$('.item').css('grid-template-columns', 'minmax(110px, 1fr) auto');
});
$('#opt2').on('click', function() {
$('.item').css('grid-template-columns', 'minmax(110px, 1fr) max-content');
});
$('#opt3').on('click', function() {
$('.item').css('grid-template-columns', 'auto 1fr');
});
});
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 25px 25px;
margin-top: 25px;
}
.item {
position: relative;
display: grid;
grid-template-columns: minmax(110px, 1fr) auto;
background-color: #2f3138;
color: #ffffff;
padding: 15px;
}
.description {
display: grid;
grid-template-rows: auto 25px;
}
.price {
width: 100%;
font-size: 22px;
line-height: 25px;
text-align: right;
}
/* styles not important */
.item::before {
position: absolute;
font-family: monospace;
font-size: 18px;
font-weight: bold;
color: #ffffff;
border-radius: 50%;
background-color: #000000;
width: 25px;
height: 25px;
line-height: 25px;
text-align: center;
border: solid 5px #2f3138;
top: -8px;
right: -8px;
}
.item:nth-child(1)::before {
content: "1";
}
.item:nth-child(2)::before {
content: "2";
}
.item:nth-child(3)::before {
content: "3";
}
.item:nth-child(4)::before {
content: "4";
}
input[type="button"] {
background-color: #000000;
border: solid 2px #2f3138;
color: #ffffff;
outline: none;
margin: 5px 15px 5px 0px;
padding: 5px 15px 8px 15px;
font-family: monospace;
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="opt1" type="button" value="Original: minmax(110px, 1fr) auto" />
<input id="opt2" type="button" value="option 2: minmax(110px, 1fr) max-content" />
<input id="opt3" type="button" value="option 3: auto 1fr" />
<div class="container">
<div class="item">
<img src="https://placeimg.com/100/120/any" />
<div class="description">
<span>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sit amet mauris semper, interdum lacus a, fermentum risus.
</span>
<div class="price">€ 3.33</div>
</div>
</div>
<div class="item">
<div class="description">
<span>Short Text</span>
<div class="price">€ 3.33</div>
</div>
</div>
<div class="item">
<img src="https://placeimg.com/100/121/any" />
<div class="description">
<span>Short Text</span>
<div class="price">€ 3.33</div>
</div>
</div>
<div class="item">
<div class="description">
<span>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sit amet mauris semper, interdum lacus a, fermentum risus.
</span>
<div class="price">€ 3.33</div>
</div>
</div>
</div>
布局表示项目列表。 HTML 是在服务器端生成的,我可以有一个带有图像的项目和一个没有图像的项目。我的目标是让所有项目的文本始终左对齐,价格始终位于右侧。
关键是 grid-template-columns: minmax (110px, 1fr) auto;
文本将 auto
带图像,而 1fr
不带图像。它总是占据它需要的space,但是在第3项中文本很短,space它需要的很少,我希望它像第1项一样扩展。
javascript 部分展示了一些仍然产生非最佳结果的尝试。
我希望我说的很清楚。 谢谢
依靠隐式网格创建。为文本和价格定义模板,然后将为图像创建一个额外的列,如果有:
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 25px;
margin-top: 25px;
}
.item {
position: relative;
display: grid;
grid-template-columns: 1fr;
grid-auto-columns: 110px; /* this is the width of the extra column */
background-color: #2f3138;
color: #ffffff;
padding: 15px;
}
img {
grid-column-end:-2; /* create an implicit column at the beginning */
}
.description {
display: grid;
grid-template-rows: auto 25px;
}
.price {
font-size: 22px;
line-height: 25px;
text-align: right;
}
/* styles not important */
.item::before {
position: absolute;
font-family: monospace;
font-size: 18px;
font-weight: bold;
color: #ffffff;
border-radius: 50%;
background-color: #000000;
width: 25px;
height: 25px;
line-height: 25px;
text-align: center;
border: solid 5px #2f3138;
top: -8px;
right: -8px;
}
.item:nth-child(1)::before {
content: "1";
}
.item:nth-child(2)::before {
content: "2";
}
.item:nth-child(3)::before {
content: "3";
}
.item:nth-child(4)::before {
content: "4";
}
input[type="button"] {
background-color: #000000;
border: solid 2px #2f3138;
color: #ffffff;
outline: none;
margin: 5px 15px 5px 0px;
padding: 5px 15px 8px 15px;
font-family: monospace;
cursor: pointer;
}
<div class="container">
<div class="item">
<img src="https://placeimg.com/100/120/any" />
<div class="description">
<span>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sit amet mauris semper, interdum lacus a, fermentum risus.
</span>
<div class="price">€ 3.33</div>
</div>
</div>
<div class="item">
<div class="description">
<span>Short Text</span>
<div class="price">€ 3.33</div>
</div>
</div>
<div class="item">
<img src="https://placeimg.com/100/121/any" />
<div class="description">
<span>Short Text</span>
<div class="price">€ 3.33</div>
</div>
</div>
<div class="item">
<div class="description">
<span>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam sit amet mauris semper, interdum lacus a, fermentum risus.
</span>
<div class="price">€ 3.33</div>
</div>
</div>
</div>