Tips for aligning two different sizes of text vertically, with one positioned to the right

There is an interesting answer on Stack Overflow that addresses how to vertically align text of different sizes. However, a user in the comments pointed out an issue with floating the second span element to the right and requested help on making it work. You can view the example from the comments here:

Here is the HTML code snippet provided:

<title>Untitled Document</title>

<body>
<div> 
  <span>100</span>
  <span>This is the desc</span> 
</div>

And here is the corresponding CSS code snippet:

div {
display:table-cell;
vertical-align:middle;
text-align:center;
height:200px;
width:300px;
margin:auto;
background:red;
border:1px solid #f00;
font-size: 40px;
}
span {
display:inline-block;
vertical-align:middle;
padding:0 10px;

}
span + span { 
  font-size:50%; float: right; 
}

I encountered the same issue myself and would like to pose it as a new question, given that the original one has already been answered without addressing this specific scenario.

Answer №1

flexbox is a fantastic solution for layouts where floating elements are not needed.

div {
  display: flex;
  height: 200px;
  width: 300px;
  margin: auto;
  background: orange;
  font-size: 20px;
  align-items: center;
}
p {
  flex: 1;
  border: 1px solid green;
  padding: 0 10px;
}
p:first-of-type {
  font-size: 50px
}
<div>
  <p>100</p>
  <p>This is the desc</p>
</div>

Answer №2

As far as my understanding goes, achieving vertical centering using only float is not possible (although I am open to learning otherwise). There might be a workaround that could provide a solution. Check out the example here.

To vertically center elements with absolute positioning without knowing their height, you can try the following approach:

top: 50%;
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
transform: translateY(-50%);

For a detailed explanation, visit this link.

I hope this provides some help in resolving the issue.

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

Choose a shade from Three.js' color selector

Currently working on a project using A-FRAME, I am in the process of creating a menu for hololens. However, I am facing some challenges with a color picker. I have developed a color picker (link: Color picker) and my goal is to select a color and have it d ...

Recreated using CSS and JavaScript: iOS "Background Open" Animation

I am currently seeking a method to replicate the iOS Safari animation that occurs when opening a link in the background (the item jumps up and quickly animates to where it can be accessed) - This same animation is also seen on OSX Safari's "save to re ...

Switching the arrow direction in Bootstrap 4 menu toggles

I'm a beginner with Bootstrap 4. My navigation menu includes a dropdown item with the standard caret pointing down, added automatically by Bootstrap. Now, I want to make the caret point up after opening the dropdown menu for the nav item. Despite ext ...

The journey of a JavaScript file within an iframe devoid of any src attribute

Consider this scenario: In a folder labeled /jscript, there are two files named my_js_file1.js and my_js_file2.js. I also have an /index.html page structured like this: <html> <head> <script type='text/javascript' sr ...

Retrieving the chosen date from a calendar using a jQuery function

Hey there, I need help with showing tasks created on a specific date by selecting that date from a full month calendar. https://i.sstatic.net/hbLtp.jpg There are multiple tasks created on the selected date, and I want to trigger a jQuery event to fetch d ...

Is there a way to ensure my React navigation screen fills the entire screen?

I am currently facing an issue where my map screen is not covering the whole screen, leaving a blank space at the bottom where the custom bottom navigator should be displayed. How can I adjust my code so that the map covers this blank space at the bottom? ...

Error Encountered: Angular - Despite successful $http.delete request, the operation does not actually take

I am currently facing an issue with deleting a customer in my Angular + PHP application. Although the application returns success without any errors, it fails to delete the customer from the database. Here is the code snippet of my Angular controller: ...

placing circular button beside pick component

I have created a code that retrieves json from a backend api and fills a select component in vue. I'm looking to include a reload button beside the select component, <!DOCTYPE html> <html lang="en"> <title>Test page app</title ...

Creating files for two HTML pages with Vue: A step-by-step guide

Currently, I am working on creating two HTML files as part of my project structure. This is how it looks: |- node_modules |- public | |- blog | | |- some-page.html | |- index.html |- src (contains all the Vue components) |- Blog.Vue |- Index.Vue ...

Bootstrap Containers Misaligned

I have been developing a website with two containers, one on the left and one on the right. By executing the following HTML code, you can see the structure: <!DOCTYPE html> <html lang="en"> <head> <link rel=& ...

We're facing some technical difficulties with the form for the school project. Furthermore, I need to ensure that the answers are

Upon inspection, it seems that the HTML code is fine, but there appears to be an issue with the JavaScript. I also need to store the answers in an array, which is a task for later. <form> <fieldset> <legend>Content:</lege ...

Opacity in IE 8 will impact every child element within

I need to apply opacity to a div element. Here is my current CSS: background: rgb(255, 255, 255); /* Fallback for non-rgba supporting browsers */ background: rgba(255, 255, 255, .7); filter: alpha(opacity=70); /* For IE 7 and older versions */ -ms-filter: ...

Unable to remove the white space appearing below and after a div in HTML

I have been working on creating a website. You can find my JSFiddle here. On my JSFiddle, you might notice a white gap above the black image labeled INSURANCE, and another white gap below it. I've tried various solutions but haven't been able to ...

How can I position a div column-10 to the right and make it stay fixed in place

I'm having an issue trying to position a div on the right side without causing content to overlap with the navbar. I've attempted using `position:absolute top:0 right:0`, but it's not working as intended. <link href="https:// ...

In JavaScript, the condition can function properly even without the "&&" logical operator, but if the "&&" is included, the condition does not operate

I need the error message to be shown if the character length in the #nameT id is less than 3 or greater than 20. When I use the && logical operator, it does not function correctly, as when the && is not present, it works perfectly when ex ...

Insert the ng-if directive into an element using a directive

I am working on an AngularJS directive that involves looking up col-width, hide-state, and order properties for a flexbox element based on its ID. I want to dynamically add an ng-if=false attribute to the element if its hide-state is true. Is there a way ...

The behavior of Elementor lightbox buttons upon being clicked

When using Android, I've noticed that the lightbox briefly displays a semitransparent cyan bar on the left and right buttons when they are pressed. Is there a way to control or prevent this behavior? Any suggestions would be appreciated! Thanks in adv ...

Adjustable columns that fill the rest of the screen's height below a navigation bar

I am facing an issue with my non-fixed navbar and two-column layout when testing the responsiveness on a mobile screen size. I need Column A to fill the remaining viewport's height, while Column B should move under Column A without hardcoding any valu ...

Unifying flex and position:fixed

I'm having difficulties when it comes to displaying a modal dialog with a fixed position within a flex layout. The header and footer of the dialog need to be set in height, while the content section should have overflow with scrollbars. I chose the fl ...

Dealing with preventing form submission in JQuery due to the use of Char(13) causing newline issues

Currently, I am facing an issue with capturing the class for my address field. I have successfully prevented the enter key (char[13]) from submitting the form. However, I now need to make an exception for text blocks, such as the address field, where I wan ...