Unit 1ii
Unit 1ii
Layouts with CSS Grids Flexbox– Responsive web design with media queries - Advanced
CSS Effects – Gradients, opacity, box-shadow - CSS3 Animations: Transforms and
Transitions – CSS Frameworks: Bootstrap
WEBSITE LAYOUT
Header
Navigation Menu
Footer
There are tons of different layout designs to choose from. However, the
structure above, is one of the most common.
Header
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;
}
1
Navigation Bar
A navigation bar contains a list of links to help visitors navigating through your
website:
Example
/* Navbar links */
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
Content
The layout in this section, often depends on the target users. The most common
layout is one (or combining them) of the following:
2
content: "";
display: table;
clear: both;
}
Content Section: The content section is the main body of the website. The
user can divide content section in n-column layout.
The most common layouts are:
1-Column Layout: It is mostly used for mobile layout.
2-Column Layout: This website layout is mostly used for tablets or laptops.
3
3-Column Layout: This website layout is mostly used for desktops.
Footer
The footer is placed at the bottom of your page. It often contains information like
copyright and contact info:
Example
.footer {
background-color: #F1F1F1;
text-align: center;
4
padding: 10px;
CSS page layout techniques allow us to take elements contained in a web page and
control where they're positioned relative to the following factors: their default position
in normal layout flow, the other elements around them, their parent container, and the
main viewport/window. The page layout techniques we'll be covering in more detail in
this module are:
Normal flow
The display property
Flexbox
Grid
Floats
Positioning
Table layout
Multiple-column layout
Each technique has its uses, advantages, and disadvantages. No technique is designed
to be used in isolation. By understanding what each layout method is designed for you'll
be in a good position to understand which method is most appropriate for each task.
CSS Flexbox
Before the Flexbox Layout module, there were four layout modes:
The Flexible Box Layout Module, makes it easier to design flexible responsive layout
structure without using float or positioning.
Flexbox Elements
To start using the Flexbox model, you need to first define a flex container.
5
Example
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
Like we specified in the previous chapter, this is a flex container (the blue
area) with three flex items:
The flex container becomes flexible by setting the display property to flex:
Example
.flex-container {
display: flex;
}
flex-direction
flex-wrap
flex-flow
justify-content
align-items
align-content
6
The CSS Flexbox Container Properties
The following table lists all the CSS Flexbox Container properties:
Property Description
align-items Vertically aligns the flex items when the items do not use all
available space on the cross-axis
flex-wrap Specifies whether the flex items should wrap or not, if there
is not enough room for them on one flex line
7
The flex-direction Property
.flex-container {
display: flex;
flex-direction: column; /
flex-direction: column-reverse;/
flex-direction: row;/
flex-direction: row-reverse;
}
The flex-wrap property specifies whether the flex items should wrap or not.
Example
.flex-container {
display: flex;
flex-wrap: wrap;/
flex-wrap: nowrap;/
flex-wrap: wrap-reverse;/
}
}
The flex-flow property is a shorthand property for setting both the flex-
direction and flex-wrap properties.
.flex-container {
display: flex;
flex-flow: row wrap;
}
8
The justify-content property is used to align the flex items:
Example
The center value aligns the flex items at the center of the container:
.flex-container {
display: flex;
justify-content: center;/
justify-content: flex-start;/
justify-content: flex-end;/
justify-content: space-around;/
justify-content: space-between;
Example
The center value aligns the flex items in the middle of the container:
.flex-container {
display: flex;
height: 200px;
align-items: center;/
align-items: flex-start;/
align-items: flex-end;/
align-items: stretch;/
align-items: baseline;
9
.flex-container {
display: flex;
height: 600px;
flex-wrap: wrap;
align-content: space-between;/
align-content: space-around;/
align-content: stretch;/
align-content: center;/
align-content: flex-start;/
align-content: flex-end;
}
The CSS Grid Layout Module offers a grid-based layout system, with rows and
columns, making it easier to design web pages without having to use floats and
positioning.
Grid Elements
A grid layout consists of a parent element, with one or more child elements.
Display Property
An HTML element becomes a grid container when its display property is set
to grid or inline-grid.
Example
.grid-container {
display: grid;
}
Example
.grid-container {
display: inline-grid;
}
can adjust the gap size by using one of the following properties:
10
column-gap
row-gap
gap
Example
The column-gap property sets the gap between the columns:
.grid-container {
display: grid;
column-gap: 50px;
}
Example
The row-gap property sets the gap between the rows:
.grid-container {
display: grid;
row-gap: 50px;
}
Example
The gap property is a shorthand property for the row-gap and the column-
gap properties:
.grid-container {
display: grid;
gap: 50px 100px;
}
Example
The gap property can also be used to set both the row gap and the column gap
in one value:
.grid-container {
display: grid;
gap: 50px;
}
11
Grid Lines
The lines between columns are called column lines.
Example
Place a grid item at column line 1, and let it end on column line 3:
.item1 {
grid-column-start: 1;
grid-column-end: 3;
}
Example
Place a grid item at row line 1, and let it end on row line 3:
.item1 {
grid-row-start: 1;
grid-row-end: 3;
}
Property Description
12
grid A shorthand property for the grid-template-rows,
grid-template-columns, grid-template-areas, grid-
auto-rows, grid-auto-columns, and the grid-auto-
flow properties
13
grid-gap A shorthand property for the grid-row-gap and grid-
column-gap properties
14
row-gap Specifies the gap between the grid rows
The @media rule is used in media queries to apply different styles for different
media types/devices.
It uses the @media rule to include a block of CSS properties only if a certain
condition is true.
Example
If the browser window is 600px or smaller, the background color will be
lightblue:
Using media queries are a popular technique for delivering a tailored style sheet
(responsive web design) to desktops, laptops, tablets, and mobile phones.
Use media queries to specify that certain styles are only for printed documents
or for screen readers (mediatype: print, screen, or speech).
15
CSS Syntax
not: The not keyword inverts the meaning of an entire media query.
only: The only keyword prevents older browsers that do not support media
queries with media features from applying the specified styles. It has no effect
on modern browsers.
and: The and keyword combines a media feature with a media type or other
media features.
They are all optional. However, if you use not or only, you must also specify a
media type.
Advantages:
We can add a breakpoint where certain parts of the design will behave
differently on each side of the breakpoint.
Desktop
Phone
16
/* For mobile phones: */
[class*="col-"] {
width: 100%;
}
}
Mobile First means designing for mobile before designing for desktop or any
other device (This will make the page display faster on smaller devices).
Instead of changing styles when the width gets smaller than 768px, we should
change the design when the width gets larger than 768px. This will make our
design Mobile First:
Example
/* For mobile phones: */
[class*="col-"] {
width: 100%;
}
There are tons of screens and devices with different heights and widths, so it is
hard to create an exact breakpoint for each device. To keep things simple you
could target five groups:
Example
/* Extra small devices (phones, 600px and down) */
@media only screen and (max-width: 600px) {...}
/* Small devices (portrait tablets and large phones, 600px and up) */
17
@media only screen and (min-width: 600px) {...}
/* Extra large devices (large laptops and desktops, 1200px and up) */
@media only screen and (min-width: 1200px) {...}
Media queries can also be used to change layout of a page depending on the
orientation of the browser.
You can have a set of CSS properties that will only apply when the browser
window is wider than its height, a so called "Landscape" orientation:
Example
The web page will have a lightblue background if the orientation is in landscape
mode:
Example
/* If the screen size is 600px wide or less, hide the element */
@media only screen and (max-width: 600px) {
div.example {
display: none;
18
}
}
You can also use media queries to change the font size of an element on
different screen sizes:
Example
/* If the screen size is 601px or more, set the font-size of <div> to 80px
*/
@media only screen and (min-width: 601px) {
div.example {
font-size: 80px;
}
}
/* If the screen size is 600px or less, set the font-size of <div> to 30px
*/
@media only screen and (max-width: 600px) {
div.example {
font-size: 30px;
}
}
CSS Gradients
Gradients are typically one color that fades into another, but in CSS we
can control every aspect of how that happens, from the direction to the
colors (as many as you want) to where those color changes happen.
CSS gradients let you display smooth transitions between two or more
specified colors.
19
Gradients are background-image
.gradient {
background-color: red;
background: red;
To create a linear gradient you must define at least two color stops. Color stops
are the colors you want to render smooth transitions among. You can also set a
starting point and a direction (or an angle) along with the gradient effect.
Syntax
Example
#grad {
Direction – Diagonal
20
Using Angles
If we want more control over the direction of the gradient, we can define an
angle, instead of the predefined directions (to bottom, to top, to right, to left, to
bottom right, etc.).
Syntax
Example
#grad {
Example
#grad {
background-image: linear-gradient(to right,
red,orange,yellow,green,blue,indigo,violet);
}
Using Transparency
CSS gradients also support transparency, which can be used to create fading
effects.
To add transparency, we use the rgba() function to define the color stops.
The last parameter in the rgba() function can be a value from 0 to 1, and it
defines the transparency of the color: 0 indicates full transparency, 1 indicates
full color (no transparency).
Example
#grad {
21
}
Repeating a linear-gradient
Example
#grad {
To create a radial gradient you must also define at least two color stops.
Syntax
background-image: radial-gradient(shape size at position, start-color,
..., last-color);
The following example shows a radial gradient with evenly spaced color stops:
Examples
#grad {
background-image: radial-gradient(red, yellow, green);
}
#grad {
background-image: radial-gradient(red 5%, yellow 15%, green 60%);
}
22
Set Shape
The shape parameter defines the shape. It can take the value circle or ellipse.
The default value is ellipse.
Example
#grad {
The size parameter defines the size of the gradient. It can take four values:
closest-side
farthest-side
closest-corner
farthest-corner
Example
#grad1 {
#grad2 {
Repeating a radial-gradient
23
Example
#grad {
background-image: repeating-radial-gradient(red, yellow 10%, green
15%);
}
Syntax
If no degree is specified, the colors will be spread equally around the center
point
Example
#grad {
#grad {
background-image: conic-gradient(red, yellow, green, blue, black);
}
A conic gradient with three colors and a degree for each color:
24
#grad {
background-image: conic-gradient(red 45deg, yellow 90deg, green 210deg);
}
Add border-radius: 50% to make the conic gradient look like a pie:
Example
#grad {
background-image: conic-gradient(red, yellow, green, blue, black);
border-radius: 50%;
}
Example
#grad {
border-radius: 50%;
#grad {
border-radius: 50%;
25
CSS Gradient Functions
Function Description
26
CSS Shadow Effects
With CSS we can add shadow to text and to elements.
Example
h1 {
Example
h1 {
Example
h1 {
Example
27
h1 {
color: white;
Example
h1 {
Multiple Shadows
To add more than one shadow to the text, you can add a comma-separated
list of shadows.
The following example shows a red and blue neon glow shadow:
Example
h1 {
h1 {
color: white;
h1 {
28
color: coral;
In its simplest use, only specify a horizontal and a vertical shadow. The default
color of the shadow is the current text-color.
Example
div {
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 300px;
height: 100px;
padding: 15px;
29
background-color: coral;
</style>
</head>
<body>
</body>
</html>
div {
div {
The spread parameter defines the spread radius. A positive value increases
the size of the shadow, a negative value decreases the size of the shadow.
30
Example
div {
The inset parameter changes the shadow from an outer shadow (outset) to
an inner shadow.
Example
div {
Example
div {
box-shadow: 5px 5px blue, 10px 10px red, 15px 15px green;
Cards
31
Example
div.card {
width: 250px;
text-align: center;
Property Description
Example
div {
opacity: 0.5; }
32
When using the opacity property to add transparency to the
background of an element, all of its child elements become transparent
as well.
This can make the text inside a fully transparent element hard to read.
If we do not want to apply opacity to child elements, use RGBA color
values instead
CSS Syntax
opacity: number|initial|inherit;
Property Values
Value Description
Example:
<!DOCTYPE html>
<html>
<head>
<style>
div {
background-color: #4CAF50;
33
padding: 10px;
div.first {
opacity: 0.1;
div.second {
opacity: 0.3;
div.third {
opacity: 0.6;
</style>
</head>
<body>
<p> welcome</p>
<div><p>opacity 1 (default)</p></div>
</body>
</html>
34
CSS Animations
To use CSS animation, need must first specify some keyframes for the
animation.
Keyframes hold what styles the element will have at certain times.
When we specify CSS styles inside the @keyframes rule, the animation will
gradually change from the current style to the new style at certain times.
The following example binds the "example" animation to the <div> element.
The animation will last for 4 seconds, and it will gradually change the
background-color of the <div> element from "red" to "yellow":
Example
@keyframes example {
to {background-color: yellow;}
div {
35
width: 100px;
height: 100px;
background-color: red;
animation-name: example;
animation-duration: 4s;
EXAMPLE:
<!DOCTYPE html>
<html>
<head>
<style>
div {
width: 100px;
height: 100px;
background-color: red;
position: relative;
animation-name: example;
animation-duration: 4s;
@keyframes example {
36
50% {background-color:blue; left:200px; top:200px;}
</style>
</head>
<body>
<h1>CSS Animation</h1>
<div></div>
</body>
</html>
Delay an Animation by
div {
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
animation-delay: 2s;
37
}
The following example will run the animation 3 times before it stops:
Example
div {
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
animation-iteration-count: 3;
38
div {
width: 100px;
height: 100px;
position: relative;
background-color: red;
animation-name: example;
animation-duration: 4s;
CSS-Transform
.element {
width: 20px;
height: 20px;
transform: scale(20);
.element {
} (or)
transform: scaleX(2);
transform: scaleY(.5);
39
Values
scale(): Affects the size of the element. This also applies to
the font-size, padding, height, and width of an element, too. It’s
also a a shorthand function for the scaleX and scaleY functions.
skewX() and skewY(): Tilts an element to the left or right, like
turning a rectangle into a parallelogram. skew() is a shorthand that
combines skewX() and skewY by accepting both values.
translate(): Moves an element sideways or up and down.
rotate(): Rotates the element clockwise from its current position.
matrix(): A function that is probably not intended to be written by
hand, but combines all transforms into one.
perspective(): Doesn’t affect the element itself, but affects the
transforms of descendent elements’ 3D transforms, allowing them
all to have a consistent depth perspective.
Skew
/* Skew points along the x-axis */
.element {
transform: skewX(25deg);
}
40
The skewX and skewY transform functions tilt an element one way
or the other.
Rotate
.element {
transform: rotate(25deg);
}
This rotates an element clockwise from its original position, whilst a
negative value would rotate it in the opposite direction
Translate
.element {
These values would be any length value, like 10px or 2.4em. One value will
move the element to the right (negative values to the left). If a second value
is provided, that second value will move it down (negative values up).
transform: translateX(value);
transform: translateY(value);
Multiple values
.element {
width: 20px;
height: 20px;
41
}
Matrix
The matrix transform function can be used to combine all transforms into
one.
matrix(0.7071067811865475, 0.7071067811865476, -
0.7071067811865476, 0.7071
3D Transforms
translate3d(x, y, z)
translateZ(z)
The third value in translate3d or the value in translateZ moves the element
toward the viewer, negative values away.
scaleZ(sz)
The third value in scale3d or the value in scaleZ affects the scaling along the
z-axis (e.g. the imaginary line coming straight out of the screen).
rotateX(value)
rotateY(value)
rotate3d(x, y, z)
42
rotateX and rotateY will rotate an element in 3D space around those axises.
rotate3d allows us to specify a point in 3D space in which to rotate the
element around.
matrix3d(…)
perspective(value)
This value doesn’t affect the element itself, but it affects the transforms of
descendent elements’ 3D transforms, allowing them to all have a consistent
depth perspective.
CSS Transitions
Properties are:
transition
transition-delay
transition-duration
transition-property
transition-timing-function
If the duration part is not specified, the transition will have no effect, because
the default value is 0.
Example
div {
width: 100px;
height: 100px;
background: red;
43
transition: width 2s;
}
ease - specifies a transition effect with a slow start, then fast, then end
slowly (this is default)
linear - specifies a transition effect with the same speed from start to end
The following example shows some of the different speed curves that can be
used:
Example
The transition-delay property specifies a delay (in seconds) for the transition
effect.
44
The following example has a 1 second delay before starting:
Example
div {
transition-delay: 1s;
Transition + Transformation
Example
div {
transition: width 2s, height 2s, transform 2s;
}
Bootstrap
Bootstrap is a powerful, feature-packed frontend toolkit. Build anything—from
prototype to production—in minutes.
1.Create a new index.html file in your project root. Include the <meta
name="viewport"> tag as well for proper responsive behavior in mobile
devices.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bootstrap demo</title>
</head>
<body>
45
<h1>Hello, world!</h1>
</body>
</html>
2. Include Bootstrap’s CSS and JS. Place the <link> tag in the <head> for
our CSS, and the <script> tag for our JavaScript bundle (including Popper
for positioning dropdowns, poppers, and tooltips) before the closing
</body>.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bootstrap demo</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.
css" rel="stylesheet" integrity="sha384-
rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F
9CUG65" crossorigin="anonymous">
</head>
<body>
<h1>Hello, world!</h1>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.
min.js" integrity="sha384-
kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I
4" crossorigin="anonymous"></script>
</body></html>
3. Hello, world! Open the page in your browser of choice to see Bootstrapped page.
46