Cascaded Style Sheet: Vighnesh P Subject Matter Expert Ethnotech Academy
Cascaded Style Sheet: Vighnesh P Subject Matter Expert Ethnotech Academy
VIGHNESH P
SUBJECT MATTER EXPERT
ETHNOTECH ACADEMY
CSS
CSS stands for Cascading Style Sheet.
CSS is used to design HTML tags.
CSS is a widely used language on the web.
HTML, CSS and JavaScript are used for web designing. It
helps the web designers to apply style on HTML tags.
It is a style sheet language which is used to describe the look
and formatting of a document written in markup language.
These are the three major benefits of CSS:
border and size had to be repeated on every web page. This was a very
long process. For example: If you are developing a large website where
fonts and color information are added on every single page, it will be
become a long and expensive process. CSS was created to solve this
problem. It was a W3C recommendation.
Example:
Here, all <p> elements on the page will be center-aligned,
specific element.
The id of an element is unique within a page, so the id selector is used to
Example:
The CSS rule below will be applied to the HTML element with
id="para1":
#para1 {
text-align: center;
color: red;
}
An id name cannot start with a number!
CSS class Selector
The class selector selects HTML elements with a specific class
attribute.
It is used with a period character . (full stop symbol) followed
Example:
In this example all HTML elements with class="center" will
and center-aligned:
p.center {
text-align: center;
color: red;
}
Example:
The CSS rule below will affect every HTML element on the
page:
*{
text-align: center;
color: green;
}
CSS Group Selector
The grouping selector is used to select all the elements with the same style
definitions.
Grouping selector is used to minimize the code. Commas are used to separate
h2 {
text-align: center;
color: red;
}
p{
text-align: center;
color: red;
}
CSS Comments
CSS comments are not displayed in the browser, but they can
help document your source code.
Comments are ignored by browsers.
A CSS comment is placed inside the <style> element, and starts
with /* and ends with */.
You can add comments wherever you want in the code.
Comments can also span multiple lines.
Example: /* This is
/* This is a single-line comment */ a multi-line
p{ comment */
color: red; /* Set text color to red */
}
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript"
src="scripts.js"></script>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<P>I have a lavender background!</P>
<H1> I am blue text </H1>
</body>
</html>
P{
width: 100%;
height: 200px;
background-color: lavender;
}
H1{
Width 50%; background-color: Green;}
<!DOCTYPE html>
<html>
<head>
<style>
P{
width: 100%;
height: 200px;
background-color: Red
}
H1{
Width 50%; background-color: Yellow;}
</style>
</head>
<body>
<P>I have a lavender background!</P>
<H1> I am blue text </H1>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
.city {
background-color: tomato;
color: white;
border: 2px solid black;
margin: 20px;
padding: 20px;
}
</style>
</head>
<body>
<div class="city">
<h2>London</h2>
<p>London is the capital of England.</p>
</div>
<div class="city">
<h2>Paris</h2>
<p>Paris is the capital of France.</p>
</div>
<div class="city">
<h2>Tokyo</h2>
<p>Tokyo is the capital of Japan.</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
#myHeader {
background-color: lightblue;
color: black;
padding: 40px;
text-align: center;
}
</style>
</head>
<body>
</body>
</html>
How to add CSS
CSS is added to HTML pages to format the document according to
information in the style sheet. There are three ways to insert CSS in HTML
documents.
Inline CSS
Inline CSS is used to apply CSS on a single line or element.
Example: <p style="color:blue">Hello CSS</p>
Internal CSS
Internal CSS is used to apply CSS on a single document or page. It can
affect all the elements of the page. It is written inside the style tag within
head section of html.
Example:
<style>
p{color:blue}
</style>
External CSS
External CSS is used to apply CSS on multiple pages or all
pages. Here, we write all the CSS code in a css file. Its
extension must be .css for example style.css.
Example:
p{color:blue}
You need to link this style.css file to your html pages like this:
◦ background-color
◦ background-image
◦ background-repeat
◦ background-attachment
◦ background-position
CSS background-color
The background-color property is used to specify the
body {
background-color: lightblue;
}
Note:
With CSS, a color is most often specified by:
Example:
Set the background image for a page:
body {
background-image: url("paper.jpeg");
}
When using a background image, use an image that does not disturb the text.
The background image can also be set for specific elements, like the <p>
element:
Example:
p{
background-image: url("paper.jpeg");
}
CSS Background Repeat
By default, the background-image property repeats the
background image horizontally and vertically. Some images
are repeated only horizontally or vertically.
Example:
body {
background-image: url(" paper.jpeg ");
}
If the image above is repeated only horizontally set,
background-repeat: repeat-x;
To repeat an image vertically, set
background-repeat: repeat-y;
Showing the background image only once is also specified by
the background-repeat property:
Example:
Show the background image only once:
body {
background-image: url(“paper.jpeg");
background-repeat: no-repeat;
}
The background image is placed in the same place as the text.
We have to change the position of the image, so that it does
not disturb the text too much.
CSS background-position
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
}
You can set the following positions:
Center,top,bottom,left,right
CSS Background Attachment
The background-attachment property is used to specify if the background
image is fixed or scroll with the rest of the page in browser window.
If you set fixed the background image then the image will not move during
scrolling in the browser.
Example:
Specify that the background image should be fixed:
body {
background-image: url("img_tree.png");
background-repeat: no-repeat;
background-position: right top;
background-attachment: fixed;
}
To Specify that the background image should scroll with the rest of the page
background-attachment: scroll;
CSS Borders
The width can be set as a specific size (in px, pt, cm, em, etc) or
p.four {
border-style: dotted;
border-width: thick;
}
The border-width property can have from one to four values
(for the top border, right border, bottom border, and the left
border):
Example:
p.one {
border-style: solid;
border-width: 5px 20px;
/* 5px top and bottom, 20px on the sides */
}
p.three {
border-style: solid;
border-width: 25px 10px 4px 35px;
/* 25px top, 10px right, 4px bottom and 35px left */
}
<!DOCTYPE html>
<html>
<head>
<title>Password Input Control</title>
</head>
<body>
<form >
User ID : <input type = "text" name = "user_id" />
<br>
Password: <input type = "password" name = "password" />
</form>
</body>
</html>
CSS Border Color
The border-color property is used to set the color of the four
borders.
The color can be set by:
border-style: solid;
border-color: red;
}
p.two {
border-style: dotted;
border-color: hsl(0, 100%, 50%);
}
The border-color property can have from one to four values (for
the top border, right border, bottom border, and the left border).
p.one {
border-style: solid;
border-color: red green blue yellow;
/* red top, green right, blue bottom and yellow left */
}
CSS Border Sides
p{
border-style: dotted solid double dashed;
}
/* Three values */
p{
border-style: dotted solid double;
}
/* Two values */
p{
border-style: dotted solid;
}
/* One value */
p{
border-style: dotted;
}
CSS Rounded Borders
The border-radius property is used to add rounded borders to an element.
Example:
p{
p.normal {
border: 2px solid red;
padding: 5px;
}
p.round1 {
border: 2px solid red;
border-radius: 5px;
padding: 5px;
}
CSS Margins
Margins are used to create space around elements, outside of any defined
borders.
Margin - Individual Sides
CSS has properties for specifying the margin for each side of an element:
◦ margin-top
◦ margin-right
◦ margin-bottom
◦ margin-left
p{
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}
OR
defined borders.
There are properties for setting the padding for each side of an element
CSS has properties for specifying the padding for each side of an element:
◦ padding-top
◦ padding-right
◦ padding-bottom
◦ padding-left
div {
padding-top: 50px;
padding-right: 30px;
padding-bottom: 50px;
padding-left: 80px;
}
padding: 25px;
of the border.
The proportion of the margin region is equal to the margin-box width
and height. It is better to separate the product from its neighbor nodes.
Padding Field
This field requires the padding of the component. This area is the space
around the subject area and inside the border-box. The height and the
width of the padding box decide its proportions.
Content Field
Material such as text, photographs, or other digital media is included in
this area.
CSS Outline
An outline is a line drawn outside the element's border.
It facilitates you to draw an extra border around an element to
get visual attention.
The outline-style property specifies the style of the outline, and can
have one of the following values:
The outline-width property specifies the width of the outline, and can have
one of the following values:
◦ thin (typically 1px)
◦ medium (typically 3px)
◦ thick (typically 5px)
◦ A specific size (in px, pt, cm, em, etc)
Example:
p.ex1 {
Example
p{
margin: 30px;
border: 1px solid black;
outline: 1px solid red;
outline-offset: 15px;
}
Difference between Border and Outline
At first glance, border and outline look similar, but there are
some very important differences between them:
h1 {
background-color: black;
color: white;
}
div {
background-color: blue;
color: white;
}
CSS 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.
Example:
h1 {
text-align: center;
}
h2 {
text-align: left;
}
h3 {
text-align: right;
}
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):
Example:
div {
text-align: justify;
}
You can combine more than one value, like overline and underline to display
text-decoration-line: overline;
}
h2 {
text-decoration-line: line-through;
}
h3 {
text-decoration-line: underline;
}
p{
text-decoration-line: overline underline;
}
All links in HTML are underlined by default. Sometimes you
see that links are styled with no underline. The text-
decoration: none; is used to remove the underline from links,
like this:
Example:
a{
text-decoration: none;
}
All CSS text-decoration Properties
text-decoration
in a text.
It can be used to turn everything into uppercase or lowercase letters, or
Example:
p.uppercase {
text-transform: uppercase;
}
p.lowercase {
text-transform: lowercase;
}
p.capitalize {
text-transform: capitalize;
}
CSS Text Spacing
text-indent
◦ The text-indent property is used to specify the indentation of the
first line of a text
letter-spacing
◦ The letter-spacing property is used to specify the space between
the characters in a text.
line-height
◦ The line-height property is used to specify the space between
lines
word-spacing
◦ The word-spacing property is used to specify the space between
the words in a text.
white-space
◦ The white-space property specifies how white-space inside an
element is handled.
CSS Text Shadow
The text-shadow property adds shadow to text.
In its simplest use, you only specify the horizontal shadow (2px)
Example:
h1 { text-shadow: 2px 2px; }
font-variant
font-weight
font-size/line-height
font-family
font-style
The font-style property is mostly used to specify italic text. This
Font Variant
The font-variant property specifies whether or not a text should be
p.small {
font-variant: small-caps;
}
Font Weight
The font-weight property specifies the weight of a font:
Example:
p.normal {
font-weight: normal;
}
p.thick {
font-weight: bold;
}
Font Size
The font-size property sets the size of the text.
If you do not specify a font size, the default size for normal text,
h1 {
font-size: 40px;
}
h1 {
font-size: 2.5em; /* 40px/16=2.5em */
}
body {
font-size: 100%;
}
In CSS there are five generic font families:
Serif fonts have a small stroke at the edges of each letter. They
create a sense of formality and elegance.
Monospace fonts - here all the letters have the same fixed
width. They create a mechanical look.
The following list are the best web safe fonts for HTML
and CSS:
◦ Arial (sans-serif)
◦ Verdana (sans-serif)
◦ Helvetica (sans-serif)
◦ Tahoma (sans-serif)
◦ Trebuchet MS (sans-serif)
◦ Times New Roman (serif)
◦ Georgia (serif)
◦ Garamond (serif)
◦ Courier New (monospace)
◦ Brush Script MT (cursive)
Font Shorthand
It is also possible to specify all the individual font properties
in one property.
Example:
p.b {
font: italic small-caps bold 12px/30px Georgia, serif;
}
CSS Links
With CSS, links can be styled in many different ways.
Links can be styled differently depending on what state they are in.
rules:
◦ a:hover MUST come after a:link and a:visited
◦ a:active MUST come after a:hover
Example:
a:link {
background-color: yellow;
}
a:visited {
background-color: cyan;
}
a:hover {
background-color: lightgreen;
}
a:active {
background-color: hotpink;
}
CSS Lists
ul.b {
list-style-type: square;
list-style-position: inside;
ol.c {
list-style-type: upper-roman;
list-style-image: url('star1.png')
ol.d {
list-style-type: lower-alpha;
}
CSS Tables
We can apply style on HTML tables for better look and feel.
There are some CSS properties that are widely used in
designing table using CSS:
◦ border
◦ border-collapse
◦ padding
◦ width
◦ height
◦ text-align
◦ color
◦ background-color
Example:
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 10px;
}
table#alter tr:nth-child(even) {
background-color: yellowgreen;
}
table#alter tr:nth-child(odd) {
background-color: lightpink;
}
table#alter th {
color: white;
background-color: indianred;
}
</style>
<table id="alter">
<tr><th>First_Name</th><th>Last_Name</th><th>Marks</th></tr>
<tr><td>Sonoo</td><td>Jaiswal</td><td>60</td></tr>
<tr><td>James</td><td>William</td><td>80</td></tr>
<tr><td>Swati</td><td>Sironi</td><td>82</td></tr>
<tr><td>Chetna</td><td>Singh</td><td>72</td></tr>
</table>
CSS Display
CSS display is the most important property of CSS which is used
These are following CSS display values which are commonly used.
display: inline;
display: inline-block;
display: block;
display: run-in;
display: none;
Block-level Elements
A block-level element always starts on a new line and takes up
the full width available (stretches out to the left and right as far
as it can).
Examples:
◦ <div>
◦ <h1> - <h6>
◦ <p>
◦ <form>
◦ <header>
◦ <footer>
◦ <section>
Inline Elements
An inline element does not start on a new line and only takes up as
Examples:
◦ <span>
◦ <a>
◦ <img>
p{
display: inline-block;
}
a{
display: block;
}
h1.hidden {
visibility: hidden;
}
span {
display: block;
<divclass="topnav">
<a class="active" href="#home">H
ome</a>
<a href=“link file">News</a>
<a href=“link file">Contact</a>
<a href=“link file">About</a>
</div>
/* Add a black background color to the top navigation */
.topnav {
background-color: #333;
overflow: hidden;
}
/* When you mouse over the navigation links, change their color */
.sidenav a:hover {
color: #f1f1f1;
}
/* On smaller screens, where height is less than 450px, change the style of the sidebar (less padding
and a smaller font size) */
@media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}