Bootstrap - Avoid small controls wrapping in mobile view - html

Consider the following Angular HTML template section:
<div class="row" [formGroup]="saveForm">
<label for="sections" class="col-md-3 col-form-label">Sections:</label>
<div class="col-md-9">
<a class="add-link" (click)="addSection()">Add Section</a>
<div class="mt-2 mb-2" formArrayName="sections">
<div *ngFor="let section of saveForm.get('sections')['controls']; let i=index" [formGroupName]="i">
<div class="row mb-2">
<div class="col-md-3">
<label for="from">From:</label>
</div>
<div class="col-md-2">
<input class="form-control" type="text" formControlName="from">
</div>
<div class="col-md-2">
<label for="to">To:</label>
</div>
<div class="col-md-2 mb-2">
<input class="form-control" type="text" formControlName="to">
</div>
<div class="col-md-1 custom-icon">
<i class="fas fa-minus-circle fa-lg clickable" (click)="deleteRow(i)"></i>
</div>
</div>
</div>
</div>
</div>
</div>
This renders like so:
Now if I go to the mobile view in the browser it looks like this:
Both inputs will be limited to no more than 4 digits so I do not need them to be that large. Ideally I would like to have the two labels with their inputs on the same line with the delete icon below them (To be honest I'm not sure what to do with it UX wise). If that is not possible at least to have each label with its input on the same line.
I think something like the following would be best:
How can I accomplish this without breaking the full desktop view?
I am using Bootstrap 4.3.1
Thank you.

This is what you need for under the md sized devices:
divide the rows into col-4 for label
divide the rows into col-6 for input (or smaller)
style the icon via position:absolute to be on the right
Working snippet below:
#media screen AND (max-width:768px) {
.custom-icon {
position: absolute;
right: 20px;
top: -10px
}
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class='container-fluid'>
<div class="row" [formGroup]="saveForm">
<label for="sections" class="col-md-3 col-form-label">Sections:</label>
<div class="col-md-9">
<a class="add-link" (click)="addSection()">Add Section</a>
<div class="mt-2 mb-2" formArrayName="sections">
<div *ngFor="let section of saveForm.get('sections')['controls']; let i=index" [formGroupName]="i">
<div class="row mb-2">
<div class="col-4 col-md-3">
<label for="from">From:</label>
</div>
<div class="col-6 col-md-2">
<input class="form-control" type="text" formControlName="from">
</div>
<div class="col-4 col-md-2">
<label for="to">To:</label>
</div>
<div class="col-6 col-md-2 mb-2">
<input class="form-control" type="text" formControlName="to">
</div>
<div class="col-1 col-md-1 custom-icon">
<i class="fa fa-minus-circle fa-lg clickable" (click)="deleteRow(i)"></i>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

you can achieve this without any custom stylesheet. its all about bootstrap classes.
I just wrapped inputs and labels in separate div and remove icon in separate div.
Please have a look at the below snippet.
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css" integrity="sha256-46qynGAkLSFpVbEBog43gvNhfrOj+BmwXdxFgVK/Kvc=" crossorigin="anonymous" />
<div class="row" [formGroup]="saveForm">
<label for="sections" class="col-md-3 col-form-label">Sections:</label>
<div class="col-md-9">
<a class="add-link" (click)="addSection()">Add Section</a>
<div class="mt-2 mb-2" formArrayName="sections">
<div *ngFor="let section of saveForm.get('sections')['controls']; let i=index" [formGroupName]="i">
<div class="row mb-2 mb-sm-0">
<div class="col-10">
<div class="row">
<div class="col-6 col-sm-3 mb-2 mb-sm-0">
<label for="from">From:</label>
</div>
<div class="col-6 col-sm-3 mb-2 mb-sm-0">
<input class="form-control" type="text" formControlName="from">
</div>
<div class="col-6 col-sm-3">
<label for="to">To:</label>
</div>
<div class="col-6 col-sm-3">
<input class="form-control" type="text" formControlName="to">
</div>
</div>
</div>
<div class="col-2 d-flex align-items-center">
<div class=" custom-icon">
<i class="fas fa-minus-circle fa-lg clickable text-danger" (click)="deleteRow(i)"></i>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

Related

Issue with bootstrap columns while nesting inside columns

The layout of page is miss-placed when columns are nested inside another column
<div class="container">
<div class="row">
<div class="col-6">
<div class="col-6">
<label>Name</label>
<input type="text" class="form-control" />
</div>
<div class="col-6">
<label>Name</label>
<input type="text" class="form-control" />
</div>
</div>
<div class="col-6"></div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-6">
<div class="row">
<div class="col-6">
<label>Name</label>
<input type="text" class="form-control" />
</div>
<div class="col-6">
<label>Name</label>
<input type="text" class="form-control" />
</div>
</div>
</div>
<div class="col-6"></div>
</div>
</div>
An advanced way:
<div class="container bg-primary" style="width: 800px; height: 100px; ">
<div class="row ml-1 h-100">
<div class="col-10 ml-0 my-auto h-75 container bg-danger">
<div class="row ml-1 h-100">
<div class="col-8 ml-0 my-auto h-75 container bg-warning">
<div class="row ml-1 h-100">
<div class="col-6 ml-0 my-auto h-75 container bg-success">
<div class="row ml-1 h-100">
<div class="col-4 ml-0 my-auto h-75 container bg-dark">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

In Bootstrap, how do I control the height of my badge?

I have a JSFiddle here:
https://jsfiddle.net/bjfm703h/
This code:
<div class="col-1 spacer"></div>
<div class="input-group input-group-alt flatpickr" id="flatpickr9" data-toggle="flatpickr" data-wrap="true">
<input id="key-date-datepicker" type="text" class="form-control kd-datepicker" data-input="" v-model="date">
<span> </span>
<span class="badge badge-danger">Danger</span>
</div>
renders as follows:
with the height of the badge being the same height as my input. Instead, I would like the height of the badge to be the same height as 'Danger' similar to:
https://getbootstrap.com/docs/4.3/components/badge/
How can I do this?
The input-group css class gives elements a flex layout with align-items: stretch property.
You can change the default property or add an additional class to align-items.
Possible solution:
.input-group{
align-items: center !important;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<div class="input-group input-group-alt flatpickr" id="flatpickr9" data-toggle="flatpickr" data-wrap="true">
<input id="key-date-datepicker" type="text" class="form-control kd-datepicker" data-input="" v-model="date">
<span> </span>
<span class="badge badge-danger">Danger</span>
</div>
I have copied and modified your JSFiddle code. Try this:
.row {
background: #f8f9fa;
margin-top: 20px;
}
.col {
border: solid 1px #6c757d;
padding: 10px;
}
#bdg{
padding-top: 12px;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<div role="document" class="modal-dialog modal-lg modal-dialog-overflow">
<div class="modal-content pl-3 pr-3">
<div class="modal-body">
<div class="modal-body-scrolled border-primary border-bottom p-0 mt-2">
<div class="form-row">
<div class="col-12">
<div class="row ml-0 mr-0">
<div class="row form-inline col-12 mt-2 mb-2 pt-2 pb-2 m-0">
<div class="row col-12 pl-0 pr-0 mb-2">
<label for="duration" class="col-1 pl-0 justify-content-start align-items-start">Label</label>
<div class="col-1 spacer"></div>
<div class="input-group input-group-alt flatpickr" id="flatpickr9" data-toggle="flatpickr" data-wrap="true">
<input id="key-date-datepicker" type="text" class="form-control kd-datepicker" data-input="" v-model="date">
<span> </span>
<span id="bdg" class="badge badge-danger">Danger</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Edit: (Solution number 2)
Simply use btn and btn-danger class of bootstrap.
.row {
background: #f8f9fa;
margin-top: 20px;
}
.col {
border: solid 1px #6c757d;
padding: 10px;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<div role="document" class="modal-dialog modal-lg modal-dialog-overflow">
<div class="modal-content pl-3 pr-3">
<div class="modal-body">
<div class="modal-body-scrolled border-primary border-bottom p-0 mt-2">
<div class="form-row">
<div class="col-12">
<div class="row ml-0 mr-0">
<div class="row form-inline col-12 mt-2 mb-2 pt-2 pb-2 m-0">
<div class="row col-12 pl-0 pr-0 mb-2">
<label for="duration" class="col-1 pl-0 justify-content-start align-items-start">Label</label>
<div class="col-1 spacer"></div>
<div class="input-group input-group-alt flatpickr" id="flatpickr9" data-toggle="flatpickr" data-wrap="true">
<input id="key-date-datepicker" type="text" class="form-control kd-datepicker" data-input="" v-model="date">
<span> </span>
<span class="btn btn-danger">Danger</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Simple change the badge class into a btn class.
Using the btn class does not mean your item has to be a button, it just gives it some of the properties.
<span class="btn badge-danger">Danger</span>

Bootstrap element not hidden

I wanna hidden search box in Small Devices(sm) size and instead of that a search button should take place. i wanna my search be like this at sm size.
enter image description here
this is my code:
<div class="container">
<div class="header">
<div class="row">
<div class="col-md-3 ">
<img src="img/1.png" class="header-logo" alt="site-logo">
</div>
<div class="col-md-6">
<div class="input-group search-box">
<div class="input-group-prepend">
<span class="input-group-text">
<i class="fas fa-search"></i>
</span>
</div>
<input type="text" class="form-control" placeholder="جستجو">
</div>
</div>
<div class="col-md-3 btn-header">
<button class="btn">ورود</button>
<button class="btn">ثبت نام</button>
</div>
</div>
</div><!-- =====HEADER DIV===== -->
</div>
Use
hidden-sm
for more details follow https://getbootstrap.com/docs/3.3/css/
<div class="input-group search-box">
<div class="input-group-prepend">
<span class="input-group-text">
<i class="fas fa-search"></i>
</span>
</div>
<!-- your search field is hidden in the sm -->
<input type="text" class="form-control hidden-sm" placeholder="جستجو">
</div>
Devices list
Extra small devices - Phones (<768px)
Small devices - Tablets (≥768px)
Medium devices -Desktops (≥992px)
Large devices -Desktops (≥1200px)
To show or hide elements with bootstrap you have to use the display utilities. For example, if you want to show only on Small Devices you must use d-none d-block d-sm-none.
Your code would be like this:
<div class="container">
<div class="header">
<div class="row">
<div class="col-md-3 ">
<img src="img/1.png" class="header-logo" alt="site-logo">
</div>
<div class="col-md-6 d-none d-md-block">
<div class="input-group search-box">
<div class="input-group-prepend">
<span class="input-group-text">
<i class="fas fa-search"></i>
</span>
</div>
<input type="text" class="form-control" placeholder="جستجو">
</div>
</div>
<div class="col-md-3 btn-header d-sm-none">
<button class="btn">ورود</button>
<button class="btn">ثبت نام</button>
</div>
</div>
</div><!-- =====HEADER DIV===== -->
</div>
You can find the doc here: https://getbootstrap.com/docs/4.0/utilities/display/

Align divs to right margin while keeping checkboxes vertically aligned

I'm using Bootstrap 4 to style a form in HTML+CSS.
I have six checkboxes with label that on certain media sizes should be display as two rows of three columns. Checkboxes on each column should be aligned. Also, the longest text on any row from the right column, should be right-aligned with the rest of the form. So far, I can only manage one of those things. So, how do I go from this:
Or this:
To this (image edited):
Below is the HTML code I'm using for the first example, with the checkboxes aligned (it's embedded within other divs but they should not have any role in this issue).
FYI, I'm adding a bit of styling with the following custom styles (included in survey01.css):
.form-inline>div>label {
white-space: nowrap;
}
#media (max-width: 767px) {
#nature-label {
margin-bottom: 0;
}
}
#media (max-width: 1199px) {
#nature-label {
margin-bottom: 0;
}
}
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- IE displays content in highest supported mode -->
<title>Do you know what proteins are?</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- width as in viewport; zoom=100% -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" media="screen" href="survey01.css" />
<!-- optimize for screens -->
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-12">
<h1 id="title" class="display-4 text-center">Test title</h1>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<p id="description" class="text-center">Test subtitle.</p>
</div>
</div>
<div class="jumbotron">
<form id="survey-form">
<div class="form-group row m-0">
<div class="container-fluid p-0">
<div class="form-row">
<div class="form-group form-group-md col-md-5 col-lg-6 mb-2">
<!-- #media modifies mb-2 -->
<div class="row m-0">
<label for="nature" id="nature-label" class="pr-2 pl-0">Do you think proteins are a natural or synthetic product?</label>
</div>
</div>
<div class="form-inline justify-content-between align-items-start col-md-7 col-lg-6 mb-3">
<div class="d-flex justify-content-around align-items-start col-12 col-sm-auto pr-0">
<label for="nature1">
<input type="radio" id="nature1" name="nature" value="natural" class="mr-1">Natural
</label>
</div>
<div class="d-flex justify-content-around align-items-start col-12 col-sm-auto pr-0">
<label for="nature2">
<input type="radio" id="nature2" name="nature" value="synthetic" class="mr-1">Synthetic
</label>
</div>
<div class="d-flex justify-content-around align-items-start col-12 col-sm-auto pr-0">
<label for="nature3">
<input type="radio" id="nature3" name="nature" value="both" class="mr-1">Both
</label>
</div>
<div class="d-flex justify-content-around align-items-start col-12 col-sm-auto pr-0">
<label for="nature4">
<input type="radio" id="nature4" name="nature" value="dontknow" class="mr-1">Don't know
</label>
</div>
</div>
</div>
</div>
</div>
<div class="form-group row m-0">
<div class="container-fluid p-0">
<div class="form-row">
<div class="form-group form-group-md col-md-4 col-lg-6 col-xl-4 mb-2">
<!-- #media modifies mb-2 -->
<div class="row m-0">
<label for="identify" id="identify-label" class="pr-2 pl-0">Which of the following qualify as 'proteins'?</label>
</div>
</div>
<div class="form-inline justify-content-between align-items-start col-md-8 col-lg-6 col-xl-8 mb-3">
<div class="d-flex justify-content-center justify-content-sm-start align-items-start col-12 col-sm-4 col-xl-auto pr-0">
<label for="identify1">
<input type="checkbox" id="identify1" name="identify" value="meat" class="mr-1">Meat
</label>
</div>
<div class="d-flex justify-content-center justify-content-sm-start align-items-start col-12 col-sm-4 col-xl-auto pr-0">
<label for="identify2">
<input type="checkbox" id="identify2" name="identify" value="arginine" class="mr-1">Arginine
</label>
</div>
<div class="d-flex justify-content-center justify-content-sm-start align-items-start col-12 col-sm-4 col-xl-auto pr-0">
<label for="identify3">
<input type="checkbox" id="identify3" name="identify" value="haemoglobin" class="mr-1">Hæmoglobin
</label>
</div>
<div class="d-flex justify-content-center justify-content-sm-start align-items-start col-12 col-sm-4 col-xl-auto pr-0">
<label for="identify4">
<input type="checkbox" id="identify4" name="identify" value="bar" class="mr-1">Protein bar
</label>
</div>
<div class="d-flex justify-content-center justify-content-sm-start align-items-start col-12 col-sm-4 col-xl-auto pr-0">
<label for="identify5">
<input type="checkbox" id="identify5" name="identify" value="egg" class="mr-1">Egg
</label>
</div>
<div class="d-flex justify-content-center justify-content-sm-start align-items-end col-12 col-sm-4 col-xl-auto pr-0">
<label for="identify6">
<input type="checkbox" id="identify6" name="identify" value="albumin" class="mr-1">Albumin
</label>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</body>

