【问题标题】:how to make div grow right instead of down如何使 div 正确增长而不是向下增长
【发布时间】:2013-12-09 22:47:57
【问题描述】:

如何使这样的 div 向右而不是向下增长?我想要图像列表的恒定高度,并且我想要一个滚动条以根据需要进行水平滚动。

http://jsfiddle.net/8J2KM/

<div style="width:700px;height:130px;overflow-x:auto;">
<div style="height:130px;overflow:hidden;">
<a href="javascript:void();">   
    <div style="width:auto;height:100px;display:inline-block;margin-right:10px;color:#888;">
        <img src="/cloudsign/web/images/layoutTemplate/8.png" alt="test1" style="border:solid 0px transparent;padding:9px 0px;width:130px;height:73px;" />
        <br />test1
</div>
</a>
<a href="javascript:void();">   
    <div style="width:auto;height:100px;display:inline-block;margin-right:10px;color:#888;">
    <img src="/cloudsign/web/images/layoutTemplate/9.png" alt="test1" style="border:solid 0px transparent;padding:9px 0px;width:130px;height:73px;" />
        <br />test1
    </div>
</a>
<a href="javascript:void();">   
    <div style="width:auto;height:100px;display:inline-block;margin-right:10px;color:#888;">
        <img src="/cloudsign/web/images/layoutTemplate/10.png" alt="test1" style="border:solid 0px transparent;padding:9px 0px;width:130px;height:73px;" />
        <br />test1
    </div>
</a>
    <a href="javascript:void();">   
        <div style="width:auto;height:100px;display:inline-block;margin-right:10px;color:#888;">
            <img src="/cloudsign/web/images/layoutTemplate/11.png" alt="test1" style="border:solid 0px transparent;padding:9px 0px;width:130px;height:73px;" />

            <br />
            test1
        </div>
    </a>

    <div style="width:auto;height:100px;display:inline-block;margin-right:10px;border:solid 3px transparent;padding:3px;color:black;font-weight:bold;">
        <img src="/cloudsign/web/images/layoutTemplate/13.png" alt="test1" style="border:solid 3px black;padding:3px;width:130px;height:73px;" />
        <br />
        test1
    </div>

    <a href="javascript:void();">   
        <div style="width:auto;height:100px;display:inline-block;margin-right:10px;color:#888;">
            <img src="/cloudsign/web/images/layoutTemplate/14.png" alt="test1" style="border:solid 0px transparent;padding:9px 0px;width:130px;height:73px;" />

            <br />
            test1
        </div>
    </a>

    <a href="javascript:void();">   
        <div style="width:auto;height:100px;display:inline-block;margin-right:10px;color:#888;">
            <img src="/cloudsign/web/images/layoutTemplate/15.png" alt="test1" style="border:solid 0px transparent;padding:9px 0px;width:130px;height:73px;" />

            <br />
            test1
        </div>
    </a>


        </div>
    </div>
</div>

【问题讨论】:

  • 为什么所有的样式都是HTML中的?只是让自己更难......

标签: html css overflow horizontal-scrolling expand


【解决方案1】:

您可以将包装 div 的大小增加到 300% 或您需要多少,或者只需将 white-space:nowrap 添加到包装 div 并为内部包装 div 指定 overflow-x:scroll。在最外层的 div 中不需要 overflow:auto。 所以前两个div:

<div style="width:700px;height:135px;white-space:nowrap">
<div style="height:135px;overflow-x:scroll;">

JSFiddlehttp://jsfiddle.net/LnApB/

同时修改这两个div的高度以适应图片+标题的高度。 当然,正如其他人已经指出的那样,整理您的 html,将您的样式放在单独的样式表中以提高可读性和可维护性。

【讨论】:

  • 空白是我需要的。该可滚动 div 的内容是动态的,因此无法知道其宽度。
【解决方案2】:

好的,让我们整理一下!

HTML:

<div id="container"> 
   <a href="javascript:void();"> <!-- Have as many as you want. Copy -> paste. -->
   <div class="block">
      <img src="/cloudsign/web/images/layoutTemplate/15.png" alt="test1" />
      <br />test1
   </div>
   </a>
</div>

CSS:

#container {
    width: 100%;
    overflow: auto;
    height: 150px;
    white-space:nowrap;
}
.block {
    width:auto;
    height:100px;
    display:inline-block;
    margin-right:10px;
    color:#888;
}
img {
    border:solid 0px transparent;
    padding:9px 0px;
    width:130px;
    height:73px;
}

DEMO HERE

这包括一个修复,看看你是否出错了。请尝试将您的 CSS 放入 HTML。它只会让你自己的生活更加艰难,使用课程.testClass 或 id 的#testID

希望对你有所帮助。

【讨论】:

    【解决方案3】:

    为了保持item div“正确增长”,您需要将父容器的width 定义为包含其内容的最大宽度(即所有子项的总宽度)。

    有多种可能的方法,请查看 this fiddle 了解选项。

    顺便说一句,建议分离您的样式,这样它们就不会被内联,除了可以大大提高维护和控制代码的能力之外,它的效率也更高。

    【讨论】:

    • 这行得通,但并不完全正确。如果我想让我的 div 宽度为 250 像素,内容的总宽度为 700 像素,该怎么办?使用您的方法,div 将始终占据可用屏幕的整个宽度或所有元素的总宽度。如果我想让它更小怎么办 - JSFIDDLE。这就是为什么white-space:nowrap; 更好的原因。看我的回答。无论如何+1
    • @Fallup - 请参阅上面第二个小提琴中的第一个示例。您需要做的就是将包装 div 的宽度从 100% 更改为您想要的任何内容..
    • 是的,没有发现。无论如何,您仍然需要计算内部 div 的宽度 [我知道这很容易,但过程中没有必要的步骤 :)] 将其与元素的总宽度相匹配,以便将其放在“1 行”上。 white-space:nowrap; 在这种情况下仍然更好。另一方面,这种方法的优势在于它可以让您指定内容应该是多少“行”,而在white-space:nowrap; 的情况下,它始终是“1 行”。
    • @Fallup,+1,完全同意
    • 这将是一个简单的解决方案,但内部 div 的大小/内容是动态的。否则显而易见的解决方案是正确设置宽度。
    【解决方案4】:
     <div style="width:300px;overflow:auto;">
    
     </div>
    

    把你的代码放在这个 div 里面

    我在这里更新了 jsfiddle 检查它。

    http://jsfiddle.net/8J2KM/2/

    【讨论】:

      【解决方案5】:

      在你的html中删除&lt;br/&gt;然后它会水平增长。我已经改变了js小提琴。看看。

      DEMO HERE

      【讨论】:

        猜你喜欢
        • 2015-02-21
        • 2012-07-03
        • 1970-01-01
        • 2011-12-28
        • 1970-01-01
        • 2012-02-14
        • 2011-03-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多