Flex bootstrap 5.1.3 div 和价格不一致

发布于 2025-01-09 09:34:48 字数 3810 浏览 1 评论 0原文

我正在使用 flex = https://getbootstrap.com/docs/4.0/utilities/flex/

如果我的内容文本增加大小,则 div 和价格不会保持一致。 https://i.sstatic.net/LatWf.jpg

我想要实现的目标:

  • 当内容文本较大或较短时,div 具有相同的大小。
  • 如果内容文本增加,价格始终与另一价格保持一致。

JSFiddle = https://jsfiddle.net/scbxef5y/

<html>

<head>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous" />
</head>
<style>
.border-gray {
    border: 1px solid #000;
    min-height: 100px;
    padding: 20px;
}
</style>


<body>
<div class="container">
    <div>
        <h4>section name</h4>
    </div>
    <!-- <div class="menu-section row align-items-baseline menu-row"> -->
    <div class="menu-section row align-items-baseline">
        <div class="menu-item col-12 col-sm-12 col-lg-6">
            <div class="border-gray">
                <div class="d-flex justify-content-between bd-highlight mb-2">
                    <div class="p-2 bd-highlight">
                        title
                        <div>
                            t contentcontent contentcontent contentcontent contentcontent content content contentcontent contentcontent content
                        </div>
                    </div>

                    <div class="p-2 bd-highlight">
                        <img itemprop="image" src="https://i.imgur.com/ZIsnpPF.jpg?auto=format&amp;w=120&amp;h=120" alt="" />
                    </div>
                </div>

                <div class="d-flex align-content-end flex-wrap">
                    <span itemscope="" itemtype="http://schema.org/offers"><span itemprop="price"><p>€5.00</p></span></span>
                </div>
            </div>
        </div>
        <div class="menu-item col-12 col-sm-12 col-lg-6">
            <div class="border-gray">
                <div class="d-flex justify-content-between bd-highlight mb-2">
                    <div class="p-2 bd-highlight">
                        title
                        <div>
                            content t contentcontent contentcontent content t contentcontent contentcontentcontent t contentcontent contentcontent content t contentcontent contentcontent content t contentcontent contentcontent content t contentcontent contentcontent content t contentcontent
                            contentcontent
                        </div>
                    </div>

                    <div class="p-2 bd-highlight">
                        <img itemprop="image" src="https://i.imgur.com/ZIsnpPF.jpg?auto=format&amp;w=120&amp;h=120" alt="" />
                    </div>
                </div>

                <div class="d-flex align-content-end flex-wrap">
                    <span itemscope="" itemtype="http://schema.org/offers"><span itemprop="price"><p>€5.00</p></span></span>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

I am using flex = https://getbootstrap.com/docs/4.0/utilities/flex/

if my content text increase size the div and the price do not stay aligned.
https://i.sstatic.net/LatWf.jpg

What I am trying to achieve:

  • div to have the same size when the content text is big or short.
  • price always be aligned with the other one if the content text increases.

JSFiddle = https://jsfiddle.net/scbxef5y/

<html>

<head>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous" />
</head>
<style>
.border-gray {
    border: 1px solid #000;
    min-height: 100px;
    padding: 20px;
}
</style>


<body>
<div class="container">
    <div>
        <h4>section name</h4>
    </div>
    <!-- <div class="menu-section row align-items-baseline menu-row"> -->
    <div class="menu-section row align-items-baseline">
        <div class="menu-item col-12 col-sm-12 col-lg-6">
            <div class="border-gray">
                <div class="d-flex justify-content-between bd-highlight mb-2">
                    <div class="p-2 bd-highlight">
                        title
                        <div>
                            t contentcontent contentcontent contentcontent contentcontent content content contentcontent contentcontent content
                        </div>
                    </div>

                    <div class="p-2 bd-highlight">
                        <img itemprop="image" src="https://i.imgur.com/ZIsnpPF.jpg?auto=format&w=120&h=120" alt="" />
                    </div>
                </div>

                <div class="d-flex align-content-end flex-wrap">
                    <span itemscope="" itemtype="http://schema.org/offers"><span itemprop="price"><p>€5.00</p></span></span>
                </div>
            </div>
        </div>
        <div class="menu-item col-12 col-sm-12 col-lg-6">
            <div class="border-gray">
                <div class="d-flex justify-content-between bd-highlight mb-2">
                    <div class="p-2 bd-highlight">
                        title
                        <div>
                            content t contentcontent contentcontent content t contentcontent contentcontentcontent t contentcontent contentcontent content t contentcontent contentcontent content t contentcontent contentcontent content t contentcontent contentcontent content t contentcontent
                            contentcontent
                        </div>
                    </div>

                    <div class="p-2 bd-highlight">
                        <img itemprop="image" src="https://i.imgur.com/ZIsnpPF.jpg?auto=format&w=120&h=120" alt="" />
                    </div>
                </div>

                <div class="d-flex align-content-end flex-wrap">
                    <span itemscope="" itemtype="http://schema.org/offers"><span itemprop="price"><p>€5.00</p></span></span>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世俗缘 2025-01-16 09:34:48

我处理这个问题的方法是查看你的两个 div。找到导致对齐问题的内容最多的那个。然后根据最大的 div 的渲染大小在较小的 div 上设置 min-height

因此,根据您的结构,右侧的 div 会比左侧的更大。内容量的差异导致 border-gray 及其第一个带有 flex 的子元素具有不同的高度。那么,我们需要在内容较少的 div 上设置 min-heights ,以便它填充可用空间并对齐您的内容。这还包括在包含大量内容的 div 上设置 max-height(主要用于调整浏览器大小和内容换行时)。

