Bootstrap 4 - Column to row on small display - css

Have a grid with 4 columns and 4 rows. What I want to happen is on a small display the 4th column becomes a 5th row on a small display vs the column that it is on a medium+ display. It's a pet project to try and understand the bootstrap grid layout better. I'm essentially making a responsive calculator for the practice. I feel like I could do this with just raw CSS but figured bootstrap had to have some utilities that would make this easier. Images for example.
Large format:
Small format:
I have a start for the markup. There is some more associated CSS but I don't think it's important in the example. I've had a few versions but this is where I'm at in the moment.
<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">±</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:
I can only pick one solution as an answer but kiranvj, WebDevBooster, and dferenc all provided Bootstrap 4 release version working solutions. I can only mark one as an answer. No slight to the rest of you guys. My hands are tied.

I would do something like this. Check in full screen mode and resize to small
<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>

I think, the simplest possible markup is the one below. It also uses the Order classes, but instead of setting the order of every button explicitly, this one touches just the "extra" buttons in the last column 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">±</button>
<button type="button" class="col-4 col-sm-3 btn-lg btn-dark">0</button>
<button type="button" class="col-4 col-sm-3 btn-lg btn-dark">.</button>
<!--
Use `col-12 col-sm-3` in case you want a full-width `=` button
In that case you could omit `.justify-content-center` at 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"/>

What I would do is create 3 additional pink div elements that render below the black divs, and then hide them using the hidden-*-up and hidden-*-down classes. When the screen is small, hide the pink divs on the right and show the pink divs on the bottom, and vice versa when the screen is small.

Since you wanted to build a calculator, I took a slightly different approach than my esteemed colleagues and created a grid that looks, feels and quacks like an actual calculator. I hope you aren't too mad about that. :-)
The main job is done by using the re-ordering classes. Those allow moving things around based on your breakpoint needs.
For example, the class combo order-3 order-md-1 says:
"Normally i.e. from the smallest screen size onwards you're gonna go to position #3 but from the medium (md) screen size and up you're gonna go to position #1."
And since the neighboring HTML elements also have order-1 as their default position, that element is gonna appear in the same order on medium sized screens as the neighbors. But on smaller than md screens that element is gonna become a "third-class citizen" and will, therefore, be positioned after the element with the order-2 class.
The <div class="w-100"></div> elements are just handy dividers. w-100 means "width:100%".
Speaking of dividers: I've noticed that your calculator has no division button, but I hope you'll figure out what to do about that.
Finally, you hadn't specified what to do with the equal sign button. So, I took the liberty to change its appearance on smaller screens. That is accomplished by first hiding the default button using the d-none d-md-block classes and then having a wider version of it appear using the d-md-none class. That last class basically says: Hide it (display:none) from md screens and up.
Here's the working code:
<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">×</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">−</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>

Related

Grouping buttons in lines with one button spacing two rows

How can I group buttons with Bootstrap (specifically with Bootstrap 4), so that if I have several lines/rows of buttons, one of the buttons would occupy two rows at once? Pretty much like on a calculator or a keyboard's num pad, where Enter occupies two rows like:
[1] [2] [3] [E]
[ 0 ] [.] [n]
I am pretty sure there is a simple way to do it, but I can't find it.
upd. After seeing the previous attempt of answer I better add a pic
Try this:
<div class="row">
<div class="col-9">
<div class="row">
<div class="col-4">
<button class="btn btn-primary btn-block">1</button>
</div>
<div class="col-4">
<button class="btn btn-primary btn-block">2</button>
</div>
<div class="col-4">
<button class="btn btn-primary btn-block">3</button>
</div>
</div>
<div class="row mt-2">
<div class="col-8">
<button class="btn btn-primary btn-block">0</button>
</div>
<div class="col-4">
<button class="btn btn-primary btn-block">.</button>
</div>
</div>
</div>
<div class="col-3">
<button class="btn btn-primary btn-block">Enter</button>
</div>
</div>
updated

Column Offset and centering not working in Small Screens (Bootstrap)

