Gandoe Dyck
Gandoe Dyck

Reputation: 97

centering responsive divs with images inside

I would like to center the following jsfiddle to be centered vertically at alll times, responsive design makes it hard for me to guess where to get a value from because all tutorials on StackOverflow have been saying "The div containing the content must have a width and height." I have made a picture illustration on how it is now(bad) and how i would want it to be (good)

bad good <-- centered vertically and horizontally on all devices/widths/orientation

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

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

Markup

<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:[email protected]" 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;
}

Upvotes: 4

Views: 96

Answers (6)

Emirhan
Emirhan

Reputation: 132

Change your css code like below:

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

Upvotes: 0

Gandoe Dyck
Gandoe Dyck

Reputation: 97

I worked it out!

<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:[email protected]" 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;
}

Upvotes: 0

VilleKoo
VilleKoo

Reputation: 2855

Ok, try this.

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%);
}

Upvotes: 0

Ram kumar
Ram kumar

Reputation: 3162

Please check code.. Please in HTML wrap with div class .centered and in css position:absolute and transform suing css3 .. Its pretty easy and cool.

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:[email protected]" 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>

Upvotes: 1

Peyman Mohamadpour
Peyman Mohamadpour

Reputation: 17944

First you should add extra div to your markup and put rowOne and rowTwo in the extra div, named middle:

Markup

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

Then you should add some 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 }

Upvotes: 1

ketan
ketan

Reputation: 19341

Give following css will do a trick:

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


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

Updated Fiddle

Upvotes: 1

Related Questions