Bootstrap 4 - The Mystery of 'col-sm-auto' Taking Precedence Over 'col-md-4'

I am currently learning Bootstrap and have encountered a challenge that I can't seem to figure out.

Let's take a look at a snippet of the code:

<button 
  className="
       col-md-4 
       col-sm-auto 
       btn 
       btn-info 
       btn-custom" 
  onClick={this.toggleModalHandler}
> Save
</button>

And here is a more detailed version of the code:

<div className="container">
                <h2 className="border-bottom mb-4 pt-sm-2">Insert Product Values</h2>
                <div className="pt-4 border-bottom">
                    <h5>Total Value: $ </h5>
                    <div className="
                            row
                            align-items-md-center 
                            mb-2 
                            flex-md-row
                            flex-sm-column" 
                        >
                        <h5 className="col-md-4 col-sm-auto mb-md-0">Accumulated Total Value: $</h5>
                        <div className="col-md-8 col-sd-12">
                            <button 
                                className="col-auto btn btn-success btn-custom" 
                                onClick={() => this.addOrcHandler()}
                            > Add Quote
                            </button> 
                            <button 
                                className="col-auto btn btn-danger btn-custom" 
                                onClick={() => this.clearQuoteHistory()}
                            > Clear Quotes
                            </button>
                            <button 
                                className="col-md-4 col-sm-auto col-3 btn btn-info btn-custom" 
                                onClick={this.toggleModalHandler}
                            > Save
                            </button>
                        </div>
                    </div>
                </div>
            </div>

From what I have learned, col-md-4 should take precedence over col-sm-auto when the screen size is larger than 768px. However, it doesn't seem to be working in this case, even though it worked for other elements.

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

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

After researching, I couldn't find a solution specific to my issue.

My confusion lies in why col-md-4 is not occupying 4 columns of the grid.

One thing to note is that it works fine if I remove col-sm-auto, and it is within a row.

Can anyone point out what mistake I might be making?

Answer №1

The .col class is essential for creating blocks within the Bootstrap Grid system. To ensure proper functionality, these blocks should always be encompassed by parent elements with the classes .row and .container. It's important to avoid mixing them with elements like button and .btn, as they serve different purposes and follow unique logic. Here is an example of correct code usage:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script>

<div class="container-fluid">
  <div class="row">
    <div class="col-md-4 col-sm-auto">
      <button class="btn btn-info btn-custom btn-block">Save</button>
    </div>
  </div>
</div>

UPDATED

To learn more about wrapping elements with the .col class, refer to https://getbootstrap.com/docs/4.0/layout/grid/#column-wrapping

We have also included the use of .btn-block to ensure that .btn elements take up 100% width within their parent .col.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.min.js"></script>

<div class="container">
  <h2 class="border-bottom mb-4 pt-sm-2">Enter Product Values</h2>
  <div class="pt-4 border-bottom">
    <h5>Total Value: $</h5>
    <div class="row align-items-md-center mb-2 flex-md-row flex-sm-column">
      <h5 class="col-md-4 col-sm-auto mb-md-0">Accumulated Total Value: $</h5>
      <div class="col-md-8 col-sd-12">
        <div class="row">
          <div class="col-sm-4 col-auto">
            <button class="btn btn-success btn-custom btn-block">Add Budget</button>
          </div>
          <div class="col-sm-4 col-auto">
            <button class="btn btn-danger btn-custom btn-block">Clear Budgets</button>
          </div>
          <div class="col-sm-4 col-auto">
            <button class="btn btn-info btn-custom btn-block">Save</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</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

Tapping on a DIV element that overlays a YouTube video on an iPad does not trigger any action

Currently, I am working on a webpage specifically designed for iPad and I have encountered an issue: I am trying to embed a YouTube video using an iframe and also need a div element to remain on top of it. To achieve this, I have added "?wmode=transparen ...

Changing the MIME type from "application/octet-stream" to "video/mp4" in HTML5: A Step-by-Step Guide

<!DOCTYPE html> <html> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> </head> <body> <video id="video" controls src="${maps.url }" height="598" width="782"> ...

Resizing images in HTML can sometimes lead to extra white space appearing underneath the

I am facing an unusual scaling issue with my website design. Whenever I resize the browser window horizontally, the image scales correctly, but it leaves a white space below it where the rest of the page should start from if the window is large. I'm ...

Merging double borders in a div using CSS is essential for creating

