Razvan Zamfir
Razvan Zamfir

Reputation: 4684

Vertically centering a block of text within a "card" with flexbox does not work

I am working on a flipping card.

The card's front displays a photo, while its back displays a block of text that looks better if vertically centered on the card.

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
.flip_container {
  width: 300px;
  height: 450px;
  border: 1px solid #ccc;
}
.flip_container img {
  width: 100%;
  height: auto;
}
.flip_container:hover .flip {
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
}
.flip {
  -webkit-transition: 0.5s;
  transition: 0.5s;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  position: relative;
}
.flip_front,
.flip_back {
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  position: absolute;
  top: 0;
  left: 0;
}
.flip_front {
  -webkit-transform: rotateY(0deg);
  transform: rotateY(0deg);
}
.flip_back {
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
  padding: 10px;
  background: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.flip_back h2 {
  margin-top: 0;
  text-align: center;
}
.flip_back p {
  text-align: justify;
  font-family: Arial, sans-serif;
  line-height: 1.5;
}
<div class="flip_container">
  <div class="flip">
    <div class="flip_front">
      <img src="//lorempixel.com/300/450" />
    </div>
    <div class="flip_back">
      <div>
        <h2>Lorem Dummy Title</h2>
        <p>We’ve taken Lorem Ipsum to the next level with our HTML-Ipsum tool. As you can see, this Lorem Ipsum is tailor-made for websites and other online projects that are based in HTML. Most web design projects use Lorem Ipsum excerpts to begin with.</p>          
      </div>
    </div>
  </div>
</div>

I have used a "classic" flexbox method to center the block of text vertically, but something is missing. My question is: what is missing?

Upvotes: 0

Views: 75

Answers (2)

Bhuwan
Bhuwan

Reputation: 16855

No need to use position:absolute for the front div. And use align-items:center and bottom:0 to back div align center.

Stack Snippet

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.flip_container {
  width: 300px;
  height: 450px;
  border: 1px solid #ccc;
}

.flip_container img {
  width: 100%;
  height: auto;
}

.flip_container:hover .flip {
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
}

.flip {
  -webkit-transition: 0.5s;
  transition: 0.5s;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  position: relative;
}

.flip_front,
.flip_back {
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
}

.flip_front {
  -webkit-transform: rotateY(0deg);
  transform: rotateY(0deg);
}

.flip_back {
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
  padding: 10px;
  background: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
}

.flip_back h2 {
  margin-top: 0;
  text-align: center;
}

.flip_back p {
  text-align: justify;
  font-family: Arial, sans-serif;
  line-height: 1.5;
}
<div class="flip_container">
  <div class="flip">
    <div class="flip_front">
      <img src="http://via.placeholder.com/300x450" />
    </div>
    <div class="flip_back">
      <div>
        <h2>Lorem Dummy Title</h2>
        <p>We’ve taken Lorem Ipsum to the next level with our HTML-Ipsum tool. As you can see, this Lorem Ipsum is tailor-made for websites and other online projects that are based in HTML. Most web design projects use Lorem Ipsum excerpts to begin with.</p>
      </div>
    </div>
  </div>
</div>

Upvotes: 0

user4676340
user4676340

Reputation:

You forgot that flexed elements aren't block elements anymore, they don't cover the whole height of their parents.

Add height: 100% to your .flip & .flip-back classes :

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
.flip_container {
  width: 300px;
  height: 450px;
  border: 1px solid #ccc;
}
.flip_container img {
  width: 100%;
  height: auto;
}
.flip_container:hover .flip {
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
}
.flip {
  -webkit-transition: 0.5s;
  transition: 0.5s;
  -webkit-transform-style: preserve-3d;
  transform-style: preserve-3d;
  position: relative;
  height: 100%;
}
.flip_front,
.flip_back {
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  position: absolute;
  top: 0;
  left: 0;
}
.flip_front {
  -webkit-transform: rotateY(0deg);
  transform: rotateY(0deg);
}
.flip_back {
  -webkit-transform: rotateY(180deg);
  transform: rotateY(180deg);
  padding: 10px;
  background: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100%;
}
.flip_back h2 {
  margin-top: 0;
  text-align: center;
}
.flip_back p {
  text-align: justify;
  font-family: Arial, sans-serif;
  line-height: 1.5;
}
<div class="flip_container">
  <div class="flip">
    <div class="flip_front">
      <img src="//lorempixel.com/300/450" />
    </div>
    <div class="flip_back">
      <div>
        <h2>Lorem Dummy Title</h2>
        <p>We’ve taken Lorem Ipsum to the next level with our HTML-Ipsum tool. As you can see, this Lorem Ipsum is tailor-made for websites and other online projects that are based in HTML. Most web design projects use Lorem Ipsum excerpts to begin with.</p>          
      </div>
    </div>
  </div>
</div>

Upvotes: 3

Related Questions