Changing from columns to rows in Bootstrap 4 for small screens

I am working on a project that involves creating a grid with 4 columns and 4 rows. The challenge I am facing is making the 4th column transition into a 5th row on small displays, instead of being displayed as a column on medium to large screens. This project is aimed at enhancing my understanding of the Bootstrap grid layout. Essentially, I am building a responsive calculator for practice purposes. While I believe I can achieve this using raw CSS, I wanted to explore if Bootstrap offers any utilities that could simplify the process.

Here is an example of the layout:

Large format:

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

Small format:

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

I have started working on the markup structure. There is additional associated CSS, but it may not be crucial for the purpose of this example. At the moment, my code looks like this:

<div class="container">
  <div class="row">
    <div class="col-md-8">
      <div class="row">
        <button type="button" class="btn-lg btn-outline-dark">1</button>
        <button type="button" class="btn-lg btn-outline-dark">2</button>
        <button type="button" class="btn-lg btn-outline-dark">3</button>
      </div>
      <div class="row">
        <button type="button" class="btn-lg btn-outline-dark">4</button>
        <button type="button" class="btn-lg btn-outline-dark">5</button>
        <button type="button" class="btn-lg btn-outline-dark">6</button>
      </div>
      <div class="row">
        <button type="button" class="btn-lg btn-outline-dark">7</button>
        <button type="button" class="btn-lg btn-outline-dark">8</button>
        <button type="button" class="btn-lg btn-outline-dark">9</button>
      </div>
      <div class="row">
        <button type="button" class="btn-lg btn-outline-light">&plusmn;</button>
        <button type="button" class="btn-lg btn-outline-dark">0</button>
        <button type="button" class="btn-lg btn-outline-light">.</button>
      </div>
    </div>
    <div class="col-md-4">
      <div class="row">
        <button type="button" class="btn-lg btn-outline-dark">X</button>
      </div>
      <div class="row">
        <button type="button" class="btn-lg btn-outline-dark">-</button>
      </div>
      <div class="row">
        <button type="button" class="btn-lg btn-outline-dark">+</button>
      </div>
      <div class="row">
        <button type="button" class="btn-lg btn-outline-dark">=</button>
      </div>
    </div>
  </div>
</div>

EDIT: While I can only select one solution as the answer, I would like to acknowledge kiranvj, WebDevBooster, and dferenc for providing functional solutions based on the Bootstrap 4 release version. My decision is limited to marking just one response as the answer, but all contributions are appreciated. Thank you for your support.

Answer №1

If I were to create something similar, I would recommend viewing it in full screen mode and then resizing it to a smaller version.

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row text-center">
  <div class="col-12 col-sm-8">
    <div class="row">
      <div class="col-4">black</div>
      <div class="col-4">black</div>
      <div class="col-4">black</div>
    </div>
    <div class="row">
      <div class="col-4">black</div>
      <div class="col-4">black</div>
      <div class="col-4">black</div>
    </div>
    <div class="row">
      <div class="col-4">black</div>
      <div class="col-4">black</div>
      <div class="col-4">black</div>
    </div>
  </div>
  <div class="col-12 col-sm-4 text-danger">
    <div class="row">
      <div class="col-4 col-sm-12">pink</div>
      <div class="col-4 col-sm-12">pink</div>
      <div class="col-4 col-sm-12">pink</div>
    </div>
  </div>
</div>

Answer №2

In my opinion, the most straightforward way to structure this markup is as follows. It also incorporates the use of Order classes, where instead of explicitly defining the order of each button, only the "extra" buttons in the last column are targeted with .order-last .order-sm-0.

<div class="container">
    <div class="row justify-content-center">
        <button type="button" class="col-4 col-sm-3 btn-lg btn-dark">1</button>
        <button type="button" class="col-4 col-sm-3 btn-lg btn-dark">2</button>
        <button type="button" class="col-4 col-sm-3 btn-lg btn-dark">3</button>

        <button type="button" class="col-4 col-sm-3 order-last order-sm-0 btn-lg btn-secondary">X</button>

        <button type="button" class="col-4 col-sm-3 btn-lg btn-dark">4</button>
        <button type="button" class="col-4 col-sm-3 btn-lg btn-dark">5</button>
        <button type="button" class="col-4 col-sm-3 btn-lg btn-dark">6</button>

        <button type="button" class="col-4 col-sm-3 order-last order-sm-0 btn-lg btn-secondary">-</button>

        <button type="button" class="col-4 col-sm-3 btn-lg btn-dark">7</button>
        <button type="button" class="col-4 col-sm-3 btn-lg btn-dark">8</button>
        <button type="button" class="col-4 col-sm-3 btn-lg btn-dark">9</button>

        <button type="button" class="col-4 col-sm-3 order-last order-sm-0 btn-lg btn-secondary">+</button>

        <button type="button" class="col-4 col-sm-3 btn-lg btn-dark">&plusmn;</button>
        <button type="button" class="col-4 col-sm-3 btn-lg btn-dark"gt;0</button>
        <button type="button" class="col-4 col-sm-3 btn-lg btn-dark">.</button>
        
        <!--
        Use `col-12 col-sm-3` if you want a full-width `=` button 
        In this case, you can omit `.justify-content-center` from the wrapper
        -->
        <button type="button" class="col-12 col-sm-3 order-last order-sm-0 btn-lg btn-secondary">=</button>
    </div>
