如何使我的html中的图像大小相等?

时间:2017-11-20 12:21:49

标签: html5 css3 bootstrap-4

我正在学习Bootstrap并且在我的html页面中我使用了它的版本4.在代码中我使用“卡片”给图像赋予形状但图像保持拉长而不对齐。 Here is my code如果有人可以提供帮助。我在代码中提到了我需要帮助。如果你去Codepen站点,你会发现错误

<!DOCTYPE html>
<html>
<head>
<title>Image Gallery</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<style type="text/css">
     body { padding-top: 70px; }
    .material-icons { font-size: 38px; }
    .card { 
        margin: 10px 0; 
        }
    .flex {
          display: flex;
          flex-wrap: wrap;
        }
</style>
</head>
<body>
  <nav >
    <nav class="navbar fixed-top navbar-expand-md navbar-dark bg-dark">
        <button  class="navbar-toggler" type="button" data-toggle="collapse" 
        data-target="#nav-content" aria-controls="nav-content" aria-
        expanded="false" aria-label="Toggle navigation">
         <span class="navbar-toggler-icon"></span>
        </button>

        <!-- Brand -->
        <a class="navbar-brand" href="#">PICS</a>

        <!-- Links -->
        <div class="collapse navbar-collapse" id="nav-content">   
        <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
                <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">About</a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="#">Contacts</a>
            </li>
        </ul>
        <!--Search-->
        <form class="form-inline my-2 my-lg-0">
            <input class="form-control mr-sm-2" type="search"   placeholder= "Search"  aria-label="Search">
            <button class="btn btn-outline-success my-2 my-sm-0" type=" submit">Search</button>
        </form>
    </div>
  </nav>
</nav>
<div class="container">
    <div class="jumbotron" style="background-color: #CCCDCE">
        <h1><i class="material-icons">photo_camera</i> The Image Gallery</h1>
        <p> My first image gallery creation</p>
    </div>
    <!--I NEED HELP HERE-->
    <div class="row flex">
        <div class="col-lg-4 col-sm-6">
            <div class="card">
                <img src="https://source.unsplash.com/ucdh5HMkRMg">
            </div>
        </div>
        <div class="col-lg-4 col-sm-6">
            <div class="card">
                <img src="https://source.unsplash.com/tLNRTxieD7k">
            </div>
        </div>
        <div class="col-lg-4 col-sm-6">
            <div class="card">
                <img src="https://source.unsplash.com/TxoMYFip9d0">
            </div>
        </div>
    </div>
    <div class="row flex">
        <div class="col-lg-4 col-sm-6">
            <div class="card">
                <img src="https://source.unsplash.com/bJHWJeiHfHc">
            </div>
        </div>
        <div class="col-lg-4 col-sm-6">
            <div class="card ">
                <img src="https://source.unsplash.com/tGTVxeOr_Rs">
            </div>
        </div>
        <div class="col-lg-4 col-sm-6">
            <div class="card">
                <img src="https://source.unsplash.com/Lm5rkxzgiFQ">
            </div>
        </div>
    </div>
    <div class="row flex">
        <div class="col-lg-4 col-sm-6">
            <div class="card">
                <img src="https://source.unsplash.com/Ecxnzl-FbV4">
            </div>
        </div>
        <div class="col-lg-4 col-sm-6">
            <div class="card">
                <img src="https://source.unsplash.com/dfo06_DqxpA">
            </div>
        </div>
        <div class="col-lg-4 col-sm-6">
            <div class="card">
                <img src="https://source.unsplash.com/_iP1Vf1WzrE">
            </div>
        </div>
    </div>
</div>


<!--JQuery-->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<!-- Popper -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script>
<!--JS-->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ" crossorigin="anonymous"></script>

1 个答案:

答案 0 :(得分:0)

在你的CSS中你实际上有几个选择。 您可以为卡片成像器设置“最大高度”和100%“宽度”,或者只选择方形图像并将宽度设置为100%。因此,如果你的所有图像都没有平方,你可以将它添加到你的css:

.card img {
   width:100%;
   height:250px; /* You can set height as whatever you want */
}