【问题标题】:Div not properly wrapping around contents (image) - Includes margin, possible float/Bootstrap issuesDiv 未正确环绕内容(图像) - 包括边距、可能的浮动/引导问题
【发布时间】:2014-10-30 20:35:22
【问题描述】:

如何在其中的图像周围调整 div 容器的大小/包装? float: right 和 margin-left: auto 可能会导致问题。

我正在努力通过正确环绕其中的图像来调整 div 的大小。请看一下我在这里引用的示例:

Link to Example

(可能值得玩弄窗口大小来帮助解释我的问题)

我是第一次练习 Bootstrap。每侧的红色块是网格块 1 和 12,蓝色和绿色部分填充剩余的 10 个。橙色大矩形是响应式图像,我希望始终保持中心间隔 20 像素。

使用 Chrome 的“检查元素”(或类似的) - 如果您检查右侧的橙色矩形,并查看容器 div (class="container-img-r") - 此 div 正在包装围绕橙色图像完全符合我的要求(尽管包括不可见的边框)。但是我没有太多运气使用左侧橙色图像的 div 容器获得相同的结果(它仍然填充蓝色父元素)

我尝试过浮动/边距/位置的不同选项,但似乎无法破解它。

这是我为相关内容准备的 CSS:

.container-img-l {
    /* float:right; ??? Nothing I tried here seemed to make a difference */
}

.container-img-r {
    float:left;
}

.item-pos-l {
    margin-left:auto;
    border-right:10px solid transparent; /* Margins just collapsed when resizing window */
    height:323px;
    width:510px;
}

.item-pos-r {
    float:left;
    border-left:10px solid transparent;
    height:323px;
    width:510px;    
}

我希望 div 准确环绕响应式图像的原因是我想在图像上覆盖更多 CSS 内容,随着窗口/设备大小的变化自动缩放/重新定位(Click here 和你'会清楚地看到我希望在哪里实现这种响应式样式)。

也许与 Bootstrap CSS 存在冲突,但我没有想法。

【问题讨论】:

    标签: css twitter-bootstrap responsive-design scale


    【解决方案1】:

    您的第一个链接看起来不像您想要响应的 html。在尝试修改您不熟悉的框架之前,最好学习响应式和流畅的(如果可能的话没有像素高度或宽度)css。此外,您的 html 中有错误 - 验证它以确保您已关闭所有元素。同时缩进和注释所有代码,避免使用内联样式。

    演示:http://jsbin.com/wazanu/2/

    http://jsbin.com/wazanu/2/edit -- 编辑链接

    CSS

    body {background:#eee}
    
    .header {padding:20px;}
    
    .portfolio-grid .col-sm-6 {
        margin-bottom: 30px
    }
    
    .img-wrapper .title {
         text-align:center;
    }
    
    @media (min-width:768px) { 
        .img-wrapper {
            position: relative;
            overflow: hidden;
        }
      
        .img-wrapper img {width:100%;}
      
        .img-wrapper .title {
            position: absolute;
            text-align:left;
            bottom: -90px;
            padding: 0 20px 20px 20px;
            height: 150px;
            background: red;
            transition: all .5s ease-in-out;
        }
        .img-wrapper .title h3 {
            margin: 0;
            height: 60px;
            line-height: 60px;
        }
        .img-wrapper:hover .title {
            bottom: 0
        }
    }
    

    HTML:

    <header class="header text-center">
       My header
    </header>
    <div class="container">
       <div class="row portfolio-grid">
          <div class="col-sm-6">
             <div class="img-wrapper">
                <img src="http://placekitten.com/g/400/300" class="img-responsive center-block" alt="">
                <div class="title">
                   <h3>Title of Project</h3>
                   <p>Content about project goes here</p>
                </div>
             </div>
          </div>
          <!--/.col-sm-6 -->
          <div class="col-sm-6">
             <div class="img-wrapper">
                <img src="http://placebear.com/g/400/300" class="img-responsive center-block" alt="">
             </div>
          </div>
          <!--/.col-sm-6 -->
          <div class="clearfix visible-sm"></div>
          <div class="col-sm-6">
             <div class="img-wrapper">
                <img src="http://placekitten.com/g/400/300" class="img-responsive center-block" alt="">
             </div>
          </div>
          <!--/.col-sm-6 -->
          <div class="col-sm-6">
             <div class="img-wrapper">
                <img src="http://placekitten.com/g/400/300" class="img-responsive center-block" alt="">
             </div>
          </div>
          <!--/.col-sm-6 -->
       </div>
       <!--/.row-->
    </div>
    <!--/.container-->
    

    【讨论】:

    • 感谢您的快速帮助。我计划在完成测试版本后重新制作我想要做出响应的整个原始页面。我已经开始将我的内容添加到您的技术中,但遇到了另一个问题。我更喜欢流体容器,因为我喜欢元素逐渐调整大小的方式,但是当我调整浏览器窗口的大小时,根据宽度,一些块“跳过”一个位置并将其他块推到下面的一行。请看这里:link.
    • Remove
      当尺寸不同但不是你的情况时很好。
    • 啊,不要删除它。使您的图像,实际图像,完全相同的大小,它们不是。
    • 啊!多么愚蠢,我忘了我还没有更新其余的。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多