learn bootstrap 4
I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.C:\Users\Muller\Desktop\Projects\vowel checker\index.jsC:\Users\Muller\Desktop\Projects\vowel checker\index.jsC:\Users\Muller\Desktop\Projects\vowel checker\index.jsvvvC:\Users\Muller\Desktop\Projects\vowel checker\index.js
Bootstrap 4
Bootstrap 4 Navigation Bar
Navigation Bars
A navigation bar is a navigation header that is placed at the top of the page:
Basic Navbar
With Bootstrap, a navigation bar can extend or collapse, depending on the screen size.
A standard navigation bar is created with the .navbar
class, followed by a responsive collapsing class: .navbar-expand-xl|lg|md|sm
(stacks the navbar vertically on extra large, large, medium or small screens).
To add links inside the navbar, use a <ul>
element with class="navbar-nav"
. Then add <li>
elements with a .nav-item
class followed by an <a>
element with a .nav-link
class:
Example
<!– A grey horizontal navbar that becomes vertical on small screens –>
<nav class=”navbar navbar-expand-sm bg-light”><!– Links –>
<ul class=”navbar-nav”>
<li class=”nav-item”>
<a class=”nav-link” href=”#”>Link 1</a>
</li>
<li class=”nav-item”>
<a class=”nav-link” href=”#”>Link 2</a>
</li>
<li class=”nav-item”>
<a class=”nav-link” href=”#”>Link 3</a>
</li>
</ul></nav>
Vertical Navbar
Remove the .navbar-expand-xl|lg|md|sm
class to create a vertical navigation bar:
Example
<!– A vertical navbar –>
<nav class=”navbar bg-light”><!– Links –>
<ul class=”navbar-nav”>
<li class=”nav-item”>
<a class=”nav-link” href=”#”>Link 1</a>
</li>
<li class=”nav-item”>
<a class=”nav-link” href=”#”>Link 2</a>
</li>
<li class=”nav-item”>
<a class=”nav-link” href=”#”>Link 3</a>
</li>
</ul></nav>
Centered Navbar
Add the .justify-content-center
class to center the navigation bar.
The following example will center the navigation bar on medium, large and extra large screens. On small screens it will be displayed vertically and left-aligned (because of the .navbar-expand-sm class):
Example
<nav class=”navbar navbar-expand-sm bg-light justify-content-center”>
…
</nav>
Colored Navbar
Use any of the .bg-color
classes to change the background color of the navbar (.bg-primary
, .bg-success
, .bg-info
, .bg-warning
, .bg-danger
, .bg-secondary
, .bg-dark
and .bg-light
)
Tip: Add a white text color to all links in the navbar with the .navbar-dark
class, or use the .navbar-light
class to add a black text color.
Example
<!– Grey with black text –>
<nav class=”navbar navbar-expand-sm bg-light navbar-light”>
<ul class=”navbar-nav”>
<li class=”nav-item active”>
<a class=”nav-link” href=”#”>Active</a>
</li>
<li class=”nav-item”>
<a class=”nav-link” href=”#”>Link</a>
</li>
<li class=”nav-item”>
<a class=”nav-link” href=”#”>Link</a>
</li>
<li class=”nav-item”>
<a class=”nav-link disabled” href=”#”>Disabled</a>
</li>
</ul>
</nav><!– Black with white text –>
<nav class=”navbar navbar-expand-sm bg-dark navbar-dark”>…</nav><!– Blue with white text –>
<nav class=”navbar navbar-expand-sm bg-primary navbar-dark”>…</nav>
Active/disabled state: Add the .active
class to an <a>
element to highlight the current link, or the .disabled
class to indicate that the link is un-clickable.
Brand / Logo
The .navbar-brand
class is used to highlight the brand/logo/project name of your page:
Example
<nav class=”navbar navbar-expand-sm bg-dark navbar-dark”>
<a class=”navbar-brand” href=”#”>Logo</a>
…
</nav>
When using the .navbar-brand
class on images, Bootstrap 4 will automatically style the image to fit the navbar vertically.
Example
<nav class=”navbar navbar-expand-sm bg-dark navbar-dark”>
<a class=”navbar-brand” href=”#”>
<img src=”bird.jpg” alt=”Logo” style=”width:40px;”>
</a>
…
</nav>
Collapsing The Navigation Bar
Very often, especially on small screens, you want to hide the navigation links and replace them with a button that should reveal them when clicked on.
To create a collapsible navigation bar, use a button with class="navbar-toggler", data-toggle="collapse" and data-target="#thetarget"
. Then wrap the navbar content (links, etc) inside a div element with class="collapse navbar-collapse"
, followed by an id that matches the data-target
of the button: “thetarget“.
Example
<nav class=”navbar navbar-expand-md bg-dark navbar-dark”>
<!– Brand –>
<a class=”navbar-brand” href=”#”>Navbar</a><!– Toggler/collapsibe Button –>
<button class=”navbar-toggler” type=”button” data-toggle=”collapse” data-target=”#collapsibleNavbar”>
<span class=”navbar-toggler-icon”></span>
</button><!– Navbar links –>
<div class=”collapse navbar-collapse” id=”collapsibleNavbar”>
<ul class=”navbar-nav”>
<li class=”nav-item”>
<a class=”nav-link” href=”#”>Link</a>
</li>
<li class=”nav-item”>
<a class=”nav-link” href=”#”>Link</a>
</li>
<li class=”nav-item”>
<a class=”nav-link” href=”#”>Link</a>
</li>
</ul>
</div>
</nav>
Tip: You can also remove the .navbar-expand-md class to ALWAYS hide navbar links and display the toggler button.
Navbar With Dropdown
Navbars can also hold dropdown menus:
Example
<nav class=”navbar navbar-expand-sm bg-dark navbar-dark”>
<!– Brand –>
<a class=”navbar-brand” href=”#”>Logo</a><!– Links –>
<ul class=”navbar-nav”>
<li class=”nav-item”>
<a class=”nav-link” href=”#”>Link 1</a>
</li>
<li class=”nav-item”>
<a class=”nav-link” href=”#”>Link 2</a>
</li><!– Dropdown –>
<li class=”nav-item dropdown”>
<a class=”nav-link dropdown-toggle” href=”#” id=”navbardrop” data-toggle=”dropdown”>
Dropdown link
</a>
<div class=”dropdown-menu”>
<a class=”dropdown-item” href=”#”>Link 1</a>
<a class=”dropdown-item” href=”#”>Link 2</a>
<a class=”dropdown-item” href=”#”>Link 3</a>
</div>
</li>
</ul>
</nav>
Navbar Forms and Buttons
Add a <form>
element with class="form-inline"
to group inputs and buttons side-by-side:
Example
<nav class=”navbar navbar-expand-sm bg-dark navbar-dark”>
<form class=”form-inline” action=”/action_page.php”>
<input class=”form-control mr-sm-2″ type=”text” placeholder=”Search”>
<button class=”btn btn-success” type=”submit”>Search</button>
</form>
</nav>
You can also use other input classes, such as .input-group-prepend
or .input-group-append
to attach an icon or help text next to the input field. You will learn more about these classes in the Bootstrap Inputs chapter.
Example
<nav class=”navbar navbar-expand-sm bg-dark navbar-dark”>
<form class=”form-inline” action=”/action_page.php”>
<div class=”input-group”>
<div class=”input-group-prepend”>
<span class=”input-group-text”>@</span>
</div>
<input type=”text” class=”form-control” placeholder=”Username”>
</div>
</form>
</nav>
Navbar Text
Use the .navbar-text
class to vertical align any elements inside the navbar that are not links (ensures proper padding and text color).
Example
<nav class=”navbar navbar-expand-sm bg-dark navbar-dark”>
<!– Links –>
<ul class=”navbar-nav”>
<li class=”nav-item”>
<a class=”nav-link” href=”#”>Link 1</a>
</li>
<li class=”nav-item”>
<a class=”nav-link” href=”#”>Link 2</a>
</li>
</ul>
<!– Navbar text–>
<span class=”navbar-text”>
Navbar text
</span>
</nav>
Fixed Navigation Bar
The navigation bar can also be fixed at the top or at the bottom of the page.
A fixed navigation bar stays visible in a fixed position (top or bottom) independent of the page scroll.
The .fixed-top
class makes the navigation bar fixed at the top:
Example
<nav class=”navbar navbar-expand-sm bg-dark navbar-dark fixed-top”>
…
</nav>
Use the .fixed-bottom
class to make the navbar stay at the bottom of the page:
Example
<nav class=”navbar navbar-expand-sm bg-dark navbar-dark fixed-bottom”>
…
</nav>
Use the .sticky-top
class to make the navbar fixed/stay at the top of the page when you scroll past it. Note: This class does not work in IE11 and earlier (will treat it as position:relative
).
Example
<nav class=”navbar navbar-expand-sm bg-dark navbar-dark sticky-top”>
…
</nav>
Bootstrap 4 Grid System
Bootstrap’s grid system is built with flexbox and allows up to 12 columns across the page.
If you do not want to use all 12 columns individually, you can group the columns together to create wider columns:
span 1 | span 1 | span 1 | span 1 | span 1 | span 1 | span 1 | span 1 | span 1 | span 1 | span 1 | span 1 |
span 4 | span 4 | span 4 | |||||||||
span 4 | span 8 | ||||||||||
span 6 | span 6 | ||||||||||
span 12 |
The grid system is responsive, and the columns will re-arrange automatically depending on the screen size.
Make sure that the sum adds up to 12 or fewer (it is not required that you use all 12 available columns).
Grid Classes
The Bootstrap 4 grid system has five classes:
.col-
(extra small devices – screen width less than 576px).col-sm-
(small devices – screen width equal to or greater than 576px).col-md-
(medium devices – screen width equal to or greater than 768px).col-lg-
(large devices – screen width equal to or greater than 992px).col-xl-
(xlarge devices – screen width equal to or greater than 1200px)
The classes above can be combined to create more dynamic and flexible layouts.
Tip: Each class scales up, so if you wish to set the same widths for sm
and md
, you only need to specify sm
.
Basic Structure of a Bootstrap 4 Grid
The following is a basic structure of a Bootstrap 4 grid:
<!– Control the column width, and how they should appear on different devices –>
<div class=”row”>
<div class=”col-*-*”></div>
<div class=”col-*-*”></div>
</div>
<div class=”row”>
<div class=”col-*-*”></div>
<div class=”col-*-*”></div>
<div class=”col-*-*”></div>
</div><!– Or let Bootstrap automatically handle the layout –>
<div class=”row”>
<div class=”col”></div>
<div class=”col”></div>
<div class=”col”></div>
</div>
First example: create a row (<div class="row">
). Then, add the desired number of columns (tags with appropriate .col-*-*
classes). The first star (*) represents the responsiveness: sm, md, lg or xl, while the second star represents a number, which should add up to 12 for each row.
Second example: instead of adding a number to each col
, let bootstrap handle the layout, to create equal width columns: two "col"
elements = 50% width to each col. three cols = 33.33% width to each col. four cols = 25% width, etc. You can also use .col-sm|md|lg|xl
to make the columns responsive.
Below we have collected some examples of basic Bootstrap 4 grid layouts.
Three Equal Columns
The following example shows how to create three equal-width columns, on all devices and screen widths:
Example
<div class=”row”>
<div class=”col”>.col</div>
<div class=”col”>.col</div>
<div class=”col”>.col</div>
</div>
Responsive Columns
The following example shows how to create four equal-width columns starting at tablets and scaling to extra large desktops. On mobile phones or screens that are less than 576px wide, the columns will automatically stack on top of each other:
Example
<div class=”row”>
<div class=”col-sm-3″>.col-sm-3</div>
<div class=”col-sm-3″>.col-sm-3</div>
<div class=”col-sm-3″>.col-sm-3</div>
<div class=”col-sm-3″>.col-sm-3</div>
</div>
Two Unequal Responsive Columns
The following example shows how to get two various-width columns starting at tablets and scaling to large extra desktops:
Example
<div class=”row”>
<div class=”col-sm-4″>.col-sm-4</div>
<div class=”col-sm-8″>.col-sm-8</div>
</div>
Containers
You learned from the previous chapter that Bootstrap requires a containing element to wrap site contents.
Containers are used to pad the content inside of them, and there are two container classes available:
- The
.container
class provides a responsive fixed width container - The
.container-fluid
class provides a full width container, spanning the entire width of the viewport
Fixed Container
Use the .container
class to create a responsive, fixed-width container.
Note that its width (max-width
) will change on different screen sizes:
Extra small <576px |
Small ≥576px |
Medium ≥768px |
Large ≥992px |
Extra large ≥1200px |
|
---|---|---|---|---|---|
max-width | 100% | 540px | 720px | 960px | 1140px |
Open the example below and resize the browser window to see that the container width will change at different breakpoints:
Example
<div class=”container”>
<h1>My First Bootstrap Page</h1>
<p>This is some text.</p>
</div>
Fluid Container
Use the .container-fluid
class to create a full width container, that will always span the entire width of the screen (width
is always 100%
):
Example
<div class=”container-fluid”>
<h1>My First Bootstrap Page</h1>
<p>This is some text.</p>
</div>
Container Padding
By default, containers have 15px left and right padding, with no top or bottom padding. Therefore, we often use spacing utilities, such as extra padding and margins to make them look even better. For example, .pt-3
means “add a top padding of 16px”: