Ways to align text vertically across different browsers without dependence on one

Scenario:

I am currently working on a personal project where I need to design playing cards for a game using CSS

Main Objective:

My goal is to align the text on the cards so that it appears centered and visually appealing

Actions Taken:

The relevant HTML code snippet is as follows:

<div class = "card_header">
    <div class = "card_cost">1</div>
</div>

And the corresponding CSS styling includes:

.card_header {
    position: relative;
    top: 110px;

    box-sizing: border-box;
    width: 100%;
    height: 75px;
}

.card_cost {
    position: absolute;
    top: 50%;
    right: 165px;
    transform: translateY(-50%);

    font-size: 60px;
    font-weight: bold;
}

Issue at Hand:

Despite my efforts, the alignment of the card_cost text within the card_header appears inconsistent across different browsers. It seems too high in Firefox and too low in Safari.

To better understand the problem, I added border: 1px dotted gray; to visualize the layout differences between Safari and Firefox:

https://i.sstatic.net/GbLi9.png https://i.sstatic.net/bRQYQ.png

It became evident that the vertical centering of text presents differently in each browser. This raises questions about browser compatibility and finding a universal solution to achieve consistent alignment. Is there an alternative approach to ensure proper vertical centering of the text?

Edit:

Upon considering suggestions about using line-height, here's the updated appearance with line-height: 1; applied to card_cost (Safari vs. Firefox):

https://i.sstatic.net/XbqYj.png https://i.sstatic.net/3XHTT.png

While this adjustment attempted to address the issue, it still falls short in achieving optimal vertical alignment, particularly noticeable in Firefox where the text remains positioned higher than desired.

Answer №1

My preferred method is to utilize an invisible :before pseudo-element on the parent container with a height of 100% and vertical-align: middle;. This allows the :before element to align other inline-block content properly:

// I like to use this style across all elements
.card_header,
.card_header * {
  font-family: arial;
  box-sizing: border-box;
}

.card_header {
  position: relative;
  top: 110px;
  width: 100%;
  height: 75px;
  text-align: center;
  background-color: rgba(100, 0, 0, .1);
}

.card_header:before {
  content: '';
  height: 100%;
  display: inline-block;
  vertical-align: middle;
  border-left: 1px solid blue;
}

.card_cost {
  display: inline-block;
  font-size: 60px;
  height: 60px;
  font-weight: bold;
  vertical-align: middle;
  background-color: rgba(0, 0, 100, .1);
}
<div class="card_header">
  <div class="card_cost">1</div>
</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

On a smaller screen size, the event listeners I add dynamically are successfully attached using the exact same HTML, however, they fail to attach on a larger screen size

I recently created a small website where I dynamically add code to HTML divs. Once the data is set, I attach click and mouse enter/leave events using the div's ID. The site consists of plain HTML, javascript, and CSS. However, I noticed that when I v ...

Can someone explain how to utilize the :valid and :invalid selectors in CSS?

Recently, I've started exploring coding and a friend mentioned that it's feasible to incorporate the :valid and :invalid selectors into my CSS for form. However, I'm unsure on how to implement this. Can anyone guide me on this? ...

Emotion (Mui) in Next.js project does not properly apply inline stylesheets in Chrome

Having trouble applying classes from Inline stylesheets generated by emotion(MUI) in the latest Chrome version (v129). The issue is only present in this specific version of Chrome, as it works fine in other browsers and even in Chrome versions <=128. I ...

Fizzy beverage with right alignment

Why are the sprites in my #tbr div aligning to the left even though its align is set to right? Any suggestions? Regular text, links, and images seem to be working correctly (aligned to the right with a 20px right margin as intended). HTML: <div id="t ...

What could be causing the cell containing my image to not resize along with the image?

I'm attempting to create a table with 4 cells, each containing an image. My goal is to use a test image for now and set the size of the image in percentage so that it resizes with the window. However, I am facing an issue where when I specify the imag ...

