manutdfan
manutdfan

Reputation: 295

How can I center contents of a div vertically and horizontally inside of it?

I have a portfolio type 3 columned layout. Every portfolio item is a div containing name of the project and a little brief. Project name is displayed first and when I hover over the div, the div flips and the brief is shown then.

When I hover over the div, brief is overflowing from top left. I want both the project name and brief to be placed in the dead center of the div. Below is the snippet code.

.flip-container {
  perspective: 1000;
  margin-right: 30px;
  background-color: red;
  margin-bottom: 20px;
}
/* flip the pane when hovered */

.flip-container:hover .flipper,
.flip-container.hover .flipper {
  transform: rotateY(180deg);
}
.flip-container,
.front,
.back {
  width: 360px;
  height: 320px;
}
/* flip speed goes here */

.flipper {
  transition: 0.6s;
  transform-style: preserve-3d;
  position: relative;
}
/* hide back of pane during swap */

.front,
.back {
  backface-visibility: hidden;
  position: absolute;
  text-align: center;
  /*top: 0;
	left: 0;*/
}
/* front pane, placed above back */

.front {
  z-index: 2;
  /* for firefox 31 */
  transform: rotateY(0deg);
}
/* back, initially hidden pane */

.back {
  transform: rotateY(180deg);
}
<div class="row">
  <div class="flip-container col-md-4 portfolio-item" ontouchstart="this.classList.toggle('hover');">
    <div class="flipper">
      <div class="front">
        <!-- front content -->
        <h3>
                        <a href="#">Project Name</a>
                      </h3>
      </div>
      <div class="back">
        <!-- back content -->
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.</p>
      </div>
    </div>
  </div>
  <div class="flip-container col-md-4 portfolio-item" ontouchstart="this.classList.toggle('hover');">
    <div class="flipper">
      <div class="front">
        <!-- front content -->
        <h3>
                      <a href="#">Project Name</a>
                    </h3>
      </div>
      <div class="back">
        <!-- back content -->
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.</p>
      </div>
    </div>
  </div>

Upvotes: 0

Views: 50

Answers (2)

Tarun Dugar
Tarun Dugar

Reputation: 8971

I have created a solution but it requires specifying the height and width of the anchor tag.

.flip-container {
  perspective: 1000;
  margin-right: 30px;
  background-color: red;
  margin-bottom: 20px;
}
/* flip the pane when hovered */

.flip-container:hover .flipper,
.flip-container.hover .flipper {
  transform: rotateY(180deg);
}

.flip-container,
.front,
.back {
  width: 360px;
  height: 320px;
}
/* flip speed goes here */

.flipper {
  transition: 0.6s;
  transform-style: preserve-3d;
  position: relative;
}
/* hide back of pane during swap */

.front,
.back {
  backface-visibility: hidden;
  position: absolute;
  text-align: center;
  /*top: 0;
	left: 0;*/
}
/* front pane, placed above back */

.front {
  z-index: 2;
  /* for firefox 31 */
  transform: rotateY(0deg);
  position: relative;
}

.front h3 {
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  height: 20px;
  width: 60px;
}
/* back, initially hidden pane */

.back {
  transform: rotateY(180deg);
}
<div class="row">
  <div class="flip-container col-md-4 portfolio-item" ontouchstart="this.classList.toggle('hover');">
    <div class="flipper">
      <div class="front">
        <!-- front content -->
        <h3>
                        <a href="#">Project Name</a>
                      </h3>
      </div>
      <div class="back">
        <!-- back content -->
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.</p>
      </div>
    </div>
  </div>
  <div class="flip-container col-md-4 portfolio-item" ontouchstart="this.classList.toggle('hover');">
    <div class="flipper">
      <div class="front">
        <!-- front content -->
        <h3>
                      <a href="#">Project Name</a>
                    </h3>
      </div>
      <div class="back">
        <!-- back content -->
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam viverra euismod odio, gravida pellentesque urna varius vitae.</p>
      </div>
    </div>
  </div>

Upvotes: 0

Hunter Turner
Hunter Turner

Reputation: 6894

You can use flexbox for this. Add the following styling to your .front and .back divs

CSS:

.front,
.back {
  backface-visibility: hidden;
  position: absolute;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
}

Here is a working fiddle.

Upvotes: 2

Related Questions