【问题标题】:Align Div elements of different sizes in a specified order按指定顺序对齐不同大小的 Div 元素
【发布时间】:2018-12-03 23:48:48
【问题描述】:

我想在containercontainer 之间保持相同的height big div 与另一个(假设)20 div 1024px1920px max width;

最重要的是,当我想让所有物品都漂浮起来并拥有一个漂亮干净的layoutgrid

只有big div 必须与container 有60% 的宽度,然后所有的小div 20% 并浮动到大的附近。

结构如下:

<div class="container">

<div class="big-div" style="width: 60%;"></div>

<div class="small-div" style="width: 20%;"></div>
<div class="small-div" style="width: 20%;"></div>
<div class="small-div" style="width: 20%;"></div>
<div class="small-div" style="width: 20%;"></div>
<div class="small-div" style="width: 20%;"></div>
<div class="small-div" style="width: 20%;"></div>
<div class="small-div" style="width: 20%;"></div>
<div class="small-div" style="width: 20%;"></div>
<div class="small-div" style="width: 20%;"></div>

</div>

【问题讨论】:

  • 到目前为止你做了什么?你有codepen吗?
  • 用 css 分享你的代码。你用 flex 试过了吗?
  • 为此研究 CSS 网格和/或引导程序和/或 flexbox。我们不会从头开始编写代码。您需要分享您的尝试。
  • 直到现在我什么都没做,因为我不知道该怎么做。我所做的就是这种结构,我正在尝试用 flex 制作一些东西,以使它们也具有响应性。

标签: jquery html css flexbox css-grid


【解决方案1】:

给你! CSS 网格。 HTML:

<div class="container">
  <div class="big-div"></div>
  <div class="small-div"></div>
  <div class="small-div"></div>
  <div class="small-div"></div>
  <div class="small-div"></div>
  <div class="small-div"></div>
  <div class="small-div"></div>
  <div class="small-div"></div>
  <div class="small-div"></div>
  <div class="small-div last-1"></div>
  <div class="small-div last-2"></div>
  <div class="small-div last-3"></div>
  <div class="small-div last-4"></div>
</div>

CSS:

.container {
  display:grid;
  grid-gap:10px;
  grid-template-rows:repeat(4, 1fr);
  grid-template-columns:repeat(6, 1fr)
}

.big-div {
  grid-column:span 4;
  grid-row:span 2;
}

.last-1 {
  grid-column:1/2;
  grid-row:4/5
}
.last-2 {
  grid-column:2/3;
  grid-row:4/5
}
.last-3 {
  grid-column:3/4;
  grid-row:4/5
}
.last-4 {
  grid-column:4/5;
  grid-row:4/5
}
.container > div {
  border:1px solid;
  background-color:green;
}

代码笔: https://codepen.io/gkilmain/pen/eKQjee

【讨论】:

  • 我不想使用浮点数。如果我使用 flex 会更好,因为它更灵活
  • 哦,您在问题I want to make all the items floated 中说浮动。看网格。它用于页面布局。浮动也很棒,但您可能需要考虑在单个布局组件而不是页面布局上使用 flex。
  • 我无法对 html 进行太多更改。其动态内容 Yii php。我必须从我的帖子中用这种结构制作一些东西
  • 我感受到你的痛苦。结帐更新答案我很乐意回答任何问题。没有 JS,纯 CSS。
【解决方案2】:
function sidebarHandler() {
          jQuery(".big-div").css({'height':(jQuery(".small-div").height()+'px')});
          jQuery(".big-div").height(jQuery(".small-div").height());
          var highestCol = Math.max(jQuery('.big-div').height(),jQuery('.container').height());
          jQuery('.big-div').height(highestCol);
          console.log(highestCol);
          console.log($('.big-div').height());
          console.log($(".big-div").css({'height':(jQuery(".small-div").height()+'px')}));
        }
        jQuery(document).ready(function() { 
            sidebarHandler();
            $(window).resize(function() {
                sidebarHandler();
            });
        });

【讨论】:

    【解决方案3】:

    这就是你需要的。

    .container
    {
        border: 1px solid black;
        display: flex;
        flex-wrap: wrap;
    }
    
    .big-div
    {
        background:red;
        flex-basis: 50%;
        top:0;
        left:0;
    }
    
    .small-div
    {
        background:blue;
        width:100px;
        color:white;
        flex-basis:25%;
    }
    
    .big-div, .small-div
    {
        border:2px solid white;
        padding: 10px;
        font-family: sans-serif;
        box-sizing:border-box;
        align-self: flex-start;
        overflow:scroll;
    }
    <div class="container">
        <div class="big-div">
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </div>
        <div class="small-div">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</div>
        <div class="small-div">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</div>
        <div class="small-div">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</div>
        <div class="small-div">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</div>
        <div class="small-div">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</div>
        <div class="small-div">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</div>
        <div class="small-div">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</div>
        <div class="small-div">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</div>
        <div class="small-div">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</div>
        <div class="small-div">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s</div>
    </div>

    【讨论】:

    • 我没有固定的元素高度。它们是流动的。此外,网格必须是响应式的,并且通过调整浏览器的大小,元素会下降(在大 div 下)。
    • @StefanIordache 更新了答案...希望这会有所帮助
    • 感谢您的回答,但您不关注我的照片。大 div 有两个小 div 的高度。
    • 那么flex是无法实现的。
    • 如果你想通过flex来完成,那么它应该有固定的高度和宽度。
    猜你喜欢
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 2013-08-27
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多