Module 2-1
Module 2-1
MODULE 2
CSS3 AND RESPONSIVE WEB DESIGN
CSS3 BASICS
Introduction to CSS3
CSS (Cascading Style Sheets) is a stylesheet language used to control the presentation and layout of HTML
documents. It allows web developers to apply styles such as colors, fonts, spacing, and positioning to HTML
elements, creating visually appealing and consistent web pages.
Each property in CSS has a name-value pair, and each property is separated by a semicolon (;).
• Separation of Content and Design: CSS separates the structure (HTML) from the design, making it
easier to manage and update styles.
• Improved Accessibility: Allows customization for different devices and screen sizes.
• Efficiency: Enables reusable styles across multiple pages, reducing code redundancy.
• Enhanced User Experience: Creates visually attractive and interactive web pages.
<!DOCTYPE html>
<html lang="en">
<head>
<style>
h1{
color:white;
background-color:red;
}
p{
color:blue;
}
</style>
</head>
<body>
<h1>MULTIMEDIA</h1>
<p>This is about to learn to create webpages. </p>
</body>
</html>
1. Inline CSS: Directly within an HTML element using the style attribute.
2. Internal CSS: Inside a <style> tag within the <head> section of the HTML document.
3. External CSS: Through a separate .css file linked to the HTML document.
1. Inline CSS:
Inline CSS is applied directly within an HTML element using the style attribute. It is useful for quick styling
of a single element but is not recommended for large projects due to poor maintainability.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Inline CSS Example</title>
</head>
<body>
<h1 style="color: blue; text-align: center;">Web Technology</h1>
</body>
</html>s
Internal CSS is defined within a <style> tag inside the <head> section of an HTML document. It is useful for
styling a single web page without affecting other pages.
In the following example, we have used the inline CSS in <p> and <h1> tag.
Ex: <head>
<style>
p{
color: blue;
font-size: 18px;
}
</style>
</head>
PROGRAM
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Internal CSS Example</title>
<style>
body {
background-color: skyblue;
}
h1 {
color: red;
text-align: center;
}
</style>
</head>
<body>
<h1>Web Technology</h1>
</body>
</html>
3. External CSS:
External CSS is written in a separate .css file and linked to an HTML document using the <link> tag. It is the
best practice for large projects as it keeps styles separate from content.
<head>
</head>
EX:
body {
background-color: lightblue;
}
h1 {
color: red;
text-align: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>External CSS Example</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
<!DOCTYPE html>
</head>
<body>
….
</body>
</html>
CSS Selectors
CSS selectors define the elements to which a set of CSS rules apply. They allow you to target HTML elements
based on attributes, relationships, or positions within the document.
This selects all elements of a particular type. The element selector selects HTML elements based on the
element name.
Syntax:
element {
property: value;
}
For example, to select all paragraphs in the document, use the selector p.
p{
text-align: center;
color: red;
}
Ex:
<html lang="en">
<head>
<style>
p{
text-align: center; color: red;
}
</style>
</head>
<body>
<p>Every paragraph will be affected by the style. </p>
<p id="para1">My name is Hareesha </p>
<p>Department of Computer Applications</p>
</body>
</html>
2. CSS id Selector
The ID selector in CSS is used to target a specific HTML element with a unique id attribute. The id of
an element is unique within a page, so the id selector is used to select one unique element.
To select an element with a specific id, write a hash (#) character, followed by the id of the element.
Syntax:
#id-name {
property: value;
}
Program:
<!DOCTYPE html>
<html lang="en">
<head>
<style>
#para1 {
text-align: center;
color: red;
}
</style>
</head>
<body>
<p id="para1">Hello World!</p>
<p>This paragraph is not affected by the style.</p>
</body>
</html>
The class selector in CSS is used to target multiple elements that share the same class name. Unlike the ID
selector (#), which applies to only one unique element, a class (.) can be reused across multiple elements.
To select elements with a specific class, write a period (.) character, followed by the class name.
Syntax:
.class-name {
property: value;
}
Program:
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1 class="center">Red and center-aligned heading</h1>
<p class="center">Red and center-aligned paragraph. </p>
</body>
</html>
The universal selector (*) in CSS selects all elements on a webpage. It is useful when you want to apply a
style to every element or reset default styles.
Syntax:
*{
property: value;
}
<!DOCTYPE html>
<html lang="en">
<head>
<style>
*{
color: green;
font-size: 20px;
}
</style>
</head>
<body>
<h2>This is heading</h2>
<p>This style will be applied on every paragraph.</p>
<p id="para1">Me too!</p>
<p>And me!</p>
</body>
</html>
The group selector in CSS is used to apply the same style to multiple elements at once. It reduces redundancy
and makes your CSS cleaner and more efficient.
Grouping selector is used to minimize the code. Commas are used to separate each selector in grouping.
Syntax:
h1, h2, p {
color: blue;
font-family: Arial, sans-serif;
}
<!DOCTYPE html>
<html lang="en">
<head>
<style>
h1, h2, p {
text-align: center;
color: blue; }
</style>
</head>
<body>
<h1>Web Technology</h1>
<h2>Web Technology (In smaller font)</h2>
<p>This is a paragraph.</p>
</body>
</html>
Properties
Font
CSS provides various properties to control the appearance of text, including font family, size, weight, style,
spacing, and more.
CSS font color is a standalone attribute in CSS although it seems that it is a part of CSS fonts. It is used to
change the color of the text.
1. By a color name
Predefined color names (e.g., red, blue, green)
2. By hexadecimal value (#RRGGBB)
A six-digit hex code (values from 00 to FF for Red, Green, and Blue).
3. By RGB
Defines colors using Red, Green, and Blue values (0–255).
<!DOCTYPE html>
<html lang="en">
<head>
<style>
body {
font-size: 100%;
}
h1 { color: red; }
h2 { color: #9000A1; }
p { color:rgb(0, 220, 98); }
}
</style>
</head>
<body>
<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<p>This is a paragraph.</p>
</body>
</html>
In CSS, the font-family property defines the typeface (font) of the text. It allows you to specify a list of fonts,
ensuring a fallback if the preferred font is unavailable.
Serif: Serif fonts include small lines at the end of characters. Example of serif: Times new roman, Georgia etc.
Sans-serif: A sans-serif font doesn't include the small lines at the end of characters. Example of Sans-serif:
Arial, Verdana etc.
2. Specific Font family: Specific fonts should be used in a font stack with generic families as a fallback.
It specifies the font family name like Arial, New Times Roman etc.
<!DOCTYPE html>
<html lang="en">
<head>
<style>
body {
font-size: 100%;
}
h1 { font-family: sans-serif; }
h2 { font-family: serif; }
p { font-family: monospace; }
}
</style>
</head>
<body>
<h1>This heading is shown in sans-serif.</h1>
<h2>This heading is shown in serif.</h2>
<p>This paragraph is written in monospace.</p>
</body>
</html>
The font-size property in CSS defines the size of text. It plays a key role in typography and affects readability,
accessibility, and design consistency.
Syntax:
p{
font-size: 16px;
}
For example: font-size: 10pt
<!DOCTYPE html>
<html lang="en">
<head>
<title>Practice CSS font-size property</title>
</head>
<body>
<p style="font-size:xx-small;"> This font size is extremely small.</p>
<p style="font-size:x-small;"> This font size is extra small</p>
<p style="font-size:small;"> This font size is small</p>
<p style="font-size:medium;"> This font size is medium. </p>
<p style="font-size:large;"> This font size is large. </p>
<p style="font-size:x-large;"> This font size is extra large. </p>
<p style="font-size:xx-large;"> This font size is extremely large. </p>
<p style="font-size:smaller;"> This font size is smaller. </p>
<p style="font-size:larger;"> This font size is larger. </p>
<p style="font-size:200%;"> This font size is set on 200%. </p>
</body>
</html>
Font styles
The font-style property in CSS defines the style of the text, such as normal, italic, or oblique.
Syntax:
p{
font-style: italic;
}
For example: font-style: italic
<html>
<head>
<style>
body {
font-size: 100%;
}
h2 { font-style: italic; }
h3 { font-style: oblique; }
h4 { font-style: normal; }
}
</style>
</head>
<body>
<h2>This heading is shown in italic font.</h2>
<h3>This heading is shown in oblique font.</h3>
<h4>This heading is shown in normal font.</h4>
</body>
</html>
CSS font variant property specifies how to set font variant of an element. It may be normal and small-caps.
<html>
<head>
<style>
p { font-variant: small-caps; }
h3 { font-variant: normal; }
</style>
</head>
<body>
<h3>This heading is shown in normal font.</h3>
<p>This paragraph is shown in small font.</p>
</body>
</html>
Syntax:
p{
font-weight: bold;
}
Colors
The color property in CSS is used to set the color of HTML elements. Typically, this property is used to set the
background color or the font color of an element.
In CSS, we use color values for specifying the color. We can also use this property for the border-color and
other decorative effects.
1. RGB format.
2. RGBA format.
3. Hexadecimal notation.
4. HSL.
5. HSLA.
6. Built-in color
1. RGB Format
RGB format is the short form of 'RED GREEN and BLUE' that is used for defining the color of an HTML
element simply by specifying the values of R, G, B that are in the range of 0 to 255.
Syntax
2. RGBA Format
It is almost similar to RGB format except that RGBA contains A (Alpha) that specifies the element's
transparency. The value of alpha is in the range 0.0 to 1.0, in which 0.0 is for fully transparent, and 1.0 is for
not transparent.
Syntax
color:rgba(R, G, B, A);
3. Hexadecimal notation
Hexadecimal can be defined as a six-digit color representation. This notation starts with the # symbol
followed by six characters ranges from 0 to F. In hexadecimal notation, the first two digits represent the red
(RR) color value, the next two digits represent the green (GG) color value, and the last two digits represent
the blue (BB) color value.
The black color notation in hexadecimal is #000000, and the white color notation in hexadecimal is #FFFFFF.
Some of the codes in hexadecimal notation are #FF0000, #00FF00, #0000FF, #FFFF00, and many more.
Syntax
color:#(0-F)(0-F)(0-F)(0-F)(0-F)(0-F);
It is a short form of hexadecimal notation in which every digit is recreated to arrive at an equivalent
hexadecimal value.
The black color notation in short hex is #000, and the white color notation in short hex is #FFF. Some of the
codes in short hex are #F00, #0F0, #0FF, #FF0, and many more.
4. HSL
Where,
Hue is a degree on the color wheel from 0 to 360. 0 is red, 120 is green, and 240 is blue.
Saturation is a percentage value. 0% means a shade of gray, and 100% is the full color.
Lightness is also a percentage. 0% is black, 50% is neither light or dark, 100% is white
Syntax
5. HSLA
It is entirely similar to HSL property, except that it contains A (alpha) that specifies the element's
transparency. The value of alpha is in the range 0.0 to 1.0, in which 0.0 indicates fully transparent, and 1.0
indicates not transparent.
Syntax
color:hsla(H, S, L, A);
6. Built-in Color
As its name implies, built-in color means the collection of previously defined colors that are used by
using a name such as red, blue, green, etc.
Box model
Box Model is a Fundamental concept in CSS that defines how elements are structured and positioned on a
webpage. It is used to develop the design and structure of a web page.
The box model in CSS is a container that contains various properties, including borders, margins, padding,
and the content itself. These properties collectively determine the dimensions and spacing of an element.
1. Content:
In the CSS Box Model, the content is the innermost part of an element where text, images, or other media are
displayed.
It is affected by width and height but can be styled using CSS properties like color, font-size, background, etc.
2. Padding:
Padding is the space between the content and the border of an element. It creates inner spacing within the
element, making content more readable.
3. Border:
A border is the outer edge of an element that surrounds the padding and content. It helps in defining
boundaries and improving visual structure.
4. Margin:
The margin is the outermost space in the CSS Box Model. It creates space between an element and its
neighbouring elements, preventing them from touching.
1. Content Area
2. Padding Area
• Dimensions are determined by the width and height of the padding box.
3. Border Area
4. Margin Area
<head>
<title>CSS Box Model</title>
<style>
.main
{
font-size:25px;
font-weight:bold;
Text-align:center;
}
.gfg
{
margin-left:50px;
border:50px solid skyblue;
width:300px;
height:200px;
text-align:center;
padding:50px;
}
.cbx
{
font-size:40px;
font-weight:bold;
color:black;
margin-top:60px;
background-color:lightgreen;
}
.ctxt
{
font-size:20px;
font-weight:bold;
background-color:white;
}
</style>
</head>
<body>
Layout
CSS Layout is essential for defining the visual structure, organization, and responsiveness of a website. CSS
website layout divides a webpage into multiple sections like header, navigation bar, content area, and footer,
making it easier to organize content and build the site.
1. Header Section
A header is usually located at the top of the website (or right below a top navigation menu). It often contains
a logo or the website name.
Example
.header {
background-color: #F1F1F1;
text-align: center;
padding: 20px;
}
2. Navigation Bar
A navigation bar contains a list of links to help visitors navigating through your website.
3. Content
The layout in this section, often depends on the target users. The most common layout is one (or combining
them) of the following:
Ex:
Unequal Columns
The main content is the biggest and the most important part of your site.
It is common with unequal column widths, so that most of the space is reserved for the main content. The
side content (if any) is often used as an alternative navigation or to specify information relevant to the main
content. Change the widths as you like, only remember that it should add up to 100% in total:
.column {
float: left;
padding: 10px;
}
/* Middle column */
.column.middle {
width: 50%;
}
</style>
</head>
<body>
<div class="row">
<div class="column side">
<h2>Side</h2>
<p>Left Column occupies 25%..</p>
</div>
</div>
4. Footer
The footer is placed at the bottom of your page. It often contains information like copyright and contact info.
.footer {
background-color: #F1F1F1;
text-align: center;
padding: 10px;
}
Layout Example
<html lang="en">
<head>
<title>CSS Website Layout</title>
<style>
*{
box-sizing: border-box;
}
body {
margin: 0;
}
.topnav {
overflow: hidden;
background-color: skyblue;
}
/* Middle column */
.column.middle {
width: 50%;
}
/* Responsive layout - makes the three columns stack on top of each other instead of next to each other
*/
@media screen and (max-width: 600px) {
.column.side, .column.middle {
width: 100%;
}
}
text-align: center;
}
</style>
</head>
<body>
<div class="header">
<h1>Header</h1>
<p>Resize the browser window to see the responsive effect.</p>
</div>
<div class="topnav">
<a href="#">Link1</a>
<a href="#">Link2</a>
<a href="#">Link3</a>
</div>
<div class="row">
<div class="column side">
<h2>Side</h2>
<p>The layout in this section, often depends on the target users.</p>
</div>
It is common with unequal column widths, so that most of the space is reserved for the main content..
</p>
</div>
<div class="footer">
<h1>Footer</h1>
</div>
</body>
</html>
Positioning
CSS positioning defines how elements are placed within a web page. It allows you to control the layout,
stacking order, and alignment of elements.
Elements with position: relative are positioned relative to their normal position in
Relative Positioning the document flow. Other elements will not fill the gap left by this element when
adjusted.
An element with position: fixed property remains in the same position relative to
Fixed Positioning
the viewport even when the page is scrolled.
Combines features of position: relative and position: fixed. The element is treated
Sticky Positioning as position: relative until it reaches a specified threshold, then it becomes position:
fixed.
Elements are then positioned using the top, bottom, left, and right properties. However, these properties will
not work unless the position property is set first. They also work differently depending on the position value.
Syntax
position: static;
position: relative;
position: absolute;
position: fixed;
position: sticky;
1. Static Positioning
HTML elements are positioned static by default. It does not accept properties like top, left, right, or bottom.
An element with position: static; is not positioned in any special way.
<head>
<style>
div.static {
position: static;
border: 3px solid red;
}
</style>
</head>
2. Relative Positioning
Relative positioning places an element relative to its normal position. You can move it using top, left, right, or
bottom.
<head>
<style>
div.relative {
position: relative;
left: 30px;
border: 3px solid red;
}
</style>
</head>
3. Absolute Positioning
Absolute positioning removes the element from the document flow and places it relative to the nearest
ancestor with a positioning context (relative, absolute, or fixed).
<style>
div.relative {
position: relative;
width: 400px;
height: 200px;
border: 3px solid #73AD21;
}
div.absolute {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
}
</style>
4. Fixed Positioning
Fixed positioning removes the element from the flow and positions it relative to the viewport. It remains in
place even when the page scrolls.
<html>
<head>
<style>
.fixed {
position: fixed;
top: 10px;
right: 10px;
background-color: lightgreen;
padding: 20px;
border: 2px solid black;
}
.content {
height: 1200px;
padding: 10px;
}
</style>
</head>
<body>
<h2>Fixed Positioning Example</h2>
<div class="fixed">Fixed Box</div>
<div class="content">
<p>Scroll down to see that the fixed box stays in place.</p>
<p>This content simulates a long page.</p>
</div>
</body>
</html>
5. Sticky Positioning
A sticky element toggles between relative and fixed, depending on the scroll position. The sticky element
stays at the top of the viewport as you scroll but only within its containing element.
<html>
<head>
<!--Driver Code Ends-->
<style>
.sticky {
position: sticky;
top: 0;
background-color: yellow;
padding: 10px;
}
</style>
<!--Driver Code Starts-->
</head>
<body>
<div class="sticky">I am sticky</div>
<p>Scroll down to see the effect.</p>
<div style="height: 1000px;"></div>
</body>
</html>
Flexbox
CSS Flexbox, short for the Flexible Box Layout module, is a powerful layout tool designed to simplify web
page layouts by arranging items in rows or columns with ease.
The Flexible Box Layout module introduces a one-dimensional layout system that handles space distribution
and item alignment effectively. It works seamlessly for horizontal (row) or vertical (column) arrangements,
making it a go-to solution for responsive designs.
Flexbox eliminates the need for floats or complex positioning, enabling responsive and dynamic layouts.
It aligns items efficiently, distributing space within a container, even when their sizes are dynamic or
unknown.
Flexbox is supported in all modern browsers, making it a reliable choice for creating flexible designs.
Features
1. Alignment: Easily aligns items along the main axis (row/column) or cross-axis (perpendicular
direction).
2. Space Distribution: Distributes space among items or gaps dynamically within a container.
3. Responsive Design: Adapts to screen sizes without extra effort, perfect for modern web layouts.
Flexbox Components
Flexbox Axes
flex: 1;
}
</style>
</head>
<body>
<div class="flex-container">
<div class="flex-item">Item 1</div>
<div class="flex-item">Item 2</div>
<div class="flex-item">Item 3</div>
</div>
</body>
</html>
Grid Layout
CSS Grid Layout is used to design responsive web layouts with rows and columns. It allows you to place items
exactly where you want, making it easier to create flexible and modern web pages.
Syntax
.class {
display: grid;
}
Property Description
Defines the amount of space between columns in a multi-column layout, often used
column-gap
with column-count in CSS Grid.
Specifies the spacing (also called gutter) between rows and columns in CSS Grid
gap
and Flexbox layouts.
A grid-based layout system in CSS that provides rows and columns, simplifying the
grid
design without using floats or positioning.
Defines the size and location of a grid item within a CSS grid layout by specifying
grid-area
the start and end lines.
Sets the size of columns that are automatically generated in a CSS Grid layout when
grid-auto-columns
more content is added.
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-rows: auto auto;
gap: 15px;
}
.grid-item {
background-color: lightgray;
padding: 15px;
text-align: center;
}
</style>
</head>
<body>
<div class="grid-container">
<div class="grid-item">Row 1</div>
<div class="grid-item">Row 2</div>
</div>
</body>
</html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.grid-item {
background-color: lightgray;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
<div class="grid-container">
<div class="grid-item">Item 1</div>
<div class="grid-item">Item 2</div>
<div class="grid-item">Item 3</div>
<div class="grid-item">Item 4</div>
</div>
</body>
</html>
Media queries
The @media rule, introduced in CSS2, made it possible to define different style rules for different media
types. CSS Media Queries are used to apply CSS styles according to the screen size.
Syntax:
/* CSS styles */
Media types specify which devices the styles should apply to. Commonly used types include:
speech Designed for screen readers that read the content aloud.
Value Description
orientation Orientation of the viewport. Landscape or portrait
max-height Maximum height of the viewport
min-height Minimum height of the viewport
height Height of the viewport (including scrollbar)
max-width Maximum width of the viewport
min-width Minimum width of the viewport
width Width of the viewport (including scrollbar)
<html>
<head>
<style>
body {
background-color: pink;
}
</body>
</html>
Responsive Web Design is designing websites that contain flexible layouts that can scale itself according
to the screen size of the device it is being viewed on. A Responsive Web Design enlarges, shrinks, resizes, or
hides content present as per the screen size of the device. Using websites that look bad on the screen can be
displeasing for the users and they might just close and stop using that specific website.
Instead of using fixed-width layouts, use fluid grid systems. This means the layout of your website will adjust
smoothly based on the screen size.
Make sure images and other media content can scale within their containers.
Use media queries in your CSS to apply different styles for different screen sizes and orientations.
Make buttons and other interactive elements large enough for finger tapping. Place navigational elements in
easily accessible areas.
Continuously test your website on different devices and browsers to make sure it looks and functions well
everywhere.
CSS fluid layouts (also known as liquid layouts) are designed to adapt gracefully to different screen sizes
and devices, ensuring optimal viewing experiences across a wide range of viewports. They contrast with
fixed layouts, which maintain rigid dimensions regardless of screen size.
• Responsive Units: Utilizing units such as percentages, viewport units, and ems instead of fixed pixels
• Reflowing Content: Content reflows to fit the available space, preventing horizontal scroll bars and
ensuring readability
• Nesting Layouts: Complex designs can be achieved by nesting fluid layout regions within each other
Flexible Images
The flexible images are responsive web design’s second pillar. They allows us to provide image solutions with
no restrictions to a fixed display size.
Syntax:
img {
width: 100%;
height: auto;
}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
img {
width: 100%;
height: auto;
}
</style>
</head>
<body>
</body>
</html>
Mobile-first design is an approach where you design your website for mobile devices first and then for
desktops.
This method is crucial because most people today use their smartphones and tablets to go online rather than
laptops or computers. Mobile-first designs ensure that your website not only looks good but also works
seamlessly across different devices, providing the best user experience. This approach helps
increase website traffic and ensures that your site adjusts to different screen sizes effectively.
CSS FRAMEWORKS
Introduction to Bootstrap
Bootstrap is a framework that is suitable for mobile-friendly web development. it is a popular front-end
framework for building responsive and mobile-first websites.
It provides pre-designed CSS, JavaScript components, and utility classes to quickly create modern and
consistent user interfaces.
Why Bootstrap?
BootStrap from CDN: This method of installing Bootstrap is easy. It is highly recommended to follow this
method.
• Go to www.getbootstrap.com and click Getting Started. Scroll down and copy the Bootstrap CDN for
CSS, JS, Popper.js, and jQuery links.
Bootstrap CSS Library:
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous">
containers
In Bootstrap, containers are fundamental building blocks for structuring your webpage’s content. They help
to organize and encapsulate content within a defined device or viewport. By using containers, you ensure
that your website is responsive, with elements aligned properly, and adaptable to different screen sizes for a
smooth and consistent layout presentation.
Class Description
Fixed-width layout for webpage content, ensuring consistent padding and
.container
alignment.
.container-fluid Fluid-width layout for webpage content, spanning the full width of the viewport.
The .container class creates a fixed-width layout that adjusts its size based on the current viewport or device.
This class ensures that content within it is aligned and padded appropriately, providing a visually consistent
presentation across different screen sizes.
The .container-fluid class provides a fluid-width layout, meaning the container stretches to occupy the full
width of the viewport. This makes it ideal for layouts that need to cover the entire screen, ensuring that the
content adjusts seamlessly across all devices and screen sizes.
Components of Bootstrap
Bootstrap components are pre-styled UI elements like buttons, forms, navbars, and more, built with HTML,
CSS, and JavaScript. They provide a consistent and responsive design framework, facilitating rapid
development of web applications with minimal customization required.
Component Description
Alerts It is a popup with a predefined message that appears after a particular action.
Customized buttons used to perform an action in forms, dialogue boxes, etc. They
Buttons
come in multiple states, sizes, and predefined styles.
Button group A group of buttons aligned in a single line, arranged vertically or horizontally.
Used to show the progress of a particular operation with a custom progress bar. It
Progress Bar
may have text labels, stacked bars, and animated backgrounds.
Displays the loading state of websites or projects. Built with HTML and CSS, doesn't
Spinner
require JavaScript.
Keeps updating the navigation bar to the currently active link based on the scroll
Scrollspy
position in the viewport.
Dropdown Drops the menu in the format of a list of links. Contextual and toggleable overlays.
Navs Creates a basic and simple navigation menu with a .nav base class.
Used to take multiple inputs at once from the user. Bootstrap has two layouts
Forms
available: stacked and inline.
Extend form controls by adding a button, button group, or text on either side of
Input groups
inputs.
Provides the location of the current page in a navigational hierarchy and adds
Breadcrumb
separators through CSS.
Carousel A slideshow of image or text content built with CSS 3D and JavaScript.
Displays a message for a small amount of time, usually a few seconds. Alert messages
Toast
designed to imitate push notifications popular in desktop and mobile systems.
Tooltip Provides small information about the element/link when the mouse hovers over it.
Popovers Displays extra information about the element/link when clicked on it.
Used to easily navigate between different pages, typically presented as a large block
Pagination
of connected links for accessibility.
Used for repetitive and complex components like tweets or blogs. Images or videos
Media Object
are placed/aligned to the left or right of the content.
Alerts
The alert messages are highlighted texts that are important to take into consideration while performing a
process. Bootstrap allows showing these alert messages on the website using predefined classes.
The .alert class followed by contextual classes are used to display the alert message on website.
Syntax:
<div class="alert> Contents... <div>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Alerts</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.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.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>
</head>
<body>
<div class="container">
<div class="alert alert-success">
<strong>Success alert!</strong>
</div>
<div class="alert alert-info">
<strong>Info alert!</strong>
</div>
<div class="alert alert-warning">
<strong>Warning alert!</strong>
</div>
</div>
</body>
</html>
Buttons
Now, buttons, and links can all look alike with Bootstrap, anything that is given that class of btn will inherit
the default look of a grey button with rounded corners. Adding extra classes will add colors to the buttons.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Buttons</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.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.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>
</head>
<body style="text-align:center;">
<div class="container mt-3">
<h2>Buttons</h2>
Navs
Nav Menu: The .nav, .nav-item and .nav-link classes are used to create navigation menu. The .nav, .nav-
item and .nav-link classes are used with <ul>, <li> and link element respectively.
<html lang="en">
<head>
<title>Nav menu</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<linkrel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script>
</head>
<body style="text-align:center;">
<div class="container">
<h2>Navigation Menu</h2>
<ul class="nav">
<li class="nav-item">
<a class="nav-link" href="#">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About us</a>
</li>
</ul>
</div>
</body>
</html>
Dropdowns
Dropdowns are one of the most important parts of an interactive website. A dropdown menu is the collection
of menu items that allow users to choose a value from the list. The .dropdown class is used to design the drop-
down menu.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Dropdowns</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.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.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>
</head>
<body style="text-align:center;">
<div class="container">
<h2>Dropdown List</h2>
<div class="dropdown">
<button type="button"
class="btn btn-success dropdown-toggle"
data-toggle="dropdown">
Select MCA Subjects
</button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">
Operating System
</a>
<a class="dropdown-item" href="#">
Web Technologies
</a>
</div>
</div>
</div>
</body>
</html>
Bootstrap Utilities
Bootstrap provides a lot of utilities to make a stylish, mobile-friendly, and responsive front-end website
without using any CSS code. The utility layout is used for showing, hiding, aligning, and spacing content.
1. Changing display: Bootstrap provides some display properties for setting the display. Some display
properties like set to display responsive and display toggling, etc.
2. Flexbox options: The Flexible Box Layout Module in bootstrap is used for designing the flexible and
responsive layout structure.
3. Margin and padding: Bootstrap use the margin and padding (spacing) utility to set the space and size of
an element.