简体   繁体   English

使用CSS垂直对齐文本

[英]vertically aligning text with CSS

I'm creating a webstore and I want the name of each product to be centered in the middle of the product image box. 我正在创建一个网上商店,我希望每个产品的名称都位于产品图片框的中间。 I'm trying to accomplish this using 'vertical-align:middle' but my efforts haven't worked I've looked at a few tutorials, however I can't seem to get it working. 我正在尝试使用'vertical-align:middle'完成此操作,但是我的努力没有奏效,我看过一些教程,但是似乎无法正常工作。

HTML/ERB HTML / ERB

<% @products.each_slice(4) do |row| %>
<div class="row-fluid">
<% row.each do |product| %>
  <div class="span3">
    <div class="storeitem">
      <div class="rollover-info">
        <p><%= link_to product.name, product %></p>
      </div>
      <%= link_to image_tag(product.images.order(:placement).first.image.url(:medium)), product if product.images.present? %>
    </div>
  </div>
<% end %>
</div>
<% end %>

CSS 的CSS

.storeitem {
 width: 210px;
 height: 210px;
 border: 1px solid #e4e4e4;
 margin-top: 10px;
 margin-bottom: 10px;
 text-align: center;
 padding: 5px;
 z-index: 1
 }

.storeitem img {
width: 90%;
height: 90%;
z-index: 1;
}

.rollover-info {
background-color:rgba(255,255,255,0.8);
border-radius: 50%;
width: 210px;
height: 210px;
text-align: center;
z-index: 10;
opacity: 1;
-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;
display: table;
}

.rollover-info p {
display: inline-block; 
vertical-align: middle; 
text-align: center; 
}

The vertical-align property in css can be a bit confusing at first, as many people (myself included) tend to have misconceptions about what it actually is. 起初,css中的vertical-align属性可能有点令人困惑,因为许多人(包括我自己)往往对它的实际含义有误解。

You can gain a comprehensive understanding of the property by reading this: 您可以通过阅读以下内容全面了解该物业:

http://www.impressivewebs.com/css-vertical-align/ http://www.impressivewebs.com/css-vertical-align/

Though in short, my advice for getting vertical alignment to work, is to use display: table-cell; 简而言之,我的建议是使用垂直对齐方式,即使用display: table-cell; on the element which has contents that need to be vertically aligned (the above article explains this). 在元素上需要垂直对齐的元素上(上面的文章对此进行了解释)。 This will require the container element to be set to display: table; 这将需要将容器元素设置为display: table; .

Table-cell is the easiest way to vertically-align content, though it may not always fit into your layout, or may take some reformatting, so I'd recommend reading up on that as well, if you are unfamiliar with it. 表格单元是垂直对齐内容的最简单方法,尽管它可能并不总是适合您的布局,或者可能需要重新格式化,所以如果您不熟悉它,我建议您也仔细阅读。

There are two methods, 1. using line-height and 2. using tables. 有两种方法:1.使用line-height和2.使用表。 The logic/application/implications of both methods are summarised here: http://phrogz.net/css/vertical-align/ 此处总结了这两种方法的逻辑/应用/含义: http : //phrogz.net/css/vertical-align/

If the product descriptions do not carry over two lines you can use the line-height method that is outlined. 如果产品说明中没有两行,可以使用概述的line-height方法。 Otherwise you will need to use display: table and display: table-cell 否则,您将需要使用display: tabledisplay: table-cell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM