What is the best way to make a button fill the entire width on smaller screens?

I am struggling with the following code snippet:

<div class="ml-auto text-right">
    <button class="btn btn-next mobile" />
</div>

Along with this block of css:

@media screen and (max-width: 576px) {
  .mobile {
    width: 100%;
    margin-left: 0;
    margin-right: 0;
    padding-left: 0;
    padding-right: 0;
    display: block;
    text-align: center;
  }
}

My goal is to have the button aligned right on desktop, while expanding to full width on mobile.

Unfortunately, my current implementation is not producing the desired result.

Can someone please provide guidance on the correct approach to achieve this?

Answer №1

In this specific instance, a media query designed for small devices max-width is illustrated. The scenario involves two <button> elements, each enclosed within a <div>. One of these <div> containers will be visible while the other will have a style of display: none.

The essential Bootstrap classes include:

To ensure specificity, the custom classes .desktop and .mobile need to be duplicated, along with utilizing !important to override the default BS classes. Usage of Bootstrap Icons is discretionary.

Switch to Full page view mode and adjust the window size.

.mobile.mobile {
  display: none !important;
}

@media (max-width: 575.98px) {
  .desktop.desktop {
    display: none !important;
  }
  .mobile.mobile {
    display: grid !important;
  }
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title></title>
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="27454848535453554657671209140914">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="492b26263d3a3d3b283964202a26273a0978677878677a">[email protected]</a>/font/bootstrap-icons.min.css" rel="stylesheet">
  <style>
   /*
     Additional CSS can be placed here or linked externally as the final <link>
  */
  </style>
</head>

<body>
  <main class="container">
    <section class="row">
      <div class="col-12">
        <div class="p-3 d-flex justify-content-end desktop">
          <button class="btn btn-lg btn-primary btn-next">
            Next <i class="bi bi-caret-right-fill"></i>
          </button>
        </div>
        <div class="p-3 d-grid mobile">
          <button class="btn btn-lg btn-primary btn-next">
            Next <i class="bi bi-caret-right-fill"></i>
          </button>
        </div>
      </div>
    </section>
  </main>
</body>

</html>

Answer №2

Give this revised CSS a try, utilize the parent class to prevent any potential conflicts

@media screen and (max-width: 576px) {
      .mobile {
        width: 100%;           
        margin: 0 auto;        
        padding: 0.5rem 1rem;  
        display: block;        
        text-align: center;   
      }
    
      .ml-auto {
        margin-left: auto;    
      }
    
      .text-right {
        text-align: initial;
      }
    }

Answer №3

By adopting the widely-used mobile-first strategy, you can streamline the process:

Check out this code snippet


/* defaults to occupying entire width */
.mobile {
  width: 100%;
}

/* adjusts based on specific dimensions - notice the change from "max-with" to "min-width" */
@media screen and (min-width: 576px) {
  .mobile {
    width: auto;
  }
}

Answer №4

The code provided does not meet your requirements. You will need to apply CSS to the button container div.

In your HTML, include a class for the button container div.

<div class="ml-auto text-right button-container">
    <button class="btn btn-next mobile">Next</button>
</div>

To align the container to the right on desktop, add the following CSS. The mobile media query is responsible for displaying it full-width on mobile screens.

.button-container {
    display: flex;
    justify-content: flex-end;
}

@media screen and (max-width: 576px) {
  .mobile {
    width: 100%;
    margin-left: 0;
    margin-right: 0;
    padding-left: 0;
    padding-right: 0;
    text-align: center;
  }

  .button-container {
    justify-content: center;
  }
}

I have tested the code above here: https://jsfiddle.net/rekhaDarshna/yv4fg9ew/

Please let me know if there is anything else you require.

Answer №5

.mybtn {
            display: block;
            background-color: green;
            color: beige;
            width: 100%;
            height: 50px;
            font-size: 30px;
            border-radius: 40px;
            border: none;
        }
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,
                                   initial-scale=1.0">
    <title>Full Width Button Example</title>
    
    </head>

<body>
    <button class="mybtn">
        Full Width Button
    </button>

</body>

</html>

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

How about: "Is there a way to show items in a list without using

I have removed the bullet points from an unordered list, but I am having trouble displaying Log with every message. The code I have doesn't seem to be working as expected. I want users to be able to differentiate between messages easily, without seein ...

What is the reason for jQuery displaying undefined when attempting to retrieve a custom data attribute using .prop()?

In my dynamic HTML generated by JavaScript, the following code snippet is included: $(".task-status").live("click", function () { alert("data-id using prop: " + $(this).prop("data-id")) alert("data-id using data: " + $(this).data("id")) ...

Problems with form functionality in React component using Material UI

Trying to set up a signup form for a web-app and encountering some issues. Using hooks in a function to render the signup page, which is routed from the login page. Currently, everything works fine when returning HTML directly from the function (signup), ...

Trimming Text with JQuery

My goal was to truncate a string and add an ellipsis at the end using jQuery, but my code doesn't seem to be working as intended. Below is the jQuery code I wrote: $(".link-content > h4").each(function(){ var len = $(this).text().length; ...

Angular: Enhancing user experience with draggable and droppable items in a flexible list

My issue involves drag and drop divs within a list. However, the divs seem to move in an unpredictable manner. They do not go where I intend them to be placed. Here is the TypeScript code: timePeriods = [ '1', '2', '3', ...

Bulma: Tips for creating equally sized buttons?

I am working with Bulma CSS and trying to ensure that all my buttons are the same size. Currently, each button appears to have a different size based on the text content. I have looked into using the "is-fullwidth" option, but it makes the buttons too la ...

Designed radio buttons failing to activate when focused using a keyboard

I'm facing an issue with radio input buttons that I've dynamically added to a div using jQuery. They function properly when clicked with a mouse, but do not get activated when using the keyboard tab-focus state. NOTE: To style the radio buttons ...

Troubleshooting inconsistent htaccess changes across different pages?

After much trial and error, I have successfully managed to run PHP code within a document that has either an .htm or .html extension. The solution that worked for me was adding the following line of code: AddType application/x-httpd-php .php .html .htm N ...

Enclose Words Inside Unconventional Outline

Place text within a uniquely shaped border… I am attempting to achieve this goal in the most responsive and backward compatible way possible. I understand that compromise may be necessary. I have made progress with the help of this thread, but the svg ...

What is the best way to incorporate JQuery into html content retrieved through an ajax request?

In my jsp file, there is a div structured like this : <div id="response" class="response"></div> After triggering an ajax call to a servlet, the content of this div gets updated with the following elements : <div id="response" class="res ...

Get rid of the <h1> tag surrounding the Wordpress logo

Currently, I am utilizing an Ultimatum child theme that was not created or selected by me. Upon inspection, I discovered that the logo is enclosed in an <h1> tag. For the sake of SEO, I require a different, more optimized logo as it seems the curre ...

Using a combination of a bootstrap class and a custom CSS class to enhance your website design

How do I apply two different classes? I have two HTML select fields where the user can choose either repair or another option in the first one. When the user selects one, the second field should appear. But it also needs to have a Bootstrap class: form-con ...

Issue arising with hover animation on a stable colored background

I recently created my own website and utilized this codepen https://codepen.io/nikolamitic/pen/vpNoNq to incorporate an animation effect on my name. While it works well overall, I encountered an issue when trying to decrease the line-height of my elements ...

Tips for preventing a column from extending beyond the edge of the browser window during resizing

Issue arises when I resize the browser window, causing the left box to extend beyond the browser boundary, making it impossible to see the contents even with the scrollbar below. <!DOCTYPE html> <html lang="en"> <head> ...

Svelte components loaded with no design aspects applied

I encountered an issue while trying to integrate the "Materialify" and "Carbon Components for Svelte" libraries into my Sapper project. The components seem to be loading, but without any associated styles. I followed the installation commands provided on t ...

Struggling to implement a sidebar in HTML using jQuery and running into issues?

I am struggling to create a template that includes a navbar, sidebar, and other elements that can be used across multiple HTML files. Despite trying different approaches, including changing the jQuery version and downloading jQuery, I am unable to make it ...

Looking for something? The search box in Material Design Lite is just like the

Utilizing material design lite, I am in the process of crafting a navigation bar. I strive to achieve a navigation bar similar to the image depicted below: https://i.sstatic.net/g7EPC.png After experimenting with some code, I managed to develop a decent ...

Avoid printing employees whose ID begins with special characters

My C# API sends all employee information from the database to my Angular web app. I need to filter out employees with IDs starting with '#' in Angular. Employee = Collaborator Below is the Angular service code that calls the API to fetch the d ...

There are two borders in place, with the second border present only on the right and bottom

Can someone help me achieve borders like the ones in this image? https://i.sstatic.net/qZHbK.png I tried using the after pseudo-element as shown below, but after learning from this discussion: Why can't an element with a z-index value cover its child ...

How can we loop through an array and display each item in a container with a fixed height until all the space is filled?

I am working with Vue and have a fixed height div container with overflow set to hidden. My goal is to iterate through a JavaScript array, adding each item to the div, and stopping when the bottom of the div space is reached. Additionally, I need this solu ...