Professional Documents
Culture Documents
Contents{
1.
What is CSS?
1.
2.
2. 3. 4. 5. 6.
{What is CSS?
CSS stands for Cascading Style Sheets Styles define how to display (X)HTML elements Styles are normally stored in Style Sheets Multiple style definitions will cascade into one
Basic formating tool Easy multiple document managment Save time by using selector classes New opportunities in formating
Structure
Styleformatting
(X)HTML
CSS
Advantages of CSS
Workflow
Basic Syntax
The selector is normally the HTML element/tag you wish to define The property is the attribute you wish to change Every property has the value
{ Syntax
To make the style definitions more readable, you can describe one property on each line
p {
text-align: center; color: black; font-family: arial }
Selectors Classification
Compound Selector
h1,h2,h3,h4,h5,h6 { color: green }
This is header h1
This is header h2
This is header h3
This is header h4
With the class selector you can define different styles for the same type of HTML element.
p.right {text-align: right} p.center {text-align: center}
<p class="right"> This paragraph will be rightaligned. </p> <p class="center"> This paragraph will be center-aligned. </p>
Using IDs
To identify the paragraph as head, use: <p id="head"><img src="scraps.jpg" alt="Online Scrapbooks width="314" height="75" /></p>
To reference the paragraph in the style sheet, use an id selector: #id {styles} #head {text-align: right; border-bottom: 1px solid orange}
13
Contextual Selectors
Use the tree structure concept to control how styles are applied to a Web page. parent descendant {styles}
body
h1
h2
h2
span
Descendant Selector
body h1 { } #navigation p {}
Child Selectors
div ol > p {}
Universal Selector
* {}
Attribute Selectors
div[href=http://home.org]
Pseudo-Class Selectors
a:active {} #nav:hover {}
An identifier is specified only once on a page and has a higher inheritance specificity than a class. A class is reusable as many times as needed in a page. Use identifiers for main sections and sub-sections of your document.
h1
h2
h2
span
p b {color: red}
body
b
h1
h2
h2
span
h1
h2
h2
span
{Cascading order
1.
2.
inside external *.css file inside the <head> tag inside an HTML element
3.
4.
Inline style
Inline styles: A style is applied to a specific element through the use of the style attribute in the elements tag. The style affects that particular tag but does not affect other tags in the document. Embedded styles: A style sheet is placed in a documents head, setting the style definitions for the entire HTML file. External styles: A style sheet is saved in a separate external file and is applied to a group of pages in a Web site.
19
Each webpage must link to the style sheet using the <link> tag
Browser reads styles definitions from mystyle.css file
Should be used when a single document has a unique style Defined in the head section by using the <style> tag
<head> <style type="text/css"> hr {color: sienna} p {margin-left: 20px} body {background-image: url("images/back40.gif")} </style> </head>
o
o
Internal Stylesheet
25
CSS units
For many of the remaining CSS values, we will need to be able to specify size measurements
These are used to specify sizes: em width of the letter m ex height of the letter x px pixels (usually 72 per inch, but depends on monitor) % percent of inherited size These are also used to specify sizes, but dont really make sense unless you know the screen resolution: in inches cm centimeters mm millimeters pt points (72pt = 1in) pc picas (1pc = 12pt)
26
Changing Background
Control over the background color of an element set an image as the background, repeat a background image
background-color
background-image
background-repeat
Text:
color direction
text-decoration
word spacing
text-align
left right center justify
{Font:
font-family
font-weigh
font-size
{List:
<head> <style type="text/css">
ul { list-style-image: url('arrow.gif') }
</style> </head> <body>
To define the display style of an element, use: display: type type is block, inline, . . .
33
Every element that is formatted by a CSS command is considered to be in an invisible box The box containing an element can have one of three display properties:
display: block
The element will start on a new line, and so will the element that follows it (an HTML paragraph is an example) (default) The element will not start on a new line, or cause the next element to start on a new line (bold is an HTML example) The element is hidden and will not appear on the display
display: inline
display: none
34
CSS Fonts
CSS Text
CSS Background
CSS Lists
margin: 5 0 5 5;
Boxes
The invisible box containing a styled element has three special areas:
The element
padding (invisible) border (can be colored) margins (invisible)
41
Padding
Padding is the extra space around an element, but inside the border To set the padding, use any or all of:
padding-top: size padding-bottom: size padding-left: size padding-right: size padding: size to set all four sides at once
size is given in the units described earlier Example: sidebar {padding: 1em; padding-bottom: 5mm}
42
Borders
You can set border attributes with any or all of: border-top:, border-bottom:, border-left:, border-right:, and border: (all at once) The attributes are:
The thickness of the border: thin, medium (default), thick, or a specific size (like 3px) The style of the border: none, dotted, dashed, solid, double, groove, ridge, inset, or outset The color of the border: one of the 16 predefined color names, or a hex value with #rrggbb or rgb(r, g, b) or rgb(r%, g%, b%)
Example: section {border-top: thin solid blue;} Note: the special styles (such as groove) are not as cool as they sound
43
Margins
Margins are the extra space outside the border Setting margins is analogous to setting padding To set the margins, use any or all of:
margin-top: size margin-bottom: size margin-left: size margin-right: size margin: size to set all four sides at once
44
CSS Shorthand
font-family: verdana, sans-serif; font-weight: bold; font-size: 12px; font: bold 12px verdana, sans-serif; padding-top: 5px; padding-right: 8px; padding-bottom: 5px; padding-left: 10px; padding: 5px 8px 5px 10px;
Floating an Element
To float an element, use: float: position position is none, left, right, or inherit #links {width: 150px; float: left; background-color: rgb(212, 142, 0); margin-right: 10px; padding: 10px; border: 2px solid black}
46
47
#note3 {position: absolute; left: 570px; top: 550px} #note2 {position: absolute; left: 170px; top: 400px}
position: absolute (this is required!) and also one or more of left: size or right: size top: size or bottom: size size can be positive or negative top: size puts an elements top size units from the page top bottom: size puts an elements bottom size units from the page bottom left: size puts an elements left side size units from the left edge of the page right: size puts an elements right side size units from the right edge of the page Changing an elements absolute position removes it from the natural flow, so other elements fill in the gap You need to be careful not to overlap other elements
position:absolute; right: 0in; bottom: 0in
49
Confusing stuff:
position: relative (this is required!) and also one or more of left: size or right: size top: size or bottom: size size can be positive or negative to move left, make left negative or right positive to move right, make right negative or left positive to move up, make top negative or bottom positive to move down, make bottom negative or top positive Setting an elements position does not affect the position of other elements, so
Confusing stuff:
There will be a gap in the elements original, natural position Unless you are very careful, your element will overlap other elements
50
To change how the browser handles content that overflows the size of an element, use: overflow: type
type is visible (default), scroll, hidden (to hide content overflow), auto (to display scrollbars only when needed)
51
Using Pseudo-Classes
usefule pseudo-classes include the link, visited, hover, and active pseudo-classes
#links a:link {text-decoration: none} #links a:visited {text-decoration: none} #links a:hover {color: black; textdecoration: underline} #links a:active {text-decoration: none}
52
Using Pseudo-Elements
<style type="text/css"> #firstp:first-line {font-variant: small-caps} #firstp:first-letter {float: left; font-size: 400%; line-height:0.8} </style> <p id="firstp">Scrapbooking is . . .
53
Be default, a style sheet is applied to all devices. To create a style sheet for specific media, add the following attribute: media=type <style type=text/css> h1 {font-family: sans-serif} </style> <style type=text/css media=screen> h1 {color: red} </style> <style type=text/css media=print> h1 {color: black} </style>
54
Pseudo-elements
Pseudo-elements describe elements that are not actually between tags in the XML document
Syntax: element:pseudo-class {...}
first-letter the first character in a block-level element first-line the first line in a block-level element (depends on the browsers current window size)
CSS Layout
Margin Padding Border Z-index Positioning Width Height Float Text-align Vertical-align
Recommendations
Remove antiquated browser checks and deliver different sheets. Consolidate all our main styles into site.css in the App_Themes folder. All CSS files should be in the App_Themes folder and have a .css extension (not .txt). Make a decision on what standard colors, fonts, alignment, etc should go into the app. Remove spacer.gifs, table layouts, and other browser hacks in lieu of proper CSS. Make a decision on how individual modules should implement their styles. Make a decision on how the CSS file is to be structured. Move inline presentation formatting to CSS external sheets.
{ Text color
<html><head> <style type="text/css"> h1 {color: green} h2 {color: #dda0dd} p {color: rgb(0,0,255)} </style> </head> <body> <h1>This is header 1</h1> <h2>This is header 2</h2> <p>This is a paragraph</p> </body> </html>
This is header 1
This is header 2
This is a paragraph