如何创建一个三列布局,其中中间列换行。

huangapple go评论54阅读模式
英文:

How to create a 3 column layout where the middle column wraps

问题

I'm trying to create a "3 column" layout like this
如何创建一个三列布局,其中中间列换行。

Where the variable length string wraps or drops below the badge if the viewport becomes too small. In this scenario I would like the chevron button to stay pinned to the upper left location.

I've tried multiple scenarios with flex containers, grid layouts, but can't get it quite right. In some scenarios I've gotten both the string and chevron to drop down or just the chevron, but neither is the desired result.

如何创建一个三列布局,其中中间列换行。
如何创建一个三列布局,其中中间列换行。

If anyone can help me understand how to create the desired layout with the intended behavior I would appreciate it.

UPDATE

I was provided this code by a bootstrap community member which solves the requirements using the native bootstrap classes and no CSS modifications required

<div class="d-flex gap-3">
<div class="flex-grow-1">
<!-- Put variable length badge and variable length string in here -->
</div>
<div class="flex-shrink-1">
<!-- Put chevron icon here -->
</div>
</div>

英文:

I'm trying to create a "3 column" layout like this
如何创建一个三列布局,其中中间列换行。

Where the variable length string wraps or drops below the badge if the viewport becomes too small. In this scenario I would like the chevron button to stay pinned to the upper left location.

I've tried multiple scenarios with flex containers, grid layouts, but can't get it quite right. In some scenarios I've gotten both the string and chevron to drop down or just the chevron, but neither is the desired result.

如何创建一个三列布局,其中中间列换行。
如何创建一个三列布局,其中中间列换行。

If anyone can help me understand how to create the desired layout with the intended behavior I would appreciate it.

UPDATE

I was provided this code by a bootstrap community member which solves the requirements using the native bootstrap classes and no CSS modifications required

&lt;div class=&quot;d-flex gap-3&quot;&gt;
  &lt;div class=&quot;flex-grow-1&quot;&gt;
    &lt;!-- Put variable length badge and variable length string in here --&gt;
  &lt;/div&gt;
  &lt;div class=&quot;flex-shrink-1&quot;&gt;
    &lt;!-- Put chevron icon here --&gt;
  &lt;/div&gt;
&lt;/div&gt;

答案1

得分: 1

以下是使用我建议的方法,在网格内部使用 flexbox 的解决方案。
这并不完美,因为我不擅长 CSS,但似乎符合您想要的行为。

关键注意事项:

  1. 为了将图标保留在最右边的自己的区域中,请使用 grid-template-columns 属性将图标区域设置为自动,其他区域设置为占用所有剩余空间。
.grid-container {
    display: grid;
    grid-template-columns: 1fr auto;
}
  1. 要使徽章和可变字符串部分左对齐,但在需要时换行,请设置 flex 容器的 justify-content: flex-start; 属性,同时设置容器项的左右边距为 0px(或至少不设置为 auto);
.flex-container > div {
    min-height: 1rem;
    margin: 5px 0px;
}
  1. 要强制将 chevron/icon 放在其区域的顶部,请将高度设置为 100%,不要设置内边距。
.icon {
    height: 100%;
    padding: 0px;
}

我为每一行添加了底边框以在视觉上分隔它们,但我知道我的解决方案并不完美,因为如果您减少内容的垂直间距,内容区域和图标区域的边框将不会对齐。如果有更多 CSS 经验的人想要编辑/修正这一点 - 请随意。

Codepen

英文:

Here's a solution using flexbox inside a grid as I suggested.
It isn't perfect as I'm not great at CSS, but seems to match the behavior you want.

A few key notes:

  1. To keep the icon in its own section on the far right, use the grid-template-columns property to set the icon region to auto, and the other section(s) to take all the remaining space
.grid-container {
    display: grid;
    grid-template-columns: 1fr auto;
}
  1. To get the badge and variable string sections to be left aligned, but also wrap when needed, we set the flex container to have the property justify-content: flex-start; while also setting the container items to have left and right margins of 0px (or at the very least, not set to auto);
.flex-container &gt; div{
    min-height: 1rem;
    margin: 5px 0px;
}
  1. To force the chevron/icon to the top of its area, we set the height to 100% with no padding
.icon {
    height: 100%; 
    padding: 0px; 
}

I added a bottom-border for each row to separate them visually, but I know my solution isn't perfect because you if you reduce the vertical padding of the content, the border for the content area and the icon area will stop lining up. If anyone with more CSS experience wants to edit/correct this - feel free.

