CSSSS
CSSSS
CSS stands for Cascading Style Sheets. HTML (when used correctly) describes the
different parts of a document– Paragraphs, section headings, quotes, images.
Formatting information (how to display it) is in separate style sheets (.css files).
CSS is for formatting side of the web, 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
CSS handles the look and feel part of a web page. Using CSS, you can control the color
of the text, the style of fonts, the spacing between paragraphs, how columns are sized
and laid out, what background images or colors are used, as well as a variety of other
effects
CSS allows you to create rules that specify how the content of an element should
appear. For example, you can specify that the background of the page is cream, all
paragraphs should appear in gray using the Arial typeface, or that all level one headings
should be in a blue, italic, Times typeface.
Advantages of CSS
1. 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.
2. Pages load faster: If you are using CSS, you do not need to write HTML tag attributes
every time. Just write one CSS rule of a tag and apply it to all the occurrences of that
tag. So, less code means faster download times.
3. Easy maintenance: To make a global change, simply change the style, and all the
elements in all the web pages will be updated automatically.
4. Superior styles to HTML: CSS has a much wider array of attributes than HTML, so
you can give a far better look to your HTML page in comparison to HTML attributes.
5. Multiple Device Compatibility: Style sheets allow content to be optimized for
more than one type of device. By using the same HTML document, different
Versions of a website can be presented for hand held devices such as PDAs and Cell
phones or for printing.
6. 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.
Using CSS, you could add a border around any of the boxes, specify its width and
height, or add a background color. You could also control text inside a box, for
example, its color, size, and the typeface used.
Examples of Styles
1) boxes; Color
Width and height Borders (color, Italics, bold, uppercase,
width, and style) Background color and lowercase, small-caps
images Position in the browser
window. 3) Specific:
There are also specific ways in which
2) Text you can style certain elements such as
Typeface lists, tables, and forms.
Size
CSS Syntax
A CSS rule-set consists of a selector and a declaration block:
Example 1:
In the following example all <p> elements will be center-aligned, with a red text color:
<! DOCTYPE html>
<html>
<head>
<style>
p{
color: red;
text-align: center;
}
</style>
</head>
<body>
<p>Hello World!</p>
<p>These paragraphs are styled with CSS.
</p>
</body>
</html>
Example 2:
<! 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>
</body>
</html>
How to use CSS
There are three ways of inserting a style sheet:
1) External style sheet
2) Internal style sheet
3) Inline style
Example:
<! DOCTYPE html>
<html>
<head>
<title>Using External CSS</title>
<link href="css/styles.css" type="text/css" rel="stylesheet" />
</head>
<body>
body {
font-family: arial;
background-color: rgb(185,179,175);}
h1 {
color: rgb(255,255,255);}
<h1>Potatoes</h1>
<p>There are dozens of different potato varieties. They are usually described as early,
second early and main crop.</p>
</body>
</html>
The <style> element should use the type attribute to indicate that the styles are
specified in CSS. The value should be text/ css.
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:
Uses the style tag (in the document head)
<Style type= “class/css”>
/* CSS information goes here */
</style>
Example:
<! DOCTYPE html>
<html>
<head>
<title>Using Internal CSS</title>
<style type="text/css">
body {
font-family: arial;
background-color: rgb(185,179,175);}
h1 {
color: rgb(255,255,255);}
</style>
</head>
<body>
<h1>Potatoes</h1>
<p>There are dozens of different potato varieties. They are usually described as early,
second early and main crop.</p>
</body>
</html>
CSS associates style rules with html elements
CSS works by associating rules with HTML elements. These rules govern
how the content of specified elements should be displayed. A CSS rule
contains two parts:
1) A selector
2) A declaration.
Selectors;
Selectors indicate which element the rule applies to.
CSS selectors are used to "find" (or select) HTML elements based on their element
name, id, class, attribute, and more.
Example:
You can select all <p> elements on a page like this (in this case, all <p> elements will
be center-aligned, with a red text color):
<html>
<head>
<style>
p{
text-align: center;
color: red;
}
</style>
</head>
<body>
<p>And me!</p>
</body>
</html>
Declarations
Indicate how the elements referred to in the selector should be styled.
Declarations are split into two parts;
i. a property and a value
ii. and are separated by a colon.
Example:
You can select all <p> elements on a page like this (in this case, all <p> elements will be center-
aligned, with a red text color):
<html>
<head>
<style>
p{
text-align: center;
color: red;
}
</style>
</head>
<body>
<p>And me!</p>
</body>
</html>
The id Selector
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element should be unique within a page, so the id selector is used to select
one unique element!
To select an element with a specific id, write a hash (#) character, followed by the id
of the element.
Example
The style rule below will be applied to the HTML element with id="para1":
<!DOCTYPE html>
<html>
<head>
<style>
#para1 {
text-align: center;
color: red;
}
</style>
</head>
<body>
<p id="para1">Hello World!</p>
<p>This paragraph is not affected by the style.</p>
</body></html>
The class Selector
The class selector selects elements with a specific class attribute.
To select elements with a specific class, write a period (.) character, followed by the
name of the class.
Example:
In the example below, all HTML elements with class="center" will be red and center-
aligned:
<!DOCTYPE html>
<html>
<head>
<style>
.center {
text-align: center;
color: red;
</style>
</head>
<body>
</body>
</html>
Further;
You can also specify that only specific HTML elements should be affected by a class.
In the example below, only <p> elements with class="center" will be center-aligned:
Example:
<!DOCTYPE html>
<html>
<head>
<style>
p.center {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1 class="center">This heading will not be affected</h1>
<p class="center">This paragraph will be red and center-aligned.</p>
</body>
</html>
Grouping Selectors
If you have elements with the same style definitions, like this:
h1 {
text-align: center;
color: red;
}
h2 {
text-align: center;
color: red;
}
p{
text-align: center;
color: red;
}
<!DOCTYPE html>
<html>
<head>
<style>
h1, h2, p {
text-align: center;
color: red;
}
</style>
</head>
<body>
<h1>Hello World!</h1>
<h2>Smaller heading!</h2>
<p>This is a paragraph.</p>
</body>
</html>
CSS Comments
Comments are used to explain the code, and may help when you edit the source code
at a later date.
Comments are ignored by browsers.
A CSS comment starts with /* and ends with */. Comments can also span multiple
lines:
Example:
<!DOCTYPE html>
<html>
<head>
<style>
p{
color: red;
text-align: center;
</style>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
Values specify the settings you want to use for the chosen properties. For
example, if you want to specify a color property then the value is the color you
want the text in these elements to be.
Last Rule
If the two selectors are identical, the latter of the two will take precedence. Here you
can see the second i selector takes precedence over the first.
Specificity
If one selector is more specific than the others, the more specific rule will take
precedence over more general ones. In this example:
h1 is more specific than *
p b is more specific than p
p#intro is more specific than p
*{
font-family: Arial, Verdana, sans-serif;}
h1 {
font-family: "Courier New", monospace;}
i{
color: green;}
i{
color: red;}
b{
color: pink;}
pb{
color: blue !important;}
pb{
color: violet;}
p#intro {
font-size: 100%;}
p{
font-size: 75%;}
1) RBG VALUES:
These express colors in terms of how much red, green and blue are used to make it up.
Values are expressed as numbers between 0 and 255.
For example: rgb (100, 100, 90)
2) HEX CODES
These are six-digit codes that represent the amount of red, green and blue in a color,
preceded by a pound or hash # sign. For example: #ee3e80
3) COLOR NAMES
Colors are represented by predefined names. However, they are very limited in number. There
are 147 predefined color names that are recognized by browsers.
/* color name */
h1 {
color: DarkCyan;}
/* hex code */
h2 {
color: #ee3e80;}
/* rgb value */
p{
color: rgb(100,100,90);}
Background Color
CSS treats each HTML element as if it appears in a box, and the background-color
property sets the color of the background for that box.
You can specify your choice of background color in the same three ways you can
specify foreground colors: RGB values, hex codes, and color names
If you do not specify a background color, then the background is transparent.
By default, most browser windows have a white background, but browser users
can set a background color for their windows, so if you want to be sure that the
background is white you can use the background-color property on the <body>
element.
body {
background-color: rgb(200,200,200);}
h1 {
background-color: DarkCyan;}
h2 {
background-color: #ee3e80;}
p{
background-color: white;}
Try out!!
<! DOCTYPE html>
<html>
<head>
<title>Using Internal CSS</title>
<style type="text/css">
body {
font-family: arial;
background-color: rgb(255,179,175);}
h1 {
color: rgb(255,185,255);}
</style>
</head>
<body>
<h1>Potatoes</h1>
<p>There are dozens of different potato
varieties. They are usually described as
early, second early and maincrop.</p>
</body>
</html>
Example:
body {font-family: Arial, Verdana, sans-serif; font-size: 12px ;}
h1 {font-size: 200 %;}
h2 {font-size: 1.3em ;}
Bold font-weight
The font-weight property allows you to create bold text. There are two values that this
property commonly takes:
i. Normal: This causes text to appear at a normal weight.
In this example, the <h1> element is uppercase, the <h2> element is lowercase, and
the credits are capitalized. In the HTML, the word by in the credits had a lowercase b.
If you do utilize the uppercase option, it is worth looking at the letter-spacing property
to increase the gap between each letter as shown on page 284. This will help improve
readability
Example:
.credits {text-decoration: underline ;}
a{
text-decoration: none ;}
In this example, the credits have been underlined. Also, the name of the breed (which
is a link) is not underlined, which it would be by default because it is a link.
Alignment text-align
The text-align property allows you to control the alignment of text. The property can take
one of four values:
i. Left: This indicates that the text should be left-aligned.
ii. Right: This indicates that the text should be right-aligned.
iii. Center: This allows you to center text.
iv. Justify: This indicates that every line in a paragraph, except the last line, should be
set to take up the full width of the containing box.
Example:
h1 {
text-align: left;}
p{
text-align: justify;}
.credits {
text-align: right;}
WEBSITE LAYOUT
A website is often divided into headers, menus, content and a footer:
Header
A header is usually located at the top of the website (or right below a top navigation
menu). It often contains a logo or the website name:
Try out:
<!DOCTYPE html>
<html lang="en">
<head>
<title>CSS Website Layout</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
margin: 0;
}
Try out:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
*{
box-sizing: border-box;
body {
margin: 0;
.header {
background-color: #f1f1f1;
padding: 20px;
text-align: center;
}
/* Style the top navigation bar */
.topnav {
overflow: hidden;
background-color: #333;
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
text-decoration: none;
.topnav a:hover {
background-color: #ddd;
color: black;
</style>
</head>
<body>
<div class="header">
<h1>Header</h1>
</div>
<div class="topnav">
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
</div>
</body>
</html>
Footer
The footer is placed at the bottom of your page. It often contains information like
copyright and contact info:
Try out
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
*{
box-sizing: border-box;
}
body {
margin: 0;
.header {
background-color: #f1f1f1;
padding: 20px;
text-align: center;
.topnav {
overflow: hidden;
background-color: #333;
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
text-decoration: none;
}
/* Change color on hover */
.topnav a:hover {
background-color: #ddd;
color: black;
.column {
float: left;
padding: 10px;
.column.side {
width: 25%;
/* Middle column */
.column.middle {
width: 50%;
.row:after {
content: "";
display: table;
clear: both;
}
/* Responsive layout - makes the three columns stack on top of each other instead of next to
each other */
.column.side, .column.middle {
width: 100%;
.footer {
background-color: #f1f1f1;
padding: 10px;
text-align: center;
</style>
</head>
<body>
<div class="header">
<h1>Header</h1>
</div>
<div class="topnav">
<a href="#">Link</a>
<a href="#">Link</a>
<a href="#">Link</a>
</div>
<div class="row">
<h2>Side</h2>
</div>
<h2>Main Content</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sit amet pretium
urna. Vivamus venenatis velit nec neque ultricies, eget elementum magna tristique. Quisque
vehicula, risus eget aliquam placerat, purus leo tincidunt eros, eget luctus quam orci in velit.
Praesent scelerisque tortor sed accumsan convallis.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas sit amet pretium
urna. Vivamus venenatis velit nec neque ultricies, eget elementum magna tristique. Quisque
vehicula, risus eget aliquam placerat, purus leo tincidunt eros, eget luctus quam orci in velit.
Praesent scelerisque tortor sed accumsan convallis.</p>
</div>
<h2>Side</h2>
</div>
</div>
<div class="footer">
<p>Footer</p>
</div>
</body>
</html>
Content
The layout in this section, often depends on the target users. The most common layout is one
(or combining them) of the following: