What is the best way to vertically center align a div containing dynamic multiple lines of text?

Here is the CSS I'm utilizing:

.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 code I'm using is as follows:

<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 for the article class is:

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

I'm having trouble vertically centering and making the text in the FRONT div dynamic and multiline. Any suggestions?

I attempted to use 'vertical-align:middle' along with 'display:table-cell', but it didn't work. Perhaps I applied them to the wrong CSS blocks.

You can find the code on jsFiddle: here

Answer №1

To create a layout where the child container is centered within the parent container, you can use the following CSS properties:

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

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

Check out this demo on JSFiddle: http://jsfiddle.net/uVSN6/1/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing a responsive design with a bootstrap grid system, featuring expandable columns for

After creating a bootstrap grid page, I am facing an issue with the layout on mobile screens. My problem arises when trying to reorder the cards properly for mobile view. Here is my current logic: <div class="row"> <div *ngFor="let col of [1, ...

Creating a sleek and dynamic bootstrap navigation bar with alignment challenges

I am facing an issue with the navigation bar I created using Bootstrap. It is not aligning as I want it to. I aim for it to be in line with the first image in the photo menu, which opens when you click on the camera icon. This alignment is important to me ...

Guide to generating an iframe that is triggered by a button click with the help of jquery

Is it possible to dynamically insert an iframe into a column div on a web page by clicking on it? I want the iframe to remain hidden when just viewing the page, but to become visible when a button is clicked. How can this be achieved? ...

Looking to design an interactive grid for generating dynamic thumbnails

I am a beginner in the field of web development and I have a desire to create a website for showcasing my portfolio. This website should feature project thumbnails along with brief descriptions, all of which should be displayed dynamically. Although I poss ...

What steps should I take to repair the footer on this webpage?

Working on a footer using bootstrap and it's looking great, but I encountered an issue with blank space appearing at a specific resolution (see image below). Bootstrap Footer: https://i.sstatic.net/XifQe.png My Footer Design: <footer> < ...

The Yahoo stock display is experiencing issues within two separate div elements on the webpage

Hey experts on stack overflow! I came across this script that allows me to fetch stock information from Yahoo. However, I'm facing an issue where the script only works for one div when I try to use it in multiple divs. I'm not a coder, so I&apos ...

Padding in submit button value is essential for ensuring proper spacing and

Is there a way to adjust the padding of a submit button value? I have tried using text-align: right;, which works fine for aligning the text to the right. However, when I attempt to use padding-left: 25px;, the text remains centered instead of moving to t ...

Concealing aspects of my game until a specific criteria is fulfilled

After successfully disabling and enabling tabs, my goal now is to hide elements until a player unlocks something, making it more rewarding. I attempted to achieve this by using the following code: <div id="turtle" style="visibility: hidden;"> ...

Customizing the default Google Chrome video player controls

Feeling tired of the mundane appearance of the Google Chrome native html5 video player? Each time they make a change, it seems to get even more unappealing. The bright white color scheme is completely unfulfilling in my opinion. I took matters into my o ...

Use CSS and JavaScript to create a visually appealing and interactive tree structure. Give the tree a dynamic design

I have been experimenting with CSS and JavaScript to create a dynamic graph based on a data table. You can view the graph that I need to replicate using CSS and JavaScript in this PDF. The example provided below showcases what I am attempting to achieve, a ...

Discover ad blocking using react

I am currently trying to detect the presence of AdBlock Plus in Firefox. While it doesn't have to be foolproof, I would like to at least disable the pre-installed ad blockers in browsers like Firefox and Chrome. After some research, it seems that a p ...

Delivering HTML with Python Socket Server

I am currently learning about HTTP/CGI and exploring how to display HTML content on a webpage through the use of the socket library in Python. However, I am encountering some confusion regarding the correct syntax for this task: #!/usr/bin/env python impo ...

How can I determine the version of gulp that is currently installed on my system?

How can I verify the gulp version currently installed on my system and determine the latest version available on the website? Despite my extensive online search and browsing through the official gulp site, I am unable to locate the specific version infor ...

What is the best method to convert specific rendered HTML content into a downloadable text file within Django?

<div class="container" > {% if sr %} {% for k in sr %} <p>Name: {{k.name}}</p> <p>Email: {{k.email}}</p> <p>Contact Number: {{k.contact_number}}</p> {% en ...

Obtaining the API for a website's functionalities

I’m in need of downloading the API file that facilitates the connection between the website and database. How can I achieve this? I've attempted to use Httrack, wget, and open explorer, but they only seem to download the website itself. While I get ...

Incorporating Calendar Features into a Traditional .html Website

Currently working on integrating a calendar function into an HTML site. Want to enable my client to easily manage their own calendar by inserting a code snippet from platforms like Google Calendar into a div on the website. As a professional SEO with web ...

Angular application featuring scrolling buttons

[Apologies for any language errors] I need to create a scrollable view with scroll buttons, similar to the image below: Specifications: If the list overflows, display right/left buttons. Hide the scroll buttons if there is no overflow. Disable the le ...

Positioning a Duplicated Element with Precision in jQuery

I am having trouble positioning a cloned div using jQuery. Even when I try to add CSS to the cloned element using the .css() method, it only affects the text and not the entire element. Here is a link to the fiddle for reference: https://jsfiddle.net/emil ...

The persistent activation of a jQuery scrollTop function

My goal is to use jQuery to create a sticky header div (with position: fixed) once it's scrolled past a certain point. This code has been working well for me: $(window).scroll(function() { var y = $(this).scrollTop(); if (y >= 200) { ...

CSS transitions/transforms are failing to impact the necessary elements

I'm currently grappling with advanced transitions and transforms, and encountering some challenges with a section I'm developing. I've managed to get everything working as needed, except for the fact that there is an h5 element positioned ov ...