Issue with Bootstrap cards overlapping while searching

On my Bootstrap 4 page, I've incorporated cards that can be searched, displaying only those with matching titles. However, I am facing two main issues. Firstly, when two matching cards are on the same row, they overlap. Secondly, if a matching card is ...

Using SVG and CSS to color multiple paths with individual colors

I am working with an svg that contains various paths, ellipses, and shapes each with different fill colors such as red and blue. When I combine them into a sprite, I want to be able to change the fill color on hover using CSS. Typically, I would remove the ...

Highlight the phrase "figure x" within the paragraph to emphasize its importance

I want to emphasize figure 1, figure2, ...all the way up to figure 111. I am limited to making changes in HTML and CSS only, without using JavaScript or jQuery. Is there a different method than just inserting <strong> in the HTML? In CSS, we have : ...

Can you make two columns in CSS that are left floated and maintain their original order?

While the title may not provide much clarity, I am struggling to put into words exactly what I am trying to achieve. I have created a layout in Photoshop which I have shared below to help illustrate my goal. Essentially, I have a blog that displays my sto ...

Is the bottom of the page getting partially hidden when there are more than two rows of elements?

This is a movie review application. When the screen size is on PC and mobile devices, there seems to be an issue where the movie review gets cut off from the 3rd row onwards. This is visible in the provided image, where only the buttons are partially cut o ...

Vertical words standing alongside an upright line

My goal is to rotate the text without any issues, but I'm having trouble with the line underneath it. The line should adjust its length based on the length of the text. In other words, as the text changes in length, the line should shrink or expand ac ...

React Nested Menu Selection

Having trouble displaying TextField values in my React app. Utilizing material UI and material-ui-phone-number packages. Noticed that values appear behind when clicking the flag due to zIndex issue. Looking for modifications only on dialog2.js For code ex ...

"Revealing the specific row and column of the selected element in each table when hovering over it

On my webpage, I have set up two tables positioned side by side like this: <!DOCTYPE html> <table> <tr> <td> <table> <tr><td>1</td><td>2& ...

Core-pages with polymer core have no set height limit

I am embarking on my first project using Polymer. I have implemented core-pages to facilitate navigation between different pages. However, I am facing an issue where the pages are not displaying with a white background-color as intended in the css. You ca ...

Toggle between two different global SCSS styles using a selector

I am in the process of upgrading my company's AngularJS project to Angular 8 using ngUpgrade. This has resulted in a hybrid application with components from both AngularJS and Angular. Additionally, I am utilizing an angular material template that inc ...

Linking JSON information to an HTML document

I'm struggling to integrate some JSON data into an HTML page using "Vanilla Javascript," but I'm unsure of how to proceed. Can you please provide assistance? I can share the HTML, CSS code, and an example of the JSON data I want to incorporate in ...

Is it possible for me to utilize a validation function to display error messages within a specific span id tag?

document.getElementById("button1").addEventListener("click", mouseOver1); function mouseOver1(){ document.getElementById("button1").style.color = "red"; } document.getElementById("button2").addEventListener("click", mouseOver); function mous ...

Navigate within a single component on the Stack by scrolling

I'm a huge fan of CSS! Although it may seem like a simple task, I am completely stumped on how to tackle it. Mission: My goal is to enable scrolling only within the List section without affecting the entire page. Here's the structure of my ...

Creating a split color background in CSS with a perfectly centered image on top

Is there a way to center and scale just one image on a webpage, rather than two? I want it to be in the middle of the screen or page and resize with the window. Image example: https://i.sstatic.net/t0AQP.png body { font-family: 'Titillium Web&a ...

What is the process for including icons on buttons?

I have been researching how to use Font Awesome software to incorporate icons into my HTML elements, but I am uncertain about the implementation process for my specific case. Currently, I have created a basic webpage with buttons and I would like to includ ...