Aligning variable width numbers within a div container

Currently experimenting with CSS to create a calendar design that mimics the look of a traditional paper calendar. One issue I've encountered is the alignment of numbers within boxes, especially when dealing with double digits.

When displaying a single digit, the number aligns perfectly in the center. However, when it comes to double digits, the alignment appears off-center.

I've thought about implementing a solution where I detect double digits and adjust the margin accordingly, but I'm hoping for a cleaner approach.

.calendar
{
    font-family:arial,sans-serif;   
    margin:0 auto;
    width:400px;
    height:400px;
    background-color:#ffffff;
    border:1px solid #000000;
    text-align:center;
    -webkit-box-shadow:10px 10px 0px 0px rgba(0, 0, 0, 0.75);
    -moz-box-shadow:10px 10px 0px 0px rgba(0, 0, 0, 0.75);
    box-shadow:10px 10px 0px 0px rgba(0, 0, 0, 0.75);
    line-height:400px;
}
.single-digit
{
    font-size:22em;
}
.double-digit
{
    font-size:20em;
}

The following code demonstrates the alignment:

<div class="calendar"><span class="single-digit">1</span></div>

In contrast, this displays the misaligned double digit:

<div class="calendar"><span class="double-digit">10</span></div>

If you have any suggestions or insights on achieving centered alignment for both single and double digits within the calendar, please share!

Thank you!

Answer №1

The issue at hand seems to be related to the way our eyes perceive centering, creating an almost optical illusion.

With a variable width font, most of the roman numerals 0, 2 through 9 have nearly identical widths, leading to equal spacing on either side of double-digit pairs like "27" or "30".

However, the digit "1" is asymmetrical, resulting in slightly more white space on the right side due to its protruding "hook".

In typesetting programs, kerning rules are used to ensure better visual alignment between characters, but unfortunately, web browsers have limited support for this feature.

To address this issue, manual adjustments on a character pair basis would be necessary whenever a "1" appears in the text. For instance, utilizing CSS rules to adjust "1", "10", "11", and so forth.

For character pairs involving "1", experimenting with adjusting letter-spacing by a small negative value could potentially enhance the overall appearance.

Please note that these adjustments heavily rely on the browser's chosen font, which you have little control over unless specifying an embedded font.

Your current HTML/CSS setup is functioning optimally; it’s simply a matter of pushing the boundaries of aesthetic possibilities within existing browsers.

Reference:

http://www.w3.org/TR/css3-fonts/#font-kerning-prop

Answer №2

If you want to improve your layout, consider implementing flexbox in the following way:

.calendar span {
    display: flex;
    align-items: center;
    justify-content: center;
}

Answer №3

The alignment of the text may appear to be off-center at first glance, but if you highlight the text, you will notice that it is actually centered. If you want to ensure that it is visibly centered without highlighting, modify the HTML as shown below:

<div class="schedule"><span class="one-digit">3</span></div>
 <div class="schedule"><span class="two-digits">15</span></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

What is the best way to show checkbox selections based on my database structure?

Two variables, one for checkboxes and the other for checked values are causing an issue in displaying them from controller to view. The code to handle this in the controller is as follows: $scope.infoParticipant = functionThatGetsParticipants(); ...

Tips on determining if a string is a properly formatted HTML:

