CSS - Tập tin định kiểu theo tầng

Chia sẻ: Lê Văn Thành | Ngày: | Loại File: DOC | Số trang:44

0
146
lượt xem
97
download

CSS - Tập tin định kiểu theo tầng

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

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....

Chủ đề:
Lưu

Nội dung Text: CSS - Tập tin định kiểu theo tầng

  1. 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 Đoạn text cần in đậm, gạch chân, màu đỏ 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 và ): 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 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ẻ ):
  2. 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; }
  3. 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
  4. HTML was never intended to contain tags for formatting a document. HTML was intended to define the content of a document, like: This is a heading This is a paragraph. When tags like , 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 tag:
  5. This header is 36 pt This header is blue This paragraph has a left margin of 50 pixels 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 tag: This is a header 1 You can see that the style
  6. sheet formats the text This is a link 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}
  7. 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: This paragraph will be right-aligned. This paragraph will be center-aligned. Note: To apply more than one class per given element, the syntax is: This is a paragraph. 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:
  8. This heading will be center-aligned This paragraph will also be center-aligned. 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 }
  9. 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 tag. The tag goes inside the head section: 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 tag, like this: hr {color:sienna} p {margin-left:20px} body {background-image:url("images/back40.gif")}
  10. 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: This is a paragraph. 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: h3 { text-align:right; font-size:20pt } If the page with the internal style sheet also links to the external style sheet the properties for h3 will be: color:red; text-align:right; font-size:20pt The color is inherited from the external style sheet and the text-alignment and the font-size is replaced by the internal style sheet. Multiple Styles Will Cascade into One
  11. Styles can be specified: • inside an HTML element • inside the head section of an HTML page • in an external CSS file Tip: Even multiple external style sheets can be referenced inside a single HTML document. Cascading order What style will be used when there is more than one style specified for an HTML element? Generally speaking we can say that all the styles will "cascade" into a new "virtual" style sheet by the following rules, where number four has the highest priority: 1. Browser default 2. External style sheet 3. Internal style sheet (in the head section) 4. Inline style (inside an HTML element) So, an inline style (inside an HTML element) has the highest priority, which means that it will override a style defined inside the tag, or in an external style sheet, or in a browser (a default value). Note: If the link to the external style sheet is placed after the internal style sheet in HTML , the external style sheet will override the internal style sheet! CSS Background CSS background properties are used to define the background effects of an element. CSS properties used for background effects: • background-color • background-image • background-repeat • background-attachment • background-position Background Color The background-color property specifies the background color of an element. The background color of a page is defined in the body selector:
  12. Example body {background-color:#b0c4de} The background color can be specified by: • name - a color name, like "red" • RGB - an RGB value, like "rgb(255,0,0)" • Hex - a hex value, like "#ff0000" In the example below, the h1, p, and div elements have different background colors: Example h1 {background-color:#6495ed} p {background-color:#e0ffff} div {background-color:#b0c4de} Background Image The background-image property specifies an image to use as the background of an element. By default, the image is repeated so it covers the entire element. The background image for a page can be set like this: Example body {background-image:url('paper.gif')} Below is an example of a bad combination of text and background image. The text is almost not readable: Example body {background-image:url('bgdesert.jpg')} Background Image - Repeat Horizontally or Vertically By default, the background-image property repeats an image both horizontally and vertically. Some images should be repeated only horizontally or vertically, or they will look strange, like this: Example
  13. body { background-image:url('gradient2.png'); } If the image is repeated only horizontally (repeat-x), the background will look better: Example body { background-image:url('gradient2.png'); background-repeat:repeat-x; } Background Image - Set position and no-repeat When using a background image, use an image that does not disturb the text. Showing the image only once is specified by the background-repeat property: Example body { background-image:url('img_tree.png'); background-repeat:no-repeat; } In the example above, the background image is shown in the same place as the text. We want to change the position of the image, so that it does not disturb the text too much. The position of the image is specified by the background-position property: Example body { background-image:url('img_tree.png'); background-repeat:no-repeat; background-position:top right; } Background - Shorthand property As you can see from the examples above, there are many properties to consider when dealing with backgrounds. To shorten the code, it is also possible to specify all the properties in one single property. This is called a shorthand property.
  14. The shorthand property for background is simply "background": Example body {background:#ffffff url('img_tree.png') no-repeat top right} When using the shorthand property the order of the property values are: • 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; } .container { text-align:center; } .center_div { border:1px solid gray; margin-left:auto;
  15. margin-right:auto; width:90%; background-color:#d0f0f6; text-align:left; padding:8px; } Hello World! This example contains some advanced CSS methods you may not have learned yet. But, we will explain these methods in a later chapter in the tutorial. More Examples How to set a fixed background image This example demonstrates how to set a fixed background image. The image will not scroll with the rest of the page.
  16. 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 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. 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 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.
  17. 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 Description Values CSS background Sets all the background properties background-color 1 in one declaration background-image background-repeat background-attachment background-position inherit background-attachment Sets whether a background image scroll 1 is fixed or scrolls with the rest of fixed the page inherit background-color Sets the background color of an color-rgb 1 element color-hex color-name transparent inherit background-image Sets the background image for an url(URL) 1 element none inherit background-position Sets the starting position of a top left 1 background image top center top right center left center center center right bottom left bottom center bottom right x% y% xpos ypos inherit
  18. background-repeat Sets if/how a background image repeat 1 will be repeated repeat-x repeat-y no-repeat inherit CSS Text The CSS text properties define the appearance of text: text example This example includes some text formatting properties. The heading uses the text-align, text-transform, and color properties. The paragraph is indented and aligned, and the underline is removed from the "Try it yourself" link. Text Color The color property is used to set the color of the text. The color can be specified by: • name - a color name, like "red" • RGB - an RGB value, like "rgb(255,0,0)" • Hex - a hex value, like "#ff0000" The default color for a page is defined in the body selector. Example body {color:blue} h1 {color:#00ff00} h2 {color:rgb(255,0,0)} For W3C compliant CSS: If you define the color property, you must also define the background-color property. Text Alignment The text-align property is used to set the horizontal alignment of a text. Text can be centered, or aligned to the left or right, or justified.
  19. When text-align is set to "justify", each line is stretched so that every line has equal width, and the left and right margins are straight (like in magazines and newspapers). Example h1 {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} h3 {text-decoration:underline} h4 {text-decoration:blink} It is not recommended to underline text that is not a link, as this often confuse users. Text Transformation The text-transform property is used to specify uppercase and lowercase letters in a text. It can be used to turn everything into uppercase or lowercase letters, or capitalize the first letter of each word. Example p.uppercase {text-transform:uppercase} p.lowercase {text-transform:lowercase} p.capitalize {text-transform:capitalize} Text Indentation
  20. The text-indentation property is used to specify the indentation of the first line of a text. Example p {text-indent:50px} More Examples Specify the space between characters This example demonstrates how to increase or decrease the space between characters. h1 {letter-spacing:2px} h2 {letter-spacing:-3px} This is heading 1 This is heading 2 This is heading 1 This is heading 2 Specify the space between lines This example demonstrates how to specify the space between the lines in a paragraph. p.small {line-height: 90%} p.big {line-height: 200%} This is a paragraph with a standard line-height. The default line height in most browsers is about 110% to 120%. This is a paragraph with a standard line-height.

CÓ THỂ BẠN MUỐN DOWNLOAD

Đồng bộ tài khoản