Align the button at the center of the carousel

While working on a personal HTML/CSS/Bootstrap 5 project as a self-taught learner, I have encountered some beginner doubts. My challenge is to ensure that the site remains responsive across various devices such as mobile and tablet. Specifically, I am struggling to center the yellow button in relation to the three bars representing the carousel slides. Despite trying techniques like margin:auto;, text-align:center; and align-items: center;, I haven't been successful in achieving this. Can anyone advise how I can consistently centralize the button next to the carousel bars regardless of screen dimensions?

https://i.sstatic.net/seKeh.jpg

  
.buttonPosition {
  position: absolute;
  bottom: 15%;
  left: 41.5%;
  font-size: 13px;
  color: black;
  display: inline-block;
  padding: 0.9em 1.4em;
  max-width: 40%;
  z-index: 2;
}

.carousel-item h5 {
  font-size: 48px;
  letter-spacing: 2px;
  color: rgb(185, 204, 240);
  text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
  bottom: 65%;
  z-index: 2;
}

.carousel-item p {
  margin: auto;
  font-size: 32px;
  line-height: 1.9;
  z-index: 2;
  position: absolute;
  top: 55%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: rgb(255, 255, 255);
}

.carousel-item {
  height: 124vh;
  min-height: 60px;
} 

.shadow {
  text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
}

.carousel-inner::before { 
  content: '';
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background: rgba(0,0,0,0.35);
  z-index: 1;
}

.active {
  color: rgb(14, 153, 100)!important;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
} 
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bbd9d4d4cfc8cfc9dacbfb8e95899588">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/@popperjs/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2a4945584f6a18041b1b041c">[email protected]</a>/dist/umd/popper.min.js" integrity="sha384-oBqDVmMz9ATKxIep9tiCxS/Z9fNfEXiDAYTujMAeBAsjFuCZSmKbSSUnQlmh/jp3" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="482a27273c3b3c3a2938087d667a667b">[email protected]</a>/dist/js/bootstrap.min.js" integrity="sha384-cuYeSxntonz0PPNlHhBs68uyIAVpIIOZZ5JqeqvYYIcEL727kskC66kF92t6Xl2V" crossorigin="anonymous"></script>


<div id="home" class="carousel slide" data-bs-ride="carousel" data-bs-interval="7500" data-bs-pause="false">
  <div class="carousel-indicators">
    <button type="button" data-bs-target="#home" data-bs-slide-to="0" class="active" aria-current="true" aria-label="Slide 1"></button>
    <button type="button" data-bs-target="#home" data-bs-slide-to="1" aria-label="Slide 2"></button>
    <button type="button" data-bs-target="#home" data-bs-slide-to="2" aria-label="Slide 3"></button>
  </div>
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="./assets/images/plants.jpg" class="w-100" alt="plants">
      <h5 class="carousel-caption">Professional landscaping services</h5>
      <p class="shadow"> From garden maintenance to cleaning and repairs, we are passionate about our work. Contact us for quotes.</p>
      <a href="#" class="btn btn-warning mt-3 buttonPosition" data-bs-toggle="modal" data-bs-target="#exampleModal">Contact Us <i class="uil uil-message button__icon"></i></a>
    </div>
    <div class="carousel-item">
      <img src="./assets/images/plant.jpg" class="w-100" alt="plant">
      <h5 class="carousel-caption">Modern and contemporary style</h5>
      <p class="shadow">Let landscape architecture be the reason for your well-being. We work in your garden with seriousness, professionalism, commitment, and dedication.</p>
      <a href="#" class="btn btn-warning mt-3 buttonPosition" data-bs-toggle="modal" data-bs-target="#exampleModal">Contact Us <i class="uil uil-message button__icon"></i></a>
    </div>
    <div class="carousel-item">
      <img src="./assets/images/garden.jpg" class="w-100" alt="garden">
      <h5 class="carousel-caption">Symbiosis between a project and your quality of life</h5>
      <p class="shadow">We take care of every green space down to the smallest detail, bringing harmony together with our knowledge and experience.</p>
      <a href="#" class="btn btn-warning mt-3 buttonPosition" data-bs-toggle="modal" data-bs-target="#exampleModal">Contact Us <i class="uil uil-message button__icon"></i></a>
    </div>
  </div>
  <button class="carousel-control-prev" type="button" data-bs-target="#home" data-bs-slide="prev">
      <span class="carousel-control-prev-icon" aria-hidden="true"></span>
      <span class="visually-hidden">Previous</span>
    </button>
  <button class="carousel-control-next" type="button" data-bs-target="#home" data-bs-slide="next">
      <span class="carousel-control-next-icon" aria-hidden="true"></span>
      <span class="visually-hidden">Next</span>
    </button>
