我刚刚开始学习引导程序,一个月进入CSS。我正在编码一个页面,我觉得我对像素和响应式设计的理解很差。
所以我在一节中有三个图像(.demo(。第一个图像(.png(为668x522 PX,另外两个(.jpeg(为548x445 Px。我正在为每个图像使用Col-MD-4。我看到最高的图像最终比其他两个都短。您可以在这里看到我的页面。请注意,最左边的图像是如何比其他两个短的图像,即使其高度在像素方面的高度更高。我的问题是
-
为什么第一个图像的高度短?(我希望它在这三个中最高(。当我测量Chrome中的其他图像时,我会发现它们的图像高度为345 x 280px。如何确定渲染尺寸?
-
我希望这三个都具有相同的高度。我做了
img.demo {height :280px ; }
,所有图像都很好地排列了,但我知道这没有响应。我尝试了img.demo { height :73% }
(母容器COL-MD-4的宽度为380 px,因此IMG的高度为73%。Demo应该给它280px的高度。但是,将其设置为IMG.Demo的高度为%。Demo不对它做任何事情。为什么那是吗? -
使所有图像具有相同高度的响应方式是什么?
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Starter Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="css/style.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html"><strong>GemniPro</strong></a>
</div>
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
<section>
<div class="jumbotron">
<div class = "container">
<div class = "row">
<div class = "col-md-6">
<h1>Capture <strong>Life</strong></h1>
<p>A fun and easy way to capture and share the moments you live for</p>
<a href= "#"><img class= "app-btn" src="img/icon_app_store.png"></a>
<a href= "#"><img class= "app-btn" src="img/icon_google_play.png"></a>
</div>
<div class = "col-md-6">
<img class ="showcase" src = "img/site_phone.png" >
</div>
</div>
</div>
</div>
</section>
<section id = "middle">
<div class = "row">
<div class ="container">
<div class = "col-md-4 ">
<img src = "img/demo1.png" class="demo" >
<h3> Lorem Ipsum</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris lobortis tempus varius. Sed porttitor ex id lectus interdum tristique.</p>
</div>
<div class = "col-md-4 ">
<img src = "img/demo2.jpg" class="demo">
<h3> Lorem Ipsum</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris lobortis tempus varius. Sed porttitor ex id lectus interdum tristique.</p>
</div>
<div class = "col-md-4 demo">
<img src = "img/demo3.jpg" class="demo">
<h3> Lorem Ipsum</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris lobortis tempus varius. Sed porttitor ex id lectus interdum tristique.</p>
</div>
</div>
</div>
</section>
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="js/bootstrap.js"></script>
<script src="js/main.js"></script>
</body>
</html>
CSS:
body {
margin-top: 50px;
}
strong {
color:aqua ;
}
section#middle {
padding:40px 0 40px 0;
}
.jumbotron {
color :#fff;
background: url("../img/site_showcase_bg.jpg");
height:500px;
overflow:hidden;
}
.jumbotron img.app-btn {
height : 60px;
width:40%
}
.jumbotron h1 {
margin-top:125px;
}
.jumbotron p {
margin-bottom:50px;
}
.jumbotron img.showcase {
width:75%;
}
#middle img.demo {
width:100%;
height:73%;
border:solid 1px #ccc;
}
图像如果不应用任何宽度或高度,则保持其长宽比。如果您将两者都应用于纵横比大于原始的方式,则图像会扭曲。
-
第一个图像的高度与宽度比大于其他2个图像,这些图像的高度和宽度相等。宽度越多,高度就越少。
-
百分比高度是根据母体高度计算的。由于
.demo
的封闭容器没有高度,因此在百分比中定义高度没有效果。 -
拍摄相同大小的图像。或
.demo
max-width: 100%;
height: 280px;
border: solid 1px #ccc;
或
.demo
max-width: 100%;
所有3张图像的高度没有太大差异,因此可以完全删除边框。;(
第一个图像稍小一点,因为它的纵横比与其他两个图像不同:
第一个图像原始大小是:668x552 |纵横比= 1.210
另外两个是:548x445 |纵横比= 1.231
他们需要具有相同的纵横比,或者您可以将它们放入DIV中并显示为background-size: contain