Center two lines of text in a div in a vertical fashion

After going through several articles on this topic, I've picked up some tricks for vertical alignment like using line-height for a single line of text, display:table for multiple divs, and Flexbox in CSS3. However, I'm still struggling to align two lines inside a div:

.keys {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center;
  min-height: 70vh;
}

.key {
  border: 0.1rem solid white;
  margin: 1rem;
  width: 5rem;
  height: 5rem;
  padding: 0.5rem 0;
  text-align: center;
  color: white;
}

kbd {
  display: block;
}
<div class="keys">
  <div class="key" data-key="65"><kbd>A</kbd><span class="sound">text</span></div>
  <div class="key" data-key="83"><kbd>S</kbd><span class="sound">text</span></div>
  <div class="key" data-key="68"><kbd>D</kbd><span class="sound">text</span></div>
  <div class="key" data-key="70"><kbd>F</kbd><span class="sound">text</span></div>
</div>

Despite having a flexbox container already set up, resorting to display:table would require an extra div which I want to avoid. I’m considering adjusting the padding or experimenting with relative positioning as potential solutions. What would be the most effective way to vertically center this text content?

Answer №1

Revised response following feedback:

In order to transform the .key into a flex container that vertically centers its content, you can add the following CSS properties (note that the existing text-align: center handles horizontal alignment):

display: flex;
flex-direction: column;
justify-content: center;

(Please be aware that the elements are currently white and might appear invisible; I included a black background for the .key elements)

.keys {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  justify-content: center;
  align-items: center;
  min-height: 70vh;
}

.key {
  display: flex;
  flex-direction: column;
  justify-content: center;
  border: 0.1rem solid white;
  margin: 1rem;
  width: 5rem;
  height: 5rem;
  padding: 0.5rem 0;
  text-align: center;
  color: white;
  background: black;
}

kbd {
}
<div class="keys">
  <div class="key" data-key="65"><kbd>A</kbd><span class="sound">text</span></div>
  <div class="key" data-key="83"><kbd>S</kbd><span class="sound">text</span></div>
  <div class="key" data-key="68"><kbd>D</kbd><span class="sound">text</span></div>
  <div class="key" data-key="70"><kbd>F</kbd><span class="sound">text</span></div>
</div>

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

Is there an issue with this html code?

Can you help me troubleshoot this HTML code snippet I have? <html> <head> <script type="text/javascript" language="JavaScript1.3" src="js/euDock.2.0.js"></script> <script type="text/javascript" language="JavaScript1.3" ...

The Struggle with Bootstrap Accordion

I'm currently in the process of learning bootstrap. I copied the code for the 'Flush' Accordion from the bootstrap docs, but my version looks completely off. Despite linking the CSS stylesheet and JS Bundle, it doesn't seem to align pro ...

Retrieve the server code periodically and automatically refresh the form elements

Let's kick things off by explaining how the application functions: (please note there are multiple users on the page such as Patient M, Patient E, and so forth) 1) Check In: Next to Patient X's name, you will find a button labeled Check In. This ...

completion of bootstrap content

When arranging content using Bootstrap, I noticed that when the image is on the left, it appears to adapt properly. However, if I place the image on the right, the layout changes from what was intended. How can I fix this issue? <div class="about&q ...

Safari (mac) experiencing issues with loading material icons properly on initial attempt

Upon my initial visit to the website, I noticed that the font appeared distorted. https://i.sstatic.net/BtUxI.png However, when I right-clicked and chose "reload page", the fonts were displayed correctly. https://i.sstatic.net/3XUcA.png This issue seem ...

Exploring Deeply Nested Routing in Angular

I've been exploring the use of multiple router outlets and encountered an issue. When using the navigateBy function of the router, I am unable to view my child route and encounter an error. However, when I access it via the routerLink in HTML, I get ...

Position the personalized SVG button to align perfectly with the text underneath within a Bootstrap 5 Card structure

Is there a way to align custom buttons in a bootstrap card such that they are 3 wide and wrap to 2 wide on small screens? Additionally, I am struggling to center the text properly below the buttons. Any assistance would be greatly appreciated. Here is m ...

Can you explain the contrast between img.height and img.style.height?

I'm currently in the process of resizing a series of images by iterating through an array and adjusting their sizes. if(items[0].height > 700 || items[0].width > 700){ items[0].style.height = "700px"; items[0].style.width = "700px"; } As ...

Do you think there is a more optimal method for achieving this?

Here is the core concept: I developed it in a fluid manner like this: Do you have any suggestions for improving the way I implemented this? ...

Issue with Next.js: Setting the width to 100vh prevents the height from being responsive on mobile devices

While I understand that using height: 100vh on mobile is generally discouraged for various reasons, I'm curious as to why height: 100vh paired with width: 100vh in Next.js doesn't produce the expected result. Instead of a full-height square, I en ...

Is it possible to switch between different fabricJS canvases seamlessly?

Consider this scenario where I have three canvas elements: <canvas id="c1" width="400" height="300"></canvas> <canvas id="c2" width="400" height="300"></canvas> <canvas ...

Using PHP foreach loop to create bootstrap 4 cards

I am attempting to showcase all of my shop items on my webpage using Bootstrap 4 cards. I want them to be displayed in a grid format, three wide and however many deep (I may consider adding pagination later). Currently, I have a PHP foreach loop that succ ...

Is it possible to retrieve the complete file path in a form?

My goal is to retrieve a file using an input element of type "file". This element is located within a partial view, and I need to send it to the controller or request it there using "Request.Form["inputFile"];". However, this method only provides me with t ...

Tips for creating dynamic rope animations with canvas and CSS3 styling

Interested in creating a canvas animation, or possibly using CSS3 for the same purpose. Looking to add an automatic rope animation, similar to it swaying gently. I've searched for scripts and other solutions, but haven't found anything suitable. ...

Find the ID of the clicked table row using HTML and JavaScript

Currently, I am trying to implement this code snippet: <td align="center"> <div class="dropdown"> <button onclick="DropdownShow(this)" class="btn btn-default glyphicon glyphicon-picture"></button> <div id="@TableR ...

Tips for displaying the CSS3 background image on your website

My website is not displaying the background-image with the URL img/hero.jpg. I attempted using ../img/hero.jpg but still no success. Are there any solutions to this issue? I also experimented with a PDF image, but it did not work either. ...

Create an HTML and CSS code that allows you to split paragraph text into columns within a

I am seeking a way to create dynamic paragraph column text using only the Here is an example of how it could be displayed in HTML: <div> <p> Sed ut perspiciatis, unde omnis iste natus error sit voluptatem accusantium doloremque laudantiu ...

Establish a vertical column that matches the height of its parent element

Is there a way to create a column that matches the height of its parent element? I have a navbar followed by a container with three columns. The challenge is to make the last right column the same height as its parent and also scrollable. In this scenario, ...

Resolved issue with fixed div flickering upon clicking link on Phonegap platform, leveraging Bootstrap

Upon attempting to click a link positioned at the bottom of the screen within a colored div using position:fixed;, I notice that the page momentarily flashes white before displaying the "new" page. Removing the fixed positioning allows the new page to load ...

jQuery - Enhancing User Experience with Dynamic Screen Updates

Is there a way to update the screen height when resizing or zooming the screen? Whenever I zoom the screen, the arrows break. I'm also curious if the method I'm using to display images on the screen is effective. It's supposed to be a paral ...