</div>

Answer №1

To convert the `a` tag to an actual `button` tag and wrap it in a `div` tag, you need to assign the `text-center` class.

Replace the following line:

<a href="#" class="btn btn-warning mt-3 buttonPosition" data-bs-toggle="modal" data-bs-target="#exampleModal">Contact Us <i class="uil uil-message button__icon"></i></a>

Add this new line:

<div class="text-center">
        <button href="#" class="btn btn-warning mt-3 text-center" data-bs-toggle="modal"
            data-bs-target="#exampleModal">Contact Us <i class="uil uil-message button__icon"></i></button>
</div>

Remove any position-related styles from the `.buttonPosition` CSS rule (`position: absolute; bottom: 15%; left: 41.5%;`).

You could also consider adding `width: 100%` to the `.carousel-indicators` class for proper centering (if not already included).

Here is a snippet of the code:

.buttonPosition {
  position: relative;
  z-index: 3;
  top: 30em;
  font-size: 13px;
  color: black;
  display: inline-block;
  padding: 0.9em 1.4em;
  max-width: 40%;
  z-index: 2;
}
    <link 
href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b2d0ddddc6c1c6c0d3c2f2879c809c81">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="395a564b5c790b170808170f">[email protected]</a>/dist/umd/popper.min.js" integrity="sha384-oBqDVmMz9ATKxIep9tiCxS/Z9fNfEXiDAYTujMAeBAsjFuCZSmKbSSUnQlmh/jp3" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="53313c3c27202721322313667d617d60">[email protected]</a>/dist/js/bootstrap.min.js" integrity="sha384-cuYeSxntonz0PPNlHhBs68uyIAVpIIOZZ5JqeqvYYIcEL727kskC66kF92t6Xl2V" crossorigin="anonymous"></script>

    <div id="home" class="carousel slide" data-bs-ride="carousel" data-bs-interval="7500" data-bs-pause="false">
      <div class="carousel-indicators">
        <button type="button" data-bs-target="#home" data-bs-slide-to="0" class="active" aria-current="true" aria-label="Slide 1"></button>
        <button type="button" data-bs-target="#home" data-bs-slide-to="1" aria-label="Slide 2"></button>
        <button type="button" data-bs-target="#home" data-bs-slide-to="2" aria-label="Slide 3"></button>
      </div>
      <div class="carousel-inner">
        <div class="carousel-item active">
          <img src="./assets/images/plants.jpg" class="w-100" alt="plants">
          <h5 class="carousel-caption">Professional Landscaping Services</h5>
          <p class="shadow">From garden maintenance to cleaning and repairs, we are passionate about our work. For quotes, just contact us.</p>
          <div class="text-center">
            <button href="#" class=" buttonPosition btn btn-warning mt-3 text-center" data-bs-toggle="modal"
              data-bs-target="#exampleModal">Contact Us <i class="uil uil-message button__icon"></i></button>
          </div>
        </div>
        <div class="carousel-item">
          <img src="./assets/images/plant.jpg" class="w-100" alt="plant">
          <h5 class="carousel-caption">Modern and Contemporary Style</h5>
          <p class="shadow">Let landscape architecture be the reason for your well-being. We work in your garden with seriousness, professionalism, commitment, and dedication.</p>
          <div class="text-center">
            <button href="#" class="btn btn-warning mt-3 text-center" data-bs-toggle="modal"
              data-bs-target="#exampleModal">Contact Us <i class="uil uil-message button__icon"></i></button>
          </div>
        </div>
        <div class="carousel-item">
          <img src="./assets/images/garden.jpg" class="w-100" alt="garden">
          <h5 class="carousel-caption">Symbiosis between a project and your quality of life</h5>
          <p class="shadow">We take care of every detail in all green spaces, bringing harmony combined with our knowledge and experience.</p>
          <div class="text-center">
            <button href="#" class="btn btn-warning mt-3 text-center" data-bs-toggle="modal"
              data-bs-target="#exampleModal">Contact Us <i class="uil uil-message button__icon"></i></button>
          </div>
        </div>
      </div>
      <button class="carousel-control-prev" type="button" data-bs-target="#home" data-bs-slide="prev">
        <span class="carousel-control-prev-icon" aria-hidden="true"></span>
        <span class="visually-hidden">Previous</span>
      </button>
      <button class="carousel-control-next" type="button" data-bs-target="#home" data-bs-slide="next">
        <span class="carousel-control-next-icon" aria-hidden="true"></span>
        <span class="visually-hidden">Next</span>
      </button>
    </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 style multiple classes within the same element (specifically an ordered list ol

Using CSS counters, I have created various styles for an ordered list <ol> by applying different classes and adjusting the styles accordingly. Now, I want the counters to adjust based on screen size, displaying smaller numbers on smaller screens. To ...

Guide to linking two input fields using a single datepicker

To achieve the goal of filling two input fields simultaneously, take a look at the following code snippet: <mat-form-field> <input matInput [matDatepicker]="startDate" formControlName="SaleDate" /> ...

When using font size in rem units, it will remain consistent across different screen sizes and not be subject to scaling

When setting padding, margin, and font size using rem units, I noticed that on larger screens the output looks good. However, on smaller screen devices, the sizes do not reduce proportionally - instead, the measurements from the larger screen persist. Wh ...

span element failed to trigger onload event

I'm encountering a basic issue with my code as a beginner. Below is the HTML snippet: <!DOCTYPE html> <html> <head> <meta http-equiv='content-type' content='text/html; charset=utf8' /> <scrip ...

Utilize a function from a separate JavaScript file by calling it within the $(document).ready(function()

Refer to this post for more information: Click here I attempted to access a function that was defined in another .js file based on the instructions from the post. However, I encountered an issue. Take a look at my code below: sildemenu.js $(document).re ...

Ways to modify the background images in doxygen

Currently, I am facing a challenge while customizing Doxygen's output and my main roadblock is the menu bar. To address this issue, I decided to generate custom CSS and HTML files provided by Doxygen by executing the command: doxygen -w html header. ...

Maintain the height of the image equal to the height of the

I've been facing challenges with adjusting the height of this image to match the div container. I've experimented with various height properties such as max-height, min-height, normal height, auto, and 100%, but none seem to be providing the desi ...

Fluctuate the window.location with jQuery or javascript

My goal is to create a functionality where clicking an image will toggle the window.location url between '#' and '#footer'. The current code I have for this is: <script> function clickarrow(){ var rd=Math.floor(Math.random() ...

What is the best way to align isotope and organize its components?

I have searched for solutions to my issue with centering isotope on my project, but none of the existing answers were sufficient. I am facing challenges trying to center isotope without creating extra space between its elements and arranging them in a spec ...

Various objects are becoming engaged and chosen instead of a single item being focused on

In my React application, I have integrated a searchable Semantic UI dropdown. The issue I am encountering is that when I select an item by typing in the search field, the element matching the search text gets the class "active" and the element at the index ...

Struggling to figure out how to make this Vue function work

I am working with a list of tasks, where each task is contained within a div element. I am looking to create a function that changes the border color of a task to red when clicked, and reverts the previous task's border to normal. I have two files: &a ...

The alignment of the image is malfunctioning

I'm currently in the process of creating a personalized website for my niece, designed to be akin to a homepage just for her. While I have some experience with coding, I've hit a roadblock when it comes to positioning an image on the page. Someth ...

Switching the position to fixed causes it to lose its justification functionality

My goal is to create a table using div elements. However, when I set the header to have a fixed position to prevent it from disappearing when scrolling through more data, all the header cells shift to the right. I attempted to adjust the margin-left proper ...

Customizing CSS for Internet Explorer browsers

I am looking to target specific CSS styles for Internet Explorer only, without affecting other browsers. Initially, I tried using conditional comments: <!--[if lt IE 7 ]><html class="ie6 ie"> <![endif]--> <!--[if IE 7 ]><html cl ...

Creating a dynamic table accordion with PHP and MySQL

I am currently working on creating an accordion table to display data retrieved from a database. I want the description data to be shown in a row below when clicking on the respective row. Despite my efforts in modifying various code snippets that I have c ...

Use the same CSS style for various attribute selectors

Is there a way to simultaneously apply the following style to a type="submit" without having to repeat the entire block of code? input[type="button"] { width: 120px; margin-left: 35px; display: block; } ...

Using a table row as a counter in HTML

I am looking for a way to automatically assign IDs to table rows using XSLT in a systematic manner. The idea is to have the ID consist of a string followed by a counter, like this: <table> <tr id="Row1"> # it can be only a number => id=" ...

Is there a way in AngularJS to set all radio buttons to false when one is chosen as true?

When I retrieve true/false string values from the database, I am unable to alter the data. The example I provided is simply a representation of the string values true or false that I receive from the database. My goal is to have a single radio button disp ...

Using jQuery to create an array variable and Passing the array to a different PHP page

Although I have managed to create a function that works perfectly for me, I am facing an issue with the jQuery part at the bottom. Currently, I am only able to trigger an alert message and not store the data as a real variable. The function is functionin ...

Overflow scrolling container with additional space to the right of an image

I am currently working on a project that involves a specific website. When examining the main battalion image, I noticed there is an excessive 300px of whitespace on the right side that I am struggling to remove. There is no hidden content or padding caus ...