我在使用Bootstrap的网格布局中遇到了我的图像问题。我试图复制http://riotdesign.eu/en/主页的中间部分作为练习。
我的图像将向左拉,高度保持固定在150像素,宽度被切断,因此图像看起来很瘦而且不是很好。我检查了其他网站代码,他们不知何故有一个150x150的图像,md网格布局为130X130,小网格布局为108x108。我怎样才能做到这一点?
我猜测130是指定sm-5网格选择的med容器的默认宽度。所以这就是他们选择调整大小的原因,这是正确的吗?尽管我付出了最大的努力,但我还是无法复制这一点。这是我的代码。
<div class="row index-row">
<div class="col-sm-4 index-left">
<div class="media-background">
<div class="row index-row">
<div class="media col-sm-5 col-md-4">
<div class="image-overlay">
<a class="btn btn-default href="#">Open</a>
<%= image_tag('computer.jpg', class: 'pull-left media-img', alt: 'About') %>
</div>
</div>
<div class="media-body col-sm-7 col-md-8">
<h4>
<%= link_to 'About', root_path, class: 'media-heading' %>
</h4>
<p class="heading-text">
"ABOUT What is Riot Design, our our clients and our specialties."
</p>
</div>
</div>
</div>
</div>
<div class="col-sm-4 index-middle">
<div class="media-background">
<div class="row index-row">
<div class="media col-sm-5 col-md-4">
<a class="pull-left" href="">
<%= image_tag('notebook.jpg', class: 'media-img', alt: 'Portfolio') %>
</a>
</div>
<div class="media-body col-sm-7 col-md-8">
<h4>
<%= link_to 'Portfolio', root_path, class: 'media-heading' %>
</h4>
<p class="heading-text">
PORTFOLIO Web design, app design, print works and photography
</p>
</div>
</div>
</div>
</div>
<div class="col-sm-4 index-right">
<div class="media-background">
<div class="row index-row">
<div class="media col-sm-5 col-md-4">
<a class="pull-left" href="">
<%= image_tag('building.jpg', class: 'media-img', alt: 'Contacts', type: 'button') %>
</a>
</div>
<div class="media-body col-sm-7 col-md-8">
<h4>
<%= link_to 'Contacts', root_path, class: 'media-heading' %>
</h4>
<p class="heading-text">
Where to find the
<strong>Creatives</strong>
contact module and more.
</p>
</div>
</div>
</div>
</div>
</div>
</div>
答案 0 :(得分:2)
要实现这一点,首先必须纠正你的HTML代码,因为它不像你提到的网站那样。用以下代码替换你的html代码:
<div class="row index-row">
<div class="col-sm-4">
<div class="media">
<div class="row ">
<div class="media-img pull-left col-sm-5 col-md-4 ">
<div class="img-overlay ">
<a class="btn btn-primary animated fadeInUp " href="/en/about/" rel="bookmark">Open</a>
</div>
<%= image_tag('computer.jpg', class: 'pull-left media-img', alt: 'About') %>
</div>
<div class="media-body col-sm-7 col-md-8 ">
<h4 class="media-heading"> <%= link_to 'About', root_path, class: 'media-heading' %></h4>
<p class="hidden-sm">"ABOUT What is Riot Design, our our clients and our specialties."</p>
</div>
</div>
</div>
</div>
<div class="col-sm-4">
<div class="media">
<div class="row ">
<div class="media-img pull-left col-sm-5 col-md-4 ">
<div class="img-overlay ">
<a class="btn btn-primary animated fadeInUp needsclick" href="/en/portfolio/" rel="bookmark">Open</a>
</div>
<%= image_tag('notebook.jpg', class: 'media-img', alt: 'Portfolio') %>
</div>
<div class="media-body col-sm-7 col-md-8 ">
<h4 class="media-heading"> <%= link_to 'Portfolio', root_path, class: 'media-heading' %></h4>
<p class="hidden-sm">PORTFOLIO Web design, app design, print works and photography</p>
</div>
</div>
</div>
</div>
<div class="col-sm-4">
<div class="media">
<div class="row ">
<div class="media-img pull-left col-sm-5 col-md-4 ">
<div class="img-overlay ">
<a class="btn btn-primary animated fadeInUp needsclick" href="/en/contacts/" rel="bookmark">Open</a>
</div>
<%= image_tag('building.jpg', class: 'media-img', alt: 'Contacts', type: 'button') %>
</div>
<div class="media-body col-sm-7 col-md-8 ">
<h4 class="media-heading"><%= link_to 'Contacts', root_path, class: 'media-heading' %></h4>
<p class="hidden-sm">Where to find the <strong>Creatives</strong> contact module and more.</p>
</div>
</div>
</div>
</div>
</div>
并将此添加到您的css文件中,图片应如您所愿:
.media-img {
overflow: hidden;
padding: 0;
position: relative;
}
.media-img .img-overlay {
background: none repeat scroll 0 0 rgba(0, 0, 0, 0.4);
height: 100%;
opacity: 0;
padding-top: 50%;
position: absolute;
text-align: center;
transition: opacity 0.3s ease-out 0s;
visibility: hidden;
width: 100%;
z-index: 4;
}
.media div.media-img img {
height: auto;
transform: scale(1, 1);
transition-duration: 2000ms;
transition-timing-function: ease-out;
width: 100%;
}
img {
vertical-align: middle;
}
.media-img img {
width: 100%;
}
.media div.pull-left {
margin-left: 14px;
margin-right: -15px;
overflow: hidden;
}