Tabs not aligning properly when window is resized smaller

I'm trying to make my tabs look like this (picture 1) at all sizes. However, when I resize my tabs they look like this (picture 2). How can I fix it?
Here's my fiddle:
https://jsfiddle.net/whywymam/86zgeLf1/
My HTML code:
<div class="Container">
<div class="row">
<div class="panel panel-center">
<div class="panel-header">
<h1>Job Category</h1>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-4 col-md-4 col-lg-4"> <a href="#" class="thumbnail">
<img class="logo" src="image/bankinglogo.png" width="180px" >
<p class="bankingl"> Banking and Finance </p>
</a>
</div>
<div class="col-sm-4 col-md-4 col-lg-4"> <a href="#" class="thumbnail">
<img class="logo" src="image/engineerlogo.png" width="180px" >
<p class="bankingl"> Banking and Finance </p>
</a>
</div>
<div class="col-sm-4 col-md-4 col-lg-4"> <a href="#" class="thumbnail">
<img class="logo" src="image/healthcare.png" width="180px" >
<p class="bankingl">Healthcare</p>
</a>
</div>
<div class="clearfix visible-sm-block visible-md-block visible-lg-block"></div>
</div>
<div class="row">
<div class="col-sm-4 col-md-4 col-lg-4"> <a href="#" class="thumbnail">
<img class="logo" src="image/technicia.png" width="185px" >
<p class="bankingl">IT</p>
</a>
</div>
<div class="col-sm-4 col-md-4 col-lg-4"> <a href="#" class="thumbnail">
<img class="logo" src="image/oilandgas.png" width="200px" >
<p class="bankingl">Oil and Gas</p>
</a>
</div>
<div class="col-sm-4 col-md-4 col-lg-4"> <a href="#" class="thumbnail">
<img class="logo" src="image/retail.png" width="180px" >
<p class="bankingl">Retail and Hospitality</p>
</a>
</div>
<div class="clearfix visible-sm-block visible-md-block visible-lg-block"></div>
</div>
<div class="panel panel-default jobPanel center-block">
<div class="row">
<div class="col-sm-6 col-sm-offset-3 col-md-6 col-md-offset-3 col-lg-6 col-lg-offset-3">
<div class="panel-body">
<div class="tabbable">
<ul class="nav nav-tabs nav-justified selected">
<li class="active">Job Search
</li>
<li>PYW
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="tab1">
<form class="form-horizontal">
<div class="form-group name-group">
<label for="keyword" class="col-sm-3 control-label">Keyword</label>
<div class="col-sm-9">
<input type="text" class="form-control" placeholder="Enter keyword" />
</div>
</div>
<div class="form-group">
<label for="location" class="col-sm-3 control-label">Location</label>
<div class="col-sm-9">
<input type="text" class="form-control" placeholder="Enter location" />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9"> Search
</div>
</div>
</form>
</div>
<!--end of tab 1-->
<div class="tab-pane" id="tab2">
<form class="form-horizontal">
<div class="form-group name-group">
<label for="keyword" class="col-sm-3 control-label">Keyword</label>
<div class="col-sm-9">
<input type="text" class="form-control" placeholder="Enter keyword" />
</div>
</div>
<div class="form-group">
<label for="location" class="col-sm-3 control-label">Location</label>
<div class="col-sm-9">
<input type="text" class="form-control" placeholder="Enter location" />
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9"> Search
</div>
</div>
</form>
</div>
<!--end of tab 2-->
</div>
<!--end of tab content-->
</div>
<!-- end of tabbable-->
</div>
<!--end panel body-->
</div>
<!-- end of formTab -->
</div>
<!--end of panel container-->
<script src="bootstrap/js/jquery-1.11.1.js"></script>
<script src="bootstrap/js/bootstrap.js"></script>
</div>
Bootstrap takes a mobile-first approach, the code you have provided doesn't show the sign-in and sign-up tabs or fields.
But looking at your HTML markup, I can clearly tell you one thing you're missing.
The classes .col-sm-* , col-md-* , col-lg-*, since Bootstrap is mobile-first the class col-xs-* , is the class that you probably want to be adding to your HTML markup.
So I guess if your markup looks something like :
<div class="container">
<div class="row">
<div class="col-sm-6">
</div>
<div class="col-sm-6">
</div>
</div>
</div>
You got to add the col-xs-* clas as well so that at smaller widths (mobile and small device screens), the columns are maintained rather than tiled one after another.
<div class="container">
<div class="row">
<div class="col-xs-6 col-md-6">
</div>
<div class="col-xs-6 col-md-6">
</div>
</div>
</div>
Further reading: Subtle magic of how the Bootstrap Grid works.