Codepen

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* {
  /*outline: 1px red solid;*/
  box-sizing: border-box;
  margin: auto;
  font-size: 20px;
  color: black;
  padding: 10px;
}

.grid-container {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-gap: 0px;
  min-width: 400px;
  max-width: 80dvw;
  background-color: lightgray;
}

.grid-container&gt;div {
  border-bottom: black 2px solid;
}

.flex-container {
  display: flex;
  flex-wrap: wrap;
  column-gap: 10px;
  row-gap: 0px;
  width: 100%;
  justify-content: flex-start;
}


/* The left and right margins needs to be set to get the spacing we want */

.flex-container&gt;div {
  min-height: 1rem;
  margin: 5px 0px;
}

div.badge {
  background-color: #233299;
  color: lightgrey;
  border-radius: 15px;
  font-weight: bold;
  width: auto;
}

div.content {
  background-color: whitesmoke;
  font-style: italic;
  width: auto;
}


/* This forces the icon div to fill the row height set by flexbox */

.icon {
  height: 100%;
  padding: 0px;
}

<!-- language: lang-html -->

&lt;main&gt;
  &lt;div class=&quot;grid-container&quot;&gt;
    &lt;div class=&quot;flex-container&quot;&gt;
      &lt;div class=&quot;badge&quot;&gt;Variable Length Badge&lt;/div&gt;
      &lt;div class=&quot;content&quot;&gt;Variable length string&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;icon&quot;&gt;&lt;svg height=&quot;48px&quot; width=&quot;48px&quot; version=&quot;1.1&quot; id=&quot;chevron&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; viewBox=&quot;0 0 414.50 414.50&quot;&gt;
        &lt;polygon style=&quot;fill:#2488FF;&quot; points=&quot;386.258,89.796 207.248,268.223 28.238,89.796 0,118.126 207.248,324.7 414.496,118.126 &quot;&gt;
        &lt;/polygon&gt; 
&lt;/svg&gt;&lt;/div&gt;
    &lt;div class=&quot;flex-container&quot;&gt;
      &lt;div class=&quot;badge&quot;&gt;Variable Length Badge Two&lt;/div&gt;
      &lt;div class=&quot;content&quot;&gt;Variable length string Two&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;icon&quot;&gt;&lt;svg height=&quot;48px&quot; width=&quot;48px&quot; version=&quot;1.1&quot; id=&quot;chevron&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; viewBox=&quot;0 0 414.50 414.50&quot;&gt;
        &lt;polygon style=&quot;fill:#2488FF;&quot; points=&quot;386.258,89.796 207.248,268.223 28.238,89.796 0,118.126 207.248,324.7 414.496,118.126 &quot;&gt;
        &lt;/polygon&gt; 
&lt;/svg&gt;&lt;/div&gt;
    &lt;div class=&quot;flex-container&quot;&gt;
      &lt;div class=&quot;badge&quot;&gt;Very long badge name forcing the string into a tiny bit of space before the chevron.&lt;/div&gt;
      &lt;div class=&quot;content&quot;&gt;Variable length string Three&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;icon&quot;&gt;&lt;svg height=&quot;48px&quot; width=&quot;48px&quot; version=&quot;1.1&quot; id=&quot;chevron&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; viewBox=&quot;0 0 414.50 414.50&quot;&gt;
        &lt;polygon style=&quot;fill:#2488FF;&quot; points=&quot;386.258,89.796 207.248,268.223 28.238,89.796 0,118.126 207.248,324.7 414.496,118.126 &quot;&gt;
        &lt;/polygon&gt; 
&lt;/svg&gt;&lt;/div&gt;
    &lt;div class=&quot;flex-container&quot;&gt;
      &lt;div class=&quot;badge&quot;&gt;Short Badge&lt;/div&gt;
      &lt;div class=&quot;content&quot;&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure
        dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;icon&quot;&gt;&lt;svg height=&quot;48px&quot; width=&quot;48px&quot; version=&quot;1.1&quot; id=&quot;chevron&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot; viewBox=&quot;0 0 414.50 414.50&quot;&gt;
        &lt;polygon style=&quot;fill:#2488FF;&quot; points=&quot;386.258,89.796 207.248,268.223 28.238,89.796 0,118.126 207.248,324.7 414.496,118.126 &quot;&gt;
        &lt;/polygon&gt; 
&lt;/svg&gt;&lt;/div&gt;
  &lt;/div&gt;

&lt;/main&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月15日 11:24:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定