Lesson - 2 (CSS-Cascading Style Sheets)
Lesson - 2 (CSS-Cascading Style Sheets)
<html>
<head>
<style type="text/css">
p {color: green}
</style>
</head>
<body> <p> The text in this paragraph will be green. </p>
<p> This paragraph too. </p>
</body>
</html>
Example:
<html>
<head>
<link rel="stylesheet" type="text/css" href="style1.css" />
</head>
<body>
<p> The text in this paragraph will be blue. </p>
</body>
</html>
The code from style1.css:
p {color:blue}
Multiple stylesheets
Stylesheets by priority:
Inline stylesheet - An inline stylesheet has the highest priority. It will
override styles declared in an internal stylesheet, an external stylesheet,
and a web browsers default styles.
Internal stylesheet - An internal stylesheet has the second highest
priority. It will override styles declared in an external stylesheet and a
web browsers default styles.
External stylesheet - An external stylesheet has the third highest
priority. It will override a web browsers default styles.
Browsers default styles - A web browsers default styles have the
lowest priority. A web browsers default styles will be used when there are
no styles set for an element in an inline, internal, or external stylesheet.
Example:
<html>
<head>
<link rel="stylesheet" type="text/css" href="style2.css" />
<style type="text/css">
<!-- p {color: orange} -->
</style>
</head>
<body>
<p style="color: yellow"> The text in this paragraph will be yellow. </p>
</body>
</html>
•CSS syntax
•Grouping styles
Example:
body {background-color: gray}
h2 {font-family: "Trebuchet MS"}
body {
background-color: gray;
color: yellow;
}
Grouping styles
You can specify that a group of tags will have the same
styles by grouping them together.
Example:
p, h1, h2, h3, h4{
color: blue;
font-family: courier;
}
In this example, all text declared with the tags <p>,
<h1>, <h2>, <h3>, and <h4> will be blue in color and
Courier in font.
Lesson -2 ( CSS-Cascading Style Sheets) 11/02/2021
CSS Classes and ID's
14
•Classes
•Id's
With classes, you can specify different styles for the same HTML tag.
For example, you can specify two paragraphs to have different color
text:
Example:
p.green {color: green;}
p.blue {color: blue;}
To specify a tag as part of a class, you have to use the class attribute
within that tag:
Example:
<p class="green">This text is green</p>
<p class="blue">This text is blue</p>
Styles for classes are specified with the . (dot) character followed by
the class name in an internal or external stylesheet.
Example:
<style type="text/css">
.text{
font-family: tahoma;
font-size: 16px;
font-weight: bold;
color: green;
}
</style>
In the above example, the class text is specified to have a font
type of tahoma, a font size of 16 pixels, to be bold, and green.
Any tag specified as part of the text class will get these
properties.
Example:
<html>
<head> <title>Background properties</title>
<style type="text/css">
body {background-color: #f0f8ff;}
h1 {background-color: lightsteelblue;}
p {background-color: rgb(220, 220, 220);}
a {background-color: #e6dcbe;} </style> </head>
<body> <h1>Some text</h1>
<p> A paragraph </p>
<a href="http://www.landofcode.com">
Landofcode.com main page </a>
</body> </html>
Example:
<html> <head> <title>Setting a background image</title> <style
type="text/css">
body {background-image: url(/https/www.scribd.com/images/ob020.jpg);}
table {background-image: url(/https/www.scribd.com/images/apple.jpg);}
</style>
</head>
<body>
<table border="2">
<tr> <td>Cell 1</td> <td>Cell 2</td> </tr>
<tr> <td>Cell 3</td> <td>Cell 4</td> </tr>
</table> </body> </html>
•S e t t i n g t e x t c o l o r
•A l i g n i n g t e x t
•D e c o r a ti n g t e x t
•S e t t i n g l e t t er s p a c i n g
Text color is set with the color property. You can specify the color
of text with a color name, an RGB value, or a hexadecimal value.
Example:
<html> <head><title>Background properties</title>
<style type="text/css">
h1 {color: green;}
p {color: #00008b;}
p.text{color: rgb(47, 49, 49);}
</style>
</head>
<body> <h1>This text is green</h1> <p>This text is darkblue</p> <p
class="text">This text is darkslategray</p> </body>
</html>