只要您遵循此方法,这应该适用于任何不同大小的内容。它主要依赖于找到最小高度的最佳位置。请参阅我在 border-gray 上设置的 min-height 以及我在其第一个子元素上设置的内联样式。

.border-gray {
  border: 1px solid #000;
  padding: 20px;
  min-height: 300px;
}
<html>

<head>
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous" />
</head>

<body>
  <div class="container">
    <div>
      <h4>section name</h4>
    </div>
    <!-- <div class="menu-section row align-items-baseline menu-row"> -->
    <div class="menu-section row align-items-baseline">
      <div class="menu-item col-12 col-sm-12 col-lg-6">
        <div class="border-gray">
          <div class="d-flex justify-content-between bd-highlight mb-2" style="min-height: 300px;">
            <div class="p-2 bd-highlight">
              title
              <div>
                t contentcontent contentcontent contentcontent contentcontent content content contentcontent contentcontent content
              </div>
            </div>

            <div class="p-2 bd-highlight">
              <img itemprop="image" src="https://i.imgur.com/ZIsnpPF.jpg?auto=format&w=120&h=120" alt="" />
            </div>
          </div>

          <div class="d-flex align-content-end flex-wrap">
            <span itemscope="" itemtype="http://schema.org/offers"><span itemprop="price"><p>€5.00</p></span></span>
          </div>
        </div>
      </div>
      <div class="menu-item col-12 col-sm-12 col-lg-6">
        <div class="border-gray">
          <div class="d-flex justify-content-between bd-highlight mb-2" style="min-height: 300px; max-height: 300px;">
            <div class="p-2 bd-highlight">
              title
              <div>
                content t contentcontent contentcontent content t contentcontent contentcontentcontent t contentcontent contentcontent content t contentcontent contentcontent content t contentcontent contentcontent content t contentcontent contentcontent content t contentcontent
                contentcontent
              </div>
            </div>

            <div class="p-2 bd-highlight">
              <img itemprop="image" src="https://i.imgur.com/ZIsnpPF.jpg?auto=format&w=120&h=120" alt="" />
            </div>
          </div>

          <div class="d-flex align-content-end flex-wrap">
            <span itemscope="" itemtype="http://schema.org/offers"><span itemprop="price"><p>€5.00</p></span></span>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>

The way I would approach this is to look at your two divs. Find the one with the most amount of content that is causing alignment issues. Then set a min-height on the smaller div based on the rendered size of the largest one.

So with your structure, the div on the right is rendered larger than the left one. The difference in the amount of content causes both border-gray and its first child with the flex to have different heights. So then, we need to set min-heights on the div with less content so that it fills the available space and aligns your content. This also includes setting a max-height on the div with lots of content (mostly for when resizing the browser and content wraps).

This should work with any varying-sized content as long as you follow this approach. It mostly relies on finding that min-height sweet spot. See the min-height I set on border-gray and the inline styles I set on its first child.

.border-gray {
  border: 1px solid #000;
  padding: 20px;
  min-height: 300px;
}
<html>

<head>
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous" />
</head>

<body>
  <div class="container">
    <div>
      <h4>section name</h4>
    </div>
    <!-- <div class="menu-section row align-items-baseline menu-row"> -->
    <div class="menu-section row align-items-baseline">
      <div class="menu-item col-12 col-sm-12 col-lg-6">
        <div class="border-gray">
          <div class="d-flex justify-content-between bd-highlight mb-2" style="min-height: 300px;">
            <div class="p-2 bd-highlight">
              title
              <div>
                t contentcontent contentcontent contentcontent contentcontent content content contentcontent contentcontent content
              </div>
            </div>

            <div class="p-2 bd-highlight">
              <img itemprop="image" src="https://i.imgur.com/ZIsnpPF.jpg?auto=format&w=120&h=120" alt="" />
            </div>
          </div>

          <div class="d-flex align-content-end flex-wrap">
            <span itemscope="" itemtype="http://schema.org/offers"><span itemprop="price"><p>€5.00</p></span></span>
          </div>
        </div>
      </div>
      <div class="menu-item col-12 col-sm-12 col-lg-6">
        <div class="border-gray">
          <div class="d-flex justify-content-between bd-highlight mb-2" style="min-height: 300px; max-height: 300px;">
            <div class="p-2 bd-highlight">
              title
              <div>
                content t contentcontent contentcontent content t contentcontent contentcontentcontent t contentcontent contentcontent content t contentcontent contentcontent content t contentcontent contentcontent content t contentcontent contentcontent content t contentcontent
                contentcontent
              </div>
            </div>

            <div class="p-2 bd-highlight">
              <img itemprop="image" src="https://i.imgur.com/ZIsnpPF.jpg?auto=format&w=120&h=120" alt="" />
            </div>
          </div>

          <div class="d-flex align-content-end flex-wrap">
            <span itemscope="" itemtype="http://schema.org/offers"><span itemprop="price"><p>€5.00</p></span></span>
          </div>
        </div>
      </div>
    </div>
  </div>
</body>

烟酉 2025-01-16 09:34:48

您是否尝试过为其中一个元素赋予高度?然后给出高度:自动;到另一个。另外,这段代码需要写得更好,我的意思是更简单,这样你就可以更容易地看到问题!

Have you tried to give height to one of the elements? then give height:auto; to the other one. also this code needs to be writen better, with that i mean to be more simplier, that way you can can see issues easier!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文