</div>


<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>

Answer №3

To enhance the layout, my approach would involve generating three more pink div components situated below the black divs. These additional elements would be concealed using classes like hidden-*-up and hidden-*-down. On smaller screens, I would hide the pink divs on the right side while revealing those at the bottom, and vice versa when the screen size is larger.

Answer №4

When tasked with creating a calculator, I took a unique approach compared to my colleagues by developing a grid that closely resembles an actual calculator in look and functionality. Hopefully, this deviation doesn't upset you too much. :-)

The magic behind the scenes lies in the re-ordering classes which allow elements to shift based on the screen size breakpoints.

For instance, using a class combo like order-3 order-md-1 dictates:

"Under normal circumstances (from the smallest size), occupy position #3 but for medium screens (md and above), switch to position #1."

Considering adjacent HTML elements default to order-1, consistency is maintained in the sequence on medium-sized screens compared to their neighbors. However, on smaller screens than md, the element with the order-2 class takes precedence, pushing our element down the priority list.

The

<div class="w-100"></div>
serve as convenient dividers denoted by w-100 which translates to "width:100%".

In terms of observations, your calculator surprisingly lacks a division button which may need addressing.

Additionally, since no instructions were provided regarding the equal sign button's behavior, I decided to introduce a slight alteration for smaller screens. Achieving this involved concealing the original button through d-none d-md-block classes and showcasing a wider alternative using the d-md-none class - essentially indicating it should only be hidden from screens sized md and above.

Below is the operational code snippet:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet">
<style>
body {
    font-family: 'Roboto Mono', monospace;
}
</style>

<div class="container">
    <div class="row mt-2">
        <div class="col-md-8">
            <div class="row">
                <button type="button" class="btn-lg btn-outline-dark order-1">1</button>
                <button type="button" class="btn-lg btn-outline-dark order-1">2</button>
                <button type="button" class="btn-lg btn-outline-dark order-1">3</button>
                <button type="button" class="btn-lg btn-outline-dark order-3 order-md-1">&times;</button>
                <div class="w-100 order-1"></div>
                <button type="button" class="btn-lg btn-outline-dark order-1">4</button>
                <button type="button" class="btn-lg btn-outline-dark order-1">5</button>
                <button type="button" class="btn-lg btn-outline-dark order-1">6</button>
                <button type="button" class="btn-lg btn-outline-dark order-3 order-md-1">&minus;</button>
                <div class="w-100 order-1"></div>
                <button type="button" class="btn-lg btn-outline-dark order-1">7</button>
                <button type="button" class="btn-lg btn-outline-dark order-1">8</button>
                <button type="button" class="btn-lg btn-outline-dark order-1">9</button>
                <button type="button" class="btn-lg btn-outline-dark order-3 order-md-1">+</button>
                <div class="w-100 order-1"></div>
                <button type="button" class="btn-lg btn-outline-light text-dark order-1">&pm;</button>
                <button type="button" class="btn-lg btn-outline-dark order-1">0</button>
                <button type="button" class="btn-lg btn-outline-light text-dark order-1">.</button>
                <button type="button" class="btn-lg btn-outline-dark order-1 d-none d-md-block">=</button>
                <div class="w-100 order-2"></div>
                <div class="w-100 order-4"></div>
            </div>
            <div class="row">
                <div class="col-3 col-sm-4 pl-0 pr-0 pr-sm-4">
                    <button type="button" class="btn-lg btn-block btn-outline-dark order-4 d-md-none">=</button>
                </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

Creating a see-through backdrop without the need for a background image

How can I create a div with a transparent background? I attempted to use background-color and opacity, but unfortunately, the border and text inside also became transparent. Check out an example here. Is there a way to achieve this without relying on a tr ...

Troubleshooting Variances in CSS Layouts on Different Devices

I have been wrestling with a challenging question regarding CSS layouts, and I believe it's time to seek advice from those who may be more knowledgeable in this area. Let's discuss why my current layout is presenting such a headache. Take a look ...

Building a tag cloud with only HTML5 and CSS3

I am looking to generate a word cloud similar to the one shown in this image : click here for reference Is there a way to create a tag cloud without relying on JavaScript? Using only HTML5 and CSS3. Appreciate any guidance. Does anyone have a solution fo ...

