html - 根据是否存在其他元素更改 div 的宽度

标签 html css width

我有两个并排的 float div。我的左边 div 占分配空间的 30%,右边 div 占 65%。但是,我的站点由 CMS 提供支持,当左侧 div 中没有内容时,它根本不会显示在浏览器中。这让我只有一个 65% 的 div 和它旁边的一些空白空间,看起来有点不漂亮。如果未显示 30% 的 div,是否有直接的 HTML/CSS2 方法指示我的 65% div 占用分配的全部空间量,或者这是否必然涉及 jQuery 和其他此类解决方案?

这是我的 JSFIDDLE

HTML

<div class="container">
    <div class="left">
        <p>Content on the left</p>
    </div>
    <div class="right">
        <p>Content on the right</p>
    </div>
</div>
<div class="container">
    <div class="right">
        <p>When there is only content in the right div, it needs to have a width value of 100%.</p>
    </div>
</div>
<div class="container">
    <div class="left">
        <p>Content on the left</p>
    </div>
    <div class="right">
        <p>Content on the right</p>
    </div>
</div>

CSS

p
    {
        padding: 10px;
    }

.container
    {
        overflow: auto;
        font-family: Arial, sans-serif; 
        color: #fff;
        margin-bottom: 20px;
    }

.container .left
    {
        float: left;
        width: 30%;
        margin-right: 5%;
        background: #555;
    }

.container .right
    {
        float: left;
        width: 65%;
        background: #c00;
    }

最佳答案

你可以尝试类似的东西:

.container .right
{
    float: left;
    min-width: 65%;
    width: auto;
    background: #c00;
}

(未测试)

关于html - 根据是否存在其他元素更改 div 的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28582014/

相关文章:

html - 移动 View 中的文本框破坏容器

javascript - 在新选项卡 'about:blank' 上打印并像数据表库一样打印后关闭

html - 负 z-index 在背景下消失

css - 为什么元素的行高(以像素为单位)受其父元素的字体大小影响?

html - 获取元素以使用可用的最大宽度,具体取决于元素的实例数量

css - 如何使 Bootstrap 菜单的宽度与元素的总宽度一样?

html - 我们如何在较小的屏幕尺寸下调整(缩小)div 的背景图像

linux - 标记音频和 ogg

javascript - 猫头鹰 slider - 重复控件问题

character - UTF-8 中最长的字符