Unit 3
Unit 3
What is CSS?
Cascading Style Sheets is a style sheet language used for describing the presentation of a document
written in a markup language such as HTML or XML.
Advantages of CSS
CSS saves time − You can write CSS once and then reuse the same sheet in multiple
HTML pages. You can define a style for each HTML element and apply it to as many
Web pages as you want.
Easy maintenance − To make a global change, simply change the style, and all elements
in all the web pages will be updated automatically.
Global web standards − Now HTML attributes are being deprecated and it is being
recommended to use CSS. So it's a good idea to start using CSS in all the HTML pages to
make them compatible with future browsers.
Platform Independence − The Script offer consistent platform independence and can
support latest browsers as well.
A CSS Syntax rule consists of a selector, property, and its value. The selector points to the
HTML element where CSS style is to be applied. The CSS property is separated by semicolons.
It is a combination of selector name followed by the property: value pair that is defined for the
specific selector.
Syntax:
For instance, we have declared a heading tag(h1) along with having assigned some property:
value pair that is used to style the heading tag. Here, h1 is selector, { color: green; font-family:
sans-serif; } is a declaration block and it can contain one or more declarations separated by
semicolons, color: green; is a property: value pair that is applied to the HTML element in order
to style them.
Every declaration has a CSS property name and a value, separated by a colon and is surrounded
by curly braces. For declaring the multiple CSS properties, it can be separated by the semicolon.
Types of CSS
(1)Inline CSS: Inline CSS contains the CSS property in the body section attached with element
is known as inline CSS. This kind of style is specified within an HTML tag using the style
attribute.
Example:
<!DOCTYPE html>
<html>
<head>
<title>Inline CSS</title>
</head>
<body>
<p style = "color:red; font-size:50px;>
example of inline stylesheet
</p>
</body>
</html>
(2)Internal or Embedded CSS: This can be used when a single HTML document must be
styled uniquely. The CSS rule set should be within the HTML file in the head section i.e the CSS
is embedded within the HTML file.
Example:
<!DOCTYPE html>
<html>
<head>
<title>Internal CSS</title>
<style>
.main {
text-align:center;
}
.GFG {
color:pink;
font-size:50px;
font-weight:bold;
}
.geeks {
font-style:bold;
font-size:20px;
}
</style>
</head>
<body>
<div class = "main">
<div class ="GFG">testing</div>
body {
background-color:powderblue;
}
.main {
text-align:center;
}
Below is the HTML file that is making use of the created external style sheet
link tag is used to link the external style sheet with the html webpage.
href attribute is used to specify the location of the external style sheet file.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="mystyle.css"/>
</head>
<body>
<div class = "main">
A computer science portal for geeks
</div>
</body>
</html>
---------------------------------------------------------------------------------------------------------------------------------------
CSS Colors
Colors are specified using predefined color names, or RGB, HEX, HSL, RGBA, HSLA
values.
Colors are specified using predefined color names as per the following:
<html>
<body>
<h1 style="background-color:Tomato;">Tomato</h1>
<h1 style="background-color:Orange;">Orange</h1>
</body>
</html>
CSS Text Colors
You can set the color of text as per the following:
<html>
<body>
<h3 style="color:Tomato;">Hello World</h3>
</body>
</html>
In CSS, colors can also be specified using RGB values, HEX values, HSL values,
RGBA values, and HSLA values:
(1)RGB Value
Each parameter (red, green, and blue) defines the intensity of the color between 0
and 255.
For example, rgb(255, 0, 0) is displayed as red, because red is set to its highest
value (255) and the others are set to 0.
To display black, set all color parameters to 0, like this: rgb(0, 0, 0).
To display white, set all color parameters to 255, like this: rgb(255, 255, 255).
(2)HEX Value
#rrggbb
Where rr (red), gg (green) and bb (blue) are hexadecimal values between 00 and ff
(same as decimal 0-255).
For example, #ff0000 is displayed as red, because red is set to its highest value (ff)
and the others are set to the lowest value (00).
(3)HSL Value
In CSS, a color can be specified using hue, saturation, and lightness (HSL) in the
form:
Hue is a degree on the color wheel from 0 to 360. 0 is red, 120 is green, and 240 is
blue.
Saturation is a percentage value. 0% means a shade of gray, and 100% is the full
color.
(4)RGBA Value
RGBA color values are an extension of RGB color values with an alpha channel -
which specifies the opacity for a color.
The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (not
transparent at all):
(5)HSLA Value
HSLA color values are an extension of HSL color values with an alpha channel -
which specifies the opacity for a color.
---------------------------------------------------------------------------------------------------------------------------------------
CSS Background
The CSS background properties are used to add background effects for elements.
background-color
background-image
background-repeat
background-attachment
background-position
background (shorthand property)
Example:
body {
background-image: url("paper.gif");
}
(3)CSS background-repeat
Showing the background image only once is also specified by the background-
repeat: no-repeat property:
Example :
body {
background-image: url("gradient_bg.png");
background-repeat: repeat-x;
}
(4)CSS background-position
Example:
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
To shorten the code, it is also possible to specify all the background properties in
one single property. This is called a shorthand property.
When using the shorthand property the order of the property values is:
background-color
background-image
background-repeat
background-attachment
background-position
body {
background: #ffffff url("img_tree.png") no-repeat right top;
}
---------------------------------------------------------------------------
CSS Borders
The CSS border properties allow you to specify the style, width, and color of an
element's border.
The border-style property can have from one to four values (for the top border,
right border, bottom border, and the left border).
Example:
<html>
<head>
<style>
p.dotted {border-style: dotted;}
p.dashed {border-style: dashed;}
</style>
</head>
<body>
<h2>The border-style Property</h2>
<p class="dotted">A dotted border.</p>
<p class="dashed">A dashed border.</p>
</body>
</html>
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.one {
border-style: solid;
border-width: 5px;
}
p.three {
border-style: solid;
border-width: 25px 10px 4px 35px; /* 25px top, 10px right, 4px bottom and
35px left */
}
The border-color property is used to set the color of the four borders.
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;
The border property is a shorthand property for the following individual border
properties:
border-width
border-style (required)
border-color
example:
p {
border: 5px solid red;
}
Example:
p {
border: 2px solid red;
border-radius: 5px;
}
---------------------------------------------------------------------------
CSS Margins
The CSS margin properties are used to create space around elements, outside of
any defined borders.
CSS has properties for specifying the margin for each side of an element:
margin-top
margin-right
margin-bottom
margin-left
Example:
p {
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}
(*) Margin - Shorthand Property
To shorten the code, it is possible to specify all the margin properties in one
property.
The margin property is a shorthand property for the following individual margin
properties:
margin-top
margin-right
margin-bottom
margin-left
margin: 25px;
o all four margins are 25px
-------------------------------------------------------------------------------------------------
CSS Padding
The CSS padding properties are used to generate space around an element's
content, inside of any defined borders.
With CSS, you have full control over the padding. There are properties for setting
the padding for each side of an element (top, right, bottom, and left).
CSS has properties for specifying the padding for each side of an element:
padding-top
padding-right
padding-bottom
padding-left
Example,
div {
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
}
To shorten the code, it is possible to specify all the padding properties in one
property.
The padding property is a shorthand property for the following individual padding
properties:
padding-top
padding-right
padding-bottom
padding-left
So, here is how it works:
padding: 25px;
o all four paddings are 25px
==================================================
The CSS height and width properties are used to set the height and width of an
element.
The height and width properties may have the following values:
auto - This is default. The browser calculates the height and width
length - Defines the height/width in px, cm, etc.
% - Defines the height/width in percent of the containing block
initial - Sets the height/width to its default value
inherit - The height/width will be inherited from its parent value
Example,
div {
height: 200px;
width: 50%;
background-color: powderblue;
}
=====================================================================
CSS Text
(1)Text Color
The color property is used to set the color of the text. The color is specified by:
Example,
h1 {
color: green;
}
(2)Background Color
Example,
body {
background-color: lightgrey;
}
(3)Text Alignment
Example,
h1 {
text-align: center;
}
h2 {
text-align: left;
}
h3 {
text-align: right;
}
The text-align-last property specifies how to align the last line of a text.
p.a {
text-align-last: right;
}
(5)Text Direction
The direction and unicode-bidi properties can be used to change the text direction
of an element:
p {
direction: rtl;
unicode-bidi: bidi-override;
}
(6)Vertical Alignment
Example,
img.a {
vertical-align: baseline;
}
img.b {
vertical-align: text-top;
}
img.c {
vertical-align: text-bottom;
}
img.d {
vertical-align: sub;
}
img.e {
vertical-align: super;
}
(7)Text Decoration
Example,
h1 {
text-decoration-line: overline;
text-decoration-color: purple;
text-decoration-style: double;
text-decoration-thickness: 5px;
}
text-decoration-line (required)
text-decoration-color (optional)
text-decoration-style (optional)
text-decoration-thickness (optional)
Example,
{
text-decoration: underline red double 5px;
}
(8)Text Transformation
Example,
p.uppercase {
text-transform: uppercase;
}
p.lowercase {
text-transform: lowercase;
}
p.capitalize {
text-transform: capitalize;
}
(9)Text Spacing
Text spacing includes following properties:
text-indent
letter-spacing
line-height
word-spacing
white-space
(10)Text Indentation
The text-indent property is used to specify the indentation of the first line of
a text:
p {
text-indent: 50px;
}
(11)Letter Spacing
The letter-spacing property is used to specify the space between the characters in
a text.
h1 {
letter-spacing: 5px;
}
(12)Line Height
p.big {
line-height: 1.8;
}
(13)Word Spacing
The word-spacing property is used to specify the space between the words in a text.
p.one {
word-spacing: 10px;
}
White Space
p {
white-space: nowrap;
}
(14)Text Shadow
In its simplest use, you only specify the horizontal shadow (2px) and the vertical
shadow (2px), blur effect (5px) and color red.
h1 {
text-shadow: 2px 2px 5px red;
}
===========================================================================
CSS Fonts
1. Serif fonts have a small stroke at the edges of each letter. They create a
sense of formality and elegance.
2. Sans-serif fonts have clean lines (no small strokes attached). They create a
modern and minimalistic look.
3. Monospace fonts - here all the letters have the same fixed width. They
create a mechanical look.
4. Cursive fonts imitate human handwriting.
5. Fantasy fonts are decorative/playful fonts.
.p1 {
font-family: "Times New Roman", Times, serif;
}
(2)Font Style
p.normal {
font-style: normal;
}
p.italic {
font-style: italic;
}
p.oblique {
font-style: oblique;
}
(3)Font Weight
p.normal {
font-weight: normal;
}
p.thick {
font-weight: bold;
}
(4)Font Variant
p.normal {
font-variant: normal;
}
p.small {
font-variant: small-caps;
}
(5)Font Size
h1 {
font-size: 40px;
}
h2 {
font-size: 1em; /* 1em=16px */
}
body {
font-size: 100%;
}
(*)Responsive Font Size
The text size can be set with a vw unit, which means the "viewport width".
p.b {
font: italic small-caps bold 12px/30px Georgia, serif;
}
===========================================================================
CSS Icons
Icons can easily be added to your HTML page, by using an icon library.
1.The simplest way to add an icon to your HTML page, is with an icon library, such
as Font Awesome.
2.Add the name of the specified icon class to any inline HTML element
(like <i> or <span>).
3.All the icons in the icon libraries below, are scalable vectors that can be
customized with CSS (size, color, shadow, etc.)
To use the Font Awesome icons, go to fontawesome.com, sign in, and get a code to
add in the <head> section of your HTML page:
<script src="https://kit.fontawesome.com/yourcode.js"
crossorigin="anonymous"></script>
Example,
<!DOCTYPE html>
<html>
<head>
<script src="https://kit.fontawesome.com/a076d05399.js" crossorigin="anonymou
s"></script>
</head>
<body>
</body>
</html>
To use the Bootstrap glyphicons, add the following line inside the <head> section of
your HTML page:
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/
css/bootstrap.min.css">
</head>
<body>
</body>
</html>
(3) Google Icons
To use the Google icons, add the following line inside the <head> section of your
HTML page:
<link rel="stylesheet"
href="https://fonts.googleapis.com/icon?family=Material+Icons">
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Materia
l+Icons">
</head>
<body>
<i class="material-icons">cloud</i>
<i class="material-icons">favorite</i>
<i class="material-icons">attachment</i>
<i class="material-icons">computer</i>
<i class="material-icons">traffic</i>
</body>
</html>
=========================================================================
CSS Links
links can be styled differently depending on what state they are in.
Example,
/* unvisited link */
a:link {
color: red;
}
/* visited link */
a:visited {
color: green;
}
/* selected link */
a:active {
color: blue;
}
======================================================================
ul.a {
list-style-type: circle;
}
ul {
list-style-image: url('sqpurple.gif');
}
"list-style-position: outside;" means that the bullet points will be outside the list
item.
"list-style-position: inside;" means that the bullet points will be inside the list item.
ul.a {
list-style-position: outside;
}
ul.b {
list-style-position: inside;
}
The list-style property is a shorthand property. It is used to set all the list
properties in one declaration:
ul {
list-style: square inside url("sqpurple.gif");
}
========================================================================
CSS Tables
Firstname Lastname
Peter Griffin
Lois Griffin
(1)Table Borders
The example below specifies a solid border for <table>, <th>, and <td> elements:
table, th, td {
border: 1px solid;
}
(2)Full-Width Table
The table above might seem small in some cases. If you need a table that should
span the entire screen (full-width), add width: 100% to the <table> element:
table {
width: 100%;
}
(3)Collapse Table Borders
The border-collapse property sets whether the table borders should be collapsed
into a single border:
table {
border-collapse: collapse;
}
The width and height of a table are defined by the width and height properties.
The example below sets the width of the table to 100%, and the height of the <th>
elements to 70px:
table {
width: 100%;
}
th {
height: 70px;
}
(5)Horizontal Alignment
The text-align property sets the horizontal alignment (like left, right, or center) of
the content in <th> or <td>.
td {
text-align: center;
}
(6)Vertical Alignment
The vertical-align property sets the vertical alignment (like top, bottom, or
middle) of the content in <th> or <td>.
By default, the vertical alignment of the content in a table is middle (for both <th>
and <td> elements).
td {
height: 50px;
vertical-align: bottom;
}
(7)Table Padding
To control the space between the border and the content in a table, use
the padding property on <td> and <th> elements:
th, td {
padding: 15px;
text-align: left;
}
(8)Horizontal Dividers
Add the border-bottom property to <th> and <td> for horizontal dividers:
th, td {
border-bottom: 1px solid #ddd;
}
(9)Hoverable Table
Use the :hover selector on <tr> to highlight table rows on mouse over:
(10)Striped Tables
For zebra-striped tables, use the nth-child() selector and add a background-
color to all even (or odd) table rows:
(11)Table Color
The example below specifies the background color and text color of <th> elements:
th {
background-color: #04AA6D;
color: white;
}
(*)CSS Responsive Table
A responsive table will display a horizontal scroll bar if the screen is too small to
display the full content:
<div style="overflow-x:auto;">
<table>
... table content ...
</table>
</div>
===============================================
CSS Pseudo-classes
Syntax
selector:pseudo-class {
property: value;
}
Example,
/* unvisited link */
a:link {
color: #FF0000;
}
/* visited link */
a:visited {
color: #00FF00;
}
/* selected link */
a:active {
color: #0000FF;
}
The :first-child pseudo-class matches a specified element that is the first child of
another element.
p:first-child {
color: blue;
}
==================================================
CSS Pseudo-elements
Syntax
selector::pseudo-element {
property: value;
}
following table contains Pseudo elements:
Example,
p::first-line {
color: #ff0000;
font-variant: small-caps;
}
p::first-letter {
color: #ff0000;
font-size: xx-large;
}
p.intro::first-letter {
color: #ff0000;
font-size: 200%;
}
h1::before {
content: url(smiley.gif);
}
h1::after {
content: url(smiley.gif);
}
::marker {
color: red;
font-size: 23px;
}
::selection {
color: red;
background: yellow;