aligning vertically within inline elements

HTML

<html>
    <head>
        <style type="text/css">
            div {
                height: 300px;
                width: 300px;
                background-color: aqua;     
                margin-bottom: 10px;
            }           
            div > span {                
                vertical-align: middle;
                background-color: orange;               
                font-size: 3em;
            }    
        </style>
    </head>
    <body>          
        <div>           
            <span>Hello!</span>
        </div>          
    </body>
</html>

In the image below, Actual showcases the outcome of the demonstrated HTML, while Expected depicts the desired design.

https://i.sstatic.net/hSGjp.png

Could it be that the CSS property vertical-align functions in this manner?

Edit :

This inquiry isn't a replication, as my aim is to grasp the performance of vertical-align: middle with inline elements. As illustrated above, maintaining or eliminating the specified property value doesn't impact the HTML configuration.

Edit2 : The demonstration under the section "A more versatile approach" highlighted in the main answer of the linked query mentioned in the reviews, unfolds an alternative format in my browser. My current web browser is Google Chrome Version 47.0.2526.106 (64-bit).

Here's an illustration of how it displays on my browser (varied from the demonstration link):

In the provided image, the span element remains affixed at the top.

https://i.sstatic.net/8hENP.png

Answer №1

vertical-align is used to align inline elements with each other, rather than positioning them within their container.

For example, if you have a taller inline element with vertical-align: middle in the same div, the "Hello" text will be centered relative to it:

div {
  height: 300px;
  width: 300px;
  background-color: aqua;
  margin-bottom: 10px;
}
div > span {
  vertical-align: middle;
  background-color: orange;
  font-size: 3em;
}
.big {
  font-size:200px;
}
<div>
  <span>Hello!</span>
  <span class="big">B</span>
</div>

There are various methods for vertically centering text in a container, but this particular technique is not one of them. You can refer to How do I vertically center text with CSS? for more information.

Answer №2

https://jsfiddle.net/a2b3cd4e/1/

Remember to include line-height: 300px in your div's CSS style rule. This will help align the content to the middle of the taller parent div, compensating for the default line height.

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

The hover state of a div will not be lost if its parent element is not being hovered over

When hovering over the second, third, or fourth item, hidden text will appear on the left side. If you hover your cursor over the hidden text, it will disappear again. I want to be able to hover over the second item, move my cursor to "hide", and click o ...

List of recommended countries with their respective flags and descriptive text

I am currently working on creating a country suggestion list that includes flags next to the selected result. While I have been successful in generating the suggestion box based on the country names, I am looking to enhance it by displaying the respective ...

Is it possible to determine the optimal dimensions for a viewbox that would accommodate all the elements of a drawing?

In the event that a drawing contains multiple shapes and paths, where all of these elements need to be fully visible without any cropping, the viewbox must encompass everything. Yet, is it possible to identify the specific values for min-x, min-y, width, ...

"Troubleshooting: Why is Angular's Equalizer from Foundation not

I'm having trouble getting Foundation Equalizer (the JS tool for equalizing div heights) to function properly. The demo is not displaying correctly. I am currently using Foundation v6.1.2 My setup includes using it in an ng-view, and in the index fi ...

The scroll function triggers even upon the initial loading of the page

Trying to solve the challenge of creating a fullscreen slider similar to the one described in this question, I created a jsfiddle (currently on hold) Despite knowing that scrolling too fast causes bugs and that scrolling both ways has the same effect, m ...

The padding of dynamically generated bootstrap buttons diminishes

I am facing an issue with a bootstrap button bar that is using the well class. The problem occurs when I create it twice in my code: once using HTML and another using dynamic javascript. The bootstrap functions properly when created with HTML. However, t ...

PHP - Easily send emails with attachments

Below are the codes I am using to send an email with an attachment: if (isset($_POST['submit'])) { @$name=stripslashes($_POST['name']); @$last_name=stripslashes($_POST['last_name']); @$phone=stripslashes($_POST['phone&a ...

Using auto margins does not properly align the image in the center of the page

I noticed that in this specific example, the image is not centered properly. Can someone explain why this might be happening? For context, I am using Google Chrome v10 on a Windows 7 machine, not Internet Explorer. <img src="/img/logo.png" style="mar ...

Showing fetched data from Firebase in an Ionic 3 HTML file

Looking for assistance on how to display data retrieved from my firebase database in my HTML file. I need help with organizing the data, starting with displaying customer user's data. Eventually, I want to make it clickable and put the user's dat ...

Having difficulty creating a basic container

As a beginner in web development using html/css, I am eager to learn and create something simple. I want to design a responsive fixed box that always maintains a 20px distance from the viewport edges. The idea is that as the screen size changes, the box ...

Using Two JavaScript Functions with Identical Names in One HTML File

I am currently facing an issue with a function where the data is retrieved from the getValue() function. The following code snippet is a fragment. grid.js function gridLoadComplete(){ var data = getValue(); } Take into account the following H ...

Is there a way to customize the look of the time selected in the <input matInput type="time" step="1" /> time picker?

Currently, I am utilizing the following code as a time picker in my Angular 9 application. My goal is to modify the selected time's color to a bright blue shade. How can I accomplish this task? <input matInput type="time" step="1&quo ...

What is the method for determining the numerical worth of the px containers?

https://i.stack.imgur.com/0K2TD.png Total width of the bar is set to 500px, with the red box at a width of 150px, the yellow box at 200px, and the green box at 50px. CSS Styles: .box { float:left; width:150px; box-shadow:3px 3p ...

Steps for leveraging pdfMake with live data

Recently delving into Angular, I've been exploring the capabilities of pdfMake. While I successfully incorporated static data, I'm facing challenges when attempting to utilize dynamic data. Any guidance on how to achieve this would be greatly app ...

Resize the p-accordion within a p-splitter using PrimeNG programmatically

Please find the code for my component below: In this component, I have a p-splitter that is divided into 3 sections. Each section contains a p-accordion, which in turn includes a p-table. <div class="card w-full"> <p-splitter [p ...

A guide to modifying the color of the Menu component in material-ui

Currently, I am utilizing the Menu component from material-ui and facing an issue while trying to modify the background color. The problem arises when I attempt to apply a color to the Menu, as it ends up altering the entire page's background once the ...

Does CSS have a selector that does not include any elements?

Considering the examples below <span id="foo_repeater_bar_0"></span> <span id="foo_repeater_batz_1"></span> I am looking for a CSS selector that will allow me to target only the elements whose id does not include foo_repeater. ...

What is the most effective method for creating a dark background with light text in Bootstrap without using SASS?

I recently created a new website using Bootstrap 5 and set the background color to black (#000000) by adding some CSS styling. However, I noticed that this change had unintended consequences on the appearance of various elements such as drop shadows, modal ...

Live visualization from a Remote System (External Network)

I am looking to visually represent real-time sensor data (streaming data) using Node.js, HTML, and MySQL. I have set up MySQL to store the real-time sensor data, an index.html file that implements Google Chart through the doPoll app.js file, and the app.js ...

What is the best way to make the child Div float to the bottom of the parent Div?

Screenshot I attempted various CSS techniques in an effort to make the .booknetic_appointment_container_footer float to the bottom of #booknetic_theme_5, but unfortunately, I was unsuccessful. The closest solution I found involved hardcoding padding, but ...