I have a list which needs to be centered in all screen sizes. I have the following code for it -
<div class="container container-fluid.body-content">
<div class="row">
<h2 style="text-align:center;">Select your Campus</h2>
</div>
<div class="row">
<div class="col-lg-6 col-lg-offset-3 col-sm-12 col-sm-offset-0 col-md-10 col-md-offset-1 btn-group-vertical" style="background-color:#EAEBED;padding:2%;" id="dispcolleges" role="group" aria-label="...">
<button type="button" class="btn btn-default" onclick="storeValue('4847453903781888','National Institute of Technology Karnataka')">National Institute of Technology Karnataka</button>
<button type="button" class="btn btn-default" onclick="storeValue('5743114304094208','Test')">Test</button></div>
</div>
</div>
It is centered with offset in large and medium screens, but not on small screens. How do I fix this?
Full code can be found here
Try this:
<div class="container container-fluid.body-content">
<div class="row">
<h2 style="text-align:center;">Select your Campus</h2>
</div>
<div class="row">
<div class="col-lg-6 col-lg-offset-3 col-sm-offset-3 col-sm-6 col-xs-10 col-md-6 col-md-offset-6 btn-group-vertical col-xs-offset-1" style="background-color:#EAEBED;padding:2%;" id="dispcolleges" role="group" aria-label="..."><button type="button" class="btn btn-default" onclick="storeValue('4847453903781888','National Institute of Technology Karnataka')">National Institute of Technology Karnataka</button><button type="button" class="btn btn-default" onclick="storeValue('5743114304094208','Test')">Test</button></div>
</div>
</div>
The reason is display: inline-block; of class btn-group-vertical
Solution: Add display: inherit; for your <div>. Try this:
<div class="container container-fluid.body-content">
<div class="row">
<h2 style="text-align:center;">Select your Campus</h2>
</div>
<div class="row">
<div class="col-lg-6 col-lg-offset-3 col-sm-12 col-sm-offset-0 col-md-10 col-md-offset-1 btn-group-vertical" style="background-color:#EAEBED;padding:2%;display: inherit;" id="dispcolleges" role="group" aria-label="..."><button type="button" class="btn btn-default" onclick="storeValue('4847453903781888','National Institute of Technology Karnataka')">National Institute of Technology Karnataka</button><button type="button" class="btn btn-default" onclick="storeValue('5743114304094208','Test')">Test</button></div>
</div>
</div>

How to reduce the space between four buttons in bootstrap 3

i'm creating a simple menu at the top of my web page using bootstrap 3, with btn and btn-info classes in four divs (col-md-3). the space between the buttons is too high. how can i reduce the space between them ? for example 5 pixels margin. it's my code :
<div class="container">
<div class="row">
<div class="col-md-3 col-sm-12 col-xs-12">
<button class="btn btn-info">
پشتیبانی
</button>
</div>
<div class="col-md-3 col-sm-12 col-xs-12">
<button class="btn btn-info">
آموزش نصب آی او اس ۹
</button>
</div>
<div class="col-md-3 col-sm-12 col-xs-12">
<button class="btn btn-info">
آموزش به دست آوردن یودید
</button>
</div>
<div class="col-md-3 col-sm-12 col-xs-12">
<button class="btn btn-info">
خانه
</button>
</div>
</div>
</div>
you can see the web site at : ios9.siblearn.ir
A tip for the future, bootply.com is better for mocking up Bootstrap issues since it has the Bootstrap framework included by default. Here is how I would set it up:
<div class="container">
<div class="row">
<div class="col-xs-12">
<button class="btn btn-info">
پشتیبانی
</button>
<button class="btn btn-info">
آموزش نصب آی او اس ۹
</button>
<button class="btn btn-info">
آموزش به دست آوردن یودید
</button>
<button class="btn btn-info">
خانه
</button>
</div>
</div>
</div>
Bootply Example

Bootstrap's pull-right is not cleared or reset

