Cascading Style Sheet: By-Rameshkumar Yadav
Cascading Style Sheet: By-Rameshkumar Yadav
1.Inline CSS
2.Internal CSS
3.External CSS
Inline CSS
• An inline style may be used to apply a
unique style for a single element.
• To use inline styles, add the style attribute
to the relevant element. The style attribute
can contain any CSS property.
• Inline styles are defined within the "style"
attribute of the relevant element.
• Example:-
<h1 style="color:blue;text-align:center;">
Coding Seekho</h1>
<p style="color:red;">This is a
paragraph.</p>
Internal CSS
• An internal style sheet may be used if one
single HTML page has a unique style.
• The internal style is defined inside the <style>
element, inside the head section.
<head>
<style>
h1 {
color: maroon;
}
</style>
</head>
• <h1>This is a heading</h1>
External CSS
• With an external style sheet, you can change the
look of an entire website by changing just one
file!
• Each HTML page must include a reference to the
external style sheet file inside the <link>
element, inside the head section.
• The external .css file should not contain any
HTML tags.
• Note: Do not add a space between the property
value and the unit:
<head>
<link rel="stylesheet" href="styles.css”>
</head>
CSS Comments
• CSS comments are not displayed in the
browser, but they can help document your
source code.
• Comments are used to explain the code, and
may help when you edit the source code at
a later date.
• Comments are ignored by browsers.
#para1 {
text-align: center;
color: red;
}
• <p id="para1">Hello World!</p>
The CSS class Selector
• The class selector selects HTML elements
with a specific class attribute.
• To select elements with a specific class,
write a period (.) character, followed by the
class name.
.center {
text-align: center;
color: red;
}
• <h1 class="center">Coding Seekho</h1>
• <p class="center">Coding Seekho</p>
The CSS class Selector
• You can also specify that only specific HTML
elements should be affected by a class.
• In this example only <p> elements with
class="center" will be red and center-
aligned:
p.center {
text-align: center;
color: red;
}
• <h1 class="center">Coding Seekho</h1>
• <p class="center">Coding Seekho</p>
The CSS class Selector
• HTML elements can also refer to more than one class.
• Note: A class name cannot start with a number!
• In this example the <p> element will be styled
according to class="center" and to class="large":
p.center {
text-align: center;
color: red;
}
p.large { font-size: 300%; }
• background-color
• background-image
• Background-size
• background-repeat
• background-attachment
• background-position
• background (shorthand property)
CSS background-color
• The background-color property specifies the
background color of an element.
• You can set the background color for multiple
HTML elements.
• With CSS, a color is most often specified by a
valid color name - like "red"
• a HEX value - like "#ff0000"
• an RGB value - like "rgb(255,0,0)“
• Example:-
body {
background-color: lightblue;
}
CSS background-
image
• The background-image property specifies an
image to use as the background of an element.
• By default, the image is repeated so it covers
the entire element.
• Example:-
body { background-image: url("paper.gif"); }
• Note: When using a background image, use an
image that does not disturb the text.
• The background image can also be set for
specific elements.
• Example:-
p { background-image: url("paper.gif"); }
CSS background-
repeat
• By default, the background-image property repeats an image
both horizontally and vertically.
• Some images should be repeated only horizontally or
vertically, or they will look stranger.
• To repeat an image vertically, set background-repeat:
repeat-y;
• To repeat an image horizontally, set background-repeat:
repeat-x;
• Showing the background image only once is also specified by
the background-repeat: no-repeat;
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
background-repeat: repeat-y;
background-repeat: no-repeat;
}
CSS background-
position
• The background-position property is used to
specify the position of the background
image.
• The background image is placed in the same
place as the text. We want to change the
position of the image, so that it does not
disturb the text too much.
• body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
• Position- left, right, center, top, bottom, or
CSS background-
attachment
• The background-attachment property
specifies whether the background image
should scroll or be fixed.
• Example:-
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: fixed;
background-attachment: scroll;
}
background -
Shorthand property
• To shorten the code, it is also possible to specify all
the background properties in one single property. This
is called a shorthand property.
• Example:-
• body {
background: #ffffff url("img_tree.png") no-repeat
right top;
}
• When using the shorthand property the order of the
property values is:
• background-color, background-image, background-
repeat, background-attachment, background-position.
• It does not matter if one of the property values is
missing, as long as the other ones are in this order.
CSS border-style
• The CSS border properties allow you to
specify the style, width, and color of an
element's border.
• The border-style property specifies what kind
of border to display.
• The border-style property can have from one
to four values (for the top border, right
border, bottom border, and the left border).
• Example:-
<style>
p {border-style: solid;}
</style>
CSS border-style
• The following values are allowed:
• dotted - Defines a dotted border
• dashed - Defines a dashed border
• solid - Defines a solid border
• double - Defines a double border
• groove - Defines a 3D grooved border.
• ridge - Defines a 3D ridged border.
• inset - Defines a 3D inset border.
• outset - Defines a 3D outset border.
• none - Defines no border
• hidden - Defines a hidden border
• Mix- {border-style: dotted dashed solid double;}
CSS border-width
• The border-width property specifies the width of the
four borders.
• The width can be set as a specific size (in px, pt, cm,
em, etc) or by using one of the three pre-defined
values: thin, medium, or thick.
• p{
border-style: solid;
border-width: medium;
}
• The border-width property can have from four values
( top, right, bottom, and left border):
• p{
border-style: solid;
border-width: 25px 10px 4px 35px;
}
CSS border-color
• The border-color property is used to set the
color of the four borders.
• p{
border-style: solid;
border-color: green;
}
• The border-color property can have four
values (top, right, bottom, and left border).
• p{
border-style: solid;
border-color: red green blue yellow;
}
CSS Border Sides
• You can specify a different border for each side.
• In CSS, there are also properties for specifying
each of the borders (top, right, bottom, and left).
• p{
border-top-style: dotted;
border-right-style: solid;
border-bottom-style: dotted;
border-left-style: solid;
}
• border-style: dotted solid double;
• border-style: dotted solid;
• border-style: dotted;
CSS Border -
Shorthand Property
• The border property is a shorthand property for
the following individual border properties:
• border-width
• border-style (required)
• border-color
• p{
border: 5px solid red;
}
• You can also specify all the individual border
properties for just one side.
• p{
border-left: 6px solid red;
}
CSS Rounded Borders
• The border-radius property is used to add
rounded borders to an element.
• p{
border: 2px solid red;
border-radius: 5px;
}
• You can also specify all the individual
rounded border value.
• p{
border: 2px solid red;
border-radius: 5px 10px;
}
CSS Margins
• Margins are used to create space around
elements, outside of any defined borders.
• With CSS, you have full control over the
margins. There are properties for setting the
margin for each side of an element (top, right,
bottom, and left).
• margin-top: 20px;
• margin-right: 40px;
• margin-bottom: 30px;
• margin-left: 25px;
• margin:auto;
• margin: 25px 50px 75px 100px;
CSS Padding
• Padding is used to create space around an
element's content, inside of any defined
borders.
• CSS has properties for specifying the
padding for each side of an element.
• padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
• padding: 25px 50px 75px 100px;
• padding: 25px 50px;
CSS Height, Width
• The CSS height and width properties are
used to set the height and width of an
element.
• The height and width properties do not
include padding, borders, or margins. It sets
the height/width of the area inside the
padding, border, and margin of the element.
• div {
height: 200px;
width: 50%;
background-color: powderblue;
}
CSS Box Model
• All HTML elements can be considered as boxes.
• In CSS, the term "box model" is used when
talking about design and layout.
• The CSS box model is essentially a box that
wraps around every HTML element. It consists
of: margins, borders, padding, and the actual
content.
• div {
width: 300px;
border: 15px solid green;
padding: 50px;
margin: 20px;
}
CSS Box Model
• In CSS, the term "box model" is used when
talking about design and layout.
CSS Outline
• An outline is a line that is drawn around
elements, OUTSIDE the borders, to make the
element "stand out".
• CSS has the following outline properties:
• outline-style: solid;
• outline-color: red;
• outline-width: 5px;
thin (typically 1px)
medium (typically 3px)
thick (typically 5px)
• outline-offset: 15px;
• outline:5px solid yellow;
CSS Text
• Text color & Text Background
• Text Alignment
• In this chapter you will learn about the
following properties:
• text-align
• text-align: center;
• text-align: left;
• text-align: right;
• text-align: justify;
Text Alignment
• The text-align property is used to set the
horizontal alignment of a text.
• A text can be left or right aligned, centered,
or justified.
• When the text-align property is set to
"justify", each line is stretched so that
every line has equal width, and the left and
right margins are straight (like in
magazines and newspapers).
CSS Text Decoration
• Text Decoration
• text-decoration-line
• text-decoration-color
• text-decoration-style
• text-decoration-thickness
• text-decoration
Text decoration line
• Add a Decoration Line to Text
• The text-decoration-line property is used to
add a decoration line to text.
• Tip: You can combine more than one value,
like overline and underline to display lines
both over and under a text.
• text-decoration-line: overline;
• text-decoration-line: line-through;
• text-decoration-line: underline;
• text-decoration-line: overline underline;
Text decoration color
• Specify a Color for the Decoration Line
• The text-decoration-color property is used
to set the color of the decoration line.
• text-decoration-line: overline;
• text-decoration-line: line-through;
• text-decoration-line: underline;
• text-decoration-line: overline underline;
• text-decoration-color: red;
Text decoration style
• Specify a Style for the Decoration Line
• The text-decoration-style property is used
to set the style of the decoration line.
• text-decoration-line: overline;
• text-decoration-line: line-through;
• text-decoration-line: underline;
• text-decoration-line: overline underline;
• text-decoration-style: solid;
Text decoration
thikness
• Specify the Thickness for the Decoration
Line
• The text-decoration-thickness property is
used to set the thickness of the decoration
line.
• text-decoration-line: overline;
• text-decoration-line: line-through;
• text-decoration-line: underline;
• text-decoration-line: overline underline;
• text-decoration-thickness: 5px;
• text-decoration-thickness: auto;
Text decoration
• The Shorthand Property
• The text-decoration property is a shorthand
property for:
• text-decoration: underline red double 5px;
• text-transform: uppercase;
• text-transform: lowercase;
• text-transform: capitalize;
CSS Text Spacing
• Letter Spacing
• The letter-spacing property is used to specify
the space between the characters in a text.
• The following example demonstrates how to
increase or decrease the space between
characters:
• letter-spacing: 5px;
• Line Height
• The line-height property is used to specify the
space between lines:
• line-height: 0.8;
CSS Text Spacing
• Word Spacing
• The word-spacing property is used to
specify the space between the words in a
text.
• The following example demonstrates how to
increase or decrease the space between
words:
• word-spacing: 10px;
• Text Indentation
• The text-indent property is used to specify
the indentation of the first line of a text:
• text-indent: 50px;
CSS Text Shadow
• Text Shadow
• The text-shadow property adds shadow to text.
• In its simplest use, you only specify the
horizontal shadow (2px) and the vertical
shadow (2px).
• text-shadow: 2px 2px;
• Next, add a color (red) to the shadow.
• text-shadow: 2px 2px red;
• Then, add a blur effect (5px) to the shadow.
• text-shadow: 2px 2px 5px red;
• text-shadow: 1px 1px 2px black, 0 0 25px blue,
0 0 5px darkblue;
CSS Fonts
• Using a font that is easy to read is
important. The font adds value to your text.
It is also important to choose the correct
color and text size for the font.
• In CSS, we use the font-family property to
specify the font of a text.
• If the font name is more than one word, it
must be in quotation marks, like: "Times
New Roman".
• Example:-
• font-family: "Times New Roman", Times,
serif;
CSS Fonts
• Font Style
• The font-style property is mostly used to
specify italic text.
• font-style: normal; - italic, oblique.
• Font Weight
• The font-weight property specifies the
weight of a font:
• font-weight: bold;
• font-weight: lighter;
• font-weight: 900;
CSS Fonts
• Font Size
• The font-size property sets the size of the text.
• Being able to manage the text size is important
in web design. However, you should not use
font size adjustments to make paragraphs look
like headings, or headings look like paragraphs.
• Note: If you do not specify a font size, the
default size for normal text, like paragraphs, is
16px (16px=1em).
• Example:-
• font-size: 40px;
CSS Google Fonts
• If you do not want to use any of the
standard fonts in HTML, you can use Google
Fonts.
• Google Fonts are free to use, and have more
than 1000 fonts to choose from.
• How To Use Google Fonts
• Just add a special style sheet link in the
<head> section and then refer to the font in
the CSS.
• <link rel="stylesheet" href="https://
fonts.googleapis.com/css?
family=Audiowide">
• font-family: "Audiowide", sans-serif;
The float Property
• The float property is used for positioning and
formatting content.
• The float property can have one of the following
values:
• left - The element floats to the left of its container
• right - The element floats to the right of its container
• none - The element does not float (will be displayed
just where it occurs in the text). This is default
• div {
float: left;
float: right;
float: none;
}
CSS :hover Selector
• The :hover selector is used to select
elements when you mouse over them.
• The :hover selector can be used on all
elements, not only on links.
• Syntax:- :hover
• Example:-
• h1:hover{
background-color: yellow;
}
CSS :hover for link
• Links can be styled with any CSS property
(e.g. color, font-family, background, etc.).
• In addition, links can be styled differently
depending on what state they are in.
• @keyframes example {
from {background-color: red;}
to {background-color: yellow;}
@keyframes
• In the example above we have specified when the style
will change by using the keywords "from" and "to" (which
represents 0% (start) and 100% (complete)).
• It is also possible to use percent. By using percent, you
can add as many style changes as you like.
• @keyframes example {
0% {background-color:red; left:0px; top:0px;}
25% {background-color:yellow; left:200px; top:0px;}
50% {background-color:blue; left:200px; top:200px;}
75% {background-color:green; left:0px; top:200px;}
100% {background-color:red; left:0px; top:0px;}
• You can also provide position to change the direction.
CSS Animations
• animation-iteration-count
• The animation-iteration-count property specifies
the number of times an animation should run.
• animation-direction
• The animation-direction property specifies
whether an animation should be played
forwards, backwards or in alternate cycles.
• reverse - The animation is played in (backwards)
• alternate - The animation is played forwards
first, then backwards
• alternate-reverse - The animation is played
backwards first, then forwards
CSS Transforms
• CSS transforms allow you to move, rotate,
scale, and skew elements.
• CSS Transforms Methods
• translate()
• rotate()
• scaleX()
• scaleY()
• scale()
• skewX()
• skewY()
• skew()
CSS Transforms
• translate()
• The translate() method moves an element
from its current position (according to the
parameters given for the X-axis and the Y-
axis).
• transform: translate(50px,100px);
• rotate()
• The rotate() method rotates an element
clockwise or counter-clockwise according to
a given degree.
• transform: rotate(20deg);
CSS Transforms
• scale()
• The scale() method increases or decreases
the size of an element (according to the
parameters given for the width and height).
• transform: scale(2, 3);
• The scaleX() method increases or decreases
the width of an element.
• transform: scaleX(2);
• The scaleY() method increases or decreases
the height of an element.
• transform: scaleY(3);
CSS Transforms
• skew()
• The skew() method skews an element along
the X and Y-axis by the given angles.
• transform: skew(20deg, 10deg);
• The skewX() method skews an element
along the X-axis by the given angle.
• transform: skewX(20deg);
• The skewY() method skews an element
along the Y-axis by the given angle.
• transform: skewY(20deg);
CSS Gradients
• CSS gradients let you display smooth
transitions between two or more specified
colors.
• CSS defines two types of gradients:
• Linear Gradients (goes
down/up/left/right/diagonally)
• Radial Gradients (defined by their center)
• background-image: linear-
gradient(direction, color-stop1, color-stop2,
...);
• background-image: radial-gradient(shape
size at position, start-color, ..., last-color);
CSS Linear Gradients
• 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.
• Direction - Top to Bottom (this is
default)
• background-image: linear-gradient(red,
yellow);
• Direction - Left to Right
• background-image: linear-gradient(to right,
red , yellow);
CSS Linear Gradients
• Direction - Diagonal
• You can make a gradient diagonally by
specifying both the horizontal and vertical
starting positions.
• background-image: linear-gradient(to bottom
right, red, yellow);
• Using Angles
• background-image: linear-gradient(180deg, red,
yellow);
• Repeating a linear-gradient
• background-image: repeating-linear-
gradient(red, yellow 10%, green 20%);
CSS Radial Gradients
• A radial gradient is defined by its center.
• To create a radial gradient you must also
define at least two color stops.
• Radial Gradient - Evenly Spaced Color Stops
(this is default)
• background-image: radial-gradient(red,
yellow, green);
• Radial Gradient - Differently Spaced Color
Stops
• background-image: radial-gradient(red 5%,
yellow 15%, green 60%);
CSS Radial Gradients
• Set Shape
• The shape parameter defines the shape. It can
take the value circle or ellipse. The default
value is ellipse.
• background-image: radial-gradient(circle, red,
yellow, green);
• The size parameter defines the size of the
gradient. It can take four values:
• closest-side, farthest-side, closest-corner
• farthest-corner
• background-image: radial-gradient(closest-
side at 60% 55%, red, yellow, black);
Form CSS
• If you only want to style a specific input
type, you can use attribute selectors.
• input[type=text] - will only select text fields
• input[type=password] - will only select
password fields
• input[type=number] - will only select
number fields
• Many Other ways you can select the form
tag.
• You can style form by using all CSS style
properties.
CSS Flexbox
• CSS Flexbox Layout Module
• flex-direction
• flex-wrap
• flex-flow
• justify-content
• align-items
• align-content
The flex-direction
Property
• The flex-direction property defines in which
direction the container wants to stack the flex
items.
• The column value stacks the flex items vertically
(from top to bottom)- flex-direction: column;
• The column-reverse value stacks the flex items
vertically (but from bottom to top)
• flex-direction: column-reverse;
• The row value stacks the flex items horizontally
(from left to right)-flex-direction: row;
• The row-reverse value stacks the flex items
horizontally (but from right to left)
• flex-direction: row-reverse;
The flex-wrap Property
• The flex-wrap property specifies whether the
flex items should wrap or not.
• The wrap value specifies that the flex items
will wrap if necessary.
• flex-wrap: wrap;
• The nowrap value specifies that the flex items
will not wrap (this is default).
• flex-wrap: nowrap;
• The wrap-reverse value specifies that the
flexible items will wrap if necessary, in
reverse order:
• flex-wrap: wrap-reverse;
The flex-flow Property
• The flex-flow property is a
shorthand property for
setting both the flex-
direction and flex-
wrap properties.
• flex-flow: row wrap;
The justify-content
Property
• The justify-content property is used to align
the flex items.
• The center value aligns the flex items at the
center of the container.
• justify-content: center;
• The flex-start value aligns the flex items at
the beginning of the container (this is
default).
• justify-content: flex-start;
• The flex-end value aligns the flex items at the
end of the container.
• justify-content: flex-end;
The justify-content
Property
• The space-around value displays the flex
items with space before, between, and after
the lines.
• justify-content: space-around;
• justify-content: space-between;
The align-items
Property
• The align-items property is used to align the
flex items.
• The center value aligns the flex items in the
middle of the container.
• align-items: center;
• The flex-start value aligns the flex items at
the top of the container.
• align-items: flex-start;
• The flex-end value aligns the flex items at
the bottom of the container.
• align-items: flex-end;
The align-content
Property
• The align-content property is used to align
the flex lines.
• The space-between value displays the flex
lines with equal space between them.
• align-content: space-between;
• The space-around value displays the flex
lines with space before, between, and after
them.
• align-content: space-around;
• The stretch value stretches the flex lines to
take up the remaining space (this is default).
• align-content: stretch;
The align-content
Property
• The center value displays display the flex
lines in the middle of the container.
• align-content: center;
• The flex-start value displays the flex lines at
the start of the container.
• align-content: flex-start;
• The flex-end value displays the flex lines at
the end of the container.
• align-content: flex-end;
CSS Flex Items
• The flex item properties are:
• order
• flex-grow
• flex-shrink
The order Property
• The order property specifies the order of
the flex items.
• The order value must be a number, default
value is 0.
• The order property can change the order of
the flex items:
• <div style="order: 3">1</div>
<div style="order: 2">2</div>
<div style="order: 4">3</div>
<div style="order: 1">4</div>
The flex-grow Property
• The flex-grow property specifies how much a
flex item will grow relative to the rest of the
flex items.
• The value must be a number, default value is 0.
• <div style="flex-grow: 8">3</div>
• justify-content: space-evenly;
• justify-content: space-around;
• justify-content: space-between;
• justify-content: center;
• justify-content: start;
• justify-content: end;
The grid-template
Property
• The align-content Property
• The align-content property is used
to vertically align the whole grid inside the
container.
• align-content: center;
• align-content: space-evenly;
• align-content: space-around;
• align-content: space-between;
• align-content: start;
• align-content: end;
CSS Grid
• Grid Columns
• The vertical lines of grid items are called columns.
• Grid Rows
• The horizontal lines of grid items are called rows.
• Grid Gaps
• The spaces between each column/row are
called gaps.
• You can adjust the gap size by using one of the
following properties:
• column-gap
• row-gap
• gap
CSS Grid
• CSS Grid Item - Grid Lines
• A grid container contains grid items.
• The grid-column Property:
• The grid-column property defines on which
column(s) to place an item.
• You define where the item will start, and
where the item will end.
• The lines between columns are
called column lines.
• grid-column-start: 1;
grid-column-end: 3;
CSS Grid
• The grid-row Property:
• The grid-row property defines on which row
to place an item.
• You define where the item will start, and
where the item will end.