流体div

时间:2015-07-13 12:37:33

标签: html css css3

我有一个棘手的问题(至少对我来说)一个div,这是一个纯粹在css中完成的翻转卡片。卡的正面显示图像,背面有不同的图像和按钮。问题是由于流体设计我必须设置填充。将所有文本放在div下面

.card {
      margin: 0 auto;
      position: relative;
      float: left;
      padding-bottom: 25%;
      width: 100%;
      max-width: 600px;
    }

    .card__front,
    .card__back {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
    }

    .card__front,
    .card__back {
      -webkit-backface-visibility: hidden;
      backface-visibility: hidden;
      -webkit-transition: -webkit-transform 0.7s;
      transition: transform 0.7s;
    }

    .card__front {
      background-image: url("http://i58.tinypic.com/mmu649.jpg");
      padding-top: 56.25%;
      background-size: cover;
      -moz-background-size: cover;
      background-position: center;
    }

    .card__back {
      background-image: url("http://i58.tinypic.com/mmu649.jpg");
      -webkit-transform: rotateY(-180deg);
      transform: rotateY(-180deg);
      padding-top: 56.25%;
      background-size: cover;
      -moz-background-size: cover;
      background-position: center;
    }

    .card.effect__hover:hover .card__front {
      -webkit-transform: rotateY(-180deg);
      transform: rotateY(-180deg);
    }

    .card.effect__hover:hover .card__back {
      -webkit-transform: rotateY(0);
      transform: rotateY(0);
    }

    .card__text {
      width: auto;
      text-align: center;
      height: 100%;
      margin-top: -32%;
    }

    .button {
      background-color: #000000;
      text-indent: 0;
      border: 1px solid #000000;
      display: inline-block;
      color: #ffffff;
      font-family: Arial;
      font-size: 21px;
      font-weight: normal;
      font-style: normal;
      height: 69px;
      line-height: 69px;
      width: 200px;
      text-decoration: none;
      text-align: center;
      opacity: 0.8;
    }

    .button:hover {
      background: -webkit-gradient(linear, left top, left bottom, color- stop(0.05, #000000), color-stop(1, #000000));
      background: -moz-linear-gradient(center top, #000000 5%, #000000 100%);
      filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#000000', endColorstr='#000000');
      background-color: #000000;
    }

    .button:active {
      position: relative;
      top: 1px;
    }
<div class="card effect__hover">
  <div class="card__front">
    <div class="card__text"></div>
  </div>
  <div class="card__back">
    <div class="card__text"> <a href="http://google.co.uk" class="button">see more</a> 
    </div>
  </div>
</div>
<!-- /card -->

以下是代码的预览:http://jsfiddle.net/j29mvoLm/

我想要的就是将每个卡片中的相同按钮居中,就像div一样。 是否可以在不使用负边际的情况下进行?

2 个答案:

答案 0 :(得分:0)

这应该是正义的:

.element {
   position: relative;
   top: 50%;
   transform: translateY(-50%);
}

display: table-cell;
vertical-align: middle;

答案 1 :(得分:0)

有可能。请参阅片段。这个答案使用嵌套的相对和绝对定位元素,并使用box-sizing:border-box;和填充来保持宽高比。

如果您需要设置最大宽度,请在卡片周围添加另一个包装元素,并在该元素上设置max-width

&#13;
&#13;
.card-wrapper {
    margin: auto;
    max-width: 600px;
}

.card {
    box-sizing: border-box;
    margin: auto;
    padding: 28.125% 50%; /* 16:9 aspect ratio */
    position: relative;
    width: 100%;
}

.card__front, .card__back {
    bottom: 0;
    height: 100%;
    left: 0;
    margin: auto;
    position: absolute;
    right: 0;
    top: 0;
    width: 100%;
}

.card__front, .card__back {
    -webkit-backface-visibility: hidden;
          backface-visibility: hidden;
    -webkit-transition: -webkit-transform 0.7s;
          transition: transform 0.7s;
}

.card__front {
    background-image: url("http://i58.tinypic.com/mmu649.jpg");
    background-size: cover;
    -moz-background-size: cover;
    background-position: center;
}

.card__back {
    background-image: url("http://i58.tinypic.com/mmu649.jpg");
    -webkit-transform: rotateY(-180deg);
          transform: rotateY(-180deg);
    background-size: cover;
    -moz-background-size: cover;
    background-position: center;
    
    
}
.card.effect__hover:hover .card__front {
    -webkit-transform: rotateY(-180deg);
          transform: rotateY(-180deg);
}

.card.effect__hover:hover .card__back {
    -webkit-transform: rotateY(0);
          transform: rotateY(0);
}
.card__text {
    height: 100%;
    position: relative;
    text-align: center;
    width: 100%;
}

.button {
    background-color: #000000;
    border: 1px solid #000000;
    bottom: 0;
    color: #ffffff;
    display: block;
    font-family: Arial;
    font-size: 21px;
    font-style: normal;
    font-weight: normal;
    height: 69px;
    left: 0;
    line-height: 69px;
    margin: auto;
    opacity: 0.8;
    position: absolute;
    right: 0;
    text-align: center;
    text-decoration: none;
    text-indent: 0;
    top: 0;
    width: 200px;
}

.button:hover {
    background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #000000), color-stop(1, #000000) );
    background:-moz-linear-gradient( center top, #000000 5%, #000000 100% );
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#000000', endColorstr='#000000');
    background-color:#000000;
}

.button:active {
    position:relative;
    top:1px;
}
&#13;
<div class="card-wrapper">
<div class="card effect__hover">
  <div class="card__front">
    <div class="card__text"></div>
  </div>
  <div class="card__back">
    <div class="card__text"> <a href="http://google.co.uk" class="button">see more</a> </div>
  </div>
</div><!-- /card -->
</div>
&#13;
&#13;
&#13;