在图像叠加中居中CSS'内容'属性

时间:2014-10-21 12:46:45

标签: html css css3

.image {
  padding: 0px;
  max-width: 200px;
  max-height: 200px !important;
  margin: 15px;
  position:relative;
  overflow: hidden;
}

.image img {
  width:100%;
  margin-top: 1px;
}

.image:after {
  content:'x';
  font-family:Helvetica;
  font-weight:900;
  color: #f5f5f5;
  font-size:24px;
  position:absolute;
  width:100%; height:100%;
  top:0; right:0;
  background:rgba(0,0,0,0.6);
  opacity:0;
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
  text-align: center;
}

.image:hover:after {
  opacity:1;
}
<div class="image">
    <img src="http://static.guim.co.uk/sys-images/Guardian/Pix/pictures/2013/1/11/1357921737290/Masterclass-in-HTML5-and--006.jpg" />
</div>

以下是我的代码的小提琴:http://jsfiddle.net/9u06fbbf/

我想要的是,将鼠标悬停在图像上方时出现的“x”垂直居中。 我试过显示:table-cell with vertical-align:middle,但我似乎无法让它正常工作。

我希望这适用于具有不同尺寸和宽高比的多个图像!

有什么想法吗?

4 个答案:

答案 0 :(得分:1)

演示 - http://jsfiddle.net/9u06fbbf/10/

删除max-widthmax-height

演示 - http://jsfiddle.net/9u06fbbf/13/

:before用于content:"x"

.image:before {
    content:'x';
    font-family:Helvetica;
    font-weight:900;
    color: #f5f5f5;
    font-size:24px;
    position:absolute;
    top:50%;
    z-index:2;
    transform:translateY(-50%);
    width:100%;
    text-align:center;
    opacity:0;
    transition: all 0.5s;
    -webkit-transition: all 0.5s;
}

答案 1 :(得分:0)

您可以将其填充以使其垂直对齐。

.image:after {
  content:'x';
  font-family:Helvetica;
  font-weight:900;
  color: #f5f5f5;
  font-size:24px;
  position:absolute;
  top: 0;
  width:100%; height:100%;
  padding-top:25%; // changed from top: 0;
  right:0;
  background:rgba(0,0,0,0.6);
  opacity:0;
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
  text-align: center;
}

答案 2 :(得分:0)

工作小提琴; 更新 http://jsfiddle.net/9u06fbbf/15/

.image {
    padding: 0px;
    max-width: 200px;
    max-height: 200px !important;
    margin: 15px;
    position:relative;
    overflow: hidden;
}
.image img {
    width:100%;
    margin-top: 1px;
}
.image:after {
    content:"";
    position:absolute;
    width:100%;
    height:100%;
    top:0;
    right:0;
    background:rgba(0, 0, 0, 0.6);
    opacity:0;
    transition: all 0.5s;
    -webkit-transition: all 0.5s;
}
.image:before {
    content:'x';
    font-family:Helvetica;
    font-weight:900;
    color: #f5f5f5;
    font-size:24px;
    position:absolute;
    top:50%;
    z-index:2;
    transform:translateY(-50%);
    width:100%;
    text-align:center;
    opacity:0;
}
.image:hover:after, .image:hover:before {
    opacity:1;
}

进一步参考 Vertical align anything with just 3 lines of CSS

答案 3 :(得分:0)

你应该添加** padding-top:25%; http://jsfiddle.net/9u06fbbf/9/

.image:after {
  padding-top:25%;
  content:'x';
  font-family:Helvetica;
  font-weight:900;
  color: #f5f5f5;
  font-size:24px;
  position:absolute;
  width:100%; height:100%;
  top:0; right:0;
  background:rgba(0,0,0,0.6);
  opacity:0;    
  transition: all 0.5s;
  -webkit-transition: all 0.5s;
  text-align: center;
}