Wad-2 Module Notes
Wad-2 Module Notes
Department of MCA
• Purpose:
including colors, fonts, spacing, and layout. It separates content (HTML) from
presentation (CSS), making it easier to maintain and update websites.
• Key Features:
Dept. of MCA
MMC205 - Web Application Development Pa g e |2
• Advantages:
• Responsiveness
Dept. of MCA
MMC205 - Web Application Development Pa g e |3
1. CSS:
CSS stands for Cascading Style Sheets. It is primarily used to provide styling
to web pages, including color, layout, background, font, and border properties.
The main objective of CSS is to improve content accessibility, provide
enhanced flexibility and control, and specify presentation characteristics.
2. CSS3:
CSS CSS3
Dept. of MCA
MMC205 - Web Application Development Pa g e |4
CSS CSS3
Uses a set of standard colors and Has a good collection of HSL, RGBA,
basic color schemes. HSLA, and gradient colors.
Can add background colors to list Lists have a special display property
items and lists, and set images for and list items have counter reset
list items. properties.
2. CSS Selectors: CSS3 selectors are more advanced than the simple
selectors offered by CSS, providing a sequence of easy-to-use and
simple selectors.
Dept. of MCA
MMC205 - Web Application Development Pa g e |5
4. Border Style: CSS3 includes new border styling features like border-
radius, border-image-slice, border-image-source, and values for "width
stretch".
2.1.2 Selectors
CSS selectors are used to target HTML elements on your pages, allowing you
to apply styles based on their ID, class, type attributes, and more. There are
mainly 5 types of selectors.
• Basic CSS Selectors: These are used to target elements by tag, .class,
or #id for fundamental styling needs.
• Combinators: Ideal for styling elements based on their DOM
relationships (e.g., parent-child or sibling relationships).
• Group Selectors: Use to apply the same styles to multiple, unrelated
elements simultaneously.
• Attribute Selectors: Perfect for styling elements based on specific
attributes or values, such as form inputs or links with certain prefixes or
states.
• Pseudo-Classes: Best for styling elements dynamically or interactively,
like :hover for user interaction or :nth-child() for structural styling.
Dept. of MCA
MMC205 - Web Application Development Pa g e |6
Dept. of MCA
MMC205 - Web Application Development Pa g e |7
Combinator Selectors
Combinators in CSS are used to define relationships between selectors,
allowing you to style elements based on their hierarchy or positioning in the
document. Common combinators include descendant ( ), child (>), adjacent
sibling (+), and general sibling (~).
1. Descendant Selectors: Targets an element inside another, such as
paragraphs inside div .For example, styling paragraphs inside a div.
<style>
div p {
color: red;
}
</style>
2. Child Selector (>): They only affects the direct child elements of a parent.
For example, styling direct children paragraphs of a div.
<style>
div>p {
margin-left: 20px;
}
</style>
3. Adjacent Sibling Selector (+): Styles an element immediately following
another .For example, making the first paragraph bold after an h1.
<style>
h1+p {
font-weight: bold;
}
</style>
4. General Sibling Selector (~): Styles all siblings that follow a specific
element. For example, italicizing all paragraphs following an h1.
<style>
h1~p {
font-style: italic;
}
</style>
Dept. of MCA
MMC205 - Web Application Development Pa g e |8
Attribute Selectors
Attribute selectors in CSS target elements based on the presence or value of
their attributes. Examples include [attr] (selects elements with the attribute),
[attr="value"] (matches specific values), and [attr^="val"] (matches values
starting with "val").
1. Presence Selector: It selects elements that contain a specific attribute.
For example, styling all inputs with a type attribute.
<style>
input[type] {
border: 2px solid black;
}
</style>
2. Attribute Value Selector: It targets elements with a particular attribute
value. For example, styling text inputs.
<style>
input[type="text"] {
background-color: yellow;
}
</style>
3. Substring Matching(^=): It matches elements where the attribute
contains a substring. For example, styling links with https in their href.
<style>
a[href^="https"] {
color: green;
}
</style>
4. Wildcard Selector (*=): Matches elements where the attribute value
contains a specific string. For example, underlining links with example in the
URL.
<style>
a[href*="example"] {
text-decoration: underline;
}
</style>
Dept. of MCA
MMC205 - Web Application Development Pa g e |9
Pseudo-Classes
Pseudo-classes in CSS define the special states of elements for styling.
Examples include :hover (applies when an element is hovered), :first-child
(targets the first child of a parent), and :nth-child(2) (targets the second child).
1. :hover: Styles elements when the user hovers over them. For example,
changing the color of a link when hovered.
<style>
a:hover {
color: red;
}
</style>
2. :focus: Styles the elements when the user focus on any particular element.
<style>
input:focus {
outline: 3px solid red;
}
</style>
3. :first-child: Styles the element which is the first child of it's parent.
<style>
p:first-child {
color: brown;
}
</style>
4. :last-child: Style's the element which is the last child of it's parent.
<style>
p:last-child {
color:green;
}
</style>
5. :not: Helps to remove a particular element from the styling index or styling
context.
<style>
Dept. of MCA
MMC205 - Web Application Development P a g e | 10
p:not(.one) {
color: blue;
}
</style>
6. Pseudo-Element's
The Pseudo Element help's to access and control a specific part of an element
for inserting content before an element or inserting content after an element.
Targeting any specific part of a word or a sentence. It is usually used to beautify
the internal content of an element.
1. ::before : It helps to insert some content before an element.
<style>
h1::before {
content: "★ "
}
</style>
2. ::after: It help's to insert some content after an element.
<style>
h1:active::before {
content: " ";
color: orangered;
}
</style>
3. ::first-line: Styles the first line of text within a block element. Line breaks
mark the beginning of a new line.
<style>
p::first-line {
color: red;
}
</style>
4. ::first-letter: It Styles the first-letter of a word or a sentence.
<style>
p::first-letter {
Dept. of MCA
MMC205 - Web Application Development P a g e | 11
color: red;
font-size: 23px;
}
</style>
5. ::placeholder: Styles the placeholder of a specific input field.
<style>
input::placeholder {
font-size: 20x;
font-family: sans-serif;
font-weight: 900;
}
</style>
CSS properties are the foundation of web design, used to style and control the
behaviour of HTML elements. They define how elements look and interact on a
webpage.
• Used to control layout, colors, fonts, spacing, and animations on web
pages.
• It is essential for making web pages responsive and accessible across
devices.
• Help maintain consistency and efficiency in web design and
development.
Example: In this example, we will see the use of many properties usage, all
the keywords mentioned inside of { and } braces are properties.
<style>
#myDIV {
width: 400px;
height: 299px;
Dept. of MCA
MMC205 - Web Application Development P a g e | 12
background-color: green;
background-repeat: no-repeat;
background-image:
url("[Link]
content/uploads/[Link]");
background-blend-mode: normal;
background-size: contain;
}
</style>
CSS Properties
The list of complete CSS properties is given below.
Properties Descriptions
@charset Rule Specifies the character encoding used in the style sheet.
animation-
It defines the direction of the animation.
direction
Dept. of MCA
MMC205 - Web Application Development P a g e | 13
Properties Descriptions
animation-play-
It specifies whether the animation is running or paused.
state
background- Set background images for an element, You can set one
image or more images as well.
Dept. of MCA
MMC205 - Web Application Development P a g e | 14
Properties Descriptions
background-
Sets the initial position for the background image.
position
border-bottom-
Set the color of the bottom border of an element.
color
border-bottom-
Define the radius of the bottom left corner of the border.
left-radius
border-bottom-
Set the style of the bottom border of an element.
style
border-bottom-
Set a specific width to the bottom border of an element.
width
Set the borders of the cell present inside the table and
border-collapse
tells..
border-image-
It is a shorthand property used to specify the distance.
outset
Dept. of MCA
MMC205 - Web Application Development P a g e | 15
Properties Descriptions
border-image-
It is used to scaling and tiling the border images.
repeat
border-left Set the width, style, and color of the left border.
border-right Set the width, style, and color of the right border.
border-right-
Set the width of right-border of an element.
width
border-top Set the width, style, and color of the top border.
Dept. of MCA
MMC205 - Web Application Development P a g e | 16
Properties Descriptions
border-width Set the border line width of all four sides of an element.
Dept. of MCA
MMC205 - Web Application Development P a g e | 17
Properties Descriptions
Dept. of MCA
MMC205 - Web Application Development P a g e | 18
Properties Descriptions
Dept. of MCA
MMC205 - Web Application Development P a g e | 19
Properties Descriptions
grid-column-gap Set the gap size between the columns in a grid layout.
Dept. of MCA
MMC205 - Web Application Development P a g e | 20
Properties Descriptions
grid-template-
It specifies the area within the grid layout.
areas
grid-template-
Set the number of rows and height of the rows in a grid.
rows
Dept. of MCA
MMC205 - Web Application Development P a g e | 21
Properties Descriptions
list-style-image Set images that will be used as the list item marker.
Dept. of MCA
MMC205 - Web Application Development P a g e | 22
Properties Descriptions
page-break-
Add a page-break-before the specified element.
before
Dept. of MCA
MMC205 - Web Application Development P a g e | 23
Properties Descriptions
Dept. of MCA
MMC205 - Web Application Development P a g e | 24
Properties Descriptions
Set the last line of the paragraph just before the line
text-align-last
break.
text-decoration-
Sets various kinds of text-decoration.
line
text-decoration-
Set the text decoration of an element.
style
text-indent Set the indentation of the first line in each block of text.
CSS transition
It is used to make some transition effects.
Property
Dept. of MCA
MMC205 - Web Application Development P a g e | 25
Properties Descriptions
word-wrap It breaks long words and wraps them into the next line.
Dept. of MCA
MMC205 - Web Application Development P a g e | 26
Property values can appear in a variety of forms. Keyword property values are
used when there are only a few possible values and they are predefined—for
example, large, medium, and small. Keyword values are not case sensitive, so
Small, SmAlL, and SMALL are all the same as small.
Length values are specified as number values that are followed immediately by
a two-character abbreviation of a unit name. There can be no space between
the number and the unit name. The possible unit names are px, for pixels; in,
for inches; cm, for centimeters; mm, for millimeters; pt, for points (a point is
1/72 inch); and pc, for picas, which are 12 points. Note that on a display, in,
cm, mm, pt, and pc are approximate measures. Their actual values depend on
the screen resolution. There are also two relative length values: em, which is
the value of the current font size in pixels, and ex, which is the height of the
letter x.
Dept. of MCA
MMC205 - Web Application Development P a g e | 27
that are followed immediately by a percent sign (%). For example, if the font
size were set to 75%, it would make the new current size for the font 75
percent of its previous value. Font size would stay at the new value until
changed again. Percentage values can be signed. If preceded by a plus sign,
the percentage is added to the previous value; if negative, the percentage is
subtracted.
URL property values use a form that is slightly different from references to
URLs in links. The actual URL, which can be either absolute or relative, is placed
in parentheses and preceded by url, as in the following property:
url([Link])
There can be no space between url and the left parenthesis.
fuchsia
or
rgb(255, 0, 255)
or
#FF00FF
CSS also includes properties for counters and strings, but they are not covered
here. Some property values are inherited by elements nested in the element
for which the values are specified. For example, the property background-color
is not inherited, but font-size is. Using a style sheet to set a value for an
inheritable property for the tag effectively sets it as a default property value
for the whole document, as in
Dept. of MCA
MMC205 - Web Application Development P a g e | 28
The CSS Box Model defines how elements are sized, positioned, and rendered
on a webpage. When a browser loads an HTML document, it creates a DOM
tree and assigns a box to each element. This box calculates the element's
dimensions and position relative to its parent or the root <html> element,
ensuring accurate layout and spacing.
Box Model Component Layout
• Content: The area where text or other content is displayed.
• Padding: Space between the content and the element's border.
• Border: A frame that wraps around the padding and content.
• Margin: Space between the element's border and neighbouring
elements.
Dept. of MCA
MMC205 - Web Application Development P a g e | 29
• The content area is the central part of the CSS box model, containing
the main content (e.g., text, images, videos, or elements like <p> or
<span>).
• It can be styled with CSS properties like height and width.
The content edge refers to the four edges of the content area
• Left content edge
• Right content edge
• Top content edge
• Bottom content edge
2. Padding Area
• The padding area is the space between the content and the border of an
element.
• It includes the areas highlighted in light green and skin color in the
example.
• The distance between the content edge and the border is the padding.
• The border marks the end of the padding area.
• The padding area contributes to the element's total dimensions.
• Padding can be adjusted using CSS properties.
• It works similarly with box-sizing: content-box and box-sizing: border-
box, but with slight calculation differences.
3. Border Area
• The area that marks the end of an element is called as the border it is
the outer fencing for the element.
• The default border properties are provided in CSS to control the
thickness of this outer fencing.
• The border area also add 's up to the complete height and width of the
element.
• The more the border width the more will be the height or width of the
element.
Dept. of MCA
MMC205 - Web Application Development P a g e | 30
• In the above image the area marked with skin color is called the border
area.
4. Margin Area
• The area outside the border of an element is called the margin area.
• Basically this area depends on the parent of the element.
• The distance between the border of the parent element and the border
of the child element is called as the margin.
• CSS has provides certain margin properties to get control over this
scenario.
Dept. of MCA
MMC205 - Web Application Development P a g e | 31
Content Area (Width) :The width of the content area is fixed at 200px.
Padding
• Padding adds extra space inside the element, around the content.
• Padding Left: 20px
• Padding Right: 20px
• Total padding width: 20px + 20px = 40px
Border
• The border, being solid, has a width, but it is calculated differently from
the padding.
• Line Width of Border: 0.4px (the width of the line itself)
• Area of Border: 1.6px (the actual space the border occupies visually)
• Border width for both sides: 1.6px (left) + 1.6px (right) = 3.2px
Total Width
• Total width of the element can be calculated by adding the padding and
border areas to the content area width.
• Formula for Total Width = (Padding-Left + Padding-Right + Border-Area-
Left + Border-Area-Right) + Content Area Width
• Total Width = (20px + 20px + 1.6px + 1.6px) + 200px = 243.2px
• The total width of the element becomes 243.2px.
The reason the total width is increased unexpectedly is because box-sizing:
content-box applies the width to the content area only .The padding and border
Dept. of MCA
MMC205 - Web Application Development P a g e | 32
are added outside the content area, leading to an increase in the overall width
and height of the element.
2. Border-Box
When the box-sizing property is set as border-box the actual dimensions of the
element's remains same as that of the actual dimensions set by the user. The
difference it makes is just that the size of the content area get's altered in a
manner so that it could accommodate the padding area and the border area so
the resultant could be equal to the actual dimensions entered by the user.
<style>
div {
height: 200px;
width: 200px;
box-sizing:border-box;
padding-left: 20px;
padding-right: 20px;
border-left: 2px solid red;
border-right: 2px solid red;
}
</style>
This code will create a box model by altering the dimensions specifically the
width of the content area to accomodate the padding and the border area with
the border line-width.
Dept. of MCA
MMC205 - Web Application Development P a g e | 33
• Width of Border and Padding Border width : 0.4px (line width) and
1.6px + 1.6px = 3.2px (total border area).
• Padding width : 20px + 20px = 40px.
• User-Entered Width : The width entered by the user is 200px, which
applies to the content area only when box-sizing: content-box is used.
• Box-Sizing Behavior :The box-sizing: content-box property adds the
padding and border outside the content area, causing the total width to
increase.
• Adjusting Content Area Width : To ensure the total width remains
200px, the extra width from padding and borders (40px + 3.2px =
43.2px) is subtracted from the total width.
• New content area width : 200px - 43.2px = 156.8px.
• Final Width Calculation : The final total width is: 156.8px (content
area) + 40px (padding) + 3.2px (border) = 200px, ensuring the user’s
entered width remains unchanged.
Dept. of MCA
MMC205 - Web Application Development P a g e | 34
Ensure the padding and border are included within the specified width/height
to maintain a fixed size for layout consistency.
<style>
div {
width: 200px;
padding: 20px;
border: 5px solid black;
box-sizing: border-box;
background-color: lightcoral;
}
</style>
The total width remains 200px, with padding and border included in the 200px
size.
3. Setting box-sizing for All Elements
Apply box-sizing: border-box to all elements globally to simplify layout
calculations and prevent unexpected element size changes.
<style>
*{
box-sizing: border-box;
}
div {
width: 100%;
padding: 20px;
border: 2px solid blue;
background-color: lightyellow;
}
</style>
All elements are sized consistently, with padding and borders included inside
the width/height.
4. Fixed Layout with box-sizing: border-box
Creating a fixed-size element with padding and border without altering the
layout dimensions.
Dept. of MCA
MMC205 - Web Application Development P a g e | 35
<style>
div {
width: 300px;
height: 150px;
padding: 10px;
border: 10px solid green;
box-sizing: border-box;
background-color: lightblue;
}
</style>
5. Creating a Responsive Box with box-sizing
Ensuring that padding and borders do not cause layout issues in a responsive
design.
<style>
*{
box-sizing: border-box;
}
.container {
max-width: 100%;
padding: 20px;
border: 5px solid purple;
background-color: lightgreen;
}
</style>
The element resizes according to the screen width, with padding and borders
included in the total size, avoiding overflow.
CSS Margins
CSS margins are used to create space around an element, separating it from
neighboring elements and the edges of the webpage. They control the layout
Dept. of MCA
MMC205 - Web Application Development P a g e | 36
<style>
.box {
margin: 20px;
}
</style>
• margin: 20px; applies a 20px margin to all four sides of the element.
CSS Margin
Dept. of MCA
MMC205 - Web Application Development P a g e | 37
• Auto: The browser calculates a suitable margin size, often used for
centering elements.
Note: We can also use negative values for margins.
Margin Properties Values
Margin
Property Description Example
Here are the examples of the margin property with different values:
Example of margin property with 4 values
margin: 40px 100px 120px 80px;
• top margin = 40px
• right margin = 100px
• bottom margin = 120px
• left margin = 80px
<style>
p{
margin: 40px 100px 120px 80px;
}
</style>
Example of margin property with 3 values
margin: 40px 100px 120px;
Dept. of MCA
MMC205 - Web Application Development P a g e | 38
• top = 40px
• right and left = 100px
• bottom = 120px
<style>
p{
margin: 40px 100px 120px;
}
</style>
Example of margin property with 2 values
margin: 40px 100px;
• top and bottom = 40px;
• left and right = 100px;
<style>
p{
margin: 40px 100px;
}
</style>
Example of margin property with 1 value
margin: 40px;
• top, right, bottom and left = 40px
<style>
p{
margin: 40px;
}
</style>
Dept. of MCA
MMC205 - Web Application Development P a g e | 39
margin: auto;
width: 50%;
border: 1px solid black;
text-align: center;
}
</style>
• margin: auto;: Automatically adjusts the left and right margins to
center the element horizontally.
• The element must have a defined width for margin: auto; to work
effectively.
Example of margin: inherit; Property
<style>
.parent {
margin: 20px;
}
.child {
margin: inherit;
border: 1px solid black;
}
</style>
• margin: inherit;: The child element inherits the margin value of its
parent.
• In this example, the child element's margin is set to 20px, matching the
parent's margin.
CSS Borders
Borders in CSS are used to create a visible outline around an element. They
can be customized in terms of
• Width: The thickness of the border.
• Style: The appearance of the border (solid, dashed, dotted, etc.).
• Color: The color of the border.
Dept. of MCA
MMC205 - Web Application Development P a g e | 40
Property Description
border-width Sets the width of the border (in pixels, points, or other units).
border-
Creates rounded corners for elements.
radius
Dept. of MCA
MMC205 - Web Application Development P a g e | 41
• border-bottom-style Property
• border-left-style Property
2. Border Width
• border-top-width Property
• border-right-width Property
• border-bottom-width Property
• border-left-width Property
3. Border Color
• border-top-color Property
• border-right-color Property
• border-bottom-color Property
• border-left-color Property
Dept. of MCA
MMC205 - Web Application Development P a g e | 42
<style>
[Link] {
border-style: dotted;
}
[Link] {
border-style: dashed;
}
[Link] {
border-style: solid;
}
[Link] {
border-style: double;
}
</style>
In this example:
• border-style is used to set the type of border around an element.
• dotted: Creates a border with dots.
• dashed: Creates a border with dashed lines.
• solid: Creates a solid, continuous border.
• double: Creates a border with two solid lines.
Dept. of MCA
MMC205 - Web Application Development P a g e | 43
In this example:
• border-width property is used to set the thickness of the border.
• You can use numeric values (e.g., 1px, 5pt, 2cm) or keywords (thin,
medium, thick) to set the border width.
• The border-style and border-color properties must be used in
conjunction with border-width to see the effect.
Dept. of MCA
MMC205 - Web Application Development P a g e | 44
• the border color is set to red using the color name red. You can also use
hex codes like #ff0000 or RGB values like rgb(255, 0, 0).
• The border-style property must be defined (e.g., solid, dashed, etc.) for
the border color to be visible.
Dept. of MCA
MMC205 - Web Application Development P a g e | 45
• The flex container can change the width, height and order of the child
elements.
Dept. of MCA
MMC205 - Web Application Development P a g e | 46
Axes of Flexbox:
1. Main Axis: Direction of flex items (left-to-right, right-to-left, top-to-
bottom, bottom-to-top).
A) left to right
flex-direction: row
B) right to left
flex-direction: row-reverse
C) top to bottom
flex-direction: column
D) bottom to top
flex-direction: column-reverse
2. Cross Axis: Perpendicular to the main axis.
Flexbox Properties:
1. Parent Properties:
• display: Defines a flex container.
• flex-direction: Defines the main axis direction.
• flex-wrap: Allows items to wrap onto multiple lines.
• flex-flow: Shorthand for flex-direction and flex-wrap .
Dept. of MCA
MMC205 - Web Application Development P a g e | 47
<head>
<meta charset="UTF-8">
<meta name="viewport" content=
"width=device-width, initial-scale=1.0">
<style>
body {
display:flex;
align-items: center;
justify-content: center;
color: green;
font-size: 50;
}
h2 {
margin: 10px;
}
</style>
</head>
Dept. of MCA
MMC205 - Web Application Development P a g e | 48
<body>
<h2>CSS Flexbox</h2>
<h2>and Its Properties</h2>
</body>
</html>
Conclusion
CSS Flexbox provides a systematic way to design flexible and responsive layout
structures, enhancing web design by allowing easy arrangement, alignment,
and distribution of space among items in a container. Understanding and using
Flexbox properties can significantly improve the efficiency and flexibility of your
web designs.
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.
Unlike older methods like floats, CSS Grid gives more control over alignment
and adapts well to different screen sizes.
grid-template-columns: auto;
Syntax
.class {
display:grid;
}
Note: You can also use display: inline-grid; to make an element an inline
grid.
Dept. of MCA
MMC205 - Web Application Development P a g e | 49
<html>
<head>
<style>
.grid-container {
display: grid;
gap: 10px;
</style>
</head>
<body>
<div class="grid-container">
</div>
</body>
</html>
Property Description
Dept. of MCA
MMC205 - Web Application Development P a g e | 50
Property Description
grid-column- It is used to set the size of the gap between the columns in
gap a grid layout.
grid-column-
It defines for which column line item will start.
start
It is used to sets the size of the gap between the rows and
grid-gap columns in a grid layout.
Dept. of MCA
MMC205 - Web Application Development P a g e | 51
Property Description
grid-template-
It is used to specify the area within the grid layout.
areas
grid-template- It is used to set the number of rows and height of the rows
rows in a grid.
Dept. of MCA
MMC205 - Web Application Development P a g e | 52
text-align: center;
}
</style>
</head>
<body>
<div class="grid-container">
<div class="grid-item">Column 1</div>
<div class="grid-item">Column 2</div>
<div class="grid-item">Column 3</div>
</div>
</body>
</html>
In this example:
• The .grid-container is set to display as a grid with three equal-width
columns (1fr each) and a 10px gap between them.
• Each .grid-item represents a column with basic styling for clarity.
Responsive Two-Row Layout
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-rows: auto auto;
gap: 15px;
}
.grid-item {
background-color: #e0e0e0;
padding: 15px;
text-align: center;
}
Dept. of MCA
MMC205 - Web Application Development P a g e | 53
</style>
</head>
<body>
<div class="grid-container">
<div class="grid-item">Row 1</div>
<div class="grid-item">Row 2</div>
</div>
</body>
</html>
In this example:
• The .grid-container is defined as a grid with two rows (auto height) and
a 15px gap between them.
• Each .grid-item represents a row with minimal styling for demonstration.
.grid-item {
background-color: #f0f0f0;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
Dept. of MCA
MMC205 - Web Application Development P a g e | 54
<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>
In this example:
• The .grid-container is defined as a grid with two columns, where the first
column takes up one fraction (1fr) of the available space, and the second
column takes up two fractions (2fr), creating unequal column widths.
• Each .grid-item represents a grid cell with minimal styling for
demonstration purposes.
Best Practices for CSS Grid Layout
• Use Flexible Units: Utilize flexible units like fr and minmax() to create
responsive layouts that adapt to various screen sizes.
• Define Explicit Grid Areas: Clearly specify grid areas using grid-
template-areas for better readability and maintainability of your code.
• Combine with Other Layout Methods: Integrate CSS Grid with
Flexbox to leverage the strengths of both and achieve complex,
responsive design
Dept. of MCA
MMC205 - Web Application Development P a g e | 55
Department of MCA
Dept. of MCA
MMC205 - Web Application Development P a g e | 56
Desktop
Tablet
Phone
It is called responsive web design when you use CSS and HTML to resize, hide,
shrink, enlarge, or move the content to make it look good on any screen.
It uses the @media rule to include a block of CSS properties only if a certain
condition is true.
Dept. of MCA
MMC205 - Web Application Development P a g e | 57
Example
Example Program:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
background-color: lightgreen;
}
<p>Resize the browser window. When the width of this document is 600
pixels or less, the background-color is "lightblue", otherwise it is
"lightgreen".</p>
</body>
</html>
Dept. of MCA
MMC205 - Web Application Development P a g e | 58
Add a Breakpoint
Earlier in this tutorial we made a web page with rows and columns, and it was
responsive, but it did not look good on a small screen.
Media queries can help with that. We can add a breakpoint where certain parts
of the design will behave differently on each side of the breakpoint.
Desktop
Phone
Example
Here we use a media query to add a breakpoint at 600px:
@media only screen and (max-width: 600px) {
.item1 {grid-area: 1 / span 6;}
.item2 {grid-area: 2 / span 6;}
.item3 {grid-area: 3 / span 6;}
.item4 {grid-area: 4 / span 6;}
.item5 {grid-area: 5 / span 6;}
}
Dept. of MCA
MMC205 - Web Application Development P a g e | 59
Example Program:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
*{
margin: 0;
box-sizing: border-box;
}
body {
font-family: "Lucida Sans", sans-serif;
}
.grid-container {
display: grid;
grid-template-areas:
'header header header header header header'
'menu main main main main right'
'footer footer footer footer footer footer';
gap: 10px;
background-color: white;
padding: 10px;
}
.item1 {
grid-area: header;
background-color: purple;
text-align: center;
color: #ffffff;
}
.item1 > h1 {
font-size: 40px;
Dept. of MCA
MMC205 - Web Application Development P a g e | 60
.item2 {
grid-area: menu;
}
.item2 ul {
list-style-type: none;
margin: 0;
padding: 0;
}
.item2 li {
padding: 8px;
margin-bottom: 7px;
background-color: #33b5e5;
color: #ffffff;
}
.item2 li:hover {
background-color: #0099cc;
}
.item3 {
grid-area: main;
}
.item3 > h1 {
font-size: 30px;
margin-bottom: 7px;
}
.item3 > p {
margin-bottom: 7px;
}
.item4 {
grid-area: right;
border: 2px solid #0099cc;
background-color: white;
padding: 15px;
Dept. of MCA
MMC205 - Web Application Development P a g e | 61
color: #000000;
}
.item4 > h2 {
font-size: 20px;
padding-bottom: 10px;
}
.item4 li {
padding: 5px;
margin-bottom: 5px;
}
.item5 {
grid-area: footer;
background-color: #0099cc;
color: #ffffff;
text-align: center;
}
<div class="grid-container">
<div class="item1"><h1>Chania</h1></div>
<div class="item2">
<ul>
<li>The Flight</li>
<li>The City</li>
<li>The Island</li>
<li>The Food</li>
Dept. of MCA
MMC205 - Web Application Development P a g e | 62
</ul>
</div>
<div class="item3">
<h1>The City</h1>
<p>Chania is the capital of the Chania region on the island of Crete.</p>
<p>The city can be divided in two parts, the old town and the modern
city. The old town is situated next to the old harbour and is the matrix
around which the whole urban area was developed.</p>
<p>Chania lies along the north west coast of the island Crete.</p>
</div>
<div class="item4">
<h2>Facts:</h2>
<ul>
<li>Chania is a city on the island of Crete</li>
<li>Crete is a Greek island in the Mediterranean Sea</li>
</ul>
</div>
</div>
</body>
</html>
Another Breakpoint
Dept. of MCA
MMC205 - Web Application Development P a g e | 63
Desktop
Tablet
Phone
Example
Here we use media queries to add breakpoints when screen is max 600px,
when screen is min 600px, and when screen is min 768px :
@media only screen and (max-width: 600px) {
.item1 {grid-area: 1 / span 6;}
.item2 {grid-area: 2 / span 6;}
.item3 {grid-area: 3 / span 6;}
.item4 {grid-area: 4 / span 6;}
.item5 {grid-area: 5 / span 6;}
}
Dept. of MCA
MMC205 - Web Application Development P a g e | 64
/* Small devices (portrait tablets and large phones, 600px and up) */
@media only screen and (min-width: 600px) {...}
Dept. of MCA
MMC205 - Web Application Development P a g e | 65
/* Extra large devices (large laptops and desktops, 1200px and up) */
@media only screen and (min-width: 1200px) {...}
Example Program
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.example {
padding: 20px;
color: white;
}
/* Extra small devices (phones, 600px and down) */
@media only screen and (max-width: 600px) {
.example {background: red;}
}
/* Small devices (portrait tablets and large phones, 600px and up) */
@media only screen and (min-width: 600px) {
.example {background: green;}
}
/* Extra large devices (large laptops and desktops, 1200px and up) */
@media only screen and (min-width: 1200px) {
.example {background: pink;}
}
Dept. of MCA
MMC205 - Web Application Development P a g e | 66
</style>
</head>
<body>
</body>
</html>
Dept. of MCA
MMC205 - Web Application Development P a g e | 67
}
}
/* If the screen size is 600px or less, set the font-size of <div> to 30px */
@media only screen and (max-width: 600px) {
[Link] {
font-size: 30px;
}
}
Dept. of MCA
MMC205 - Web Application Development P a g e | 68
2. Easy to Understand
• Make sure your website is simple and easy to use. Arrange features
and elements clearly so that users don’t get confused or lost. A clean,
neat layout helps users find what they need quickly and easily.
3. Consistency
• Maintain a consistent look and feel across all device sizes. This means
keeping the design elements, like fonts and colors, the same whether
the site is viewed on a phone, tablet, or computer. Consistency makes
your website look professional and cohesive.
4. User Engagement
• Engage users by allowing them to control and interact with your
website. For example, on a mobile device, use swipe gestures for
menus that are typically on the left side of the screen on a PC. This
interactivity builds user trust and makes the website more enjoyable to
use.
Best Practices for Responsive Web Design
Some of the best practices for a responsive web design that you can follow
are:
• Mobile-First Approach: Design for the smallest screen size possible
first and then optimise the design for larger screens. T ensuring that
website is accessible for mobile users, who make up a significant
amount of web traffic.
• Accessible Content: Make your website accessible to all users,
including those with disabilities. This includes proper use of HTML
semantic elements, ensuring keyboard navigability, and providing
alternative text for images.
• Responsive Media: Media elements should be able to resize
themselves to fit within the confines of the user's device.
• Landscape Orientation: Make sure to optimise your landscape
orientation to improve usability.
Dept. of MCA
MMC205 - Web Application Development P a g e | 69
Dept. of MCA
MMC205 - Web Application Development P a g e | 70
For example:
css
.container {
width: 100%;
}
.column {
width: 33.33%; /* 1/3 of the container */
}
This means the column will always take up 1/3 of the available width,
whether it's on a desktop, tablet, or mobile screen.
Key Features
• Flexible Layout: Adapts automatically to screen size.
• Proportional Scaling: Elements scale relative to the screen or
container.
• Mobile-Friendly: Works well for various devices without separate
designs.
How It Works
Instead of:
css
.column {
width: 300px;
}
Use:
css
.column {
width: 25%; /* Adapts to 25% of the parent container */
}
This enables a layout that changes dynamically with the viewport.
Dept. of MCA
MMC205 - Web Application Development P a g e | 71
<div class="container">
<div class="box">Box 1</div>
<div class="box">Box 2</div>
<div class="box">Box 3</div>
</div>
</body>
</html>
CSS ([Link])
/* Base styles */
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
Dept. of MCA
MMC205 - Web Application Development P a g e | 72
display: flex;
flex-wrap: wrap;
padding: 10px;
box-sizing: border-box;
}
Dept. of MCA
MMC205 - Web Application Development P a g e | 73
Summary
A fluid grid is a flexible layout technique that uses relative sizing to ensure
web content adapts to different screen sizes. It’s a foundation of responsive
design, helping create websites that work seamlessly across desktops, tablets,
and smartphones.
Dept. of MCA
MMC205 - Web Application Development P a g e | 74
HTML ([Link])
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flexible Images Example</title>
<link rel="stylesheet" href="[Link]">
</head>
<body>
<div class="container">
<h2>Responsive Image Example</h2>
<img src="[Link] alt="Sample Image">
<p>This image resizes based on the width of the container.</p>
</div>
</body>
</html>
CSS ([Link])
body {
margin: 0;
font-family: Arial, sans-serif;
}
.container {
width: 90%;
max-width: 800px;
margin: auto;
Dept. of MCA
MMC205 - Web Application Development P a g e | 75
padding: 20px;
}
Result:
• On large screens: Image displays at full container width (up to
800px).
• On smaller screens: Image scales down proportionally to fit the
screen.
Dept. of MCA
MMC205 - Web Application Development P a g e | 76
Mobile-First Design
What is Mobile-First Design?
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.
To create a successful mobile-first website, consider the following tips:
Dept. of MCA
MMC205 - Web Application Development P a g e | 77
Dept. of MCA
MMC205 - Web Application Development P a g e | 78
Dept. of MCA
MMC205 - Web Application Development P a g e | 79
Dept. of MCA
MMC205 - Web Application Development P a g e | 80
Department of MCA
Dept. of MCA
MMC205 - Web Application Development P a g e | 81
<head>
<link
href="[Link]
css"
rel="stylesheet"
integrity="sha384-
QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALE
wIH"
crossorigin="anonymous">
<script
src="[Link]
[Link]"
integrity="sha384-
YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz
"
crossorigin="anonymous"></script>
</head>
<body>
<div class="container text-center">
<h1 class="text-success">
Welcome to GeeksforGeeks
</h1>
<p class="text-dark">A Simple Example of Bootstrap</p>
</div>
</body>
</html>
Dept. of MCA
MMC205 - Web Application Development P a g e | 82
Output
Bootstrap Components
Bootstrap offers a rich set of pre-designed components to streamline UI
development. These include buttons, cards, modals, navbars, and more.
• Accordion
• Alerts
• Badges
• Breadcrumb
• Buttons
• Button Group
• Card
• Carousel
Dept. of MCA
MMC205 - Web Application Development P a g e | 83
• Close Button
• Collapse
• Dropdowns
• List Group
• Modal
• Navbar
• Offcanvas
• Popovers
• Pagination
• Progress
• Scrollspy
• Spinners
• Toasts
• Tooltips
Bootstrap 5 Accordion
Bootstrap 5 Accordion organizes content into collapsible panels, enabling
space-efficient presentation of information. It allows users to toggle between
panel visibility, enhancing user experience, and content organization within
web applications.
Bootstrap 5 Accordion
Term Description
Dept. of MCA
MMC205 - Web Application Development P a g e | 84
Syntax:
<div class="accordion" id="myAccordion">
<div class="accordion-item">
<h2 class="accordion-header" id="header1">
<button class="accordion-button" type="button"
data-bs-toggle="collapse"
data-bs-target="#panel1Collapse">
First Panel
</button>
</h2>
<div id="panel1Collapse"
class="accordion-collapse collapse show"
aria-labelledby="panel1Heading">
<div class="accordion-body">
This is the content for first Panel.
</div>
</div>
</div>
<!-- Add your more panels here -->
</div>
<!DOCTYPE html>
<html>
<head>
<title>Accordion Example</title>
<link rel="stylesheet"
href=
"[Link]
/>
</head>
<body>
<h2 class="text-center">
Dept. of MCA
MMC205 - Web Application Development P a g e | 85
Bootstrap 5 Accordion
</h2>
<div id="collapseTwo"
class="accordion-collapse collapse"
aria-labelledby="headingTwo"
data-bs-parent="#accordionExample">
<div class="accordion-body">
C++ is a high level
programming language
used for developing
applications, games,
etc.
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header" id="headingOne">
<button class="accordion-button collapsed"
type="button"
data-bs-toggle="collapse"
data-bs-target="#collapseOne"
aria-expanded="false"
aria-controls="collapseOne">
Java
Dept. of MCA
MMC205 - Web Application Development P a g e | 86
</button>
</h2>
<div id="collapseOne"
class="accordion-collapse collapse"
aria-labelledby="headingOne"
data-bs-parent="#accordionExample">
<div class="accordion-body">
Java is a programming
language that is used
in modern development
worldwide.
</div>
</div>
</div>
</div>
</div>
<script src=
"[Link]
js">
</script>
</body>
</html>
Bootstrap 5 Alerts
Syntax:
<div class="alert alert-type"> Contents of the alert... <div>
Types: There are eight types of alerts available in Bootstrap 5. The classes of
these alerts are given below:
• alert-primary
Dept. of MCA
MMC205 - Web Application Development P a g e | 87
• alert-secondary
• alert-success
• alert-danger
• alert-warning
• alert-info
• alert-light
• alert-dark
Example 1: This example we demonstrates Bootstrap 5 alerts with different
colors: primary, secondary, success, and danger, each displaying
"GeeksforGeeks" as content.
<!DOCTYPE html>
<html>
<head>
<title>
Bootstrap 5 | Alerts
</title>
<!-- Load Bootstrap -->
<link rel="stylesheet" href=
"[Link]
alpha1/css/[Link]">
</head>
<body>
<div style="text-align: center;
width:600px;">
<h1 style="color: green;">
GeeksforGeeks
</h1>
</div>
<div id="canvas" style="width: 600px;
height: 200px; margin: 20px;">
<div class="alert alert-primary" role="alert">
GeeksforGeeks
</div>
<div class="alert alert-secondary" role="alert">
GeeksforGeeks
</div>
<div class="alert alert-success" role="alert">
GeeksforGeeks
Dept. of MCA
MMC205 - Web Application Development P a g e | 88
</div>
<div class="alert alert-danger" role="alert">
GeeksforGeeks
</div>
</div>
</body>
</html>
Output:
Bootstrap 5 Buttons
Bootstrap 5 Buttons are pre-styled components in the Bootstrap framework,
offering consistent design and functionality. They streamline development by
providing ready-to-use button styles, sizes, and behaviors, ensuring a
cohesive and responsive user interface across web applications with minimal
CSS customization.
Currently Active Property:
Class="btn-primary"
Syntax:
<button class="badge bg-type"> Button Text <button>
Types: Following are the nine types of buttons available in Bootstrap 5:
• btn-primary
• btn-secondary
Dept. of MCA
MMC205 - Web Application Development P a g e | 89
• btn-success
• btn-danger
• btn-warning
• btn-info
• btn-light
• btn-dark
• btn-link
Examples of Bootstrap 5 Buttons
Example 1: This example uses shows the working of the first five types of
buttons in Bootstrap 5.
<!DOCTYPE html>
<html>
<head>
<title>
Bootstrap 5 Buttons
</title>
Dept. of MCA
MMC205 - Web Application Development P a g e | 90
</button>
<button type="button" class="btn btn-light">
Light
</button>
<button type="button" class="btn btn-dark">
Dark
</button>
<button type="button" class="btn btn-link">
Link
</button>
</div>
</body>
</html>
Output:
Bootstrap Utilities
Bootstrap utilities are small, reusable classes that provide additional
functionality and control. They include classes for spacing, alignment, borders,
and background colors.
• Background
• Borders
• Colors
• Display
• Flex
• Float
• Interactions
• Overflow
Dept. of MCA
MMC205 - Web Application Development 2024-25 P a g e | 91
• Position
• Shadows
• Sizing
• Spacing
• Text
• Vertical align
• Visibility
Bootstrap 5 Background
Last Updated : 25 Jul, 2024
•
Bootstrap 5 Background is used with the help of the keyword bg. It offers
various colors like primary, success, info, warning, danger, secondary, dark,
and light.
• Background Color: The background color of the elements is set to
contextual class like .bg-primary, .bg-success, and so on.
• Background gradient: A linear gradient is applied to the backgrounds
by adding color value and the .bg-gradient class to it.
• Sass: This involves the Sass map and spacing utilities that are declared
in Utility API.
The below examples illustrate the Bootstrap 5 Background:
Example 1: The following code demonstrates different background Color
classes of Bootstrap 5.
<!DOCTYPE html>
<html lang="en">
<head>
<link href=
"[Link]
rel="stylesheet"
integrity=
Dept. of MCA
MMC205 - Web Application Development P a g e | 92
"sha384-
EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmL
ASjC"
crossorigin="anonymous">
<script src=
"[Link]
js"
integrity=
"sha384-
MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVX
M"
crossorigin="anonymous">
</script>
</head>
<body>
<h1 class="text-center text-success">
GeeksforGeeks
</h1>
<h2 class="text-center">
Bootstrap 5 Background
</h2>
Dept. of MCA
MMC205 - Web Application Development P a g e | 93
</body>
</html>
Bootstrap 5 Borders
Bootstrap 5 Borders offer predefined border styles to elements. Classes like
.border, .border-0, .border-top, .border-bottom, .border-left, and .border-
right provide easy customization for borders, including color, width, and
radius, enhancing UI design.
Bootstrap 5 Borders:
Property Description
Border
Utilizes theme colors to change border color.
Color
Border
Increases or decreases border width.
Width
Border
Utilizes rounded classes for rounded corners.
Radius
Dept. of MCA
MMC205 - Web Application Development P a g e | 94
<html lang="en">
<head>
<title> Bootstrap 5 Borders </title>
<link href=
"[Link]
rel="stylesheet" integrity=
"sha384-
EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmL
ASjC"
crossorigin="anonymous">
<script src=
"[Link]
js"
integrity=
"sha384-
MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVX
M"
crossorigin="anonymous">
</script>
</head>
<body>
<h2 class="text-center">Bootstrap 5 Borders</h2>
<div class="text-center">
<span class="border border-4">1</span>
<span class="border-top border-5">2</span>
<span class="border-end border-4">3</span>
<span class="border-bottom border-5">4</span>
<span class="border-start border-3">5</span>
</div>
</body>
</html>
Output:
Dept. of MCA
MMC205 - Web Application Development P a g e | 95
Bootstrap 5 Colors
Bootstrap 5 Colors utility is used to set the text colors, into green as a success,
and blue as a primary. There are lots of other informative colors that we need
like danger, warning, and so on. In this article, we will know about all the text
colors that can be used through Bootstrap 5.
Bootstrap 5 Colors Classes:
Class Description
Note: According to the docs, they have used only text-color classes, but we
can use background classes in a similar way, for that we just need to replace
the text with bg(text-primary to bg-primary.)
Syntax:
<p class="Colors Class">
Text Content
</p>
Dept. of MCA
MMC205 - Web Application Development P a g e | 96
<body class="m-2">
<div>
<strong>Bootstrap 5 Colors</strong>
</div>
<br>
<div>
<!-- Bootstrap 5 Color Classes are used -->
<p class="text-primary">
Text Color Class: text-primary
</p>
<p class="text-secondary">
Text Color Class: text-secondary</p>
<p class="text-success">
Text Color Class: text-success</p>
<p class="text-danger">
Text Color Class: text-danger</p>
<p class="text-warning">
Text Color Class: text-warning</p>
<p class="text-info">
Text Color Class: text-info</p>
</div>
</body>
</html>
Dept. of MCA
MMC205 - Web Application Development P a g e | 97
Dept. of MCA
MMC205 - Web Application Development P a g e | 98
mkdir bootstrap-sass-customization
cd bootstrap-sass-customization
Step 3: Initialize npm
Inside the folder, initialize a new npm project:
npm init -y
Step 4: Install Bootstrap and Sass
Now install Bootstrap and Sass via npm:
npm install bootstrap@5 sass
Step 5: Update your [Link] with scripts to compile Sass:
{
"scripts": {
"build-css": "sass scss/[Link] dist/[Link] --watch"
}
}
Folder Structure
Folder Structure
Updated Dependencies
"dependencies": {
"bootstrap": "^5.3.3",
"sass": "^1.79.3"
}
Dept. of MCA
MMC205 - Web Application Development P a g e | 99
<head>
<link rel="stylesheet" href="dist/[Link]">
</head>
<body>
<button class="btn btn-primary m-5">Primary Button</button>
</body>
</html>
// scss/[Link]
@import "../node_modules/bootstrap/scss/bootstrap";
// Correct way
.btn-primary {
&{
background-color: #e74c3c;
border-color: #e74c3c;
}
&:hover {
background-color: #c0392b;
}
}
Dept. of MCA
MMC205 - Web Application Development P a g e | 100
Output
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="dist/[Link]">
</head>
<body>
<button class="btn btn-primary">Primary Button</button>
</body>
</html>
Dept. of MCA
MMC205 - Web Application Development P a g e | 101
// scss/[Link]
@import "../node_modules/bootstrap/scss/bootstrap";
&:hover {
background-color: #35c02b;
}
}
<body>
<header class="custom-header">
Welcome to My Custom Bootstrap Page
</header>
</body>
</html>
Dept. of MCA
MMC205 - Web Application Development P a g e | 102
// scss/[Link]
@import "../node_modules/bootstrap/scss/bootstrap";
&:hover {
background-color: #27ae60;
// Darker green on hover
}
}
Output
Dept. of MCA