Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i vertically center align div with dynamic multiple line text

Below is the CSS i am using:

        .flip-container
           {
        -webkit-perspective: 1000;
        -moz-perspective: 1000;
        perspective: 1000;


    }



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

    .flip-container, .front, .back {
                  width: 150px;
                  height: 150px;

    }

    .flipper {
        -webkit-transition: 0.6s;
        -webkit-transform-style: preserve-3d;
        -moz-transition: 0.6s;
        -moz-transform-style: preserve-3d;
         transition: 0.6s;
         transform-style: preserve-3d;
         position: relative;
    }

    .front, .back {
        -webkit-backface-visibility: hidden;
        -moz-backface-visibility: hidden;
        backface-visibility: hidden;
        display: table-cell;
        vertical-align:middle;

        position: absolute;
        top: 0;
        left: 0;

    }

    .front {
         z-index: 2;
         text-align: center;
    }

    .back {

        -webkit-transform: rotateY(180deg);
        -moz-transform: rotateY(180deg);
        transform: rotateY(180deg);

    }

    .front .name {
        font-size: 2em;
        display: inline-block;
        background: rgba(33, 33, 33, 0.9);
        color: #f8f8f8;
        font-family: Courier;
        padding: 5px 10px;
        border-radius: 5px;
        bottom: 60px;
        left: 25%;
        position: absolute;
        text-shadow: 0.1em 0.1em 0.05em #333;

        -webkit-transform: rotate(-20deg);
        -moz-transform: rotate(-20deg);
        transform: rotate(-20deg);
    }

    .back-logo {
        position: absolute;
        top: 40px;
        left: 90px;
        width: 160px;
        height: 117px;
        background: url(logo.png) 0 0 no-repeat;

    }

    .back-title {
        font-weight: bold;
        color: #00304a;
        position: absolute;
        top: 180px;
        left: 0;
        right: 0;
        text-align: center;
        text-shadow: 0.1em 0.1em 0.05em #acd7e5;
        font-family: Courier;
        font-size: 2em;

    }

    .back p {
        position: absolute;
        bottom: 40px;
        left: 0;
        right: 0;
        text-align: center;
        padding: 0 20px;

    }

The HTML that i am using looks like this :

<div id="article"><div class="flip-container" ontouchstart="this.classList.toggle('.'hover'.');">
<div class="flipper">
 <div class="front">
   <!-- front content -->
  </div>
  <div class="back">
  <!-- back content -->
  <img src="" width="160" height="160" /> </div>
</div>
</div> 
</div>

The CSS of the article class is:

#article {
margin: 5px;
float: left; 
height: 150px;
width: 150px;}

I am unable to make the fext in the FRONT div center vertically centered. Also it would be dynamic and multiline. Any Ideas?

I tried using 'vertical-align:middle' along with 'display:table-cell' but that didn't help or maybe i just applied them to the wrong CSS Blocks.

The Code is on jsFiddle: here

like image 778
Arihant Avatar asked Feb 27 '13 04:02

Arihant


People also ask

How do I keep my div vertically centered?

You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How do I vertically align a div in HTML?

Use the position property with the “relative” value for the parent element to place it relative to its normal position. Use the position property with the “absolute” value for the child element to place it relative to its positioned parent element. Add the height, margin-top, top, border, and width properties.

How do I vertically center a div inside another div?

Vertically centering div items inside another div Just set the container to display:table and then the inner items to display:table-cell . Set a height on the container, and then set vertical-align:middle on the inner items. This has broad compatibility back as far as the days of IE9.

How do I center align text vertically in CSS?

To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.


1 Answers

You can use display: table for your parent container and display: table-cell for your child container:

#article {
    display:table;
    height: 150px;
    width: 150px;
    float: left; 
    text-align: center;
}

.front {
    display: table-cell;
    vertical-align:middle; 
}

Fiddle Demo: http://jsfiddle.net/uVSN6/1/

like image 66
Eli Avatar answered Oct 18 '22 07:10

Eli