I was working on a sample where I had to align a button group to the right.
I found the css class .pull-right.
While it correctly moved the button group, I was unable to reset the next section.
I've used the clearfix mixin on several objects, I cannot clear the way section intersects with the button group.
The sample on bootply (link below):
<div class="container foo">
<div class="btn-group pull-right" role="group">
<button type="button" class="btn btn-default">Left</button>
<button type="button" class="btn btn-default">Middle</button>
<button type="button" class="btn btn-default">Right</button>
</div>
<section class="debug text-center">
<h1>Bootstrap starter template</h1>
<p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
</section>
</div>
And the css
.foo
{
.clearfix();
}
.foo::before,
.foo::after {
content: " "; // 1
display: table; // 2
}
.foo::after {
clear: both;
}
Due to missing less mixin support I manually implemented the mixin
Here is a runnable example: http://www.bootply.com/8GgDsWc8kZ
I've been googling this and I put foo into every div there, no place cleared the section from intersecting.
Any hints appreciated.
Just add a div between the buttons and the box with the clearfix class from Bootstrap applied to it.
BOOTPLY
HTML:
<div class="container foo">
<div class="btn-group pull-right" role="group">
<button type="button" class="btn btn-default">Left</button>
<button type="button" class="btn btn-default">Middle</button>
<button type="button" class="btn btn-default">Right</button>
</div>
<div class="clearfix"></div>
<section class="debug text-center">
<h1>Bootstrap starter template</h1>
<p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
</section>
</div>
You appear to be missing some tags. That's what's causing Bootstrap to not function the way you expect.
BOOTPLY
<div class="container foo">
<div class="row">
<div class="col-md-3 pull-right">
<div class="btn-group" role="group">
<button type="button" class="btn btn-default">Left</button>
<button type="button" class="btn btn-default">Middle</button>
<button type="button" class="btn btn-default">Right</button>
</div>
</div>
</div>
<section class="debug text-center row">
<div class="col-md-12">
<h1>Bootstrap starter template</h1>
<p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
</div>
</section>
</div>
I'd suggest that you take some time to read over the grid system documentation on the Bootstrap website.
Here you go, I added a boostrap "row" and "col-xs-12" around your 3 buttons, as well as around your section.
<div class="container foo">
<div class="row">
<div class="col-xs-12">
<div class="btn-group pull-right" role="group">
<button type="button" class="btn btn-default">Left</button>
<button type="button" class="btn btn-default">Middle</button>
<button type="button" class="btn btn-default">Right</button>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<section class="debug text-center">
<h1>Bootstrap starter template</h1>
<p class="lead">Use this document as a way to quickly start any new project.<br> All you get is this text and a mostly barebones HTML document.</p>
</section>
</div>
</div>
</div>
http://www.bootply.com/0TSYSI4T8b

How to set an button align-right with Bootstrap?

I am learning Bootstrap. The structure is a container within some context. At the bottom of the container, I put a button next to an description.
Now, I want to set the button align to the right without break the bootstrap structure.
What should I do? I had set the "float:right" to the button style, but it appeared bad.
The button is outside the "alert-info" background and not vertical align. Any better method?
<div class="alert alert-info">
Summary:Its some description.......testtesttest
<button type="button" class="btn btn-primary btn-lg" style="float:right;">Large button</button>
</div>
Bootstrap 5 implementation:
The class name is now "float-end" instead of "pull-right"
<div class="alert alert-info clearfix">
<a href="#" class="alert-link">
Summary:Its some description.......testtesttest
</a>
<button type="button" class="btn btn-primary btn-lg float-end">
Large button
</button>
</div>
Bootstrap 4 (and under) implementation:
Just add a simple pull-right class to the button, and make sure the container div is clearfixed:
<div class="alert alert-info clearfix">
<a href="#" class="alert-link">
Summary:Its some description.......testtesttest
</a>
<button type="button" class="btn btn-primary btn-lg pull-right">
Large button
</button>
</div>
This worked for me:
<div class="text-right">
<button type="button">Button 1</button>
<button type="button">Button 2</button>
</div>
Try this:
<div class="row">
<div class="alert alert-info" style="min-height:100px;">
<div class="col-xs-9">
<a href="#" class="alert-link">Summary:Its some
description.......testtesttest</a>
</div>
<div class="col-xs-3">
<button type="button" class="btn btn-primary btn-lg">Large button</button>
</div>
</div>
</div>
Demo:
http://jsfiddle.net/Hx6Sx/1/
This work for me in bootstrap 4:
<div class="alert alert-info">
Summary:Its some description.......testtesttest
<button type="button" class="btn btn-primary btn-lg float-right">Large button</button>
</div>
On bootstrap 4, you can do this add pull-right to your classname:
<div class="container">
<div class="btn-block pull-right">
Search
Apple
Sony
</div>
</div>
You can use the class name float-right:
function Continue({show, onContinue}) {
return(<div className="row continue">
{ show ? <div className="col-11">
<button class="btn btn-primary btn-lg float-right" onClick= {onContinue}>Continue</button>
</div>
: null }
</div>);
}

Resources