Upon examining this sample, it's evident that the borders do not blend together. css: div{ float:left; background-color:moccasin; width:100px; height:100px; border:1px solid tomato; } The number of divs is arbitrary, and only on ...

Retrieving PHP variables within the HTML document of the current page

Suppose I have a page with a link <a href='edit_info.php?id=1001'>1001</a>. In the edit_info.php page, I want to use this id to query a database, like so: SELECT * FROM table WHERE id = $_GET['id'] Now, I need to populate ...

Creating an HTML page with R Markdown: Placing an image in the upper right corner and adjusting the position of the title

Looking to add a company logo image to the top right corner of my R markdown report, and then shift the title down by 3 or 4 cm for a letterhead-like appearance. Does anyone have suggestions on how I can achieve this in my .Rmd file? Appreciate any assist ...

Layout featuring center-aligned fixed-width design with elements stretching across the entire width of the page

How can I create a centered fixed-width layout for my page while also having headings with a background that extends over the whole page, without having to break up the single fixed-width+margin auto div into many separate divs? ...

Is it possible to incorporate a next.js image onto the background design?

I've encountered an issue while attempting to create a fixed background image with a parallax effect using Tailwind CSS and Next.js Image. If you need to see an example of this in action, check out this Template Monster theme. Here's the code s ...

Creating a Mondrian-inspired design using a combination of red, blue, and yellow within an HTML table

Is there anyone who can assist me in recreating this painting using HTML <table> tag and CSS within a browser? The image to replicate: https://i.stack.imgur.com/84y4I.jpg I attempted to complete this task, but my instructor is dissatisfied with th ...

JQuery function within the "onclick" event

$('.more').click(function showFriendInfo(id) { $('#MoreFriendInfo'+id).toggle(); }); I created this function called showFriendInfo(id), but how can I properly link it in HTML/PHP to obtain the id? I attempted the following ...

Saving the retrieved data from a JQuery $.post request into a JavaScript global variable

Currently utilizing Javascript and JQuery. A declaration of a Variable var RoleID=""; is stationed outside all functions. There exists a function: role_submit(){ var role=$('#emp_role').val(); var url="submitrole.php"; $.post(url, {role2: rol ...

enabling the editing of two input fields by clicking on a button

Looking to make two input fields editable with a button click. Once edited, the data from these fields will be sent to separate columns in a database using ajax. Struggling to find a solution through online search. Any advice is appreciated! ...

CSS background image referencing in React to the public folder's path

I am currently working on a project using Create-React-App and I am trying to set a background image for my header section. Here is how I'm attempting to do it: background-image: url('~/Screenshot_11.png'); However, I encountered an error w ...

The webpage is drifting sideways to the right without any content being displayed

Recently, I launched a new website and encountered an issue with a static page I designed. While it may not be a critical problem, it is certainly bothering me! If you want to check out the website in question, visit: In essence, I have used CSS to hide ...

Using jQuery to enable scrolling and dynamically changing classes

I'm currently working on enhancing my website with a feature that changes the opacity of the first section to 0.4 when a user scrolls more than 400 pixels down the page. I attempted the following code without success: if($("html, body").offset().top ...

Example of a functional typoscript implementation for a bootstrap navbar

Currently using typo3 8 and considering transitioning from bootstrap3 to bootstrap4. Does anyone have a proven typscript for implementing a bootstrap4 navbar? Your help is greatly appreciated. Thank you in advance! ...

Invoke a node.js function from within an HTML document

Seems like a recurring question. The closest solution I found was on this page: execute a Nodejs script from an html page? However, I'm still having trouble grasping it. Here's the scenario: I have an express server set up with the following ...

When it comes to printing, the @Media CSS rule

I have a specific structure on my HTML page: <html> <head></head> <body> <nav> .... navigation menu </nav> <div> <div></div> <div class="to-print"> <h2>Th ...

What is the best way to connect added elements together?

I am currently utilizing Plupload to upload files. Due to specific requirements, I need to place FilesAdded "inside" init as demonstrated below. The issue I'm facing is the inability to utilize jQuery.remove() on elements that have been appended usin ...

What is the best way to place multiple images on top of one another within a single div, and make them break apart with animation upon hovering?

HTML: <div class="mySlides"> <img src="1.jpg"> <img src="2.jpg"/> <img src="3.jpg"/> <img src="4.jpg"/> <img src="5.jpg"/> </div> CSS: .mySlides { padding-top: 25%; padding-left: 5%; ...