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

Align checkboxes horizontally to resemble a progress bar styling

Currently, I have a series of checkboxes that are displayed sequentially on the page. <div class="myCheck"> <h5>Here's what you've selected so far</h5> <label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect ...

Is there a way to programmatically simulate clicking on the "Cancel search" button?

I have a text input field with type "search". In order to perform UI testing, I need to simulate clicking on the "cancel search" button: The code for this specific input field is as follows: <input type="search" value="user"> Although the cancel b ...

Changing the positions of objects on a resized HTML Canvas

I am currently developing a tool that allows users to draw bounding boxes on images using the Canvas and Angular. Everything was working smoothly until I encountered an issue with zooming in on the canvas causing complications when trying to draw bounding ...

In jQuery, a dropdown selection can be filled with multiple textboxes sharing the same class

I'm experimenting with the idea of using multiple textboxes with the same class filled with different dropdown options that also have the same class. However, I am encountering some issues. When I click on a dropdown option, it changes the value in a ...

Creating CSS styles to ensure text takes up the largest size possible without wrapping or extending beyond the screen borders

Looking for a way to display text at its maximum size without any wrapping or overflowing the screen? I attempted using @media and adjusting font-size, but things got too complex. My goal is to have the text on example.com displayed at the largest possible ...

The problem with clearing floats in IE7 (as well as 6)

Currently, I am facing an issue where I have a list that I want to split into three columns by using the float left property and then clearing the first column. Everything seems to be working fine in IE8 and FF, however, IE7 is causing the second column t ...

Creating an HTML table by populating it with data from a JavaScript array

Imagine you have a JavaScript array structured like this: [ "Heading 1", "Heading 2", "Data 1", "Data 2", "Data 3", "Data 4", ] Your task is to generate an HTML table from this array that looks like the one in this link: https://i.stack.imgur.com/7laUR. ...

overlay the div or image with a translucent color

I have a div containing an image with the results from my database, carefully designed and positioned within each div. However, I would like to highlight certain results by overlaying them with a color. I am seeking a method to achieve this effect withou ...

AJAX - Implementing a delay in displaying AJAX results

My search function uses AJAX to retrieve data from the web-server, and I am trying to implement a fade-in animation for each search result. I want the results to load and fade in one by one with a slight delay between them. Currently, it seems like all th ...

Should the event happen inside a div section

How can I determine if an element is currently positioned within a specific div? I am working on a vertical scrolling menu where the height of the navigation div is set and overflow is hidden to show only a portion of the links at a time. To navigate thr ...

Using VBA to Populate Dropdown List on Webpage from an iFrame

I am facing an issue with my code that aims to interact with a webpage by clicking on a link and then modifying dropdown lists within an Iframe. Specifically, I am attempting to populate the Manufr dropdown list based on data from the Year iFrame using VBA ...

Incorporating Bootstrap Content: A Guide to Including a PHP File within Another PHP File

I am encountering an issue when trying to import a PHP file containing my navigation bar into another PHP file. Here's the code for the navigation file: <?php echo " <html> <head> <nav class = "navbar navbar-default navbar-fixed-top ...

Set the class of the list item to "active" class

My approach to styling involves using a UL and li class to contain form selection options. I plan on hiding the radio button and using its value upon form submission. I have managed to enable checking the radio box when the <li> is clicked, but for s ...

Choose children input textboxes based on the parent class during the onFocus and onBlur events

How can I dynamically add and remove the "invalid-class" in my iti class div based on focus events in an input textbox, utilizing jQuery? <div class="form-group col-md-6"> <div class="d-flex position-relative"> & ...

The issue of JQuery mobile customizing horizontal radio buttons failing to function on physical devices has been identified

Not long ago, I posed a query on Stackoverflow regarding the customization of horizontal jQuery-mobile radio buttons. You can find the original post by following this link How to customize horizontal jQuery-mobile radio buttons. A developer promptly respon ...

Rotate the image as you hover your cursor over it

I am trying to implement a script where the image is rotated when the user hovers over it. Unfortunately, the hovering effect is not functioning as expected. $("#alcazar-image").rotate({ bind: { mouseover : function() { $(this).rotate({anima ...

Vuetify's v-badge showcasing an exceptionally large number in style

Encountering an issue with using v-badge and v-tab when dealing with large numbers in a v-badge. Managed to find a CSS workaround by setting width: auto; for adjusting the size of v-badge to accommodate huge numbers, but now facing an overlap with my v-ta ...

Initializing the $(this) variable prior to the function declaration

I am faced with the task of performing multiple checks based on the IDs of certain elements, all of which vary slightly. So, I created several functions to handle this, and rather than repeatedly using $(this).children ..., I wanted to create a short vari ...

Adjust the vertical size and smoothly lower a text input field

When a user clicks on a textarea, I want it to smoothly change height to 60px and slide down in one fluid animation. Check out the JSFiddle example here: http://jsfiddle.net/MgcDU/6399/ HTML: <div class="container"> <div class="well"> ...

The offcanvas menu in the NextJS Tailwind website does not handle hover or tap events properly when outside of the parent dimensions

My header includes an off-canvas menu that slides in from the right side. Everything seems to be working fine, except for one issue: when the menu is open and visible, the mouse and touch events pass through it to the content underneath. Check out the cod ...