What is the best way to vertically center text within a div?

After successfully centering my text horizontally in my div using text-align: center, I'm now faced with the challenge of centering it vertically. Despite my attempts with vertical-align: middle;, I haven't been able to achieve the desired result.

For reference, here is a link to the codepen: http://codepen.io/anon/pen/Ioiaq

The code containing the text is located at the bottom of the HTML panel, and the text is "INFO."

Below is the current code snippet:

<div id="inf" style="
                     text-align:center;  
                     background-color: white; 
                     position: absolute; top:50%; left: 45%; width: 10%; height: 6%; 
                     z-index: 10;  
                     display: inline-block;">

  <a href="#"><span>INFO</span></a>
  </div>

Any suggestions on how to resolve this alignment issue are welcome, and the solution doesn't have to involve CSS specifically.

Answer №1

In order to vertically center a single line of text, you have the option to utilize CSS line-height with a matching value to the height you have set.

Answer №2

Using line-height coupled with vertical-align: middle; is a great solution if JavaScript is not being utilized.

Check out this JSFIDDLE for a demo.

Here is the HTML code:

<div id="inf">
    <a href="#"><span>INFO</span></a>
</div>

And the corresponding CSS:

#inf { 
    text-align:center;  
    background-color: white; 
    z-index: 10;  
    display: inline-block;
    width: 100%;
    line-height: 400px;
    vertical-align: middle;
}

Answer №3

To center content vertically, consider utilizing the CSS property vertical-align.

vertical-align: middle;

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

Fade effect on content in Bootstrap carousel

I am currently making some updates to the website mcelroymotors.com. One issue I have encountered while working on the homepage carousel is that the caption only pops up on the first slide, and does not appear on any of the subsequent slides. I would lik ...

Creating horizontal cards with Angular MaterialWould you like to learn how to design

I need help converting these vertical cards into horizontal cards. Currently, I am utilizing the Angular Material Cards component. While I can successfully create cards in a vertical layout, my goal is to arrange them horizontally. Below is the code sni ...

Having trouble with the alignment of CSS menu in Safari?

Can someone please assist me with a strange issue I am experiencing on my website? The menu appears misaligned when using Safari, and it's been driving me crazy. I've tried troubleshooting for hours without success. Here is a link to the site: I ...

Webpage with visual representation

Currently in the process of redesigning my university's drama department website, I'm looking to incorporate three images side by side with spacing and captions. The captions need to have a header that's styled differently from the caption i ...

Modify individual list item attributes

I have been attempting to modify the background of the list item labeled as home in order to ensure that it displays hover properties even when not being interacted with. Despite specifying this in the class, the appearance does not change. Below is the H ...

Completely shrink the middle row first when resizing the browser before shrinking the other rows

Utilizing a simple three row structure: <div class="EditorHeaderWrapper"> <h1>Title</h1> </div> <div class="EditorMainRowWrapper"> // Content of the main row goes here </div> <div class="EditorFooterWrapper"> ...

The issue with the page not appearing correctly on IE9 is currently being resolved

I am utilizing bootstrap alongside a bootswatch theme and HTML5 boilerplate to design a webpage. While it appears correctly on Chrome and Firefox, the display is off on IE9 and IE8 mode. However, everything works fine when compatibility mode is enabled in ...

The variability of jQuery values

I have created a website control with jQuery that features a long bar consisting of four colored divs. Users have the ability to drag the join between two divs in order to resize it, all within the confines of the bar itself (keeping the overall width of t ...

Transform a dynamic Maya model into JSON format for seamless integration with Three.js

After creating a Maya model in Maya 2008, I need to convert it to a JSON format for use with three.js. I have experimented with different methods including: 1) Utilizing the threeJsFileTranslator.py Maya plugin provided in the threejs package. 2) Trying ...

Creating grid layouts in Bootstrap 4 with buttons

Recently, I've delved into Bootstrap 4 and HTML5/CSS. My goal is to design a text area with two buttons located on the side (horizontally centered) like this: https://i.sstatic.net/TnHSy.png Below is the code snippet that I have so far: <link ...

What is the best way to implement an AppBar that fades in and out when scrolling within a div?

I'm trying to implement a Scrollable AppBar that hides on scroll down and reappears when scrolling up. Check out this image for reference To achieve this functionality, I am following the guidelines provided by Material-UI documentation export defa ...

Adjust the CSS to give <a> elements the appearance of plain text

Is it possible to style an anchor tag to appear identical to plain text? Consider the following scenario: <p> Here is some text and here is a <a class='no-link' href="http://www.example.com" >link</a></p> I have atte ...

Can PurgeCSS be implemented with Next.js and module.scss files?

Is there a way to use purgeCSS with component level .scss files (filename.module.scss) in order to remove unused CSS? The hashed classnames make it tricky, especially in a next.js app that heavily relies on module.scss files for styling. This thread here ...

How can I switch the visibility of two A HREF elements by clicking on one of them?

Let me break it down for you in the simplest way possible. First off, there's this <a href="#" id="PAUSE" class="tubular-pause">Pause</a> and then we have a second one <a href="#" id="PLAY" class="tubular-play">Play</a> Al ...

Is there a way to incorporate an animated element within the track of my circular progress bar?

My circular progress bar features two paths, with one path increasing in length as data is received, eventually turning the entire circle red. Here is the SVG HTML code: <path d="M 50,50 m 0,-47 a 47,47 0 1 1 0,94 a 47,47 0 1 1 0,-94" stroke="#A9B0B7" ...

Leveraging JavaScript Functions in HTML

I am having an issue with a JavaScript file containing two similar functions that are executed through an HTML form. While the first function runs smoothly, the second function does not display correctly. It seems like I might be calling or executing the ...

Generate JSON on-the-fly with ever-changing keys and values

I am new to coding and I'm attempting to generate JSON data from extracting information from HTML input fields. Can anyone guide me on how to create and access JSON data in the format shown below? { Brazilians**{ John**{ old: 18 } ...

Having trouble with a basic API Get request?

I'm trying my hand at making a simple get request to fetch a random quote from an API. When I hardcode the change of the quote on button click, everything works smoothly. $(document).ready(function() { $("#quotebtn").on('click', functio ...

Sleek and versatile sidebar reaching full height

Is there a way to make my sidebar cover the full height of the screen without using position: fixed? The code I've tried doesn't seem quite right. Any tips? JSFindle: http://jsfiddle.net/Pw4FN/57/ if($(window).height() > $('#page-contai ...

I am looking to initiate the animation by triggering the keyframe upon clicking a button

Is there a way to create an animation triggered by a button click instead of loading the page? Each table data cell has its own class with a unique keyframe defining the style changes over time. @-webkit-keyframes fadeIn { from { opacity:0; ...