Styling an image with CSS at the top

image with 3 lines on itI am looking to create a color element that resembles a border but slightly overlaps the bottom part of the image - similar to the example shown in the linked picture. I attempted to use CSS borders, but it did not achieve the desi ...

Stop Internet Explorer from automatically scrolling when it gains focus

Please access this fiddle using internet explorer (11): https://jsfiddle.net/kaljak/yw7Lc1aw/1/ When the page loads, the <p> tag is focused and Internet Explorer slightly scrolls the element so that the table border becomes invisible... document.qu ...

Include a new class for angular's user interface tabs

Utilizing Angular UI to build my tabs, I followed the code provided in the documentation: <form name="outerForm" class="tab-form-demo"> <div class="tabbable full-width-tabs"> <uib-tabset active="activeForm"> ...

Unable to find '.file.scss' in the directory '../pages'

I am currently in the process of migrating my Ionic 3 app to version 4. However, I have encountered an issue where some pages are not recognizing the SCSS file from the TypeScript component. @Component({ selector: 'car-id', templateUrl: &apo ...

Comparing "Complete JavaScript" with "Complete JavaScript Bundle" in Bootstrap 4

Something seems to be missing here. The examples are using min.js, but the bundle.min.js contains extra code that doesn't seem to be described anywhere... https://getbootstrap.com/docs/4.0/getting-started/download/ Code: vs ...

The width of the flexbox child item is too narrow for the content it contains

I am facing an issue with the Flexbox child element not aligning correctly with the content width. Here is the code snippet: https://i.sstatic.net/jtCnSuhF.png .pw-event-options { -webkit-box-flex: 0; -webkit-flex: 0 0 280px; -moz-box-flex: 0; ...

How can I replicate the Firefox style in IE8 using Bootstrap and CSS?

My webpage is styled with Bootstrap v2 and looks great in Firefox. However, I'm struggling to apply the same CSS color style in IE8: .navbar-inverse .navbar-inner { background-color: #1b1b1b; background-image: -moz-linear-gradient(top, #fab ...

Issue with Reactstrap Bootstrap Navbar functionality within a React application

After using 'create react-app' to create a new React app, I decided to include a navbar from Reactstrap. Following the instructions on the Reactstrap website, I copied and pasted the code for the navbar component: import React from 'react&a ...

The symbols ♥ and ♦ in HTML are displaying as black on Android devices, despite being styled as red in the CSS

In the midst of my card game development using HTML, JS, and CSS on the Meteor platform. To represent the hearts suit, I utilize &hearts; and for diamonds, it's &diams;. The color scheme is crimson with color: #FF0000; in the CSS code. The fon ...

Combining Content, Attr, and Url in a single statement

I've been utilizing the content attribute for quite some time now, but today I decided to explore something different. Instead of relying on JS to show an image tooltip, I was curious if it could be done dynamically using CSS. So I attempted the foll ...

What is the best way to make a Dojo TitlePane overlap using CSS?

My dilemma involves a jsfiddle featuring two TitlePane widgets in the central pane's top right corner. Currently, clicking on the right TitlePane ("Switch Basemap") moves the left TitlePane ("Map Overlays") to the left. What I want is for the right Ti ...

Is there a way to showcase several items simultaneously on a bootstrap 5 carousel slide?

I currently have a bootstrap 5 carousel with controls that I want to modify. My goal is to create a slideshow where each slide displays 2 items, and I can scroll through one image at a time. Starting from here: https://i.sstatic.net/ROgoL.png And ending h ...

Simultaneous Activation of Hover Effects on Multiple Elements

I have an array of objects that I'm iterating over. Here is the array: const socialLinks = [ { id: 1, child: ( <> <FaLinkedin className='text-2xl' /> Linkedin </> ), hre ...

Are there any CSS hacks available to address the combination of position: sticky and overflow within the parent element?

I've encountered a sticky position issue when the overflow property is set to auto on a parent element. I've tried various solutions like adding an extra wrapper or using clip instead of auto, but none have worked. While I did find a solution usi ...

Managing active dropdown menus in VueJS

I'm having trouble figuring out why my navigation menu and method to open subitems on click are not working correctly. [![dropdown_menu][1]][1] new Vue({ el: '#app', data: { //menu "menu_title": "a", "child_ro ...

A step-by-step guide on uploading a CSV file in Angular 13 and troubleshooting the error with the application name "my

I am currently learning angular. I've generated a csv file for uploading using the code above, but when I try to display it, the screen remains blank with no content shown. The page is empty and nothing is displaying Could it be that it's not ...

Is there a way for me to determine if a user has engaged with a form?

I'm surprised by how difficult it was to find information on this topic through Google. Currently, my struggle lies in wanting my form fields to change color based on validity only after the user has interacted with the form. I don't want invali ...