chapter three (1)
chapter three (1)
4
CSS Syntax
Some CSS styles are inherited and some not
Text-related properties are inherited font-size, font-
family, line-height, text-align, list-style, etc
Box-related and positioning styles are not inherited -
width, height, border, margin, padding, position,
float, etc.> elements do not inherit color and text-
decoration
5
CSS Syntax
/* These properties are inherited */
p{
color: blue;
font-size: 18px;
}
div {
color: red;
font-size: 14px;
}
section {
font-family: Arial, sans-serif;
}
/* The paragraph will inherit color and font-size from div and font-family: Arial
from section */If a p is inside the div
Properties that are commonly inherited:
color
font-family
font-size
line-height
text-align
visibility
Non-Inherited Properties:
•margin, padding, border, background, width, height, etc., are not inherited by default.
Style Sheets Syntax
Stylesheets consist of rules, selectors, declarations, properties and
values
8
Selectors
Selectors used to determine on which element the rule will be
applied:
9
Linking HTML and CSS
HTML (content) and CSS (presentation) can be linked in three
ways:
Inline: the CSS rules in the style attribute
No selectors are needed
Embedded: in the <head> in a <style> tag
External: CSS rules in separate file (best)
Usually a file with .css extension
Linked via <link rel="stylesheet" href=…>
10
Inline Styles: Example
inline-styles.html
<!DOCTYPE html">
<head>
<title>Inline Styles</title>
</head>
<body>
<p>Here is some text</p>
<!--Separate multiple styles with a semicolon-->
<p style="font-size: 30pt">Here is some
more text</p>
<p style="font-size: 20pt;color:
#0000FF" >Even more text</p>
</body>
</html>
11
Embedded Styles
• Embedded in the HTML in the <style> tag:
• The <style> tag is placed in the <head> section of the
document
12
Embedded Styles: Example
<!DOCTYPE html>
<html>
<head>
<style>
body {background-color: powderblue;}
h1 {color: blue;}
p {color: red;}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
</body>
</html>
13
External CSS Styles
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 (see http://www.csszengarden.com/)
link tag (with a rel attribute)
• Specifies a relationship between current document
and another document
<link rel="stylesheet" type="text/css" href="styles.css">
<!DOCTYPE html>
body
<html>
{
<head>
background-color: pink;
<link rel="stylesheet" href="styles.css">
}
</head>
h1 {
<body>
color: blue;
<h1>This is a heading</h1>
}
<p>This is a paragraph.</p>
p {
</body>
color: red;
</html>
}
15
Text-related CSS Properties
color – specifies the color of the text
font-size – size of font: xx-small, x-small, small,
medium, large, x-large, xx-large, smaller, larger or
numeric value
font-family – comma separated font names
• Example: verdana, sans-serif, etc.
• There should always be at least one generic font
font-weight can be normal, bold, bolder, lighter or a
number in range [100 … 900]
16
CSS Rules for Fonts (2)
• font-style
• Values: normal, italic, oblique
• text-decoration
• Values: none, underline, line-trough, overline,
blink
• text-align
• Values: left, right, center, justify
17
Shorthand Font Property
• font
• Shorthand rule for setting multiple font properties at
the same time
font:italic normal bold
12px/16px verdana
• is equal to writing this:
font-style: italic;
font-variant: normal;
font-weight: bold;
font-size: 12px;
line-height: 16px;
font-family: verdana;
18
Backgrounds
• background-image
• URL of image to be used as background, e.g.:
background-image:url("back.gif");
• background-color
• Using color
• background-repeat
• repeat-x, repeat-y, repeat, no-repeat
• background-attachment
• fixed / scroll
19
Backgrounds (2)
Background-position: specifies vertical and horizontal
position of the background image
• Examples:
background-position: top left;
Each property can be defined separately for left, top, bottom and
right
• border-top-style, border-left-color, …
21
Width and Height
width – defines numerical value for the width of element,
e.g. 200px
25
Positioning
position: defines the positioning of the element in the page
content flow
The value is one of:
• static (default)
• relative – relative position according to where the element
would appear with static position
• absolute – position according to the innermost positioned
parent element
• fixed – same as absolute, but ignores page scrolling
26
Float
• float: the element “floats” to one side
• left: places the element on the left and following content
on the right
• right: places the element on the right and following
content on the left
• margins of floated elements do not collapse
• floated inline elements can apply height
27
Clear
• clear
• Clearing floats
29
Visibility
• visibility
• Determines whether the element is visible
• hidden: element is not rendered, but still occupies
place on the page (similar to opacity:0)
• visible: element is rendered normally
30
Display
• display: controls the display of the element and the way it is
rendered and if breaks should be placed before and after the element
• block: breaks are placed before AND after the element (<div> is
a block element)
31
Overflow
• overflow: defines the behavior of element when content
needs more space than you have specified by the size
properties or for other reasons. Values:
visible (default) – content spills out of the element
auto - show scrollbars if needed
scroll – always show scrollbars
hidden – any content that cannot fit is clipped
32
Other CSS Properties
• cursor: specifies the look of the mouse cursor
when placed over the element
• Values: crosshair, help, pointer, progress,
move, hair, col-resize, row-resize, text,
wait, copy, drop, and others
• white-space – controls the line breaking of text.
Value is one of:
• nowrap – keeps the text on one line
• normal (default) – browser decides whether to
brake the lines if needed
33
Table Styling Properties
• Table Borders
table, th, td {
border: 1px solid;
}
• Table widith thight
• table {
width: 100%;
height:40%;
}
Cont.
• CSS Table Alignment
td {
text-align: center;
vertical-align: bottom;
}
CSS: Creating Navigation Bars
<ul id="navbar">
<li><a href="/tests">Home</a></li>
<li><a href="/studyroom">About US</a></li>
<li><a href="/flashcards">contact US</a></li>
<li><a href="/library">help</a></li>
<li><a href="/testimonials">login</a></li>
</ul>
Cont..
#navbar {
list-style-type: none;
margin: 0;
padding: 0;
width: 200px;
background-color: #EEEEEE;
}
Cont..
ul#navbar li a {
display: block;
color: #000000;
font-weight:bold;
padding: 8px 16px;
text-decoration: none;
}
Cont..
ul#navbar li a:hover {
background-color: orange;
color: white;
}
Cont..
ul#navbar li {
display: inline;
CSS Measuring Units
• CSS Units
• CSS has several different
units for expressing a
length.
42