英文:
How can I make an element be next to multiple other elements while sharing a parent container?
问题
我有一些未知数量的元素,它们旁边总是需要有一个元素,就像这样:
╔═══════╦═══╗
║ ║ ║
╠═══════╣ ║
║ ║ ║
╠═══════╣ ║
║ ║ ║
╠═══════╣ ║
║ ║ ║
╚═══════╩═══╝
它们不能是两个单独的容器,我的CSS已经相当复杂,而且我也在与其他人合作,所以到处更改会相当困难。
我尝试在右侧元素上设置 grid-row
为 1 / -1
,但只有在明确设置了 grid-template-rows
时才能使用负值,但我不知道有多少列。
我尝试过的另一种方法是使用CSS计数器来设置 grid-row-end: counter(count)
或设置 grid-template-rows: repeat(counter(count), auto)
,但它不会输出一个数字。
我该怎么办?我应该只将它们做成两个单独的容器吗?使用JavaScript也不是一个很好的解决方案。
编辑:一个奇怪但不太可靠的解决方案,在大多数情况下应该可以工作,假设其余的布局已知,你可以设置 grid-column: 1 / span 99
。我之前尝试过 1 / 99
,但这个更好,不会创建新的行。
英文:
I have an unknown amount of elements and next to them they need to have an element always next to them like this:
╔═══════╦═══╗
║ ║ ║
╠═══════╣ ║
║ ║ ║
╠═══════╣ ║
║ ║ ║
╠═══════╣ ║
║ ║ ║
╚═══════╩═══╝
They can't be two separate containers, my CSS is already quite complex and I'm working with other people as well so it would be quite difficult to change it everywhere.
I tried setting grid-row
on the right element to 1 / -1
, but you can only use negative values if you have explicity set grid-template-rows
, but I don't know how many columns there are.
Something else I tried was using a CSS counter to set either grid-row-end: counter(count)
or setting grid-template-rows: repeat(counter(count), auto)
, but it doesn't output a number.
What can I do? Should I just make it be two separate containers? Using JavaScript is not a great solution either.
Edit: a strange solution that isn't very reliable, but should work in most situations assuming the rest of the layout is known, you can set grid-column: 1 / span 99
. I tried doing 1 / 99
before but this is better and doesn't create new rows.
答案1
得分: 2
解决方案 1:使用两个容器
您可以使用flexbox
来实现相同的布局。您可以将容器分为两列,left
和right
,然后将left
容器分为四个较小的容器。我添加了一个具有固定尺寸的示例,仅供参考。希望这有所帮助。
.container {
max-width: 500px;
width: 100%;
height: 1000px;
border: 1px solid black;
display: flex;
align-items: center;
padding: 20px;
}
.left, .right {
height: 100%;
border: 1px solid red;
padding: 10px;
}
.right {
width: 30%;
}
.left {
width: 70%;
display: flex;
flex-direction: column;
}
.row {
width: 100%;
height: 250px;
margin: 5px 0;
}
.first-row {
border: 1px solid blue;
}
.second-row {
border: 1px solid green;
}
.third-row {
border: 1px solid purple;
}
.fourth-row {
border: 1px solid orange;
}
<div class="container">
<div class="left">
<div class="row first-row"></div>
<div class="row second-row"></div>
<div class="row third-row"></div>
<div class="row fourth-row"></div>
</div>
<div class="right"></div>
</div>
解决方案 2:使用一个容器(不建议)
您也可以使用一个容器来实现,但我不建议这样做,因为如您所说,左侧的框将是动态的。这里是一个示例,但其中的元素和高度都是固定的。
我建议切换到两个单独的容器,因为这对于您想要实现的布局更清晰。
.container {
max-width: 500px;
width: 100%;
height: 1200px;
border: 1px solid black;
display: flex;
align-items: center;
padding: 20px;
flex-direction: column;
flex-wrap: wrap;
}
.row {
width: 70%;
height: 240px;
margin: 5px 0;
float: left;
}
.fifth-row {
margin: 5px;
height: 100%;
width: 30%;
border: 1px solid red;
}
.first-row {
border: 1px solid blue;
}
.second-row {
border: 1px solid green;
}
.third-row {
border: 1px solid purple;
}
.fourth-row {
border: 1px solid orange;
}
<div class="container">
<div class="row first-row"></div>
<div class="row second-row"></div>
<div class="row third-row"></div>
<div class="row fourth-row"></div>
<div class="fifth-row"></div>
</div>
英文:
Solution 1: With two containers
You can achieve the same output using flexbox
. You can split your container into two columns left
and right
, and the split your left
container into four smaller containers. I added an example bellow with fixed dimensions just for referencing. Hope that helps.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.container {
max-width: 500px;
width: 100%;
height: 1000px;
border: 1px solid black;
display: flex;
align-items: center;
padding: 20px;
}
.left, .right {
height: 100%;
border: 1px solid red;
padding: 10px;
}
.right {
width: 30%;
}
.left {
width: 70%;
display: flex;
flex-direction: column;
}
.row {
width: 100%;
height: 250px;
margin: 5px 0;
}
.first-row {
border: 1px solid blue;
}
.second-row {
border: 1px solid green;
}
.third-row {
border: 1px solid purple;
}
.fourth-row {
border: 1px solid orange;
}
<!-- language: lang-html -->
<div class="container">
<div class="left">
<div class="row first-row"></div>
<div class="row second-row"></div>
<div class="row third-row"></div>
<div class="row fourth-row"></div>
</div>
<div class="right"></div>
</div>
<!-- end snippet -->
Solution 2: With One container (Not recommended)
You can achieve that also with one container but I do not recommend it because as you say you the boxes on the left will be dynamic. Here is any example but with fixed elements and fixed heights.
I would suggestion switching to two separate containers since it is a more clear layout for what you want to achieve.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.container {
max-width: 500px;
width: 100%;
height: 1200px;
border: 1px solid black;
display: flex;
align-items: center;
padding: 20px;
flex-direction: column;
flex-wrap: wrap;
}
.row {
width: 70%;
height: 240px;
margin: 5px 0;
float: left;
}
.fifth-row {
margin: 5px;
height: 100%;
width: 30%;
border: 1px solid red;
}
.first-row {
border: 1px solid blue;
}
.second-row {
border: 1px solid green;
}
.third-row {
border: 1px solid purple;
}
.fourth-row {
border: 1px solid orange;
}
<!-- language: lang-html -->
<div class="container">
<div class="row first-row"></div>
<div class="row second-row"></div>
<div class="row third-row"></div>
<div class="row fourth-row"></div>
<div class="fifth-row"></div>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论