Tài liệu CSS - Tập tin định kiểu theo tầng ppt

45 660 0
Tài liệu CSS - Tập tin định kiểu theo tầng ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

CSS Bước tới: menu, tìm kiếm Trong tin học, các tập tin định kiểu theo tầng – dịch từ tiếng Anh là Cascading Style Sheets (CSS) – được dùng để miêu tả cách trình bày các tài liệu viết bằng ngôn ngữ HTML và XHTML. Ngoài ra ngôn ngữ định kiểu theo tầng cũng có thể dùng cho XML, SVG, XUL v.v . Các đặc điểm kỹ thuật của CSS được duy trì bởi World Wide Web Consortium (W3C). Thay vì đặt các thẻ qui định kiểu dáng cho văn bản HTML (hoặc XHTML) ngay trong nội dung của nó, bạn nên sử dụng CSS. Tác dụng • Hạn chế tối thiểu việc làm rối mã HTML của trang web bằng các thẻ quy định kiểu dáng (chữ đậm, chữ in nghiêng, chữ có gạch chân, chữ màu .), khiến mã nguồn của trang web được gọn gàng hơn, tách nội dung của trang web và định dạng hiển thị, dễ dàng cho việc cập nhật nội dung. • Tạo ra các kiểu dáng có thể áp dụng cho nhiều trang web, giúp tránh phải lặp lại việc định dạng cho các trang web giống nhau. Sử dụng Có 3 cách để sử dụng CSS. 1. Áp dụng trực tiếp trên một đối tượng nhất định bằng thuộc tính style <span style="font-weight:bold;text- decoration:underline;color:#FF0000;">Đoạn text cần in đậm, gạch chân, màu đỏ</span> 2. Đặt CSS ở đầu trang web để áp dụng kiểu dáng cho một mình trang ấy o Đặt đoạn CSS trong header của web (giữa <head> và </head>): <style type="text/css"> body {font-family:verdana;color:#0000FF;} // Kiểu chữ trong trang web là "Verdana", màu chữ thông thường là màu xanh dương </style> 3. Đặt các thuộc tính CSS vào một tệp riêng biệt (*.css), có thể đưa vào nhiều trang khác nhau o Nội dung tệp style.css: body {font-family:verdana;color:#0000FF;} • Đặt tệp này vào trang web bằng đoạn mã (mã có thể nằm ngoài thẻ <head>): <link rel="stylesheet" type="text/css" href="style.css"> Chú thích: Mở đầu bằng /* và kết thúc bằng */. Cú pháp: cú pháp của : tên_css { thuộc_tính: giá_trị_của_thuộc_tính; } ví dụ: body { background: #eeeeee; font-family: Verdana, Arial, serif; } Tính kế thừa: như ví dụ trên thì tòan bộ các tag HTML có tên body + những tag HTML nằm trong body đều được định dạng theo body, nghĩa là background: #eeeeee và font-family: Verdana, Arial, serif; mà body là thẻ lớn nhất chưa nội dung của website cho nên tất cả các tag khác đều sử dụng các định dạng của body trong trường hợp muốn sử dụng một định dạng khác trong một đối tượng nhỏ hơn body giả sử: p thì chỉ việc định nghĩa thêm đối tượng đó p {font-family: Tahoma, serif;} lúc này tất cả nội dung trong thẻ HTML đều có font là Tahoma chứ khônng phải Verdana của body Tính kết hợp: có thể định nghĩa nhiều css cùng một thuộc tính ví dụ: h1, h2, h3, h4, h5, h6 { color: #009900; font-family: Georgia, sans-serif; } => định nghĩa cung một thuộc tính cho tất cả các tag h1,h2,h3,h4,h5,h6 thay vì phải định nghĩa: h1{ color: #009900; font-family: Georgia, sans-serif; } h6 { color: #009900; font-family: Georgia, sans-serif; } CSS Tutorial Save a lot of work with CSS! In our CSS tutorial you will learn how to use CSS to control the style and layout of multiple Web pages all at once. Start learning CSS now! CSS Demo With CSS you can create good-looking websites with nice effects. With CSS you can control the text (like font, color, size, etc.), and the layout (like backgrounds, margin, padding, etc.) of a website, in one single file! With CSS you save a lot of work! CSS Introduction What You Should Already Know Before you continue you should have a basic understanding of the following: • HTML / XHTML If you want to study these subjects first, find the tutorials on our Home page. What is CSS? • CSS stands for Cascading Style Sheets • Styles define how to display HTML elements • Styles were added to HTML 4.0 to solve a problem • External Style Sheets can save a lot of work • External Style Sheets are stored in CSS files CSS Demo An HTML document can be displayed with different styles: See how it works Styles Solved a Big Problem HTML was never intended to contain tags for formatting a document. HTML was intended to define the content of a document, like: <h1>This is a heading</h1> <p>This is a paragraph.</p> 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, the World Wide Web Consortium (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. CSS Saves a Lot of Work! CSS defines HOW HTML elements are to be displayed. Styles are normally saved in external .css files. External style sheets enable you to change the appearance and layout of all the pages in a Web site, just by editing one single file! CSS Syntax Examples • Look at Example 1 • Look at Example 2 This is the style sheet file (ex1.css): body {background-color: yellow} h1 {font-size: 36pt} h2 {color: blue} p {margin-left: 50px} The HTML file below links to an external style sheet with the <link> tag: <html> <head> <link rel="stylesheet" type="text/css" href="ex1.css" /> </head> <body> <h1>This header is 36 pt</h1> <h2>This header is blue</h2> <p>This paragraph has a left margin of 50 pixels</p> </body> </html> The result is in the frame below: This header is 36 pt This header is blue This paragraph has a left margin of 50 pixels This is the style sheet file (ex2.css): body {background-color: tan} h1 {color:maroon; font-size:20pt} hr {color:navy} p {font-size:11pt; margin-left: 15px} a:link {color:green} a:visited {color:yellow} a:hover {color:black} a:active {color:blue} The HTML file below links to an external style sheet with the <link> tag: <html> <head> <link rel="stylesheet" type="text/css" href="ex2.css" /> </head> <body> <h1>This is a header 1</h1> <hr /> <p>You can see that the style sheet formats the text</p> <p><a href="http://www.w3schools.com" target="_blank">This is a link</a></p> </body> </html> The result is in the frame below: This is a header 1 You can see that the style sheet formats the text This is a link Syntax The CSS syntax is made up of three parts: a selector, a property and a value: selector {property:value} The selector is normally the HTML element/tag you wish to define, the property is the attribute you wish to change, and each property can take a value. The property and value are separated by a colon, and surrounded by curly braces: body {color:black} Note: If the value is multiple words, put quotes around the value: p {font-family:"sans serif"} Note: If you want to specify more than one property, you must separate each property with a semicolon. The example below shows how to define a center aligned paragraph, with a red text color: p {text-align:center;color:red} To make the style definitions more readable, you can describe one property on each line, like this: p { text-align:center; color:black; font-family:arial } Grouping You can group selectors. Separate each selector with a comma. In the example below we have grouped all the header elements. All header elements will be displayed in green text color: h1,h2,h3,h4,h5,h6 { color:green } The class Selector With the class selector you can define different styles for the same type of HTML element. Say that you would like to have two types of paragraphs in your document: one right-aligned paragraph, and one center-aligned paragraph. Here is how you can do it with styles: p.right {text-align:right} p.center {text-align:center} You have to use the class attribute in your HTML document: <p class="right">This paragraph will be right-aligned.</p> <p class="center">This paragraph will be center-aligned.</p> Note: To apply more than one class per given element, the syntax is: <p class="center bold">This is a paragraph.</p> The paragraph above will be styled by the class "center" AND the class "bold". You can also omit the tag name in the selector to define a style that will be used by all HTML elements that have a certain class. In the example below, all HTML elements with class="center" will be center-aligned: .center {text-align:center} In the code below both the h1 element and the p element have class="center". This means that both elements will follow the rules in the ".center" selector: <h1 class="center">This heading will be center-aligned</h1> <p class="center">This paragraph will also be center-aligned.</p> Do NOT start a class name with a number! This is only supported in Internet Explorer. Add Styles to Elements with Particular Attributes You can also apply styles to HTML elements with particular attributes. The style rule below will match all input elements that have a type attribute with a value of "text": input[type="text"] {background-color:blue} The id Selector You can also define styles for HTML elements with the id selector. The id selector is defined as a #. The style rule below will match the element that has an id attribute with a value of "green": #green {color:green} The style rule below will match the p element that has an id with a value of "para1": p#para1 { text-align:center; color:red } Do NOT start an ID name with a number! It will not work in Mozilla/Firefox. CSS Comments Comments are used to explain your code, and may help you when you edit the source code at a later date. A comment will be ignored by browsers. A CSS comment begins with "/*", and ends with "*/", like this: /*This is a comment*/ p { text-align:center; /*This is another comment*/ color:black; font-family:arial } CSS How To . When a browser reads a style sheet, it will format the document according to it. Three Ways to Insert CSS There are three ways of inserting a style sheet: • External style sheet • Internal style sheet • Inline style External Style Sheet An external style sheet is ideal when the style is applied to many pages. With an external style sheet, you can change the look of an entire Web site by changing one file. Each page must link to the style sheet using the <link> tag. The <link> tag goes inside the head section: <head> <link rel="stylesheet" type="text/css" href="mystyle.css" /> </head> An external style sheet can be written in any text editor. The file should not contain any html tags. Your style sheet should be saved with a .css extension. An example of a style sheet file is shown below: hr {color:sienna} p {margin-left:20px} body {background-image:url("images/back40.gif")} Do not leave spaces between the property value and the units! "margin-left:20 px" (instead of "margin-left:20px") will work in IE, but not in Firefox or Opera. Internal Style Sheet An internal style sheet should be used when a single document has a unique style. You define internal styles in the head section of an HTML page, by using the <style> tag, like this: <head> <style type="text/css"> hr {color:sienna} p {margin-left:20px} body {background-image:url("images/back40.gif")} </style> </head> Inline Styles An inline style loses many of the advantages of style sheets by mixing content with presentation. Use this method sparingly! To use inline styles you use the style attribute in the relevant tag. The style attribute can contain any CSS property. 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> Multiple Style Sheets If some properties have been set for the same selector in different style sheets, the values will be inherited from the more specific style sheet. 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: [...]... All CSS Font Properties The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or CSS2 ) Property font font-family font-size font-style font-variant Description Sets all the font properties in one declaration Values font-style font-variant font-weight font-size/line-height font-family caption icon menu message-box small-caption status-bar inherit Specifies... background image will be repeated background-attachment background-position inherit scroll fixed inherit color-rgb color-hex color-name transparent inherit url(URL) none inherit top left top center top right center left center center center right bottom left bottom center bottom right x% y% xpos ypos inherit repeat repeat-x repeat-y no-repeat inherit 1 1 1 1 1 CSS Text The CSS text properties define the appearance... Aligns the text in an element text-decoration Adds decoration to text text-indent Indents the first line of text in an element text-shadow Values color ltr rtl normal number length % normal length left right center justify none underline overline line-through blink length % none color length CSS 1 2 1 1 1 1 1 text-transform unicode-bidi vertical-align white-space word-spacing Controls the letters in... properties background-color in one declaration background-image background-repeat CSS 1 background-attachment background-color Sets whether a background image is fixed or scrolls with the rest of the page Sets the background color of an element background-image Sets the background image for an element background-position Sets the starting position of a background image background-repeat Sets if/how... font family for text family-name generic-family inherit Specifies the font size of text xx-small x-small small medium large x-large xx-large smaller larger length % inherit Specifies the font style for text normal italic oblique inherit Specifies whether or not a text should be normal CSS 1 1 1 1 1 displayed in a small-caps font font-weight Specifies the weight of a font small-caps inherit normal bold... • • background-color background-image background-repeat background-attachment background-position It does not matter if one of the property values are missing, as long as the ones that are present are in this order This example uses more advanced CSS Take a look: Advanced example body { margin-left:200px; background:#5d9ab2 url('img_tree.png') no-repeat top left;... {text-align:center} p.date {text-align:right} p.main {text-align:justify} Text Decoration The text-decoration property is used to set or remove decorations from text The text-decoration property is mostly used to remove underlines from links for design purposes: Example a {text-decoration:none} It can also be used to decorate text: Example h1 {text-decoration:overline} h2 {text-decoration:line-through}... paragraph, with a text-bottom alignment. All CSS Text Properties The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or CSS2 ) Property color direction Description Sets the color of a text Sets the text direction line-height Sets the distance between lines letter-spacing Increase or decrease the space between characters text-align Aligns the text... body { background-image:url('smiley.gif'); background-repeat:no-repeat; background-attachment:fixed } The background-image is fixed Try to scroll down the page. The background-image is fixed Try to scroll down the page. The background-image is fixed Try to scroll down the page. The background-image is fixed Try to scroll... background-image is fixed Try to scroll down the page. The background-image is fixed Try to scroll down the page. The background-image is fixed Try to scroll down the page. The background-image is fixed Try to scroll down the page. All CSS Background Properties The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or CSS2 ) Property . CSS Bước tới: menu, tìm kiếm Trong tin học, các tập tin định kiểu theo tầng – dịch từ tiếng Anh là Cascading Style Sheets (CSS) – được dùng. các tài liệu viết bằng ngôn ngữ HTML và XHTML. Ngoài ra ngôn ngữ định kiểu theo tầng cũng có thể dùng cho XML, SVG, XUL v.v . Các đặc điểm kỹ thuật của CSS

Ngày đăng: 13/12/2013, 23:15

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan