html - 两个 div 之间的 div 根据需要使用多少

标签 html css web

我有 3 个 div,如下所示

<div>
    <div id='left' style='background:red;float:left;height:150px;width:150px;'>
    </div>
    <div id='right' style='background:green;float:right;height:150px;'>
        <p>Hallo</p>
    </div>
    <div id='center' style='background:blue;height:150px;'>
        <p>Hallo</p>
    </div>
</div>

中心的 div 应该只在需要的时候填充空间。
尽可能多,然后它应该显示水平滚动条
最后一个 div 不应该换行到下一行!
就像我在上面的示例中得到的一样,它会一直填充所有内容,即使在不需要时也是如此。
但我需要,正确的 div 尽可能靠近中心 div。

<div>
    <div id='left' style='background:red;float:left;height:150px;width:150px;'>
    </div>
    <div id='center' style='background:blue;float:left;white-space: nowrap;overflow-x:auto;overflow-y:hidden;'>
        <p>Hallo</p>
        <p>Hallo</p>
        <p>Hallo</p>
        <p>Hallo</p>
        <p>Hallo</p>
        <p>Hallo</p>
        <p>Hallo</p>
    </div>
    <div id='right' style='background:green;height:150px;'>
        <p>Hallo</p>
    </div>
</div>

此示例显示了我想要的内容,但在这种情况下,如果我在中心 div 中添加一些字符,它会将正确的 div 换行,如下所示:

    <div>
    <div id='left' style='background:red;float:left;height:150px;width:150px;'>
    </div>
    <div id='center' style='background:blue;float:left;white-space: nowrap;overflow-x:auto;overflow-y:hidden;'>
        <p>Hallo aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</p>
        <p>Hallo</p>
        <p>Hallo</p>
        <p>Hallo</p>
        <p>Hallo</p>
        <p>Hallo</p>
        <p>Hallo</p>
    </div>
    <div id='right' style='background:green;height:150px;'>
        <p>Hallo</p>
    </div>
</div>

最佳答案

您的问题需要使用 JS 来计算您正在使用的 div 的宽度(因为如果未设置宽度,溢出将不起作用),然后设置正确的百分比宽度以使其流畅。

请注意,当您调整浏览器大小时,此答案将不起作用(太懒了)...

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
 $(document).ready(function() {
    var wrapperWidth = $('#wrapper').width(); // get width of main wrapper
    var wrapperRight = $('#right').width(); // get width of right
    var wrapperRight = (wrapperRight/wrapperWidth)*100; //get percentage width of right
    var wrapperLeft = (150/wrapperWidth)*100; //get percentage width of left
    var wrapperCenter = 100 - (wrapperLeft + wrapperRight); // get percentage width of center
    $('#left').css('width',wrapperLeft+'%'); // add inline css for width
    $('#center').css('width',wrapperCenter+'%'); // add inline css for width
    $('#right').css('width',wrapperRight+'%'); // add inline css for width
 });
</script>
<div id='wrapper' style='width: 100%;'>
    <div id='left' style='background:red;float:left;height:150px;width:150px;'>
    </div>
    <div id='center' style='background:blue;float:left; overflow-x: scroll; overflow-y: hidden;'>
        <p>Hallo aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</p>
        <p>Hallo</p>
        <p>Hallo</p>
        <p>Hallo</p>
        <p>Hallo</p>
        <p>Hallo</p>
        <p>Hallo</p>
    </div>
    <div id='right' style='background:green;height:150px; float: left;'>
        <p>Hallo</p>
    </div>
</div>

关于html - 两个 div 之间的 div 根据需要使用多少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13498221/

相关文章:

javascript - 无法将 PHP 变量传递给 javascript 函数

javascript - 悬停时更改图像颜色的替代方法(过滤器不起作用)

html - 如何动态等于DIV高度

c# - 将 C# 应用程序转换/移植到基于 Web 的技术

html - 如何使用元音符号生成 javadoc 文档?

css - 文本区域的名称出现在底部

javascript - 用ajax上课

web - bmp 文件应该用于网站吗

http - 如何为单个流提供多个连接?

javascript - 如何将字符串转换为对象