Responsive Button Sizing with Bootstrap 4

Would it be possible to use Bootstrap 4 to automatically apply the 'btn-sm' class (small button) when the media breakpoint is xs?

I want the buttons to remain their default size unless the screen size is xs, where they should switch to btn-sm automatically.

Answer №1

Utilize the media-breakpoint-between mixin to apply the .btn-sm sizing to all elements with the class .btn...

/* Apply .btn-sm size to all .btn on xs screens */
@include media-breakpoint-between(xs,sm){
  .btn {
      @include button-size($btn-padding-y-sm, $btn-padding-x-sm, $font-size-sm, $btn-border-radius-sm);
  }
}


Update - Bootstrap 4.1
The button-size mixin has been updated to:

@include media-breakpoint-between(xs,sm){
  .btn {
      @include button-size($input-btn-padding-y-sm, $input-btn-padding-x-sm, $font-size-sm, $line-height-sm, $btn-border-radius-sm);
  }
}

Answer №2

With Bootstrap 4, it is possible to accomplish this task using only utility classes.

<a href="#" class="btn btn-primary d-flex justify-content-center d-md-table mx-auto">Schedule a demonstration</a>

Answer №3

To ensure a button is responsive, it's important to also make the text within it responsive.

One way to achieve this is by using vw (Viewport Width) for the font size:

@media (max-width: 576px) {
  .responsive-content {
    font-size: 6vw;
  }
}
<button type="button" class="responsive-content btn btn-primary>
  responsive button
</button>

Try resizing the screen to see the responsive effect in action.

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

Tips for arranging backend data horizontally within Bootstrap horizontal cards

After setting up my Angular application, I created a dashboard page and made API calls for dynamic signals (signal-1, signal-2, etc). To showcase this data, I decided to use Bootstrap horizontal cards. However, I'm facing an issue with displaying the ...

Using Flexbox to ensure two elements do not appear next to each other

When viewing on large screens, I want all elements to be on one row. On smaller screens, I want them to be on three rows. Specifically, I don't want to see the button next to the H1 element on the same row. How can flexbox help solve this problem? Tha ...

The Bootstrap Modal consistently displays the initial record, regardless of the row that is selected

I'm having an issue with a table that displays records and two buttons for edit and delete. When I click on the buttons, a Bootstrap modal opens, but it always displays the first record regardless of which row is clicked. <!DOCTYPE html> <htm ...

Alter the preceding element within the DOM upon hovering with CSS

Could there be a CSS solution to this dilemma or would we need the assistance of JavaScript to navigate the DOM? Here's the HTML snippet in question: <div class="thumb_ctn"> <div class="overlay"> <div id="link_text">SOME ...

Using inline JavaScript to style an element when clicked

Looking for assistance with styling a link. I've connected an external stylesheet to my HTML file, so that part is all set. The goal is to modify the text-decoration when the onclick event occurs. Here's what I currently have: <a class="dopel ...

Show Struts2 error message on an HTML webpage

I have included error and message handling in my Struts2 action class using the addActionMessage and addActionError methods. I am now looking for code that will allow me to display these messages and errors on the HTML page. Is it feasible to do so? ...

Enhancing the appearance of child elements using CSS modules in Next.js

My Countdown component implementation includes: import styles from "./Countdown.module.scss"; import React from "react"; import useTimer from "hooks/useTimer"; import cn from "classnames"; function Countdown({ date, ...

Issue with jQuery animation: Background color does not change upon scrolling

Here is my fiddle link: https://jsfiddle.net/jzhang172/owkqmtcc/5/ I am attempting to change the background color of the "content" div when scrolling anywhere within it. The goal is for the background color to change when scrolling and revert back to its ...

What is the best way to align the action buttons on this webpage?

I'm struggling to center both buttons on the page. I've tried using margin:0 auto 0 auto; in the position property, but it doesn't seem to work. Strangely, when I used a table instead of a div, it worked. I'm curious to know why I can&a ...

Tips for implementing lazy loading with an owl carousel featuring background images

Is there a way to add lazy loading to a semi custom owl carousel that uses background images instead of regular img tags? I've tried using Owl carousel's function for lazy loading but it doesn't seem to work. How can I achieve this? This is ...

Implementing Bootstrap 4 accordions within a Knockout foreach loop, adjusting the icon upon expansion/collapse dilemma

I have a collection of collapsible sections within a main collapsible section like: <div id="parentAccordion" class="card-accordion"> <div class="card"> <div class="card-header bg-black text-white pointer-cursor"> ...

Refresh a particular element using javascript

I'm new to Javascript and I am trying to refresh a specific element (a div) using only Javascript when that div is clicked. I came across the location.reload() function, but it reloads the entire page which doesn't fit my requirements. Upon clic ...

Aligning Header and Search Icon in Perfect Harmony with the Body

I've been struggling with alignment issues on my Tumblr blog. Specifically, I'm trying to align a header and a search icon. You can find my site here. Here is the mockup I am aiming for: However, if you visit my site, you'll notice that ...

The CSS within the WebComponent nesting is not valid

Upon testing, I have found that writing CSS directly using nesting is effective. However, when used within a WebComponent and nested with the :host selector, it becomes invalid. Below is a demo code snippet showcasing this issue. My Chrome version number i ...

Leveraging HTML5's local storage functionality to save and manage a collection of list elements within `<ul>`

I need help with saving a to-do list in HTML so that it persists even after refreshing the browser. Can anyone assist me? html <!DOCTYPE html> <html> <head> <title>My To-Do List</title> <link rel="sty ...

Tips for creating seamless image transitions using a toggle button

My transition effect is working smoothly, but I am having trouble setting it up for the image. How can I resolve this issue? I attempted the following: var lightsOff = document.querySelector("#lights-off"); var lightsOn = document.querySelector("#lights-o ...

Creating a responsive HTML, CSS, and JavaScript popup: A step-by-step guide

I'm facing a challenge in making my HTML, CSS, and JavaScript popup responsive. Despite looking into various resources, I haven't been able to find a solution that fits my code. However, I am confident that it is achievable to make it responsive! ...

Adding an id to a ul tag using JavaScript

I am trying to dynamically add an ID called "myMenu" using JavaScript to a ul element for a search filter. Unfortunately, I am unable to directly access the ul tag to change it, so I need to do it via JavaScript. As I am new to JavaScript, I am looking t ...

The main-panel div's width increase is causing the pages to extend beyond the window's width limit

I recently downloaded a Reactbootstrap theme and managed to integrate it with NextJS successfully. However, I am facing an issue where my <div className="main-panel"> in the Layout.js is extending slightly beyond the window. It seems like t ...

Using media queries within specific CSS classes

I am attempting to create the following CSS code: .myClass { @media all (max-width: 1024px) { left: 33%; } @media all (min-width:1025px) { left: 25%; } } Unfortunately, it doesn't seem to be working as expected. Is t ...