【问题标题】:Why is the img-fluid class in Bootstrap 4 not resizing my images in their divs?为什么 Bootstrap 4 中的 img-fluid 类没有在他们的 div 中调整我的图像大小?
【发布时间】:2019-02-10 03:34:08
【问题描述】:

我在这里有一个 Bootply 游乐场示例:https://www.bootply.com/eXlkXkUvDJ

我正在尝试对齐底部的三个图像,以便最左侧和最右侧的图像与主图像和标题齐平。我还希望图像完全填充 div——我用黑色突出显示了第一个图像 div,这样你就可以看到图像没有完全填充它。

我检查了 Bootstrap 4 文档,看起来“img-fluid”类应该使图像填充每个 div,但事实并非如此。

这是我的 HTML 代码:

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Aisha Wells Portfolio</title>
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="main.css">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<div class="container" alt="this is the main container">
    <div class="row">
        <div class="col-md-6" alt="udacity logo">
            <img src="http://cdn.onlinewebfonts.com/svg/img_369247.png" alt="fake logo" width="50" height="50" class="img-responsive">
        </div>
        <div class="col-md-6 text-right text-uppercase" alt="my name">
                <h1> Shere Wells </h1>

                <h3> Writer and Filmmaker </h3>

        </div>
    </div>
</div>
<div class="container" alt="this is the main container">
    <div class="row">
        <div class="col-md-12 rule">
            <hr>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12 main-img" alt="this is the main image">
            <img class="img-fluid" src="http://www.placepuppy.net/400/250" width="100%">
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
                <h4>Featured Work</h4>

        </div>
    </div>
    <div class="container">
        <div class="row">
            <div class="col-md-4 first" alt="First project image">
                <img class="img-fluid" src="https://placekitten.com/250/250" alt="kitteh">
                    <h5>First Name</h5>

            </div>
            <div class="col-md-4 second" alt="Second project image">
                <img class="img-fluid" src="https://placekitten.com/250/250" alt="kitteh">
                    <h5>Second Name</h5>

            </div>
            <div class="col-md-4 third" alt="Third project image">
                <img class="img-fluid" src="https://placekitten.com/250/250" alt="kitteh">
                    <h5>Third Name</h5>

            </div>
        </div>
    </div>
</div>
<div id="push"></div>

这是我的 CSS 代码:

/* CSS used here will be applied after bootstrap.css */

@charset "UTF-8"; 
/*CSS Document for My Portfolio Page*/

h1 { 
    color: #2d3c49; 
    float: right; 
    font-family: 'Montserrat', sans-serif;
}

h3 {
    color:#7d97ad;
    float: right;
    font-size: 20px;
    font-family: 'Montserrat', sans-serif;

}

h4 {
    color:#7d97ad;
    font-family: 'Montserrat', sans-serif;

}

h5 {
    color:#7d97ad; 
    font-family: 'Montserrat', sans-serif;
}

hr {
    border-top-style: solid;
    border-top-width: thick;
    border-top-color:#7d97ad; 
    padding-top:20px;

  }

.first {
    background-color:black; 

}

.col-md-6 {
    background-color:black; 

}

【问题讨论】:

  • 在此处提供您的代码 sn-p
  • @Mahadevan 我编辑了我的帖子!这有帮助吗?

标签: css twitter-bootstrap


【解决方案1】:

在img标签上添加w-100

<img class="w-100 img-fluid" src="https://placekitten.com/250/250" alt="kitteh">

同时删除小猫行开始前的第二个“容器”div。

容器内不需要另一个容器。

Bootply link

【讨论】:

  • 天哪,谢谢你——成功了!你是怎么想出来的?我在 Bootstrap 4 文档中没有看到任何关于“w-100”的内容。
  • 我使用 bootstrap 已有 4-5 年了。它现在只是本能:p 在文档中阅读此getbootstrap.com/docs/4.0/utilities/sizing
  • @WeCareALot 如果它对您有用,请点击赞成/反对按钮下方的勾号图标,接受此答案
猜你喜欢
  • 2020-11-25
  • 1970-01-01
  • 2019-09-02
  • 1970-01-01
  • 1970-01-01
  • 2019-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多