【问题标题】:Adding a full-width image to a Bootstrap 3 row/column?将全角图像添加到 Bootstrap 3 行/列?
【发布时间】:2016-09-07 17:20:42
【问题描述】:

我有一个 Bootstrap 3 网格。

我需要添加一个 960 像素宽的标题图片。

但是,如果我将它添加到一行中,列上 10 像素的通常填充会“偏移”我的图像,因为它不再“适合”:

我知道如何强制使其工作,但我想知道我是否缺少 BS3 中的一些修饰符类来使其工作。

而且,是的,我知道我可以使用 css 背景图像,但客户想要一个图像。

【问题讨论】:

  • 你试过基本的负边距吗-left:-10px ;或位置:相对+左:-10px?
  • 是的,正如我所说,我可以强制它工作,我的问题是 Bootstrap 是否有一个我忽略的用于此目的的类。
  • 带走你的某些类并给它一个没有边距或填充的行类,我相信。你没有给出代码,所以有趣的猜测;)这个阅读将有助于helloerik.com/…
  • "if I add it to a row, the usual padding of 10px on a column "offsets" my image" 实际上,如果您直接在row 下添加 100% 宽度的图像,那么您可以拥有全宽图像没有问题。而如果您将图像添加到宽列 col-sm-12,则 列 将具有填充并且您的图像将被偏移。因此,只需将图像直接添加到row,而不将其嵌套在col-*-12 中。根据我的调查,将全角图像放到行中没有问题,但我无法弄清楚如何使用全角图像(除非你把图像 width: calc(100% +30px);
  • 这是我在使用引导程序时遇到过很多次的问题 - 如前所述,您基本上有两个选择:

标签: html css twitter-bootstrap responsive-design twitter-bootstrap-3


【解决方案1】:

在您的 CSS 中添加一个类,删除边距以在列内制作全角图像,然后将该类添加到列之后。

.bosom-none {
    margin-right: -15px; // Removes the right gap
    margin-left: -15px;  // Removes the left gap
}

<div class="container">
    <div class="row">
        <div class="colum-md-6 bosom-none">
            <img src="your-image.jpg">
        </div>
    </div>
</div>

【讨论】:

  • Biddingger 无法完成这项工作。看起来好像它可以工作,但它只删除左边距。但图像不会延伸到右边缘。即使您添加到.bosom-none { width: 100%; },它仍然无法正常工作,因为以像素为单位的图像宽度是在 应用left- 和right-margin 之前计算的。这意味着如果列宽是 130 像素,那么 100% 的图像将被计算为 100 像素(如果您想在边上有 2 个 15 像素的边距,这很有意义)。但随后margin-left: -15 只是将图像向左移动。但是margin-right: -15; 什么都不做。右边有 30px 的间隙
  • 投标人已接近。对我有帮助的只是将 padding-left 和 -right 设置为那个 bosom-none 的 0。 Negative-Margin 搞砸了 :)
  • 你可以将 width:calc(100% + 30px) 添加到你的 bosom-none 类中
【解决方案2】:

你可能有别的东西把它搞砸了,因为你可以看到,一个容器,一个行,然后一个 img 给你没有边距。

<div class="container" style="background:green">
    <div class="row">
        <img src="http://www.placehold.it/150x150">
    </div>
</div>

http://jsfiddle.net/dBNwq/1

这是整个页面

<!DOCTYPE html>
<html>
    <head>
        <link href="css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
        <div class="container" style="background:green;">
            <div class="row">
                    <img src="http://www.placehold.it/960x150">
            </div>
            <div class="row" style="background:red;">
                Some other content
            </div>
        </div>
    </body>
</html>

【讨论】:

  • 您根本没有使用 Bootstrap 3,只是直接使用 html。所以,是的,它当然有效。请阅读问题。
  • 改用container-fluid。
  • 只有列 (.col-*-*) 可以是 .rows 的子级
【解决方案3】:

这是我在使用 Bootstrap 时遇到过很多次的问题。你基本上有三个选择:

我通常选择选项#1。

请注意,这并不能回答您的问题。它的意思是写这篇文章以供遇到同样问题但不知道如何强制工作的其他人参考。

您问题的实际答案:不,Bootstrap 不提供这样的修饰符类。

【讨论】:

    【解决方案4】:

    只需将 div class="container" 替换为 div class="container-fluid"

    就是这样!

    【讨论】:

    • .container-fluid 使您的整个应用程序 100% 宽,而不是应用最大宽度并使其居中 (.container)。这与提到的内容被填充列包含的问题无关
    【解决方案5】:

    您还可以创建一个类来覆盖它所在的容器...

    .heroLg {
        size:cover;
        -webkit-size: cover;
        -moz-size: cover;
        -o-size: cover;
    }
    

    然后将类添加到您的图像中。

    【讨论】:

    • 我认为你的意思是背景尺寸,这只适用于背景图片,不适用于 IMG 元素
    【解决方案6】:

    将图像放入 span12

    <div class="row">
         <div class="span12">
             <img src="image.jpg" />
         </div> 
    </div>
    

    【讨论】:

    • Bootstrap 3 已删除 span12 以换取 col-md-12 等。阅读问题。 -1
    猜你喜欢
    • 2017-09-14
    • 2016-03-29
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多