Chapter 3 CSS (Ip)
Chapter 3 CSS (Ip)
By.Abrahim A. 1
Outline
• Why use style sheets?,
• HTML vs. CSS,
• Anatomy of a CSS Rule,
• Linking HTML and CSS,
By.Abrahim A. 2
Introduction to CSS
• CSS stands for Cascading Style Sheet.
• A CSS allows you to separate web sites HTML content from its style.
• We use HTML file to arrange the content.
• All of the presentation/formatting like fonts, colors, background,
borders, text formatting, link effects, etc. are accomplished within a
CSS.
• CSS is a web page layout method that has been added to HTML to
give web developers more control over their design and content
layout.
• Using CSS allows a designer to create a standard set of commands
that controls the style of all subsequent pages.
• These commands are embedded inside the web page or from an
external file/page.
By.Abrahim A. 3
Introduction to CSS…
• With CSS you can add style (fonts, colors, spacing, and
size) to web documents.
• More advanced techniques control the layout of the page
without the use of tables or other cumbersome HTML.
• When tags like <font>, and color attributes were added to the HTML 3.2
specification, it started a nightmare for web developers.
• Development of large web sites, where fonts and color information were added to
every single page, became a long and expensive process.
• To solve this problem, W3C created CSS.
• In HTML 4.0, all formatting could be removed from the HTML document, and
stored in a separate CSS file.
• All browsers support CSS today.
By.Abrahim A. 5
Introduction to CSS…
• CSS is a breakthrough in web design because it allows
developers to control the style and layout of multiple web
pages all at once.
• As a web developer you can define a style for each HTML
element and apply it to as many web pages as you want.
• To make a global change, simply change the style, and all
elements in the Web are updated automatically.
Benefits of CSS
• The benefits of using style sheets are:
• Better type and layout controls - Presentational HTML
never gets close to offering the kind of control over type,
backgrounds, and layout that is possible with CSS.
By.Abrahim A. 6
Benefits
• Separation of style and content has many benefits, but has only become practical in recent
years due to improvements in popular web browser‘s CSS implementations
• Speed: Overall, user‘s experience of a site utilizing style sheets will generally be quicker
than sites that don‘t use the technology.
• Maintainability: Holding all the presentation styles in one file significantly reduces
maintenance time and reduces the chance of human errors, thereby improving
presentation consistency.
• Accessibility: Sites that use CSS with either XHTML or HTML are easier to tweak so that
they appear extremely similar in different browsers (Explorer, Mozilla, Opera, Safari, etc.).
• Customization: If a page's layout information is all stored externally, a user can decide to
disable the layout information entirely, leaving the site's bare content still in a readable
form.
• Consistency: Because the semantic file contains only the meanings an author intends to
convey, the styling of the various elements of the document's content is very consistent.
• Portability:
CSS Revisions/Levels
• There are three levels/versions of CSS:
• CSS1,
• CSS2, and
• CSS3
CSS 1
• The first CSS specification to become an official W3C
Recommendation is CSS level1
• It was published in December 1996.
• Among its capabilities are support for
• Font properties such as typeface and emphasis
• Color of text, backgrounds, and other elements
• Text attributes such as spacing between words, letters, and lines of text
• Alignment of text, images, tables and other elements
• Margin, border, padding, and positioning for most elements
• Unique identification and generic classification of groups of attributes
By.Abrahim A. 8
CSS Revisions/Levels…
CSS 2
• CSS level 2 specification was developed by the W3C and
published as a Recommendation in May 1998.
• It is a superset of CSS 1
• CSS 2 includes a number of new capabilities like absolute, relative,
and fixed positioning of elements and z-index, the concept of
media types, support for aural style sheets and bidirectional text,
and new font properties such as shadows.
• CSS level 2 revision 1 or CSS 2.1 fixes errors in CSS 2
• It removes poorly-supported or not fully interoperable features and
adds already-implemented browser extensions to the specification.
By.Abrahim A. 9
CSS Revisions/Levels..
CSS3
• CSS Level 3 builds on CSS Level 2 module by module, using the CSS2.1
specification as its core.
• Each module adds functionality and/or replaces part of the CSS2.1 specification.
• The CSS Working Group intends that the new CSS modules will not contradict
the CSS2.1 specification: only that they will add functionality and refine
definitions.
By.Abrahim A. 10
CSS Syntax
• A CSS rule has two main parts:
• a selector, and
• one or more declarations.
• The selector is normally the HTML element you want to style.
• Usually, it is the HTML tags to be styled. E.g. <p>, <h1>
By.Abrahim A. 12
CSS Syntax…
.
By.Abrahim A. 13
CSS Syntax…
• CSS declarations alway ends with a semicolon.
• Declaration groups are surrounded by curly brackets
p {color:red; text-align:center;}
By.Abrahim A. 14
By.Abrahim A. 15
CSS Comments
• Comments are used to explain your code, and may help you
when you edit the source code at a later date.
• CSS comments are ignored by browsers.
• Example:
<h1 style="color: red">Introduction</h1>
By.Abrahim A. 17
Linking CSS to HTML…
II. Creating Embedded Styles
• We use the <style> tag for this
• <style> tag has a property called type which should be set to text/css
for CSS styling.
By.Abrahim A. 18
Linking CSS to HTML…
• In internal method each HTML file contains
the CSS code needed to style the page.
• This means any changes you want to make to
one page, will have to be made to all.
• This method can be good if
• you need to style only one page, or
• you want different pages to have varying styles.
By.Abrahim A. 19
Linking CSS to HTML…
III. Creating an External Style Sheet
• An external CSS file can be created with any text or HTML editors
such as notepad or Dreamweaver.
• A CSS file contains no HTML, only CSS.
• You have to save the CSS file with the .css file extension.
• You can link to the file externally by placing one of the following
links in the head section of every HTML file you want to style with
the CSS file.
• All of the elements higher than a particular element in the hierarchy are its
ancestors.
• Two elements with the same parent are siblings.
By.Abrahim A. 21
Inheritance…
• When you write a font-related style rule using the p element as a
selector, the rule applies to all of the paragraphs in the document as
well as the inline text elements they contain.
• Some style sheet properties are inherited and others do not.
• In general, properties related to the styling of text — font size, color,
style, etc — are passed down.
• Properties such as borders, margins, backgrounds, and so on that affect
the boxed area around the element tend not to be passed down.
• You can use inheritance to your advantage when writing style sheets.
• For example, if you want all text elements to be rendered in the
Verdana font face, you could write separate style rules for every
element in the document and set the font-face to Verdana.
• A better way would be to write a style for body element, and let all the
text elements contained in the body inherit that style.
By.Abrahim A. 22
Inheritance…
Example: All texts in the following page is displayed as red because of inheritance
<html>
<head>
<title> CSS </title>
<style type=”text/css”>
body { color: red;}
</style>
</head>
<body>
<h2> Well Known Novels </h2>
<p> Romeo and Juliet </p>
<p> Things Fall Apart</p>
<p>Kingdom of God is Among You</p>
</body>
</html>
By.Abrahim A. 23
Conflicting styles: the Cascade
• Ever wonder why they are called “cascading” style sheets?
• CSS allows you to apply several style sheets to the same document, which
means there are bound to be conflicts.
• For example, what should the browser do if a document’s imported style
sheet says that h1 elements should be red, but its embedded style says h1s
should be purple?
By.Abrahim A. 25
Conflicting styles: the Cascade…
• For example, to make paragraph text blue always, use the
following rule:
p {color: blue !important;}
By.Abrahim A. 26
Grouped Selectors
• If you ever need to apply the same style property to a
number of elements, you can group the selectors into one
rule.
• This one rule has the same effect as the five rules listed
separately.
h1, h2, p, div, img { border: 1px solid blue; }
By.Abrahim A. 27
Rule order
• If there are conflicts within style rules of identical
weight, whichever one comes last in the list wins.
• Take these three rules, for example:
<style type="text/css">
p { color: red; }
p { color: blue; }
p { color: green; }
</style>
• In this scenario, paragraph text will be green because the
last rule in the style sheet overrides the earlier ones.
By.Abrahim A. 28
Cascading style sheets:
• Cascading style sheet languages such as CSS allow style information
from several sources to be blended together.
• However, not all style sheet languages support cascading. To define
a cascade, authors specify a sequence of LINK and/or STYLE
elements.
• The style information is cascaded in the order the elements appear
in the HEAD.
• In the following example, we specify two alternate style sheets
named "compact".
• If the user selects the "compact" style, the user agent must apply
both external style sheets, as well as the persistent "common.css"
style sheet.
Cascading style sheets:
• If the user selects the "big print" style, only the alternate style sheet
"bigprint.css" and the persistent "common.css" will be applied.
<LINK rel="alternate stylesheet" title="compact" href="small-base.css" type="text/css">
<LINK rel="alternate stylesheet" title="compact" href="small-extras.css" type="text/css">
<LINK rel="alternate stylesheet" title="big print" href="bigprint.css" type="text/css">
<LINK rel="stylesheet" href="common.css" type="text/css">
• Here is a cascade example that involves both the LINK and STYLE elements.
<LINK rel="stylesheet" href="corporate.css" type="text/css">
<LINK rel="stylesheet" href="techreport.css" type="text/css">
<STYLE type="text/css">
p.special { color: rgb(230, 100, 180) }
</STYLE>
Media-dependent cascades:
• A cascade may include style sheets applicable to different
media.
• Both LINK and STYLE may be used with the media attribute.
The user agent is then responsible for filtering out those
style sheets that do not apply to the current medium.
• In the following example, we define a cascade where the
"corporate" style sheet is provided in several versions: one
suited to printing, one for screen use and one for speech-
based browsers (useful, say, when reading email in the car).
Media-dependent cascades:
• The "techreport" stylesheet applies to all media.
• The color rule defined by the STYLE element is used for print and
screen but not for aural rendering.
<LINK rel="stylesheet" media="aural" href="corporate-aural.css"
type="text/css">
<LINK rel="stylesheet" media="screen" href="corporate-screen.css"
type="text/css">
<LINK rel="stylesheet" media="print" href="corporate-print.css"
type="text/css">
<LINK rel="stylesheet" href="techreport.css" type="text/css"> <STYLE
media="screen, print" type="text/css"> p.special { color: rgb(230, 100,
180) }
</STYLE>
Inheritance and cascading:
• When the user agent wants to render a document, it needs to
find values for style properties, e.g. the font family, font style,
size, line height, text color and so on.
• The exact mechanism depends on the style sheet language, but
the following description is generally applicable:
• The cascading mechanism is used when a number of style rules
all apply directly to an element.
• The mechanism allows the user agent to sort the rules by
specificity, to determine which rule to apply.
• If no rule can be found, the next step depends on whether the
style property can be inherited or not.
Inheritance and cascading:
• Not all properties can be inherited. For these properties the
style sheet language provides default values for use when
there are no explicit rules for a particular element.
• If the property can be inherited, the user agent examines the
immediately enclosing element to see if a rule applies to that.
• This process continues until an applicable rule is found.
• This mechanism allows style sheets to be specified compactly.
• For instance, authors may specify the font family for all
elements within the BODY by a single rule that applies to the
BODY element.
Preferred and alternate style sheets:
• HTML allows authors to associate any number of external
style sheets with a document.
• The style sheet language defines how multiple external
style sheets interact (for example, the CSS "cascade" rules).
• Users may select their favorite among these depending on
their preferences. For instance, an author may specify one
style sheet designed for small screens and another for
users with weak vision (e.g., large fonts).
• User agents should allow users to select from alternate
style sheets.
Specifying external style sheets:
• Authors specify external style sheets with the following
attributes of the LINK element:
• Set the value of href to the location of the style sheet file.
• The value of href is a URI.
• Set the value of the type attribute to indicate the
language of the linked (style sheet) resource.
Specifying external style sheets:
• This allows the user agent to avoid downloading a style
sheet for an unsupported style sheet language.
• Specify that the style sheet is persistent, preferred, or
alternate:
• To make a style sheet persistent, set the rel attribute to
"stylesheet" and don't set the title attribute.
• To make a style sheet preferred, set the rel attribute to
"stylesheet" and name the style sheet with the title attribute.
• To specify an alternate style sheet, set the rel attribute to
"alternate stylesheet" and name the style sheet with the title
attribute.
Specifying external style sheets:
• User agents should provide a means for users to view and pick from
the list of alternate styles.
• The value of the title attribute is recommended as the name of each
choice.
• <LINK href="mystyle.css" rel="stylesheet" type="text/css"> Setting
the title attribute makes this the author's preferred style sheet:
• Default-Style: "compact"
• If two or more META declarations or HTTP headers specify
the preferred style sheet, the last one takes precedence.
HTTP headers are considered to occur earlier than the
documentHEAD for this purpose.
• If two or more LINK elements specify a preferred style
sheet, the first one takes precedence.
• Preferred style sheets specified with META or HTTP
headers have precedence over those specified with the
LINK element.
Selectors
• Selectors are used to declare which elements a style applies to, a
kind of match expression. Selectors may apply to all elements of a
specific type, or only those elements which match a certain
attribute
• A simple selector is either a type selector or universal selector
followed immediately by zero or more attribute selectors,
ID selectors, or pseudo-classes, in any order.
• The simple selector matches if all of its components match.
• A selector is a chain of one or more simple selectors separated by
combinators.
• Combinators are: whitespace, ">", and "+". Whitespace may appear
between a combinators and the simple selectors around it.
Selectors
• The elements of the document tree that match a selector
are called subjects of the selector.
• A selector consisting of a single simple selector matches any
element satisfying its requirements. Prepending a simple
selector and combinators to a chain imposes additional
matching constraints, so the subjects of a selector are
always a subset of the elements matching the rightmost
simple selector.
• One pseudo-element may be appended to the last simple
selector in a chain, in which case the style information
applies to a subpart of each subject.
Grouping
• In the next example, however, the style rule will only match an H1 element that
has an ID value of "z98y". The rule will not match the P element in this example:
<HEAD>
<TITLE>Match H1 only</TITLE>
<STYLE type="text/css">
H1#z98y {letter-spacing: 0.5em}
</STYLE>
</HEAD>
<BODY>
<P id=z98y>Wide text</P>
</BODY>
• ID selectors have a higher precedence than attribute selectors. For example, in
HTML, the selector #p123 is more specific than [ID=p123] in terms of the cascade.
Styling HTML with CSS
• Styling Backgrounds
• Styling Text
• Styling Fonts
• Styling Links
• Styling Lists
• Styling Tables
• CSS Class and ID
By.Abrahim A. 59
1. Styling Backgrounds
• You can style the background of an element in one declaration with
the background property.
• Values:
• background color
• image
• position
• repeat
• attachment
background-attachment: value;
• Values:
• fixed
• Scroll
Background Repeat
• You can set if an image set as a background of an element is to
repeat (across=x and/or down=y) the screen.
background-repeat: no-repeat | repeat | repeat-x | repeat-y;
By.Abrahim A. 62
Styling Backgrounds…
• Example
<style type=“text/css”>
body {
background-image: url("tulips.jpg");
background-position: top right;
background-repeat: repeat;
}
</style>
Background Position
• You can position an image used for the background of an element
using the background-position property.
background-position: value;
By.Abrahim A. 63
Styling Backgrounds…
• Values:
• top left
• top center
• top right
• center left
• center center
• center right
• bottom left
• bottom center
• bottom right
• x-% y-%
• x-pos y-pos
• Example
p { background-image: url("tulips.jpg");
background-position: top right;
}
By.Abrahim A. 64
2. Styling Text
Color
• You can set the color of text with the following:
color: value;
• Example:
p {color: blue;}
By.Abrahim A. 65
2. Styling Text…
Letter Spacing
• You can adjust the space between letters in the following manner.
• Setting the value to 0 prevents the text from justifying.
• You can use negative values.
• Negative values make the text overlap each other.
letter-spacing: value;
By.Abrahim A. 66
2. Styling Text…
Word Spacing
• You can adjust the space between words in the following manner. You
can use negative values.
word-spacing: value;
• Possible values are
• normal
• length
Line height
• You can set the distance between lines in the following way:
line-height: value;
• Possible values are
• normal
• number
• length
• Percentage(%)
By.Abrahim A. 67
2. Styling Text…
Text Align
• You can align text with the following:
text-align: value;
• Examples:
This text is aligned left.
This text is aligned center.
This text is aligned right.
By.Abrahim A. 68
2. Styling Text…
Text Decoration
• You can decorate text with the following:
text-decoration: value;
• Examples:
This text is underlined.
This text has a line through it.
This text is overlined.
By.Abrahim A. 69
2. Styling Text…
Text Transform
• You can control the case of letters in an (X)HTML element with the
following:
text-transform: value;
• e.g.
h3 { text-transform: uppercase; }
By.Abrahim A. 70
2. Styling Text…
Text Indent
• You can indent the first line of text in an HTML element with the
following:
text-indent: value;
Text Direction
• You can sets the text direction
direction: value;
By.Abrahim A. 71
2. Styling Text…
unicode-bidi
• Possible value:
• normal
• embed
• bidi-override
By.Abrahim A. 72
2. Styling Text…
• For the direction property to affect reordering in inline elements,
the unicode-bidi property’s value must be embed or override.
• Example:
div {
direction: rtl;
unicode-bidi: bidi-override;
}
…
<div> Hebrew and Arabic are written from right to left. </div>
By.Abrahim A. 73
2. Styling Text…
White Space
• You can control the whitespace in an HTML element with the following:
white-space: value;
• This property declares how white space(tab, space, etc) and line
break(carriage return, line feed, etc.) inside the element is handled.
By.Abrahim A. 74
2. Styling Text…
• Values have the following meanings:
• normal: makes user agents to collapse sequences of white space,
and break lines as necessary to fill line boxes.
• pre: prevents user agents from collapsing sequences of white space.
Lines are only broken at preserved newline characters.
• nowrap: This value collapses white space as for ’normal’, but
suppresses line breaks within text.
• pre-wrap: This value prevents user agents from collapsing
sequences of white space. Lines are broken at preserved newline
characters, and as necessary to fill line boxes.
• pre-line: This value directs user agents to collapse sequences of
white space. Lines are broken at preserved newline characters, and
as necessary to fill line boxes.
By.Abrahim A. 75
3. Styling Fonts
Font
• The font property can set the style, weight, variant, size, line height and font-
type:
font: [ font-style || font-variant || font-weight] || font-size [ /line-height] || font-family ];
• Example:
font: italic bold normal small/1.4em Verdana, sans-serif;
• The above would set the text of an element to an italic style a bold weight a
normal variant a relative size a line height of 1.4em and the font to Verdana or
another sans-serif typeface.
By.Abrahim A. 76
3. Styling Fonts…
Font -Family
• You can set what font will be displayed in an element with the font-
family property.
• There are 2 choices for values:
• family-name
• generic family
• If you set a family name it is best to also add the generic family at the
end as this is a prioritized list.
• So if the user does not have the specified font name, it will use the
same generic family.
font-family: Verdana, sans-serif;
By.Abrahim A. 77
3. Styling Fonts…
Font Size
• You can set the size of the text used in an element by using the font-size property.
font-size: value;
By.Abrahim A. 78
3. Styling Fonts…
• The following table provides user agent guidelines for the
absolute-size mapping to HTML heading and absolute
font-sizes
CSS absolute- xx-small x-small small medium large x-large xx-large
size values
HTML font sizes 1 2 3 4 5 6 7
By.Abrahim A. 80
3. Styling Fonts…
Length Units in CSS
• There are two types of length units:
• relative and
• absolute.
• The absolute units consist of the physical units (in, cm, mm, pt, pc) and the
px unit:
• in: inches — 1in is equal to 2.54cm.
• cm: centimeters
• mm: millimeters
• pt: points — the points used by CSS are equal to 1/72nd of 1in.
• pc: picas — 1pc is equal to 12pt.
• px: pixel units — 1px is equal to 0.75pt.
By.Abrahim A. 81
3. Styling Fonts…
• Example: all the following are possible
h1 { margin: 0.5in } /* inches */
h2 { line-height: 3cm } /* centimeters */
h3 { word-spacing: 4mm } /* millimeters */
h4 { font-size: 12pt } /* points */
h4 { font-size: 1pc } /* picas */
p { font-size: 12px } /* px */
• Example:
body
{
font-size: 12px;
text-indent: 3em; /* i.e. 36px - based on font-size*/
}
By.Abrahim A. 83
3. Styling Fonts…
• The ex means the x-height.
• The x-height is so called because it is often equal to the height of the
lowercase ‘x’.
• However, an ex is defined even for fonts that do not contain an ‘x’.
By.Abrahim A. 84
3. Styling Fonts…
• The x-height of a font can be found in different ways.
• Some fonts contain reliable metrics for the x-height.
• If reliable font metrics are not available, user agents may determine the
x-height from the height of a lowercase glyph.
• In the cases where it is impossible or impractical to determine the x-
height, a value of 0.5em should be used.
• Example:
h1 { margin: 0.5em } /* em */
h1 { margin: 1ex } /* ex */
• Example:
h1 { line-height: 1.2em }
• This means the line height of ‘h1’ elements will be 20% greater than
the font size of the h1 elements.
By.Abrahim A. 85
3. Styling Fonts…
• On the other hand:
h1 { font-size: 1.2em }
• This means that the font-size of h1 elements will be 20%
greater than the font size inherited by h1 elements.
By.Abrahim A. 86
3. Styling Fonts…
Font Style
• You can set the style of text in a element with the font-style property:
font-style: value;
• Font Variant
• You can set the variant of text within an element with the font-variant
property:
font-variant: value;
By.Abrahim A. 87
3. Styling Fonts…
Font Weight
• You can control the weight of text in an element with the font-weight property:
font-weight: value;
By.Abrahim A. 89
3. Styling Fonts…
inherited value Bolder Lighter
100 400 100
200 400 100
300 400 100
400 700 100
500 700 100
600 900 400
700 900 400
800 900 700
900 900 700
Example:
body { font-weight: 600;}
p { font-weight: bolder } /* this is based on 600 and it changes to 900*/
By.Abrahim A. 90
4. Styling Links
• User agents commonly display unvisited links differently from
previously visited ones.
• CSS provides the pseudo-classes ’a:link’ and ’a:visited’ to
distinguish them:
• The a:link pseudo-class applies for links that have not yet been visited.
• The a:visited pseudo-class applies once the link has been visited by the
user.
• The a:hover pseudo-class applies while the user designates an element
with some pointing device, but does not activate it. For example, a
visual user agent could apply this pseudo-class when the mouse pointer
hovers over a box generated by the element.
• The a:active pseudo-class applies while an element is being activated
by the user. For example, between the times the user presses the mouse
button and releases it.
• The a:focus pseudo-class applies while an element has the focus
(accepts keyboard events or other forms of text input).
By.Abrahim A. 91
4. Styling Links…
• Below are the various ways you can use CSS to style links.
a:link {
color: #009900;
text-decoration: overline;
}
a:visited {color: #999999;}
a:hover {color: #333333;}
a:focus {color: #333333;}
a:active {color: #009900;}
By.Abrahim A. 92
4. Styling Links…
• Remark: You must declare the a:link and a:visited before
you declare a:hover.
• Furthermore, you must declare a:hover before you can
declare a:active.
• Using the above code will style all links on your web page,
unless you declare a separate set of link styles for a certain
area of your webpage.
• Look at pseudo-classes sub-section for more.
By.Abrahim A. 93
5. Styling Lists
List Style
• You can control the appearance of ordered and unordered
lists in one declaration with the list-style property
list-style: value value value;
• Values:
• image
• position
• type
• Values
• inside
• outside
By.Abrahim A. 95
5. Styling Lists…
List Style Type
• You can control the type of bullet ordered and unordered lists use with the list-style-
type property
list-style-type: value;
• Values
• none
• disc
• circle
• square
• decimal
• decimal-leading-zero
• lower-roman
• upper-roman
• lower-alpha
• upper-alpha
• lower-greek
• lower-latin
• upper-latin
• armenian
• Georgian
By.Abrahim A. 96
6. Styling Tables
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:
table, th, td{
border: 1px solid black;
}
• Notice that the table in the example above has double borders.
• This is because both the table, th, and td elements have separate
borders.
By.Abrahim A. 97
6. Styling Tables…
Collapse Borders
• The border-collapse property sets whether the table borders are collapsed into a
single border or separated:
table{
border-collapse: collapse;
}
table, th, td{
border: 1px solid black;
}
By.Abrahim A. 98
6. Styling Tables…
Table Text Alignment
• The text in a table is aligned with the text-align and vertical-align properties.
• The text-align property sets the horizontal alignment, like left, right, or
center:
td{
text-align: right;
}
• The vertical-align property sets the vertical alignment, like top, bottom, or
middle:
td {
height: 50px;
vertical-align: bottom;
}
By.Abrahim A. 99
6. Styling Tables…
Table Width and Height
• Width and height of a table is defined by the width and height properties.
• The example below sets the width of the table to 100%, and the height of the th
elements to 50px:
table{
width:100%;
}
th{
height:50px;
}
By.Abrahim A. 100
6. Styling Tables…
Table Padding
• To control the space between the border and content in a table, use the
padding property on td and th elements:
td{
padding:15px;
}
Table Color
• The color of the borders, and the text and background color of th elements
can be specified:
table, td, th{
border:1px solid green;
}
th{
background-color:green;
color:white;
}
By.Abrahim A. 101
6. CSS Class
• Controlling the way all HTML elements look can be useful, but also limiting.
• It's excellent to be able to change every paragraph, table cell or image with one
line of CSS code
• But sometimes you'll want to change only few paragraphs or images, not all of
them.
• You can add CSS code through the style attribute of each element, but for more
elements that method gets too complicated.
By.Abrahim A. 102
6. CSS Class…
• For example, paragraph can be defined in CSS file as follows:
p{
font-size: small;
color: #333333
}
• However, let’s say you want to change the word "sentence" in the paragraph
formatted by the above CSS to green bold text, while leaving the rest of the
sentence untouched.
• This can be done by using class.
By.Abrahim A. 103
6. CSS Class…
• There are two types of classes:
• generic classes that can be applied to any element, and
• classes that can be applied only to a certain type of HTML element.
I. Generic Classes
• Their selector starts with a dot (.), which states that it is a class.
• You can name it anything you like:
.important { background-color: #FFFFDE; }
.emphasis { font-family: Verdana; }
.boooring { color: Gray; }
• To apply a class to a certain HTML element, use its class attribute where
you state the class name without the dot.
By.Abrahim A. 104
6. CSS Class…
<div class="emphasis">The big match today …</div>
<i class="boooring">This sentence looks boring</i>
• These classes can be applied only to a specified HTML element, in this case a
DIV element.
<div class="big">Big bold text.</div>
<span class="big">Normal text - class not applied.</span>
By.Abrahim A. 105
6. CSS Class…
• Example: in your paragraph, you put this:
<span class="greenboldtext">sentence</span>
By.Abrahim A. 107
Pseudo Classes…
• Example:
a:link {
text-decoration: underline;
font-weight: normal;
color: #003300;
}
a:visited {
text-decoration: underline;
font-weight: normal;
color: #999999;
}
By.Abrahim A. 108
CSS ID
• IDs are similar to classes, except once a specific ID has been declared it cannot be
used again within the same (X)HTML file.
• The syntax of ID selectors is very similar to classes, but instead of a dot you must
use a hash sign (#).
By.Abrahim A. 109
7. CSS Box Model
• In CSS, the term box model is used when talking about design and layout.
• The CSS box model is essentially a box that wraps around HTML elements, and
it consists of: margins, borders, padding, and the actual content.
• The box model allows us to place a border around elements and space elements in
relation to other elements.
• The image below illustrates the box model.
By.Abrahim A. 110
7. CSS Box Model…
By.Abrahim A. 111
7. CSS Box Model…
• Padding - Clears an area around the content.
• The padding is affected by the background color of the box
• Content - The content of the box, where text and images appear
• In order to set the width and height of an element correctly in all browsers, you
need to know how the box model works.
By.Abrahim A. 112
7. CSS Box Model…
Border
• You can set the color, style and width of the borders around an
element in one declaration by using the shorthand border
property.
border: 1px solid #333333;
• Values:
• width
• style
• color
• Values:
• color name
• hexadecimal number
• RGB color code
• transparent
By.Abrahim A. 114
7. CSS Box Model…
Border Style
• You can set the style of a border independently with the border-style
property.
border-style: value;
• Values:
• dashed
• dotted
• double
• groove
• hidden
• inset
• none
• outset
• ridge
• solid
By.Abrahim A. 115
7. CSS Box Model…
By.Abrahim A. 116
7. CSS Box Model…
Border Width
• You can set the width of a border independently with the border-
width property.
border-width: value;
• Values:
• Length
• Thin
• Medium
• Thick
• Or you can set the elements for each borders side individually
By.Abrahim A. 117
7. CSS Box Model…
Border Bottom
• You can set the color, style and width of the bottom border around an element in one
declaration with the border-bottom property.
border-bottom: 1px solid #333333;
• Values:
• Width
• style
• color
• Or you can set each value individually
• You can set the style of the bottom border around an element with the border-bottom-
style property:
border-bottom-style: value;
By.Abrahim A. 118
7. CSS Box Model…
Border Bottom Width
• You can set the width of the bottom border around an element with the border-bottom-
width property.
border-bottom-width: value;
Border Left
• You can set the color, style and width of the left border around an element with the border-
left property.
border-left: 1px solid #333333;
• Values:
• style
• Width
• color
• Values:
• color
• style
• width
• Or you can set each value individually
By.Abrahim A. 120
7. CSS Box Model: CSS Margin
• The margin clears an area around an element outside the border.
• The margin does not have a background color, and is completely transparent.
By.Abrahim A. 121
• Example:
#A {
margin: 4em;
border: 1px solid red;
background: #FCF2BE;
}
#B {
margin-top: 2em;
margin-right: 250px;
margin-bottom: 1em;
margin-left: 4em;
border: 1px solid red;
background: #FCF2BE;
}
body {
margin: 0 10%;
border: 1px solid red;
background-color: #BBE09F;
}
7. CSS Box Model: CSS Padding
• The padding clears an area around the content (inside the border) of an element.
• The padding is affected by the background color of the element.
• The top, right, bottom, and left padding can be changed independently using separate
properties.
• A shorthand padding property can also be used, to change all padding at once.
• Syntax:
padding: length | percentage | auto | inherit
Examples:
padding: 10px; /* Applied to all sides. */
padding: 10px 6px; /* First is top & bottom, second is left & right. */
By.Abrahim A. 123
7. CSS Box Model: CSS Padding…
Property Description Values
Padding A shorthand property for setting all the padding-top
padding properties in one declaration padding-right
padding-bottom
padding-left
By.Abrahim A. 124
7. CSS Box Model: CSS Padding…
• Example:
blockquote {
padding-top: 1em;
padding-right: 3em;
padding-bottom: 1em;
padding-left: 3em;
background-color: #D098D4;
}
<body>
<blockquote>
Applying masks to the glasses is the most labor intensive part of the process. ….
</blockquote>
By.Abrahim A. 125
7. CSS Box Model: CSS Padding…
By.Abrahim A. 126
CSS Display and Visibility
• The display property specifies if/how an element is displayed. The
syntax is as follows:
display: none | block | inline;
• A value none hides an element, and it will not take up any space.
• The element will be hidden, and the page will be displayed as if the
element is not there.
• Example:
li { display: inline; }
By.Abrahim A. 128
<html>
<head>
<title>Display and Visibility</title>
<style type="text/css">
li { display: inline;}
a { display: block; }
</style>
</head>
<body>
There are different types of mini computers:
<ol>
<li>Desktop</li>
<li>Laptop</li>
<li>Palmtop</li>
</ol>
<a href="here.html"> first link </a>
<a href="there.html"> second link </a>
</body>
</html>
CSS Display and Visibility…
By.Abrahim A. 130
CSS Display and Visibility…
• The visibility property specifies if an element should be visible or hidden.
visibility: hidden | visible;
• hidden hides an element, but it will still take up the same space as before.
• The element will be hidden, but still affect the layout.
By.Abrahim A. 131
CSS Display and Visibility…
Float
• With float property, an element can be pushed to the left or right,
allowing other elements to wrap around it.
• Float is very often used for images, but it is also useful when working
with layouts.
• The syntax is:
float: left | right | none;
By.Abrahim A. 135
CSS Display and Visibility…
• absolute: Absolutely positioned elements are removed from the
document flow entirely and positioned relative to a containing element.
• Unlike relatively positioned elements, the space they would have occupied is
closed up and hence they have no influence on the layout of surrounding
elements.
By.Abrahim A. 136
CSS Display and Visibility…
• The actual position of an element is specified with four offset properties:
top/right/bottom/left: length | percentage | auto;
• Example:
<html>
<head>
<style type=”text/css”>
span {
position: relative;
top: 30px; left: 60px;
background-color: fuchsia;
}
</style>
</head>
<body>
<p> Along the road he came upon a man who had <span>never worn any trouser </span>, and
who was trying to put on a pair. So he had fastened them to tree … </p>
</body>
</html>
By.Abrahim A. 137
CSS Display and Visibility…
By.Abrahim A. 138
CSS Display and Visibility…
Z-index
• When elements are positioned outside the normal flow, they
can overlap other elements.
• The z-index property specifies the stack order of an element
i.e. which element should be placed in front of, or behind, the
others.
• Syntax:
z-index: value| auto;
• An element can have a positive or negative stack order.
By.Abrahim A. 141
CSS Display and Visibility…
• Cursor
• It is possible to set the type of cursor to be displayed on
HTML elements.
• This property specifies the type of cursor to be displayed
for the pointing device.
• Syntax:
cursor: url | auto | crosshair | default | pointer | move | e-
resize | ne-resize | nw-resize | n-resize | se-resize | sw-
resize | s-resize | w-resize | text | wait | help;
By.Abrahim A. 142