Bài giảng Web technologies and e-Services - Bài 6: CSS - Nguyễn Bình Minh

Web Development  
Chapter 6. CSS  
1
Before and after using CSS  
2
2
1
login.html  
u …  
u <link rel="stylesheet" type="text/css" href=“style.css">  
u …  
u <table class="forumline" width="280" border="0"  
cellspacing="1" cellpadding="2">  
u <tr class="formstyle><td>  
style.css  
….  
body  
{
font-family: Verdana,Tahoma,Geneva,Arial,Helvetica,sans-serif;  
font-size: 12px; …  
}
.formstyle  
{
background-color: #D7E5F5;  
font-family: Verdana,Tahoma,Geneva,Arial,Helvetica,sans-serif;  
font-size: 12px;  
}
.forumline  
{
background-color: …  
} …  
3
3
Content  
2Specifying and applying style rules  
3Style class  
4Some useful properties  
5CSS box model  
4
4
2
1. Introduction to CSS  
u Cascading Style Sheet  
u Created by Hakon Wium Lie of MIT in  
1994  
u Has become the W3C standard for  
controlling visual presentation of web  
pages  
5
5
1.1. Benefits of CSS  
u Simple syntax: easy to learn  
u Powerful and flexible way to specify the  
formatting of HTML elements  
– Can define font, size, background color,  
background image, margins, etc  
u Separates presentation (design elements)  
from content (structural logic)  
– HTML contains content and structure of a web  
page.  
– CSS defines a style of a web page – how the  
content is displayed  
6
3
1.1. Benefits of CSS (2)  
u Share style sheets across multiple  
documents or entire Web site  
– Easy to maintain consistent pages  
– Can update a common style à Reflected in all  
pages that use it  
u Cost Savings  
– Reduced Bandwidth Costs  
u One style sheet called and cached  
u CSS require less code  
– Higher Search Engine Rankings  
u Cleaner code is easier for search engines to index  
u Greater density of indexable content  
7
1.2. CSS Basics  
u CSS defines the way that HTML  
elements should be presented:  
Positioning (e.g. left, right or centered)  
Font (size and family)  
Text decoration (e.g. underlined)  
Borders (solid, dashed, invisible)  
Image usage (e.g. for backgrounds and  
bullets)  
8
4
1.3. CSS Does Not…  
u Rorder HTML  
– E.g. won’t sort a table  
u Perform calculations  
– Won’t sum a shopping basket  
u Filter  
– Won’t decide what to show  
– Though JavaScript can set display or visibility  
of elements in order to achieve this  
u These can all be done on the server  
– Or using XSLT or JavaScript on the client  
9
1.4. Types of CSS Styles  
u (Browser default)  
u External styles  
– written in a separate document and then attached to  
various Web documents  
– External style sheets can affect any document they are  
attached to  
u Internal styles (embedded styles)  
– embedded in the head of the document.  
– embedded styles affect only the tags on the page they  
are embedded in  
u Inline Style  
– written directly in the tag on the document  
10  
10  
5
Content  
1Introduction to CSS  
3Style class  
4Some useful properties  
5CSS box model  
11  
11  
2.1. Specifying Style Rules  
u General form of rule  
selector { property: value }  
Or  
selector { property1: value1;  
property2: value2;  
...  
propertyN: valueN }  
u Example  
H1 { texalign: center;  
color: blue }  
12  
12  
6
2.1. Specifying Style Rules (2)  
u The selector is the link between the HTML  
document and the style. It specifies what  
elements are affected by the declaration.  
u The declaration is that part of the rule that  
sets forth what the effect will be  
13  
13  
2.1. Specifying Style Rules (3)  
u Grouping selectors and rules  
H1 { fonweight: bold }  
H2 { fonweight: bold }  
H3 { fonweight: bold }  
àH1, H2, H3 { fonweigh: bold }  
àWhat is different?  
b backgrouncolor:yellow;}  
bcolor:blu;}  
u A selector may have more than one  
declaration  
H1 { color: green }  
H1 { texalign: center }  
14  
14  
7
2.2. Applying styles to the  
document  
u Inline style  
– Apply a style sheet to an individual element  
using the style attribute  
u Embedded style  
– Apply the basic, documenwide style sheet for  
the document by using the style element  
u External style  
– Link an external style sheet to the document  
using the link element or  
– Import a style sheet using the CSS @import  
notation.  
15  
15  
2.2.1. Inline style  
u Using Style attribute  
u For individual elements  
<H1 STYLE=“color: blue; fonsize: 20pt;”>  
A large purple Heading  
</H1>  
16  
16  
8
2.2.2. Embedded style  
u Using Style element  
u Putting the style sheet inside a style element at  
the top of your document  
<HTML>  
<HEAD><TITLE>Bach's home page</TITLE>  
<STYLE> H1, H2 { color: green } </STYLE>  
</HEAD>  
<BODY>  
<H1>Bach's home page</H1>  
<P>Johann Sebastian Bach was a prolific composer. Among his  
works are: <UL> <LI>the Goldberg Variations  
<LI>the Brandenburg Concertos  
<LI>the Christmas Oratorio </UL>  
<H2>Historical perspective</H2>  
<P>Bach composed in what has been referred to as the Baroque  
period.  
</BODY>  
</HTML>  
17  
17  
2.2.2. Embedded style (2)  
<STYLE type=“text/css”>  
<––  
H1, H2 { color: green }  
->  
</STYLE>  
18  
18  
9
Tree structures and inheritance  
u Just as children inherit from their parents,  
HTML elements inherit stylistic properties.  
u CSS property values set on one element  
will be transferred down the tree to its  
descendants  
<STYLE TYPE="textcs">  
BODY { color: green }  
</STYLE>  
19  
19  
Overriding inheritance  
u Sometimes children don't look like  
their parents.  
u E.g.  
<STYLE TYPE="textcs">  
BODY { color: green }  
H1 { color: navy }  
</STYLE>  
20  
20  
10  
2.2.3. External style  
u Using Link element  
u This is true “separation” of style and content.  
u Keeping all your styles in an external document is simpler  
<HEAD>  
<LINK REL=“stylesheet” TYPE=“texcs” HREF=“stylesmystyles.cs”>  
</HEAD>  
/* mystyles.css - a simple style sheet */  
body {  
margileft: 10%;  
margiright: 10%;  
color: black;  
background: white;  
}
21  
21  
Content  
1Introduction to CSS  
2Specifying and applying style rules  
4Some useful properties  
5CSS box model  
22  
22  
11  
3.1. Element Style Classes  
u Proceed the HTML element by a period and a class  
name  
// Define an "abstract" paragraph type  
P.abstract { margileft: 0.5in;  
margiright: 0.5in;  
fonstyle: italic }  
u To use, supply the name of the style class in the  
CLASS attribute of the HTML element  
<H1>New Advances in Physics</H1>  
<P CLASS="abstract">  
This paper gives the solution to three previously  
unsolved problems: turning lead into gold,  
antigravity, and a practical perpetual motion machine.  
23  
23  
3.2. Global Style Classes  
u omit the element name  
// Style available to all elements  
.blue { color: blue; fonweight: bold }  
u To use, simple specify the style class in  
the CLASS attribute of the HTML element  
<H2 CLASS="blue">A Blue Heading</H2>  
<-- Apply to a section of text ->  
This text is in the default color, but  
<SPAN CLASS="blue">this text is blue.</SPAN>  
24  
24  
12  
3.3. Styles through UseDefined IDs  
u An ID is like a class but can be applied  
only once in a document  
<HEAD>  
<TITLE>...</TITLE>  
<STYLE TYPE="textcs">  
<--  
#foo { color: red }  
->  
</STYLE>  
</HEAD>  
<BODY>  
...  
<P ID="foo">  
...  
25  
</BODY>  
25  
Content  
1Introduction to CSS  
2Specifying and applying style rules  
3Style class  
5CSS box model  
26  
26  
13  
4.1. Useful Font Properties  
u fonweight  
– Relative weight (boldness) of font  
normal | lighter | bold | bolder | 100 | 200 | ... | 900  
H1 { fonweight : 200 }  
H2 { fonweight : bolder }  
u fonstyle  
– Font face type within a family  
normal | italic | oblique  
P { fonstyle : normal }  
TH { fonsytle : italic }  
27  
27  
4.1. Useful Font Properties (2)  
u fonsize  
– Either relative or absolute size of font  
– pp, in, cm, mm | em, e, p, % | xlarge | large |  
large | medium | small | small | xsmall | smaller |  
larger  
STRONG { fonsize: 150% }  
P { fonsize: 14pt }  
P { fonsize: xlarge }  
u fonfamily  
– Typeface family for the font  
H1 { fonfamily: Arial }  
28  
28  
14  
4.2. Useful Text Properties  
u texdecoration  
– Describes text additions or “decorations” that are added to the  
text of an element  
none | underline | overline | linthrough | blink  
– E.gP { texdecoration: underline }  
u verticaalign  
– Determines how elements are positioned vertically  
– top | bottom | baseline | middle | sub | super | textop |  
texbottom | %  
u texalign  
– Determines how paragraphs are positioned horizontally  
left | right | center | justify  
29  
29  
4.2. Useful Text Properties (2)  
u texindent  
– Specifies the indentation of the first line of the  
paragraph  
– +– p, p, in, cm, mm | +– em, e, p, %  
E.gP { texindent: 25px } /* Hanging indent */  
u linheight  
– Specifies the distance between two  
consecutive baselines in a paragraph  
normal | number | p, p, in, cm, mm | em, e, p, %  
.double { linheight: 200% }  
.triple { linheight: 3 } /* 3x the font size */  
DIV { linheight: 1.5em }  
30  
30  
15  
4.3. Useful Foreground and  
Background Properties  
u color  
– Color of the text or foreground color  
– coloname | #RRGGBB | #RGB | rgrr, gg, bb) |  
rgrr%, gg%, bb%)  
P { color : blue }  
H1 { color : #00AABB }  
H3 { color : rg(255, 0, 0 ) } /* red */  
u backgroundimage  
– Specifies an image to use as the background of region  
none | urfilename)  
H2 { backgrounimage: urBluedrop.gi);}  
31  
31  
4.3. Useful Foreground and  
Background Properties (2)  
u backgrounrepeat  
– Specifies how to tile the image in the region  
repeat | repeax | repeay | norepeat  
BODY {  
backgrounimage: urBluedot.gi);  
backgrounrepeat: repeax;  
}
u background  
– Lets you combine properties in a single entry  
P { background: urwallpaper.jp) repeax }  
32  
32  
16  
Content  
1Introduction to CSS  
2Specifying and applying style rules  
3Style class  
4Some useful properties  
33  
33  
5. CSS Box Model  
u Each HTML element have the rectangular “box”  
u Each box has a content area and optional  
surrounding paddin, border and margin area  
Top  
Margin  
Border  
Padding  
Right  
Content (Text/Image)  
Left  
Bottom  
34  
17  
CSS Box Modeexample  
div#boxtest {  
backgrouncolor: red; color: white;  
padding: 1em;  
border: 1em solid green;  
Top  
margin: 1em;  
Margin  
Border  
}
Padding  
Right  
Content (Text/Image)  
Left  
Bottom  
35  
CSS Box Modecolor  
u Padding - same as the element’s  
backgrouncolor  
u Border - may have its own color borde-  
color property)  
u Margin - always transparent  
(same as its ancestor's backgrouncolo)  
Margin  
Border  
Padding  
Content (Text/Image)  
36  
18  
CSS Box Model edge sizes  
u Inner edge (Black line): Content itself or CSS width  
and height property may define the size  
u Padding edge (Pink): IE + padding width  
u Border edge (Brown): PE + border width  
u Outer (margin) edge (Dotted): BE + margin width  
37  
CSS Box Mode– width (1)  
u margi, paddin, bordewidth  
– Define the width for all directions at once  
u margito, paddinto, bordetowidth  
– Define the width for each specific direction  
– top, right, lef, bottom  
Margin  
Border  
Padding  
Content  
(Text/Image)  
38  
19  
CSS Box Mode– width (2)  
u Effective values for box width  
u <length> - e.g. 10pt, 3px, 1.2em  
– Effective for border, padding, margin  
u <percentage> - e.g. 10%  
– Effective only for padding, margin  
– Calculated with respect to the width of the  
generated box’s containing block  
u Thin, medium, thick  
– Effective only for border  
39  
Border properties  
u bordewidth or bordetowidth (top, right, left,  
bottom)  
– Specify the line width  
u bordecolor or bordetocolor (top, right, left,  
bottom)  
– Specify the line color by the color name or RGB values  
u bordestyle or bordetostyle (top, right, left,  
bottom)  
– Specify the line style of box’s border  
– Values: solid, dotted, dashed, double, groove, ridge, inset,  
outset, hidden  
– Special value non” means width 0  
u border or bordetop (top, right, left, bottom)  
– shorthand property for setting the width, style, and color  
– e.g. “border: 1em solid black;”  
40  
20  
Tải về để xem bản đầy đủ
pdf 24 trang Thùy Anh 27/04/2022 8640
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Web technologies and e-Services - Bài 6: CSS - Nguyễn Bình Minh", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pdfbai_giang_web_technologies_and_e_services_bai_6_css_nguyen_b.pdf