Chapter 3 Cascading Style Sheet (CSS)
Chapter 3 Cascading Style Sheet (CSS)
A cascading style sheet is a separate file which contains all the information about how the text,
pictures and layout of your web pages actually looks. CSS allows you to change the color of any
image on the page, add backgrounds and borders, change the visual appearance of elements, as
well as customize the entire layout of your page. Additionally, CSS allows you to keep your HTML
simple because all the formatting is stored in the CSS. CSS is efficient, too, because it allows you
to reuse a style across multiple pages. If HTML gives your pages structure, CSS give them beauty.
3.1.1. CSS basics
Styles are simply a list of selectors. Each selector can have a number of style rules. Each rule
describes some attribute of the selector. To set up a style, keep the following in mind
Begin with the style tags:- describe your style in the header area
Include the style type in the header area: - The style type is always “text/css”. The
beginning of <style> tag always looks like the following
<style type = “text/css”>
Define an element: - Use the element name to begin the definition of a particular
element’s style. For example the style rule for the body element is designated like the
following
body {
color: yellow;
background-color: red;
}
Use braces ({}) to enclose the style rules
Give a rule name (attributes)
A colon (:) character always follows the rule name
Enter the rule’s value :- attribute value is followed by a semicolon
3.1.2. CSS Syntax (CSS Selectors and Declarations)
Class Selector one specified after the period (or full border: 1px solid #665544;
background-color: #efefef;
stop) symbol
padding: inherit;
Defined using period and any unique
}
name
Matches an element whose id #intro {
ID Selector attribute has a value that matches the font-size: 100%;
one specified after the pound or hash color: pink;
symbol font-family: sans-
Defined using hash or pound sign serif;
( # ) and any unique name }
Example
<body>
<p style = "color:green;font-size:20px;"> Long Live for Ethiopia</p>
</body>
Internal Style Sheet
o Internal style sheet rules are included in the header section of the HTML
document using <style> tag
Example
<html>
<head>
<style type = "text/css">
.wow { color: red;
font-size:20px; }
</style>
</head>
<body>
<p class = "wow"> Long Live for Ethiopia</p>
</body>
</html>
External Style Sheet
o To use style sheet rules to various pages, it is always recommended to define a
common style sheet in a separate file and include it in HTML files using <link>
tag.
Example
<head>
The above css rule applies to <body> element and it indicates that the background color of the
body element should be red.
The main components included in a css rule are described as follow
Selector :- indicates which element or elements the declaration applies to
Declaration: - sets out how the elements referred to in the selector should be styled.
It split into two parts, separated by a colon
Property :- the property of the selected element(s) that you want to affect
Value:- specification of the property
In the above example, the <div> element with a class called page inherits the padding size from
the CSS rule that applies to the <body> element.
The properties are grouped together into related functionality; for example, there are properties
that allow you to control the presentation of tables, lists, and backgrounds. The following table
shows some of the main properties available to you.
In CSS, these two design aspects can be styled with the following two properties:
Example
h1 {
color: red;
background-color: blue;
In the example above, the text of the heading will appear in red, and the background of the heading
will appear blue.
Foreground Properties
The color property sets the foreground color of an element (typically, the color of the text).
Syntax
Syntax:
color: name-of-the-color;
Example
h1 {
color: black;
}
3. RGB/RGBA Value: Here R stands for Red, G stands for Green, and B stands for Blue. The
color will be assigned to the text by using the range of these values. These values range
from 0 to 255.
Syntax:
color: RGB(value, value, value);
Example
h1 {
color: RGB(0, 0, 0);
}
p {
color: RGB(0, 150, 0);
}
4. Hexa-Decimal Value: It represents the value of the color in hexadecimal format. It should
start with the prefix #. These values ranges from #000000 to #FFFFFF.
Syntax:
color: #RRGGBB;
Example
Syntax:
color: HSL(value, value, value);
Example
h1 {
color: HSL(0, 0, 0);
}
p{
color: HSL(147, 50%, 47%);
}
6. initial: This value will set the value of the color to its default value.
Syntax:
color: initial;
Example
p {
font-size: 20px;
color: initial;
}
Background Properties
The CSS background properties are used to define the background effects for elements. There are lots of
properties to design the background. CSS background properties are described as follows.
Several properties allow you to control the appearance of text in your documents. These can be
split into two groups:
Those that directly affect the font and its appearance (including the typeface used, whether
it is regular, bold or italic, and the size of the text)
Those that would have the same effect on the text irrespective of the font used (these
include color of the text and the spacing between words or letters)
The CSS font is used to set the font’s content of the HTML element. There are many font properties
in CSS which are mentioned and briefly discussed below
Content: This property is used to displays the text, images, etc, that can be sized using the
width & height property.
Padding: This property is used to create space around the element, inside any defined
border.
border: This property is used to cover the content & any padding, & also allows to set the
style, color, and width of the border.
margin: This property is used to create space around the element ie., around the border
area.
Content Area: This area consists of content like text, images, or other media content. It is
bounded by the content edge and its dimensions are given by content-box width and height.
Example
<!DOCTYPE html>
<head>
<style>
.main {
font-size: 32px;
font-weight: bold;
text-align: center;
}
#box {
padding-top: 40px;
width: 400px;
height: 100px;
border: 50px solid green;
margin: 50px;
text-align: center;
font-size: 32px;
font-weight: bold;
}
</style>
Example
The following html code is the navigation menu section with 5 list-items named “Home”, “About
Us”, “Our Products”, “Careers”, and “Contact Us” of a particular website.
<!DOCTYPE html>
<link rel="stylesheet" href="css/style2.css">
<header>
<div id="top-header">
<div id="logo">
<img src="images/loc.png" />
</div>
<nav>
<div id="menu">
<ul>
<li class="active"><a href="#">Home</a></li>
<li><a href="#">About Us</a></li>
<li><a href="#">Our Products</a></li>
<li><a href="#">Careers</a></li>
Styling overall Header: There is not much styling needed for the header tag. The header
tag is just needed to be set to “overflow: hidden” to prevent window from overflowing on
browser resize.
header{
overflow: hidden;
}
Styling Navigation Bar (#top-header): Set a fixed height of 60px for the navigation bar
and align the texts to center.
#top-header{
text-align: center;
height: 60px;
}
Styling Logo(#logo): Remove padding from the parent div of logo. Make both parent and
image floated towards left and assign widths to them.
/*************************/
/* Styling Header */
/*************************/
header{
overflow: hidden;
}
#top-header{
#menu li{
display: inline-block;
padding: none;
margin: none;
}
#menu li a, #menu li span{
display: inline-block;
padding: 0em 1.5em;
text-decoration: none;
font-weight: 600;
text-transform: uppercase;
line-height: 60px;
}
#menu li a{
color: #FFF;
}
#menu li:hover a, #menu li span{
background: #FFF;
color: #0074D9;
border-left: 1px solid #0074D9;
text-decoration: none;
}
3.3.6. Layout and Positioning Properties