将响应中的div与

时间:2015-12-22 03:50:39

标签: html css css3

我想将下面的jsfiddle放在中心垂直居中,响应式设计让我很难猜到从哪里获取值,因为StackOverflow上的所有教程都在说" div包含内容必须有宽度和高度。" 我已经制作了一幅关于它现在如何(坏)以及我希望它如何(好)的图片说明

bad good< - 在所有设备/宽度/方向上垂直和水平居中

https://jsfiddle.net/34o9vcba/9/

https://jsfiddle.net/34o9vcba/9/embedded/result/

标记

<div class="wrapper">
  <div class="rowOne">
    <div class="telefoonButton">
      <a href="tel:0652333817" title="Bel ons"><img src="http://eightytwenty.nl//wordpress/wp-content/uploads/2015/12/phoneNook.png" alt="telefoon" class="Center-Block Absolute-Center is-Image"></a>
    </div>
    <div class="mailButton">
      <a href="mailto:slotenmakerdenhaag@gmail.com" title="Mail ons"><img src="http://eightytwenty.nl/wordpress/wp-content/uploads/2015/12/mailNook.png" alt="mail" class="Center-Block Absolute-Center is-Image"></a>
    </div>
  </div>
  <div class="rowTwo">
    <div class="infoButton">
      <a href="contact" title="Bel ons"><img src="http://eightytwenty.nl//wordpress/wp-content/uploads/2015/12/infoNook.png" alt="telefoon" class="Center-Block Absolute-Center is-Image"></a>
    </div>
    <div class="contactButton">
      <a href="contact#contact" title="Mail ons"><img src="http://eightytwenty.nl//wordpress/wp-content/uploads/2015/12/contactNook.png" alt="mail" class="Center-Block Absolute-Center is-Image"></a>
    </div>
  </div>
</div>

CSS

.wrapper {
    height: 80vh;
    margin: 0 auto;
    width: 90%;
}
.rowOne, .rowTwo {
    width: 50%;
    height: auto;
    float: left;
}
.Absolute-Center.is-Image {
  height: auto;
}

.Absolute-Center.is-Image img {
  width: 50%;
  height: auto;
}

6 个答案:

答案 0 :(得分:1)

首先,您应该在标记中添加额外的div,并将rowOnerowTwo放入名为div的额外middle中:

<强>标记

<div class="wrapper">
    <div class="middle">
        <div class="rowOne"></div>
        <div class="rowTwo"></div>
    </div>
</div>

然后你应该添加一些CSS:

<强> CSS

.wrapper{
    display: table;
    position: absolute;
    height: 100%;
    width: 100%;
}
.wrapper .middle {
    display: table-cell;
    vertical-align: middle;
}
.wrapper div{ text-align:center }
.wrapper div a{ display: inline-block }

答案 1 :(得分:1)

给下面的css会做一个技巧:

.rowOne > div {
    text-align: center;
}


.rowTwo > div {
    text-align: center;
}

Updated Fiddle

答案 2 :(得分:1)

请检查代码..请HTMLdiv课程.centered和css position:absolute以及transform起诉css3。{非常简单凉爽。

https://jsfiddle.net/34o9vcba/19/

.wrapper {
    height: 100vh;
    margin: 0 auto;
    position: relative;
    width: 100%;
}
.rowOne, .rowTwo {
    width: 50%;
    height: auto;
    float: left;
}
.Absolute-Center.is-Image {
  height: auto;
}

.centered {
  position: absolute;
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
  text-align: center;
}

.Absolute-Center.is-Image img {
  width: 50%;
  height: auto;
}
<div class="wrapper">
  <div class="centered">
    <div class="rowOne">
      <div class="telefoonButton">
        <a href="tel:0652333817" title="Bel ons"><img src="http://eightytwenty.nl//wordpress/wp-content/uploads/2015/12/phoneNook.png" alt="telefoon" class="Center-Block Absolute-Center is-Image"></a>
      </div>
      <div class="mailButton">
        <a href="mailto:slotenmakerdenhaag@gmail.com" title="Mail ons"><img src="http://eightytwenty.nl/wordpress/wp-content/uploads/2015/12/mailNook.png" alt="mail" class="Center-Block Absolute-Center is-Image"></a>
      </div>
    </div>
    <div class="rowTwo">
      <div class="infoButton">
        <a href="contact" title="Bel ons"><img src="http://eightytwenty.nl//wordpress/wp-content/uploads/2015/12/infoNook.png" alt="telefoon" class="Center-Block Absolute-Center is-Image"></a>
      </div>
      <div class="contactButton">
        <a href="contact#contact" title="Mail ons"><img src="http://eightytwenty.nl//wordpress/wp-content/uploads/2015/12/contactNook.png" alt="mail" class="Center-Block Absolute-Center is-Image"></a>
      </div>
    </div>
  </div>
</div>

答案 3 :(得分:0)

好的,试试吧。

html, body {
height: 100%;
}

body {
margin: 0;
}

.wrapper {
height: 100%;
width: 100%;
margin: 0 auto;
text-align: center;
}

.centered {
position: relative;
overflow: hidden;
top: 50%;
transform: translateY(-50%);
-webkit-transform: translateY(-50%);
}

答案 4 :(得分:0)

我解决了!

<div class="wrapper">
    <div class="middle">
  <div class="rowOne">
    <div class="telefoonButton">
      <a href="tel:0652333817" title="Bel ons"><img src="http://eightytwenty.nl//wordpress/wp-content/uploads/2015/12/phoneNook.png" alt="telefoon" class="Center-Block Absolute-Center is-Image"></a>
    </div>
    <div class="mailButton">
      <a href="mailto:slotenmakerdenhaag@gmail.com" title="Mail ons"><img src="http://eightytwenty.nl/wordpress/wp-content/uploads/2015/12/mailNook.png" alt="mail" class="Center-Block Absolute-Center is-Image"></a>
    </div>
  </div>
  <div class="rowTwo">
    <div class="infoButton">
      <a href="contact" title="Bel ons"><img src="http://eightytwenty.nl//wordpress/wp-content/uploads/2015/12/infoNook.png" alt="telefoon" class="Center-Block Absolute-Center is-Image"></a>
    </div>
    <div class="contactButton">
      <a href="contact#contact" title="Mail ons"><img src="http://eightytwenty.nl//wordpress/wp-content/uploads/2015/12/contactNook.png" alt="mail" class="Center-Block Absolute-Center is-Image"></a>
    </div>
  </div>
  </div>
<div>
</div>

.telefoonButton, .mailButton, .infoButton, .contactButton {
  float: left;
}
.wrapper{
    display: table;
    position: absolute;
    height: 100%;
    width: 100%;
}
.wrapper .middle {
    display: table-cell;
    vertical-align: middle;
}
.wrapper div{ text-align:center }
.wrapper div a{ display: inline-block }
.rowOne, .rowTwo {
    width: 50%;
    height: auto;
    float: left;
}
.Absolute-Center.is-Image {
  height: auto;
}

.Absolute-Center.is-Image img {
  width: 50%;
  height: auto;
}

答案 5 :(得分:0)

更改您的css代码,如下所示:

.Absolute-Center.is-Image {
    display: block;
    margin: 0px auto;
  }