Tips for aligning text in the center of a button across various screen sizes using bootstrap4 or CSS

I am facing an issue with the alignment of button text. It appears to be off-center on certain screen sizes, as shown in the images below.

On my iPad, it looks fine:

https://i.sstatic.net/agDR3.png

However, on my iPhone, the positioning is not centered:

https://i.sstatic.net/RorNj.png

HTML Code:

<div class="row">
  <div class="col-lg-12 col-md-12 col-sm-12">
    <div @fade class="img-wrapper">
      <img [src]="imgPath" class="img-fluid">
      <div class="img-overlay">
        <button routerLink="/test" 
                class="btn btn-success fill-container testButton">
                TEST
        </button>
      </div>
    </div>
  </div>
</div>

CSS Styles:

.img-wrapper {
    position: relative;
}

.img-overlay {
    position: absolute;
    bottom: 31%;
    left: 13.5%;

    width: 34%;
    height: 4%;
    background-color: red;
}

.testButton {
    font-size: 3vw;
    line-height: 20px;
}
.fill-container {
    width: 100%;
    height: 100%;
}

If you have any suggestions on how I can correct this alignment issue, please let me know.

Thank you!

Answer №1

The solution is quite straightforward: By refraining from interfering with the functionality of native Bootstrap 4 classes using custom css that disrupts them, simply adding the text-center class suffices.

Below is a snippet of code illustrating how this class effectively performs its task:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

<div class="container">
    <div class="row">
        <div class="col-lg-12 col-md-12 col-sm-12">
            <div @fade class="img-wrapper">
                <img [src]="imgPath" class="img-fluid">
                <div class="img-overlay text-center">
                    <button routerLink="/test" 
                            class="btn btn-success fill-container testButton">
                        TEST
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

However, introducing css tweaks that disrupt Bootstrap will inevitably lead to the necessity for additional css adjustments to rectify issues stemming from the initial modifications.

In such instances, it is advisable to revert back and eliminate all customized css. Instead, rely on the inherent capabilities of native Bootstrap 4 classes to fulfill your requirements as they possess the versatility to handle almost every layout-related need without resorting to css workarounds.

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

Passing a PHP array to a JavaScript function using a button click event in an HTML form

It seems there was some confusion in my approach. I'm working with a datatable that has an edit button at the end. Clicking on this button opens a modal, and I want to pass the data from the table to the modal. While I can send it as a single variabl ...

Angular 4 incorporating a customized Bootstrap 4 accordion menu for seamless navigation

I am trying to implement a nested menu using a JSON object in Angular 4. Below is the code I have written. <div id="panel-group"> <div class="panel panel-default" *ngFor="let mainItem of objectKeys(my_menu); let i = index"> <div class ...

What is the best way to incorporate both year and month filters using a dropdown menu on a traditional blog website?

I am currently working on a static blog article website and I am looking to implement a 'filter by year and month' feature. This will allow users to easily sort through blog articles based on their preferences. While I have successfully added a ...

No elements can be located by Selenium through the use of css, xpath, name, or id in Python

I'm facing a challenge logging into a forum after bypassing the cookie message by switching to its iframe. Despite trying to access elements on the page, I'm encountering difficulties. Here's the code I'm using: #set path, open firefox ...

Moving a popup div along with a marker in Leaflet can be achieved by using the set

After creating a map using leaflet and adding markers to different locations, I implemented code that displays a popup div with a message when a marker is clicked. Here is the code snippet: markers.on("click", function(d) { div.html("This is Some info ...

What is the best way to customize the appearance of only one of two identical tables using HTML and CSS?

I have two tables that look identical: <html> <head> <style> td, th { text-align: left; padding: 28px; } </style> </head> <body> <table> <tr> ...

Should I be concerned about including a non-existent CSS class?

When it comes to using jQuery and finding the values of text fields, adding classes seems like a common practice. However, I am concerned about how this may impact page loading. Is it efficient for the browser to search for all these classes? In the past, ...

Remember to retain the most recently chosen item in the dropdown menus

My Bootstrap drop down lists allow users to select items from a MySQL table, and everything is functioning correctly. However, I am facing an issue where the drop downs reset to their default values every time (as expected since defaults are set in HTML). ...

Is there a way to display the button only when the user is able to enter an email in the input field?

I have a form for users to update their profile and a button to delete their account. I want the delete account button to only be visible if the user enters their email. $(document).ready(function() { var user_email = $('input[name="emp_email"]&a ...

Creating a functional dropdown form in Ruby On Rails: A Step-by-Step Guide

Having an issue with implementing a dropdown form in the navigation bar of my Rails application. The problem arises randomly - sometimes it works smoothly, while other times I have to refresh the page for it to function properly. The Rails version being u ...

Is it possible for me to alter the script for my button's onclick

Is there a way to replicate all properties of a div when creating a clone using JavaScript code? I have an existing script that successfully creates a clone of a div when a button is pressed, but it does not copy the CSS properties. How can I ensure that t ...

strange glitch found in jquery ui resizable

Experimenting with jquery UI, I came across an unusual bug. After clicking a button to make a div resizable, the div unexpectedly moves below the button. resizable.html: <html> <head> <link href="my.css" rel="stylesheet" type=" ...

Large Quotation marks with an HTML/CSS design

Is there a way to style a quote using large quotation marks? I've tried various methods found online, such as using Blockquote with the :before and :after CSS styles. However, I'm running into issues with using an image within a div for responsi ...

Apply a chosen style to the element that was clicked on, while removing the style from the rest

Here is an example of my ul li structure: <div id="categoryTree"> <ul> <li id="cat_15"> <a class="hasSubCat" href="javascript:void(0);"><img src="images/icons/folder.gif" border="0" alt="Folder" title=" Folder ">N ...

Set up four divs in a cascading arrangement across four separate columns

Looking to set up 4 cascading divs that create the appearance of being in 4 columns. Here is the given html structure (unable to be modified) <div class="col1"> 1111 <div class="col2"> 2222 <div cl ...

What is the best way to generate a specified number of rows with 3 columns in each row using jquery?

After spending 3 hours trying to create a boostrap 'card' with 3 identical cards per row, I unfortunately failed. Here is the code I attempted: $.getJSON("./listings.php", function(e) { $.each(e, function(i, e){ if (e.id != ...

What is the best way to change the date format of a JSON string to a custom format?

Hello, I am seeking advice on how to convert a JSON string date from a response into the format of "8/24/2016". I attempted to use a dateFilter.js file, but encountered errors. Here is my attempted code: Below is the dateFilter.js code that resulted in an ...

Hover effect alters background color

I've successfully created a box around text, but now I want it to change color when hovered over. However, I'm facing difficulty in achieving this effect. An image has also been attached for reference. Please review the image and locate the socia ...

The default settings prevent window.open (pop-ups blocked) from opening

I've been working on adding a share link for my website and I came up with the following code: function handleFacebook(shortURL) { $(".facebook").click(function(e) { e.preventDefault(); window.open( 'https://www.facebook.com/ ...

css failing to load properly following javascript redirection

Upon clicking the button labeled id=cancel, the user will be directed to index.php. $('#cancel').click(function() { if(changes > 0){ $('#dialog-confirm').dialog('open'); }else{ window.location.href = ". ...