Alignment text centrally in a button

I'm currently facing an issue with vertically aligning the text within a button to the center. We are using Bootstrap buttons across different devices and platforms, and everything seems to be working fine except for one specific device where the alignment is off.

Here is how the button appears on Chrome and most other devices: Chrome, Chrome DevTools

And this is how it looks on our problematic device: Device, Device DevTools

The text in the button on our device appears slightly above the center. I have tried adjusting the line height, utilizing CSS reset and normalize techniques, as well as tweaking various other CSS properties, but unfortunately, none of them have succeeded in properly centering the text vertically.

Below are the overrides we have implemented for the button:

.btn {
  font-family: $open-sans;
  font-size: 14px;
  font-weight: bold;
  border-color: #cfcfcf;
  text-transform: uppercase;
  text-align: center !important;
}

The button code snippet:

<button type="button" class="btn">Close</button>

We also attempted using <a /> and <input> tags based on the documentation, but these methods did not resolve the issue:

<a class="btn" role="button">Close</a>
<input class="btn" type="button" value="Close">

A noticeable distinction I observed is that the text content within the element (highlighted in blue in DevTools) is aligned to the top on our problem device, whereas it is centered within the element on other browsers. Normalizing this alignment discrepancy has proven to be challenging.

In case it could provide any insights, the troublesome device utilizes Android System WebView version 74.0.3729.186.

EDIT: Upon further examination of the DevTools screenshots, the issue does not pertain to aligning the inner content box centrally within the button, as it already appears correctly centered. The actual challenge lies in aligning the text properly within the content box. Adjusting the line-height property has been ineffective, as there still remains a gap beneath the text within the content box.

I initially suspected excessive internal padding within the font, however, changing the font did not yield different results.

Answer №1

There may be two potential issues to consider:

  1. Padding-bottom: Verify if the padding top and bottom are equal. Typically, it should be set to padding: .375rem .75rem;.
  2. Line-height: Ensure that it is 24px. If there is only one line of text, you can also remove it by setting line-height: 0
.btn {
  font-family: $open-sans;
  font-size: 14px;
  font-weight: bold;
  border-color: #cfcfcf;
  text-transform: uppercase;
  text-align: center !important;
  line-height: 0 !important; /* NEW */
}


If the issue persists, utilize these two classes.

  1. d-flex: Transform it into a flex container

  2. align-items-center: Vertically align the content in the center.


<button type="button" class="btn d-flex align-items-center">Close</button>

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

Is there a way to center li elements evenly on mobile devices, regardless of the text length?

Take a look at these two images showcasing my website on different devices: Mobile: https://i.sstatic.net/spsYj.png Desktop: https://i.sstatic.net/AvFiN.png I have organized the content using simple ul and li tags. How can I adjust the layout so that ea ...

How come my image is not aligned to the right of the header in my HTML code?

My attempt to align an image to the right side of my heading using code was unsuccessful. Here is the HTML snippet I used: /* Aligning with CSS */ #colorlib-logo img { display: flex; align-items: center; width: 30px; height: 30px; } #colorli ...

Having trouble with Python Bottle <br /> not functioning properly?

I encountered a perplexing issue and I am unsure of how to resolve it: There is a text area where users can input their text. For example, let's say the following is entered : This is the user's text After storing this in my database and ret ...

What is the best way to place my website's logo in the top-left corner using html and css?

Hello everyone. I am currently working on customizing a website using an HTML and CSS template for a project, but I need to make some modifications. My main goal is to add the company logo to the header, specifically in the top-left corner, however, I&apo ...

How can I make a single <input> element that can handle both files and urls simultaneously?

Is there a way to enable my users to import both local and remote files using just one input field? A possible solution, inspired by Stackoverflow, is to implement tabs: Another approach could be using radio buttons like this: <style> .display ...

The auto setting in the CSS clamp function is not functioning properly when used as the minimum, value,

