Chapter 7 Bootstrap & Jquery
Chapter 7 Bootstrap & Jquery
Chapter 7 Bootstrap & Jquery
Bootstrap is a free and open-source CSS framework directed at responsive, mobile-first front-end
web development. It contains CSS- and (optionally) JavaScript-based design templates for typography,
forms, buttons, navigation, and other interface components.
Bootstrap is a free and open-source CSS framework directed at responsive, mobile-first front-end web
development. It contains CSS- and (optionally) JavaScript-based design templates
for typography, forms, buttons, navigation, and other interface components.
As of August 2021, Bootstrap is the tenth most starred project on GitHub, with over 152,000 stars,
behind freeCodeCamp (over 328,000 stars), Vue.js framework, React library, TensorFlow and
Advantages of Bootstrap:
Easy to use: Anybody with just basic knowledge of HTML and CSS can start using
Bootstrap
Responsive features: Bootstrap's responsive CSS adjusts to phones, tablets, and
desktops
Mobile-first approach: In Bootstrap 3, mobile-first styles are part of the core framework
Browser compatibility: Bootstrap is compatible with all modern browsers (Chrome,
Firefox, Internet Explorer, Edge, Safari, and Opera)
Bootstrap uses HTML elements and CSS properties that require the HTML5 doctype.
Always include the HTML5 doctype at the beginning of the page, along with the lang
attribute and the correct character set:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
</html>
Chapter_7 Web Programming GTC 2014 E.C
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-
beta.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-
beta.2/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>Alerts</h2>
<div class="alert alert-success">
<strong>Success!</strong> Used to indicate successful or positive action.
</div>
<div class="alert alert-info">
<strong>Info!</strong> Used to indicate a neutral informative change or action.
</div>
<div class="alert alert-warning">
<strong>Warning!</strong> Used to indicate a warning that might need attention.
</div>
<div class="alert alert-danger">
<strong>Danger!</strong> Used to indicate a dangerous or potentially negative action.
</div>
<div class="alert alert-primary">
<strong>Primary!</strong> Used to indicate an important action.
</div>
<div class="alert alert-secondary">
<strong>Secondary!</strong> Used to indicate a slightly less important action.
</div>
<div class="alert alert-dark">
<strong>Dark!</strong> Dark grey alert.
</div>
<div class="alert alert-light">
<strong>Light!</strong> Light grey alert.
</div>
</div>
</body>
</html>
Chapter_7 Web Programming GTC 2014 E.C
Buttons
Use Bootstrap’s custom button styles for actions in forms, dialogs, and more with support for
multiple sizes, states, and more.
Examples
Bootstrap includes several predefined button styles, each serving its own semantic
purpose, with a few extras thrown in for more control.
The following example shows the code for the different button styles:
The following example shows the code for the different button styles:
Button Sizes
Bootstrap provides four button sizes:Large Normal Small XSmall
The classes that define the different sizes are:
.btn-lg
.btn-sm
.btn-xs
The following example shows the code for different button sizes:
Example
<button type="button" class="btn btn-primary btn-lg">Large</button>
<button type="button" class="btn btn-primary">Normal</button>
<button type="button" class="btn btn-primary btn-sm">Small</button>
<button type="button" class="btn btn-primary btn-xs">XSmall</button>
Alerts
Provide contextual feedback messages for typical user actions with the handful of available and
flexible alert messages.
Examples
Chapter_7 Web Programming GTC 2014 E.C
Alerts are available for any length of text, as well as an optional close button. For proper styling,
use one of the eight required contextual classes (e.g., .alert-success). For inline dismissal, use
the alerts JavaScript plugin.
Text
Documentation and examples for common text utilities to control alignment, wrapping, weight,
and more.
Text alignment
Easily realign text to components with text alignment classes. For start, end, and center
alignment, responsive classes are available that use the same viewport width breakpoints as the
grid system.
Word break
Prevent long strings of text from breaking your components' layout by using .text-break to
set word-wrap: break-word and word-break: break-word. We use word-wrap instead of the more
common overflow-wrap for wider browser support, and add the deprecated word-break: break-
word to avoid issues with flex containers.
mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
Copy
<p class="text-
break">mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm</p>
Note that breaking words isn’t possible in Arabic, which is the most used RTL language. Therefore .text-
break is removed from our RTL compiled CSS.
Text transform
Transform text in components with text capitalization classes.
Font size
Quickly change the font-size of text. While our heading classes (e.g., .h1–.h6) apply font-size, font-
weight, and line-height, these utilities only apply font-size. Sizing for these utilities matches
HTML’s heading elements, so as the number increases, their size decreases.
Quickly change the font-weight or font-style of text with these utilities. font-style utilities are
abbreviated as .fst-* and font-weight utilities are abbreviated as .fw-*.
Line height
Change the line height with .lh-* utilities.
This is a long paragraph written to show how the line-height of an element is affected by our
utilities. Classes are applied to the element itself or sometimes the parent element. These classes
can be customized as needed with our utility API.
This is a long paragraph written to show how the line-height of an element is affected by our
utilities. Classes are applied to the element itself or sometimes the parent element. These classes
can be customized as needed with our utility API.
This is a long paragraph written to show how the line-height of an element is affected by our
utilities. Classes are applied to the element itself or sometimes the parent element. These classes
can be customized as needed with our utility API.
This is a long paragraph written to show how the line-height of an element is affected by our
utilities. Classes are applied to the element itself or sometimes the parent element. These classes
can be customized as needed with our utility API.
<p class="lh-1">This is a long paragraph written to show how the line-height of an element is
affected by our utilities. Classes are applied to the element itself or sometimes the parent
element. These classes can be customized as needed with our utility API.</p>
<p class="lh-sm">This is a long paragraph written to show how the line-height of an element is
affected by our utilities. Classes are applied to the element itself or sometimes the parent
element. These classes can be customized as needed with our utility API.</p>
<p class="lh-base">This is a long paragraph written to show how the line-height of an element is
affected by our utilities. Classes are applied to the element itself or sometimes the parent
element. These classes can be customized as needed with our utility API.</p>
<p class="lh-lg">This is a long paragraph written to show how the line-height of an element is
affected by our utilities. Classes are applied to the element itself or sometimes the parent
element. These classes can be customized as needed with our utility API.</p>
Monospace
Chapter_7 Web Programming GTC 2014 E.C
This is in monospace
Copy
Reset color
Reset a text or link’s color with .text-reset, so that it inherits the color from its parent.
<p class="text-muted">
Muted text with a <a href="#" class="text-reset">reset link</a>.
</p>
Text decoration
Colors
Colorize text with color utilities. If you want to colorize links, you can use the .link-* helper
classes which have :hover and :focus states.
<p class="text-primary">.text-primary</p>
<p class="text-secondary">.text-secondary</p>
<p class="text-success">.text-success</p>
<p class="text-danger">.text-danger</p>
<p class="text-warning bg-dark">.text-warning</p>
<p class="text-info bg-dark">.text-info</p>
<p class="text-light bg-dark">.text-light</p>
<p class="text-dark">.text-dark</p>
<p class="text-body">.text-body</p>
<p class="text-muted">.text-muted</p>
<p class="text-white bg-dark">.text-white</p>
<p class="text-black-50">.text-black-50</p>
Chapter_7 Web Programming GTC 2014 E.C
Background color
Similar to the contextual text color classes, set the background of an element to any contextual
class. Background utilities do not set color,
Chapter_7 Web Programming GTC 2014 E.C
Background gradient
By adding a .bg-gradient class, a linear gradient is added as background image to the backgrounds.
This gradient starts with a semi-transparent white which fades out to the bottom.
Chapter_7 Web Programming GTC 2014 E.C
Tables
Documentation and examples for opt-in styling of tables (given their prevalent use in JavaScript
plugins) with Bootstrap.
Overview
Due to the widespread use of <table> elements across third-party widgets like calendars and date
pickers, Bootstrap’s tables are opt-in. Add the base class .table to any <table>, then extend with
our optional modifier classes or custom styles. All table styles are not inherited in Bootstrap,
meaning any nested tables can be styled independent from the parent.
Using the most basic table markup, here’s how .table-based tables look in Bootstrap.
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
Chapter_7 Web Programming GTC 2014 E.C
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td colspan="2">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
Variants
<tr class="table-secondary">...</tr>
<tr class="table-success">...</tr>
<tr class="table-danger">...</tr>
<tr class="table-warning">...</tr>
<tr class="table-info">...</tr>
<tr class="table-light">...</tr>
<tr class="table-dark">...</tr>
Accented tables
Striped rows
Use .table-striped to add zebra-striping to any table row within the <tbody>.
Hoverable rows
Add .table-hover to enable a hover state on table rows within a <tbody>.
Copy
<table class="table table-hover">
...
</table>
Copy
Active tables
Highlight a table row or cell by adding a .table-active class.
<table class="table">
<thead>
...
</thead>
<tbody>
<tr class="table-active">
...
</tr>
<tr>
...
</tr>
<tr>
<th scope="row">3</th>
<td colspan="2" class="table-active">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
</tr>
<tr>
...
</tr>
<tr>
<th scope="row">3</th>
<td colspan="2" class="table-active">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table
What is jQuery?
jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML
document traversal and manipulation, event handling, animation, and Ajax much simpler with
an easy-to-use. With a combination of versatility and extensibility, jQuery has changed the way
that millions of people write JavaScript.
HTML/DOM manipulation
CSS manipulation
HTML event methods
Effects and animations
AJAX
Utilities
The jQuery team knows all about cross-browser issues, and they have written this knowledge into the
jQuery library. jQuery will run exactly the same in all major browsers.
There are several ways to start using jQuery on your web site. You can:
The jQuery library is a single JavaScript file, and you reference it with the HTML <script> tag
(notice that the <script> tag should be inside the <head> section):
<head>
<script src="jquery-3.5.1.min.js"></script>
</head>
Tip: Place the downloaded file in the same directory as the pages where you wish to use it.
jQuery CDN
If you don't want to download and host jQuery yourself, you can include it from a CDN (Content
Delivery Network).
Google CDN:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
jQuery Syntax
The jQuery syntax is tailor-made for selecting HTML elements and performing some action on the
element(s).
Examples:
jQuery uses CSS syntax to select elements. You will learn more about the selector syntax in the next
chapter of this tutorial.
$(document).ready(function(){
});
This is to prevent any jQuery code from running before the document is finished loading (is ready).
It is good practice to wait for the document to be fully loaded and ready before working with it. This
also allows you to have your JavaScript code before the body of your document, in the head section.
Here are some examples of actions that can fail if methods are run before the document is fully
loaded:
Tip: The jQuery team has also created an even shorter method for the document ready event:
$(function(){
});
jQuery Selectors
jQuery selectors are used to "find" (or select) HTML elements based on their name, id,
classes, types, attributes, values of attributes and much more. It's based on the existing CSS
Selectors, and in addition, it has some own custom selectors.
All selectors in jQuery start with the dollar sign and parentheses: $().
Chapter_7 Web Programming GTC 2014 E.C
The jQuery element selector selects elements based on the element name.
$("p")
Example
Example
$(document).ready(function(){
$("button").click(function(){
$("p").hide();
});
});
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element.
An id should be unique within a page, so you should use the #id selector when you want to
find a single, unique element.
To find an element with a specific id, write a hash character, followed by the id of the HTML
element:
$("#test")
Example
When a user clicks on a button, the element with id="test" will be hidden:
Example
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
});
Chapter_7 Web Programming GTC 2014 E.C
Example
When a user clicks on a button, the elements with class="test" will be hidden:
Example
$(document).ready(function(){
$("button").click(function(){
$(".test").hide();
});
});
$("ul li:first") Selects the first <li> element of the first <ul>
$("a[target='_blank']") Selects all <a> elements with a target attribute value equal to "_blank"
$("a[target!='_blank']") Selects all <a> elements with a target attribute value NOT equal to "_blank"
For a complete reference of all the jQuery selectors, please go to our jQuery Selectors Reference.
If your website contains a lot of pages, and you want your jQuery functions to be easy to maintain,
you can put your jQuery functions in a separate .js file.
When we demonstrate jQuery in this tutorial, the functions are added directly into
the <head> section. However, sometimes it is preferable to place them in a separate file, like this
(use the src attribute to refer to the .js file):
Example
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="my_jquery_functions.js"></script>
</head>
Examples:
The term "fires/fired" is often used with events. Example: "The keypress event is fired, the moment
you press a key".
The next step is to define what should happen when the event fires. You must pass a function to the
event:
$("p").click(function(){
// action goes here!!
});
$(document).ready()
The $(document).ready() method allows us to execute a function when the document is fully
loaded. click()
The function is executed when the user clicks on the HTML element.
The following example says: When a click event fires on a <p> element; hide the
current <p> element:
Example
$("p").click(function(){
$(this).hide();
});
dblclick()
mouseenter()
mouseleave()
mousedown()
mouseup()
hover()
Chapter_7 Web Programming GTC 2014 E.C
focus()
The focus() method attaches an event handler function to an HTML form field.
The function is executed when the form field gets focus:
Example
$("input").focus(function(){
$(this).css("background-color", "#cccccc");
});
blur()
The blur() method attaches an event handler function to an HTML form field.
The function is executed when the form field loses focus:
Example
$("input").blur(function(){
$(this).css("background-color", "#ffffff");
});
Example
$("p").on({
mouseenter: function(){
$(this).css("background-color", "lightgray");
},
mouseleave: function(){
$(this).css("background-color", "lightblue");
},
click: function(){
$(this).css("background-color", "yellow");
}
});