CSS (Cascading Style Sheets) is a language used to style and lay out web pages. It allows separation of document content from document presentation and formatting. The document discusses various CSS properties like color, font, text, margins, links, and lists that control the look and formatting of HTML elements. It also describes how to style tables and their borders, alignment, padding and other properties. External, internal, and inline styling methods are covered along with the cascade order of multiple style sheets.
2. What is CSS?
CSS stands for Cascading Style Sheets
CSS describes how HTML elements are to be
displayed on screen, paper, or in other media
CSS saves a lot of work. It can control the layout
of multiple web pages all at once
External stylesheets are stored in CSS files
Priyanka Pradhan
3. Why Use CSS?
CSS is used to define styles for your web
pages, including the design, layout and
variations in display for different devices and
screen sizes.
Priyanka Pradhan
4. CSS Syntax
A CSS rule-set consists of a selector and a declaration block:
The selector points to the HTML element you want to style.
The declaration block contains one or more declarations
separated by semicolons.
Each declaration includes a CSS property name and a value,
separated by a colon.
A CSS declaration always ends with a semicolon, and
declaration blocks are surrounded by curly braces.
In the following example all <p> elements will be center-aligned,
with a red text color:
Priyanka Pradhan
5. Example
p {
color: red;
text-align: center;
}
When a browser reads a style sheet, it will format
the HTML document according to the information
in the style sheet.
Priyanka Pradhan
7. <!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightblue;
}
h1 {
color: white;
text-align: center;
}
p {
font-family: verdana;
font-size: 20px;
}
</style>
</head>
<body>
<h1>My First CSS Example</h1>
<p>This is a paragraph.</p>
</body>
</html>
Priyanka Pradhan
8. My First CSS Example
This is a paragraph.
Priyanka Pradhan
9. Three Ways to Insert CSS
There are three ways of inserting a style sheet:
External style sheet
Internal style sheet
Inline style
Priyanka Pradhan
10. External Style Sheet
Each page must include a reference to the
external style sheet file inside the <link>
element.
The <link> element goes inside the <head>
section.
Priyanka Pradhan
13. Here is how the "mystyle.css" looks:
body {
background-color: lightblue;
}
h1 {
color: navy;
margin-left: 20px;
}
Priyanka Pradhan
14. Internal Style Sheet
An internal style sheet may be used if one
single page has a unique style.
Internal styles are defined within the <style>
element, inside the <head> section of an
HTML page.
Priyanka Pradhan
16. This is a heading
This is a paragraph
Priyanka Pradhan
17. Inline Styles
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.
Priyanka Pradhan
20. Multiple Style Sheets
If some properties have been defined for the
same selector (element) in different style
sheets, the value from the last read style
sheet will be used.
Priyanka Pradhan
21. Assume that an external style sheet has the
following style for the <h1> element:
h1 {
color: navy;
}
Priyanka Pradhan
22. then, assume that an internal style sheet also
has the following style for the <h1> element:
h1 {
color: orange;
}
Priyanka Pradhan
23. If the internal style is defined after the link to
the external style sheet, the <h1> elements
will be "orange"
Priyanka Pradhan
27. CSS Colors
Colors are specified using predefined color
names, or RGB, HEX, HSL, RGBA, HSLA
values.
Priyanka Pradhan
28. Background Color
You can set the background color for HTML
elements:
<h1 style="background-
color:DodgerBlue;">Hello World</h1>
<p style="background-
color:Tomato;">Para1</p>
Priyanka Pradhan
29. Text Color
You can set the color of text:
<h1 style="color:Tomato;">Hello</h1>
<p style="color:DodgerBlue;"> World </p>
<p style="color:MediumSeaGreen;">Para1</p>
Priyanka Pradhan
30. Border Color
You can set the color of borders:
<h1 style="border:2px solid Tomato;">Hello
World</h1>
<h1 style="border:2px solid Violet;">Hello
World</h1>
Priyanka Pradhan
31. CSS Margins
The CSS margin properties are used to create
space around elements, outside of any
defined borders.
Priyanka Pradhan
32. CSS has properties for specifying the margin for
each side of an element:
margin-top
margin-right
margin-bottom
margin-left
Priyanka Pradhan
33. Example
p {
margin-top: 100px;
margin-bottom: 100px;
margin-right: 150px;
margin-left: 80px;
}
Priyanka Pradhan
34. If the margin property has four values:
margin: 25px 50px 75px 100px;
top margin is 25px
right margin is 50px
bottom margin is 75px
left margin is 100px
Priyanka Pradhan
35. Margin - Shorthand Property
p {
margin: 25px 50px 75px 100px;
}
Priyanka Pradhan
36. Styling Links
Links can be styled with any CSS property
(e.g. color, font-family, background, etc.).
Example
a {
color: hotpink;
}
Priyanka Pradhan
37. In addition, links can be styled differently depending
on what state they are in.
The four links states are:
a:link - a normal, unvisited link
a:visited - a link the user has visited
a:hover - a link when the user mouses over it
a:active - a link the moment it is clicked
Priyanka Pradhan
38. /* unvisited link */
a:link {
color: red;
}
/* visited link */
a:visited {
color: green;
}
/* mouse over link */
a:hover {
color: hotpink;
}
/* selected link */
a:active {
color: blue;
}
Priyanka Pradhan
39. When setting the style for several link states,
there are some order rules:
a:hover MUST come after a:link and a:visited
a:active MUST come after a:hover
Priyanka Pradhan
40. Text Decoration The text-decoration property is mostly used to remove underlines from
links:
a:link {
text-decoration: none;
}
a:visited {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
a:active {
text-decoration: underline;
}
Priyanka Pradhan
41. CSS Text
The color property is used to set the color of the
text. The color is specified by:
a color name - like "red"
a HEX value - like "#ff0000"
an RGB value - like "rgb(255,0,0)"
Priyanka Pradhan
42. The default text color for a page is defined in the
body selector.
Example
body {
color: blue;
}
h1 {
color: green;
}
Priyanka Pradhan
64. An Image as The List Item
Marker
The list-style-image property specifies an
image as the list item marker:
Example
ul {
list-style-image: url('sqpurple.gif');
}
Priyanka Pradhan
65. Remove Default Settings
The list-style-type:none property can also be
used to remove the markers/bullets. Note that
the list also has default margin and padding.
To remove this, add margin:0 and
padding:0 to <ul> or <ol>:
Priyanka Pradhan
67. Table Borders
To specify table borders in CSS, use
the border property.
The example below specifies a black border
for <table>, <th>, and <td> elements:
Priyanka Pradhan
70. Horizontal Alignment
The text-align property sets the horizontal
alignment (like left, right, or center) of the
content in <th> or <td>.
By default, the content of <th> elements are
center-aligned and the content of <td>
elements are left-aligned.
Priyanka Pradhan
72. 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).
Priyanka Pradhan
74. Table Padding
To control the space between the border and
the content in a table, use
the padding property on <td> and <th>
elements.
Priyanka Pradhan