I have been trying to use the css function clamp() to control the height of my div, but for some reason it is not behaving as I expected. .container{ height: clamp(200px, auto, 400px); } Interestingly, it works perfectly fine when I define the heights ...

Ensuring Your IMG is Perfectly Centered in DIV Across all Browsers

I have tried all the tips from the top Google results, but I am still struggling to center an image within a div. For example, the popular tricks mentioned in this link: or http://www.w3.org/Style/Examples/007/center.en.html do not seem to work in IE 8. ...

extracting an attribute from a class's search functionality

Let's consider the following HTML structure: <div id="container"> <div> <div class="main" data-id="thisID"> </div> </div> </div> If I want to retrieve the value inside the data-id attribute ...

What are the steps to create a button with similar design using css3?

How can I create a button with the same lower part as shown in this picture? I've already achieved 50% of it with border-radius, but need help expanding the lower part to match. Any suggestions? https://i.sstatic.net/3ZJFS.jpg .list{ height: 280px; ...

compress a website to display advertisement

Here is a JSFiddle link I would like to share with you: I am currently working on squeezing the webpage to display an ad on the right side. http://jsfiddle.net/5o6ghf9d/1/ It works well on desktop browsers, but I am facing issues with iPad Safari/Chrome ...

How about mixing up your backgrounds with an overlay effect for a unique look?

Hey there, I'm currently working on adding random backgrounds to my website through an overlay, but I've hit a roadblock when it comes to displaying them. Here is the code I'm working with: .css / .php #intro { background: ...

The Angular ng-style feature is responsible for applying a style attribute that is subsequently not maintained in proper sync

My angular website retrieves data from a Web API as its back end. Initially, a generic background is displayed. However, after the user logs in, the background image URL specific to the customer is fetched and utilized in the ng-style attribute. Upon the ...

Switching the background hue of the chat box after each message

Does anyone know how to change the colors of each message in a chat window using CSS and HTML? I'm trying to customize my stream but can't figure it out. Here is an example for reference. ...

Can you explain how to modify the hover color of a word with select letters already colored using CSS?

Changing colors of a link before and during hover state One problem I'm facing is having a link where the first and last letters are red while the ones in between are black. This color combination should remain the same, even if the link has been vis ...

Is there a way to overlap shapes (transform) using ::before and ::after in CSS? I'm facing an issue where Edge is not showing the shape on top

I designed two diagonal shapes to overlay a container with a background image using the pseudo elements ::before and ::after. While this setup functions correctly in Firefox and Chrome, it is not working as expected in Edge. What could be causing this di ...

Webpage featuring tabs aligned horizontally

Can anyone guide me on setting up horizontal tabs similar to the design in this image: https://i.sstatic.net/ewXO4.png? Below is the code snippet: <label class="formlabel">Title 1:</label> <div id="radio_form"> <label><inpu ...

:before pseudo-element causing interference with child elements

Struggling to understand how an absolutely positioned :before element works. I need a large quote mark to be placed behind a testimonial. I opted for a :before element because it is a font, making it scalable for retina displays and saving an extra HTTP re ...

The absence of a datepicker in Bootstrap has become glaringly obvious

My form includes a single date input using <input type='date' class='form-control'>. When utilizing the form-control feature from Bootstrap 5, the default white color is applied to the date picker, matching the rest of the form. ...

It seems like Flexbox is ignoring the flex-direction: column property set for a header element

I'm a bit confused because it seems like setting flex-direction: column on an element should display the children of that element in a horizontal row. My layout is simple - at the top, I have the logo, navigation links, and some controls. Below that, ...

Is there a way to troubleshoot and resolve the error code I am encountering while using Angular Bootstrap and

1- When working on "app.component.html," I utilized a starter template code found at this link 2- To install Bootstrap 4, I ran the command "npm install bootstrap@4 --save" in the terminal. 3- I added references to style.css as per the Bootstrap installa ...