【问题标题】:Increasing <li> margin-bottom without pushing bottom div down在不向下推底部 div 的情况下增加 <li> margin-bottom
【发布时间】:2018-01-15 09:36:03
【问题描述】:

我正在尝试完成类似 instagram 帖子的示例:https://www.instagram.com/p/BXgsKFLBczA/?taken-by=claymicro 列表扩展但不向下推页脚 在我的示例中,如果我将 margin-bottom 添加到评论类中,它会推动&lt;div class="post-details-container"&gt; 的高度,并且会有间隙。 我想要的是增加 cmets 之间的 margin-bottom 但这应该会增加 &lt;div class="body&gt; 容器的溢出,而不是扩展其他 div 并在图像容器中留下绿色间隙。

Jsfiddle 示例:https://jsfiddle.net/ou21qe09/2/

如果我向评论类添加超过 5 像素,它会扩展并在图像容器中留下绿色间隙。还将 padding-top 添加到 &lt;div class="header"&gt; 也会推动 div 高度并留下绿色间隙,请注意,我无法以主体或评论容器的像素为单位设置 max-height,因为图像大小会有所不同,这决定了 &lt;div class="post-details-container" 高度。

    .comment {
       margin-bottom:10px;
}

我该怎么办?在 &lt;div class="post-details-container"&gt; 上设置 max-height 或其他一些解决方法?

非常感谢!如果需要更多解释,请告诉我:)。

【问题讨论】:

  • 我不明白。
  • 好吧,我链接到了 instagram 帖子,该帖子显示了我要完成的工作,添加新 cmets 不会推动细节 div 的高度,并且 cmets 容器溢出只会增加,在我的示例中增加 cmets 会导致高度帖子详细信息 div 增加,并在图像容器中留下间隙

标签: html css


【解决方案1】:

我只是在您的 JsFiddle 上更改了一些内容。检查一下。 Modified JSFiddle

.body {background: yellow;padding: 0 15px 0 15px;height: 50vh;overflow-y: auto;}

【讨论】:

    【解决方案2】:

    溢出滚动应该允许尽可能多的 cmets...但是显然有些东西不起作用。也许你应该重新考虑总体布局 - 以及如何使用 flex-box:https://codepen.io/sheriffderek/pen/mMWRWY

    标记

    <div class="thing">
    
        <div class="left">
            <img src="https://placehold.it/1600x1300" alt="">
        </div>
    
        <div class="right">
            <header>
                header
            </header>
    
            <main>
                <h2>main comments?</h2>
    
                <ul>
                    <li>comment</li>
                    <!-- ... -->
                </ul>
            </main>
    
            <footer class='footer1'>
                footer 1
            </footer>
    
            <footer class='footer2'>
                footer 2
            </footer>
        </div>
    
    </div>
    


    手写笔:

    .thing
        display: flex
        // border: 2px solid red
        .left
            flex-basis: 70%
            img
                display: block
                width: 100%
                height: auto
        .right
            flex-basis: 30%
            //
            display: flex
            flex-direction: column
            h2
                padding: 10px 0
                font-size: 20px
                margin-bottom: 10px
            header, main, footer
                padding: 10px
            header
                flex-grow: 0
                background: gray
            main
                flex-grow: 0
                // border: 2px solid blue
                overflow: scroll
                ul
                    li
                        margin-bottom: 10px
            footer
                background: gray
                flex-grow: 0
            .footer1
                background: lightgreen
    


    编译的 CSS:

    .thing {
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
    }
    .thing .left {
      -ms-flex-preferred-size: 70%;
          flex-basis: 70%;
    }
    .thing .left img {
      display: block;
      width: 100%;
      height: auto;
    }
    .thing .right {
      -ms-flex-preferred-size: 30%;
          flex-basis: 30%;
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
      -webkit-box-orient: vertical;
      -webkit-box-direction: normal;
          -ms-flex-direction: column;
              flex-direction: column;
    }
    .thing .right h2 {
      padding: 10px 0;
      font-size: 20px;
      margin-bottom: 10px;
    }
    .thing .right header,
    .thing .right main,
    .thing .right footer {
      padding: 10px;
    }
    .thing .right header {
      -webkit-box-flex: 0;
          -ms-flex-positive: 0;
              flex-grow: 0;
      background: #808080;
    }
    .thing .right main {
      -webkit-box-flex: 0;
          -ms-flex-positive: 0;
              flex-grow: 0;
      overflow: scroll;
    }
    .thing .right main ul li {
      margin-bottom: 10px;
    }
    .thing .right footer {
      background: #808080;
      -webkit-box-flex: 0;
          -ms-flex-positive: 0;
              flex-grow: 0;
    }
    .thing .right .footer1 {
      background: #90ee90;
    }
    

    【讨论】:

    • 我认为应该是……但我没有时间弄清楚那部分。 JavaScript 没有任何问题。 :) 整个页面在某些时候都是 JS...
    • 是的,我知道,它只是服务器端渲染的,所以窗口对象在客户端运行之前不可用,但在javascript运行之前弄清楚了如何计算图像的高度:)。不管怎样,你得到了我需要的 90%,非常感谢你:)
    • 啊...我能想到的所有其他选项可能会使用更多的 JS。我只是非常关心它的响应式布局......如果 windowWidth 发生变化等等......但也许你有一个固定宽度的窗口?
    • 是的,我在主容器上设置了固定宽度,别担心,你做的比我要求的要多:)。剩下的我会想办法的
    猜你喜欢
    • 2011-02-10
    • 1970-01-01
    • 2013-08-26
    • 2013-04-12
    • 1970-01-01
    • 2014-01-11
    • 2017-12-20
    • 1970-01-01
    • 2015-07-15
    相关资源
    最近更新 更多