Chapter 3
Chapter 3
Chapter 3
CSS is a cornerstone technology of the World Wide Web, alongside HTML and
JavaScript.
Cascading: determines how styles get applied to our webpages when there are
conflicting rules.
Decoration of text or image to be displayed in HTML elements: styles define how to
display HTML element: define sizes, spacing, fonts, colors, layout, etc. 2
Example
3
Cont…
Tags like <font>, and color attributes were added to the HTML 3.2
specification.
Development of large websites, where <font> tag and color attributes were
added to every single page, became a long and expensive process and terrible
for web developers.
To solve this problem, the group of people in World Wide Web Consortium
(W3C) created and maintains CSS.
CSS removed the style formatting from the HTML 4.0 and stored in a
separate CSS file.
This file enable you to change the appearance and layout of all the pages in a
website, just by editing one single file. All browsers support CSS today.
4
Advantages of CSS
CSS saves time (a lot of work): we can write CSS once and reuse same
CSS file in multiple HTML pages.
Pages load faster: If we are using CSS, we do not need to write HTML
tag attributes every time. Just write one CSS rule of a tag and apply to all
the occurrences of that tag. So less code means faster download times.
Easy maintenance: to make a global change, simply change the style and
all elements in all web pages will be updated automatically.
5
Cont…
Global web standards: Now HTML attributes are being deprecated and its being
recommended to use CSS.
Multiple devices compatibility (Platform Independence): Style sheets allow content to be
optimized for many types of devices. E.g. PDA and cell phones.
Better type and layout controls.
Less work: change the appearance of an entire site by editing one style sheet.
Potentially smaller documents and faster downloads.
More accessible sites: making it more accessible for mobile devices by creating responsive
website.
How the style sheet work?
Responsive Web Design is about using HTML and CSS to automatically resize, hide, shrink, or enlarge, a website, to make it look good on all
devices (desktops, tablets, and phones).
A responsive web design make your page look good on any device! It uses HTML and CSS.
When making responsive web pages, add the following <meta> element in all your web pages
Responsive images are images that scale nicely to fit any browser size.
If the CSS width property is set to 100%, the image will be responsive and scale up and down.
• ✦ CSS1 defines basic style functionality, with limited font and limited positioning support.
• ✦ CSS2 adds aural properties, paged media, better font and positioning support. Many other
properties have been refined as well.
Keep in mind that you don’t have to specify the level/version of CSS you are using for your
documents.
The Style Syntax and Rules
A rule says how a particular page element (whether it is heading, a paragraph, or block quote)
should be displayed.
All style rules follow the same basic format. A CSS rule has two main parts (sections):
The selector is followed by the formatting property definitions, which are enclosed in braces ({ }).
10
Cont…
Syntax:
Selector {property : value;}
or
Selector {property1 :
value1;
property2: value2;
Property3:value3 ;
}
selector declaration
Example:
p { color : red }
property Declaration
Cont…
The declaration:-
• The declaration is made up of a property and a value.
• There can be more than one declaration in a single rule
• Each declaration must end by semicolons.
• Declarations are very similar to attribute names and their values.
Style Sheets Syntax and rule-cont.
• CSS Comments: are used to explain your code, and may help you when you
edit the source code at a later date. Comments are ignored by browsers.
To use inline styles you use the style attribute in the relevant tag. The style
attribute can contain any CSS property.
No selectors are needed in this type.
The example shows how to change the color and the left margin of a paragraph:
<p style="color:sienna;margin-left:20px">This is a paragraph.</p>
An inline style loses many of the advantages of style sheets by mixing content
with presentation.
Embedded/Internal style sheet
Embedded in the HTML in the <style> tag:
<style type="text/css">
The <style> tag is placed in the <head> section of the document. style tag must contain a type
attribute that identifies the content of the style element as “text/css”
• type attribute specifies the MIME (Multipurpose Internet Mail Exchange) type of the enclosed style
sheet.
MIME describes the format of the content
Other MIME types include text/html, image/gif, text/javascript.
Used for document-specific styles.
An internal style sheet should be used when a single document has a unique style.
Cont…
• A more compact method for adding a style sheet is to embed a style block at the
top of the HTML document using the <STYLE> tag and its rules apply only to that
document.
Example:
<html>
<head>
<style type="text/css">
h1 {color:red;} Output:
p {font-size:12pt;
font-family: sans-serif;
}
</style>
<title>title</title>
</head>
<body>
<h1>red heading </h1>
<p>paragraph 1</p>
</body>
</html>
External style sheet
♣External linking
Separate pages can all use a shared style sheet
Only modify a single file to change the styles across your entire Web site.
An external style sheet is ideal when one style sheet file is applied to many pages.
♣link tag (with a rel attribute)
• Specifies a relationship between current document and another document.
19
External style sheet:
Example
Example:
<html>
<head>
< link rel="stylesheet" type="text/css“ href=“mytest.css“>
</head>
<body>
<h1>We are Computer Science Staff</h1>
<p>We are surfing for new technology</p>
</body>
<html>
• The REL attribute defines the linked document's relation to the document, that is a "Stylesheet“.
Cont…
• body{
• color:red;
• background-color:yellow;
• }
• p{
• color:blue;
• background-color:white;
• }
• For example: An external style sheet has these properties for the h3 selector:
• h3{
•color:red;
•text-align:left;
•font-size:8pt;
•}
• And an internal style sheet has these properties for the h3 selector:
• <style>
• h3{
• text-align:right;
• font-size:20pt;
• }
22
• </ style>
Cont…
• If the page with the internal style sheet also links to the external style sheet the properties for h3 will be:
•color:red;
•text-align:right;
•font-size:20pt;
•The color is inherited from the external style sheet and the text-alignment and the font-size is replaced by the internal style sheet.
23
Cascading order
• Cascade: deals with precedence of style rules or CSS properties (i.e. the order in which
properties are applied to an HTML document). It solves conflict situations.
• What style will be used when there is more than one style specified for an HTML
element?
When we define a set of style rules for a specific tag in external, internal as well as inline
style, the browser creates a virtual declaration list by merging all style rules together by
following rules of cascade, and then applies the merged virtual declaration list into specific
tag.
Generally speaking, we can say that all the styles will "cascade" into a new "virtual" style
sheet by the following rules, where number four has the highest priority, which means that
it will override all other style:
•1. Browser default (Default CSS)
•2. External style sheet
24
•3. Embedded (Internal) style sheet
Cont…
25
Example
Style Inheritance
• Inheritance: deals with how styles poured down from a parent element to its
child elements.
In general, properties related to the styling of text font size, color, style
(family), etc. are inherited. If we set a font-family on the body element, then it
will get inherited by all the elements within the study.
However, properties such as borders, margins, and so on, that affects the
boxed area around the element tend not to be inherited.
27
CSS Selectors
• Universal selector: Rather than selecting elements of a same type, the universal
selector quite simply matches the name of any element type.
• Example:
Descendant Selector
• Descendant Selector: Suppose you want to apply a style rule to a particular element only when it
lies inside a particular element. As shown in the following example, style rule will apply to <em> element
only when it lies inside <ul> tag.
• Example:
ID selector
• ID selector: We learned about the id attribute that gives an
element a unique identifying name.
• Thus, we can define style rules based on the id attribute of the
elements. All the elements having that id will be formatted
according to the defined rule.
• The symbol that identifies ID selector is the # (hash) symbol.
Class Selector
• We can define style rules based on the class attribute of the elements. All
the elements having that class will be formatted according to the defined
rule.
• To apply a property to all elements of the same class, omit the element
name in the selector.
.special{
color:red;
}
Cont…
• Example (Id and class)
Attribute selector
• We can also apply the styles to HTML elements with particular attributes.
• The example below will match all the input elements having a type attribute with a value of
text.
• Example:
Pseudo class
selectors
• Pseudo selectors are indicated by the colon (:) character.
• Anchor pseudo classes
• There are four main pseudoclasses that can be used as selectors:
– a:link :-Applies a style to unclicked (unvisited) links
– a:visited :-Applies a style to links that have already been clicked
– a:hover:- Applies a style when the mouse pointer is over the link
– a:active :-Applies a style while the mouse button is pressed
To use all four anchor pseudo classes in a single style sheet, they need to
appear in a particular order in order to function properly, the required order is
:link, :visited, :hover, :active.
Text properties
• It is possible :-
– To change the color of a text
– Increase or decrease the space between character in a text
– Decorate a text and more
– Values: color value (name or numeric), RGB
– Default: depends on the browser
Text properties
• Color property is used to change the color of the text.
• Text-align: is used to Control the alignment for a section text. Values: left, right or center.
eg. text-align: center;
Text-decoration: is used to control what the text looks like
values: underline, overline or line-through, none
• E.g. Text-align: underline;
Text-transformation: This property is used to specify uppercase and lowercase letters in a
text.
Values: none, lowercase, uppercase or capitalize
Text-indent: This property is used to specify the indentation of the first line of a
paragraph.
Font related properties
• Font-family: properties is used to control the type of font shown on the page: values: arial,
courier, sans-serif and etc.
E.g. font-family: arial;
• Values: one or more font or generic font family names, separated by commas.
Example: verdana, sans-serif, etc.
The browser loads the first one that is available.
There should always be at least one generic font.
The majority of professional websites use Verdana because it was specially designed to be
legible at small sizes on computer monitors.
Cont…
The box model allows us to add a border around elements, and to define space between
elements.
In order to set the width and height (content) of an element correctly in all browsers, you need
to know how the box model works.
Border
properties
• Allow you to specify the style, color and width of the element
border.
• Border-color: Controls the border color of the section.
• E.g. Border-color: blue;
• Border-style: Controls the style of a border. Values: none, solid,
double.
• E.g. Border-style: solid; dotted; dashed; double and others.
• Border-width: Controls the width of a border.
• E.g. Border-width: 2px;
Border
properties
• The Border-radius property defines the radius of the
element's corners.
Values: any CSS measurement.
E.g. border-radius: 5px;
• Multiple bolder declaration: allows multiple declaration
on of values.
E.g. border: 2px solid red;
Margin and padding
• Margin is the space around an element’s border.
• Margin is used to give space outside an element.
• Margin value can be positive or negative.
• Margin: A shorthand property for setting all the margin properties in one
declaration.
• Margin-left: Sets the left margin of an element.
• Margin-right: Sets the right margin of an element.
• Margin-top: Sets the top margin of an element.
• Margin-bottom: Sets the bottom margin of an element.
Cont…
Example:
P{
margin-right: 5px;
margin-left: 5px;
margin-bottom: 5px;
margin-top: 5px;
}
Cont…
• Padding: is the space between an element’s border and the
element’s content.
• Padding: adding properties are used to generate space around an
element's content, inside of any defined borders.
Example:
div{
padding-top: 5px;
padding-right: 8px;
padding-bottom: 5px;
padding-left: 8px;}