1. Trang chủ
  2. » Công Nghệ Thông Tin

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

45 661 0
Tài liệu đã được kiểm tra trùng lặp

Đ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

Thông tin cơ bản

Tiêu đề Tài liệu CSS - Tập tin định kiểu theo tầng ppt
Trường học University of Computer Science and Technology of Vietnam
Chuyên ngành Computer Science
Thể loại Tài liệu hướng dẫn
Thành phố Hà Nội
Định dạng
Số trang 45
Dung lượng 317 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

• 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 • E

Trang 1

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

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

Trang 2

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 {

Trang 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:

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

Trang 4

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}

Trang 5

<body>

<h1>This header is 36 pt</h1>

<h2>This header is blue</h2>

<p>This paragraph has a left

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}

Trang 6

<hr />

<p>You can see that the style

sheet formats the text</p>

body {color:black}

Note: If the value is multiple words, put quotes around the value:

p {font-family:"sans serif"}

Trang 7

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:

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"

Trang 8

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}

Trang 9

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:

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:

hr {color:sienna}

p {margin-left:20px}

Trang 10

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:

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:

Trang 11

Multiple Styles Will Cascade into One

Styles can be specified:

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

<head>, the external style sheet will override the internal style sheet!

Trang 12

The background-color property specifies the background color of an element.

The background color of a page is defined in the body selector:

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:

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

Trang 13

The background image for a page can be set like this:

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:

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');

Trang 14

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

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:

Trang 16

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

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

Trang 17

<p>The background-image is fixed Try to scroll down the page.</p>

<p>The background-image is fixed Try to scroll down the page.</p>

<p>The background-image is fixed Try to scroll down the page.</p>

<p>The background-image is fixed Try to scroll down the page.</p>

<p>The background-image is fixed Try to scroll down the page.</p>

<p>The background-image is fixed Try to scroll down the page.</p>

<p>The background-image is fixed Try to scroll down the page.</p>

<p>The background-image is fixed Try to scroll down the page.</p>

<p>The background-image is fixed Try to scroll down the page.</p>

<p>The background-image is fixed Try to scroll down the page.</p>

<p>The background-image is fixed Try to scroll down the page.</p>

<p>The background-image is fixed Try to scroll down the page.</p>

<p>The background-image is fixed Try to scroll down the page.</p>

<p>The background-image is fixed Try to scroll down the page.</p>

<p>The background-image is fixed Try to scroll down the page.</p>

</body>

</html>

All CSS Background Properties

The number in the "CSS" column indicates in which CSS version the property is defined (CSS1 or CSS2)

background Sets all the background properties

in one declaration

background-color background-image background-repeat

1

Trang 18

background-attachment background-position

inherit

background-attachment Sets whether a background image

is fixed or scrolls with the rest of the page

scrollfixedinherit

1

background-color Sets the background color of an

element

color-rgb color-hex color-name

transparentinherit

1

background-position Sets the starting position of a

background image

top lefttop centertop rightcenter leftcenter centercenter rightbottom leftbottom centerbottom right

Trang 19

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

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

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)

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:

Trang 20

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

Trang 21

Specify the space between characters

This example demonstrates how to increase or decrease the space between characters

Specify the space between lines

This example demonstrates how to specify the space between the lines in a paragraph

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

</p>

<p class="small">

This is a paragraph with a smaller line-height

This is a paragraph with a smaller line-height

This is a paragraph with a smaller line-height

</p>

<p class="big">

Trang 22

This is a paragraph with a bigger line-height.

This is a paragraph with a bigger line-height

This is a paragraph with a bigger line-height

This is a paragraph with a smaller line-height This is a paragraph with a smaller line-height This

is a paragraph with a smaller line-height

This is a paragraph with a bigger line-height This is a paragraph with a bigger line-height This is

a paragraph with a bigger line-height

Set the text direction of an element

This example demonstrates how to change the text direction of an element

<div>Some text Default writing direction.</div>

<div class="ex1">Some text Left-to-right direction.</div>

</body>

</html>

Some text Default writing direction

.Some text to-right direction

Left-Increase the white space between words

This example demonstrates how to increase the white space between words in a paragraph

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

TỪ KHÓA LIÊN QUAN

🧩 Sản phẩm bạn có thể quan tâm

w