My question is about checking whether selected strings in a web form are valid HTML syntax. How can I achieve this on a button click? function chkhtml() { var code = $("<table><td>"); code.each(function () { if( ...

Django - issue with table display showing empty row due to query set

In my project, I have two models: one named Season and the other one named Game: # Season class Season(models.Model): teams = models.ManyToManyField('Team', related_name='season_teams', blank=True) current = models.BooleanF ...

Has anyone come across an XSLT that can effectively convert Apple Pages files into high-quality HTML?

When Apple’s word processor/DTP app Pages saves its files, it does so as zipped folders with an XML file containing the content and attachments like images stored as separate files. I am interested in converting this content into HTML format, using < ...

Tips for utilizing [ngClass] with various class situations in Angular4

My current div structure is as follows: <div class="class-a" [ngClass]="{'class-b': !person.something}"> However, I now need to add an additional condition... I want the div to have class-a if one condition is met, class-b if another con ...

The nodes on a 2-dimensional grid overlap each other when the browser window is resized

In my project, I have set up a grid with 24 rows and 64 columns, totaling to 24x64 nodes. However, I am facing an issue where the nodes overlap when I open the console window. I am looking for a solution to automatically resize all nodes based on changes ...

Is it possible to use v-if in conjunction with a style tag to specify a different source file? Alternatively, is there a more efficient method I

I attempted the example provided below, but unfortunately, it did not function as expected. The reason behind my endeavor is that adding numerous modifiers (--tuned) to achieve the desired outcome seemed impractical. Therefore, I decided to try and link ...

The alignment issue persists with the menu header, as the div is not functioning correctly

I've been struggling to properly align my menu on the website. As it stands, when a user arrives and is not logged in, they see the header below: https://i.stack.imgur.com/mTKse.png Once the user logs in, the login/register text is replaced by a dro ...

sending additional parameters within a URL query string

Is it even possible to include a URL with get parameters within another get parameter? For example, I'd like to pass http://example.com/return/?somevars=anothervar as the value for the "return" parameter as shown below: http://example.com/?param1=4&a ...

CSS is not referred to as animation

Every time we hit a key, the dinosaur receives a jump class that should activate an animation. Unfortunately, the animation does not play. animation not functioning <!DOCTYPE html> <html lang="en"> <head> <meta c ...

Overlapping Dropdown Javascript Menus

To achieve the desired effect in the header, I need to create two expandable dropdown menus for the first two titles. My coding experience is limited, especially when it comes to javascript and jquery. Currently, I have managed to create the effect for one ...

Determine whether the textfield is a number or not upon losing focus

I'm seeking advice on implementing a feature using jQuery and JavaScript. I want to create a text field that, when something is inputted, will automatically add .00 at the end if it's only a number. However, if someone inputs something like 2.00, ...

Deactivate while maintaining menu options

Currently, I have a Vue.js form that manages adding and updating data for patients. The issue at hand involves a <select> tag which allows for the addition of a relative. This select field is populated by an array called PPrelations, which is sourced ...

What is the best way to create a unique transition for every component?

I have three elements that I would like to display with a transition effect when they appear or disappear. There is one main element and the other two show up when you click the corresponding button. You can view my current code sample here: https://jsfid ...

Harvest information using BeautifulSoup and Python

Exploring the world of web scraping with BeautifulSoup in Python, I am interested in extracting the package name and price of each app from the Android Play Store. For retrieving the package name, I implemented the following code : url = "https://play.go ...

When clicking on a checkbox's assigned label, Chrome and IE may experience delays in firing the change event

When a user checks a checkbox under a specific condition, I want to display an alert message and then uncheck the checkbox. To achieve this, I am utilizing the click function on the checkbox to internally uncheck it and trigger necessary events. I have a ...

Troubleshooting CSS problems with positioning 3 fluid div elements

I am in need of 3 dynamic div containers. container for main content image display container that changes size container on the side with changing width The issue I am facing is that the text needs to be below the image container which has a wider width ...

Prestashop 1.7 - Enhanced top menu navigation with drop-down subcategories

While using the ps_mainmenu top menu with the default Prestashop theme, I noticed that the drop-down menu only works for parent categories and not subcategories. You can see this in the attached image and HTML code from Chrome. I'm looking for a way t ...

Is it true that textarea is not compatible with AJAX .val() or .text() methods?

I'm attempting to utilize AJAX requests in order to transmit textarea data to a google form, however it appears that .val() isn't functioning correctly with textarea specifically. How can I resolve this issue? My goal is to enable individuals to ...

Is there a way to smoothly incorporate a new animation into a page, while ensuring that existing elements adjust to make room for it?

I am looking to smoothly fade and move a hidden element (display:none) within the page, shifting other elements to animate into their new positions as necessary. When I reveal the hidden element by changing it to "display:initial," it should seamlessly a ...