Html / CSS大小的图像

时间:2018-05-21 17:43:30

标签: html css ruby-on-rails

我想以相同的固定尺寸显示照片(我使用carieerwave通过表格添加): articles with image

这是我的代码:

<div class="section">
    <div class="container">
        <% @posts.take(4).each do |post| %>
          <div class="carda"  style="width: 500px; float:left; margin: 20px; height: 430px;">
            <div class="card-content">
              <%= link_to image_tag(post.image_url, width: 500, height: 250), post %>
              <p class="title" style="margin-top: 5px; color:black">
      <%= raw link_to post.title, post  %>
              </p>
            </div>
          </div>
      <% end %>
    </div>
</div>

我不知道为什么他们有其他尺寸

1 个答案:

答案 0 :(得分:0)

我想说在这种情况下你想要的图像具有相同的尺寸,如果不是相同的高度。否则你最终会得到这个:

.card-container {
  display: flex;
  justify-content: space-between;
}

.card {}

.card-image {
  width: 150px;
  height: 150px;
}

.card-image img {
  display: block;
  width: 100%;
}


/* Alternative with BG images */

.card-image-bg {
  width: 150px;
  height: 150px;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.card:first-of-type .card-image-bg {
  background-image: url("http://via.placeholder.com/350x150");
}

.card:nth-of-type(2) .card-image-bg {
  background-image: url("http://via.placeholder.com/250x190");
}

.card:last-of-type .card-image-bg {
  background-image: url("http://via.placeholder.com/400x400");
}
<div class="section">
  <div class="card-container">
    <div class="card">
      <div class="card-image"><img src="http://via.placeholder.com/350x150"></div>
      <p class="title">Image 1 caption 1</p>
    </div>
    <div class="card">
      <div class="card-image"><img src="http://via.placeholder.com/250x190"></div>
      <p class="title">Image 2 caption 2</p>
    </div>
    <div class="card">
      <div class="card-image"><img src="http://via.placeholder.com/400x400"></div>
      <p class="title">Image 3 caption 3</p>
    </div>
  </div>
</div>

<div class="section">
  <div class="card-container">
    <div class="card">
      <div class="card-image-bg"></div>
      <p class="title">Image 1 caption 1</p>
    </div>
    <div class="card">
      <div class="card-image-bg"></div>
      <p class="title">Image 2 caption 2</p>
    </div>
    <div class="card">
      <div class="card-image-bg"></div>
      <p class="title">Image 3 caption 3</p>
    </div>
  </div>
</div>