Archive for the ‘Học CSS’ category

Bài 6: Các thuộc tính của font chữ và định nghĩa font chữ cho văn bản

January 31st, 2008

Các thuộc tính về font chữ sẽ cho phép bạn thay đổi họ font (font family), độ đậm (boldness), kích thước (size) và kiểu font (style).

01Đặt font cho đoạn văn bản.

Để đặt một loại font chữ nào đó cho đoạn văn bản thì chúng ta sẽ sử dụng thuộc tính font-faily:

p {
font-family: Arial, Tahoma, Verdana, sans-serif;
}

Thông thường bạn cần phải khai báo họ của font ở cuối (trong ví dụ trên thì sans-serif là chỉ tới 1 họ font) để trong trường hợp máy của người duyệt Web không có các font như mình đã đặt thì nó sẽ lấy font mặc định của họ font trên.

02Đặt đoạn văn bản sử dụng font nhãn caption.

 

p.caption {
font: caption;
}

03Đ

ặt kích thước font cho đoạn văn bản.

Khi chúng ta muốn những đoạn văn bản hoặc tiêu đề có kích thước của chữ khác nhau, chúng ta có thể sử dụng thuộc tính font-size:

h1 {
font-size: 20px;
}

h3 {
font-size: 12px;
}

04Định lại kích thước font bằng thuộc tính font-size-ajust:

p {
font-size-ajust: 0.60;
}

05Đặt kiểu font cho đoạn văn bản.

Chữ đậm, chữ nghiêng,… được đặt với thuộc tính font-style:

 

p {
font-style: italic; /* normal | italic | oblique */
}

06

 

Muốn hiển thị font ở dạng small-caps hoặc ở dạng normal thì chúng ta sẽ sử dụng thuộc tính font-variant. Thuộc tính này có hai giá trị normalsmall-caps

p {
font-variant: normal; /* normal | small-caps */
}

07Đặt độ đậm nhạt của font.

Khi chúng ta muốn thay đổi độ đậm nhạt của văn bản chúng ta sẽ dùng thuộc tính font-weight:. Chúng ta có thể đặt giá 3 loại giá trị cho thuộc tính này 1. normal(bình thường), 2. bold(đậm), 3. 300(đặt dạng số)

h3 {
font-weight: bold;
}

08Khai báo các thuộc tính font ở dạng shorthand.

p {
font: italic small-caps 900 12px arial;
}

Bài 5: Các vấn đề về văn bản và cách định dạng văn bản

January 31st, 2008

Thuộc tính CSS text cho phép bạn hoàn toàn có thể quản lí được các thuộc tính của văn bản, bạn có thể quản lí được sự ẩn hiện của nó, thay đổi màu sắc, tăng hoặc giảm khoảng cách giữa các ký tự trong một đoạn, căn chỉnh việc dóng hàng (align),…

Các thuộc tính của text mà CSS hỗ trợ

Đặt màu cho một đoạn văn bản

Để đặt màu cho một đoạn văn bản chúng ta có thể dùng thuộc tính: color: #mã màu;

p {
color: #333333;
}

Đặt màu nền cho đoạn văn bản.

Bạn có thể đặt màu nền (background) cho đoạn văn bản bằng thuộc tính background-color: #mã màu;

p {
background-color: #FFFF00;
}

Căn chỉnh khoảng cách giữa các ký tự.

Khoảng cách giữa các ký tự trong một đoạn văn bản có thể được tăng hoặc giảm bởi thuộc tính letter-spacing: khoảng cách;

h3 {
letter-spacing: 2em;
}

h1 {
letter-spacing: -3em;
}

Căn chỉnh khoảng cách giữa các dòng.

Thuộc tính line-height: khoảng cách; sẽ giúp bạn căn chỉnh khoảng cách giữa các dòng trong một đoạn văn bản.

p {
line-height: 150%; // line-height: 15px;
}

Dóng hàng

Để gióng hàng cho một đoạn văn bản chúng ta sẽ dùng thuộc tính text-align: vị trí;

p {
text-align: left; /* left | center | right */
}

Trang hoàng thêm cho đoạn văn bản.

Một đường gạch chân hoặc đường gạch ngang dòng văn bản sẽ làm cho đoạn văn bản của bạn thêm sinh động. Để tô điểm thêm cho đoạn văn bản chúng ta sẽ dùng thuộc tính text-decoration: thuộc tính;

h3 {
text-decoration: underline; /* Gạch chân */
}

h2 {
text-decoration: line-through; /* Gạch ngang */
}

h1 {
text-decoration: overline; /* kẻ trên */
}

Chỉnh vị trí của đoạn văn bản (indent).

Thuộc tính text-indent: vị trí; sẽ căn chỉnh vị trí của dòng văn bản theo chiều ngang.

h1 {
text-indent: -2000px; /* text-indent: 30px; */
}

Điều kiển các ký tự trong một đoạn văn bản.

Bạn có thể điều khiển toàn bộ đoạn văn bản là chữ hoa hay chữ thường bởi thuộc tính text-transform: kiểu chữ;

p.uppercase {
text-tranform: uppercase;
}

p.lowercase {
text-tranform: lowercase;
}

p.capitalize {
text-tranform: capitalize;
}

Đặt hướng cho đoạn văn bản.

Hướng của đoạn văn bản có thể từ trái qua phải hay từ phải qua trái chúng ta có thể điều khiển bởi thuộc tính direction: hướng;

div.rtl {
direction: rtl; /* Right to left */
}

div.ltr {
direction: ltr; /* Left to right */
}

Tăng khoảng cách giữa các từ.

Khoảng cách giữa các từ có thể được tăng bởi thuộc tính word-spacing: khoảng cách;

  word-spacing: 30px;

Làm mất tác dụng của đường bao của một thẻ HTML.

Để làm mất tác dụng đường bao của một thẻ HTML chúng ta dùng thuộc tính white-space: giá trị;

p {
white-space: nowrap;
}

Thuộc tính white-space sẽ làm cho toàn bộ đoạn văn bản ở trên một dòng.

Bài 4: Thuộc tính background và cách định nghĩa background cho một thẻ

January 31st, 2008

Trong bài 4 này chúng ta sẽ đi tìm hiểu thuộc tính background trong CSS, nó sẽ cho phép chúng ta tùy biến màu nền của một thẻ HTML đặt một ảnh làm nền (background), làm cho một ảnh có thể lặp đi lặp lại (repeat) theo chiều ngang hoặc theo chiều dọc, chúng ta cũng có thể định vị một ảnh ở một vị trí nào đó trên trang.

Các trình duyệt hỗ trợ bao gồm: IE: Internet Explorer, F: Firefox, N: Netscape.

Dữ liệu đang được cập nhật…

Bài 3: Làm sao chèn CSS vào trong trang Web

January 31st, 2008

Bạn đã có một file CSS của bạn, bây giờ công việc tiếp theo là làm thế nào để chèn những đoạn CSS của bạn vào trong trang, Và xem chúng hoạt động như thế nào. Trong phần này chúng ta sẽ đi tìm hiểu chi tiết về cách chèn một đoạn style trong trang HTML hay liên kết tới một file CSS viết sẵn.

Khi trình duyệt đọc đến CSS, thì toàn bộ Website sẽ được định dạng theo các thuộc tính đã được khai báo trong phần CSS. Có ba cách cho phép chúng ta chèn định dạng CSS vào trong Website.

1. CSS được khai báo trong file riêng.

Toàn bộ mã CSS được chứa trong file riêng có phần mở rộng .css là một ý tưởng được dùng khi một file CSS sẽ được áp dụng cho nhiều trang khác nhau. Bạn có thể thay đổi cách hiển thị của toàn bộ site mà chỉ cần thay đổi một file CSS. Trong cách này thì file CSS sẽ được chèn vào văn bản HTML thông qua thẻ <link>…</link>. Ta có cú pháp như sau:

<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css"
medial="all" />
</head>

<body>
</body>
</html>

Trình duyệt sẽ đọc toàn bộ các định dạng được quy định trong file mystyle.css và định dạng cho văn bản HTML.

File CSS có thể được soạn thảo bằng một số trình duyệt khác nhau. Trong file không được chứa mã HTML, khi ghi lại chúng ta bắt buộc phải ghi lại với phần mở rộng là .css. Giả sử chúng trong file mystyle.css ở trên chứa đoạn mã sau:

hr {color: sienna}
p {margin-left: 20px}
body {background-image: url("images/back40.gif")}

Không bao giờ sử dụng khoảng trắng trong nhãn, giả sử rằng nếu bạn dùng margin-left: 20 px; thay cho margin-left: 20px; thì IE6 sẽ hiểu còn các trình duyệt như Firefox, Opera sẽ không hiểu

2. Chèn CSS trong tài liệu HTML

Chèn thẳng CSs trong tài liệu được áp dụng trong trường hợp những định dạng CSS này chỉ giành riêng cho tài liệu HTML đó. Khi bạn chèn trực tiếp thì đoạn mã của bạn phải đặt trong thẻ <style> và đặt trong phần <head>.

<head>
<style type="text/css">
hr {color: sienna}
p {margin-left: 20px}
body {background-image: url("images/back40.gif")}
</style>
</head>


Có một số trình duyệt cũ sẽ không hiểu thẻ <style>, nó sẽ bỏ qua thẻ này. Tuy nhiên thì nội dung trong thẻ <style> vẫn hiển thị ra trang HTML. Vì vậy mà chúng ta sẽ phải dùng định dạng chú thích trong HTML để chứa phần nội dung của thẻ <style>.

<head>
<style type="text/css">
<!–
hr {color: sienna}
p {margin-left: 20px}
body {background-image: url("images/back40.gif")}
–>
</style>
</head>

3. Chèn trực tiếp vào thẻ của HTML(inline style)

Inline style được sử dụng nhiều trong trường hợp một thẻ HTML nào đó cần có style riêng cho nó.

Inline style được áp dụng cho chính thẻ HTML đó, cách này sẽ có độ ưu tiên lớn nhất so với hai cách trên. Dưới đây là một ví dụ mà chúng ta dùng Inline style

<p style="color: sienna; margin-left: 20px">
This is a paragraph
</p>

4. Nhiều Stylesheet

Trong trường hợp mà có một số thẻ có cùng định dạng, chúng ta có thể gộp chúng lại với nhau. Giả sử như sau:

h1, h2, h3 {
margin-left: 10px;
font-size: 150%;

}

Giống đoạn mã trên thì cả ba thẻ h1, h2, h3 đều có cùng 3 thuộc tính như trên.

Bài 2: Cú pháp của CSS

January 31st, 2008

Sau khi hiểu là nắm bắt được một số đặc tính của CSS chúng ta tiếp tục đi tìm hiểu về cú pháp và cách khai báo của các thẻ CSS

Cú pháp của CSS được chia làm 3 phần. phần thẻ chọn (selector), phần thuộc tính (property), phần nhãn (value).

selector {property: value}

Nếu nhãn của bạn có nhiều từ bạn nên đặt nhãn của bạn vào trong dấu nháy kép

p {font-family: "sans serif"}

Trong trường hợp thẻ chọn của bạn nhiều thuộc tính thì các thuộc tính sẽ được ngăn cách bởi dấu (;).

p {text-align:center;color:red}

Khi thẻ chọn có nhiều thuộc tính thì chúng ta nên để mỗi thuộc tính ở trên một dòng riêng biệt.

p {  text-align: center;  color: black;  font-family: arial}

Bài 1: Cơ bản về CSS

January 31st, 2008

Trong bài mở đầu này chúng ta sẽ đi tìm hiểu một số khái niệm và đặc tính của CSS, mà chúng ta cần chú ý trong suốt quá trình làm việc với CSS

I. CSS là gì

CSS (Cascading Style Sheets) được hiểu một cách đơn giản đó là cách mà chúng ta thêm các kiểu hiển thị (font chữ, kích thước, màu sắc…) cho một tài liệu Web

II. Một số đặc tính cơ bản của CSS

  1. CSS quy định cách hiển thị của các thẻ HTML bằng cách quy định các thuộc tính của các thẻ đó (font chữ, màu sắc). Để cho thuận tiện bạn có thể đặt toàn bộ các thuộc tính của thẻ vào trong một file riêng có phần mở rộng là ".css"

    CSS nó phá vỡ giới hạn trong thiết kế Web, bởi chỉ cần một file CSS có thể cho phép bạn quản lí định dạng và layout trên nhiều trang khác nhau. Các nhà phát triển Web có thể định nghĩa sẵn thuộc tính của một số thẻ HTML nào đó và sau đó nó có thể dùng lại trên nhiều trang khác.

  2. Có thể khai báo CSS bằng nhiều cách khác nhau. Bạn có thể đặt đoạn CSS của bạn phía trong thẻ <head>…</head>, hoặc ghi nó ra file riêng với phần mở rộng ".css", ngoài ra bạn còn có thể đặt chúng trong từng thẻ HTML riêng biệt

    Tuy nhiên tùy từng cách đặt khác nhau mà độ ưu tiên của nó cũng khác nhau. Mức độ ưu tiên của CSS sẽ theo thứ tự sau.

    1. Style đặt trong từng thẻ HTML riêng biệt
    2. Style đặt trong phần <head>
    3. Style đặt trong file mở rộng .css
    4. Style mặc định của trình duyệt

    Mức độ ưu tiên sẽ giảm dần từ trên xuống dưới.

  3. CSS có tính kế thừa: giả sử rằng bạn có một thẻ <div id="vidu"> đã được khai báo ở đầu file css với các thuộc tính như sau:
    #vidu {  width: 200px;  height: 300px;}

    Ở một chỗ nào đó trong file css bạn lại khai báo một lần nữa thẻ <div id="vidu"> với các thuộc tính.

    #vidu {  width: 400px;  background-color: #CC0000;}

    Sau đoạn khai báo này thì thẻ <div id="vidu"> sẽ có thuộc tính:

    #vidu {  width: 400px; /* Đè lên khai báo cũ */  height: 300px;  background-color: #CC0000;}

Two columns with color

January 27th, 2008

 

How do you make a two column CSS layout with color in either column that spreads to the full height of the page?

This article will show you how to build a basic two column layout that has:

  • Liquid width (based on the browser window size or viewport)
  • Full width header and footer
  • color in either or both columns
  • color that stretches the full height of the columns - no matter which of them is longer

This method uses a background image to create the illusion of a colored column which means that the narrower column must be set to an exact pixels width. This may not suit your particular needs. However, there are many different methods that can be used to achieve a two column layout. Look around before you decide which one is right for you!

 

Step 1. Setting the containers

Let’s start by creating 5 basic containers and giving them all a unique ID:

<div id="container">
   <div id="banner">
   </div>
   <div id="nav">
   </div>
   <div id="content">
   </div>
   <div id="footer">
   </div>
</div>

Step 2. Drop in the content

The next step is to drop in some representational content. Try to use content that reflects how the real text will function within each container. It is also important that the content - even at this early stage - should be semantically correct. So, you should use standard HTML elements to mark up each block of content. If you have a heading, wrap it in a heading tag, starting with <h1></h1>. If it is normal content, each paragraph should be wrapped within <p></p>.

Where possible, these HTML elements will be styled using type selectors, rather than having to resorting to additional classes or IDs.

<div id="container">
   <div id="banner">
      <h1>Site name</h1>
   </div>
   <div id="nav">
      <p>
         Lorem ipsum..
      </p>
   </div>
   <div id="content">
      <h2>
         Page heading
      </h2>
      <p>
         Ut wisi enim..
      </p>
   </div>
   <div id="footer">
      Footer stuff here
   </div>
</div>

Step 3. Styling the body

3.1 Margin and padding

Some browsers have a default padding set on the <body> element, and others use a default margin. To force the page layouts to sit in the top left corner of the viewport, you must remove both margin and padding from the <body>.

body
{
margin: 0;
padding: 0;
}

3.2 Font-family

You can also set the font-family for the entire page. Be aware that some browsers do not properly inherit font-family. This may mean that table elements or form elements need to be styled with a font-family as well.

It is important to set a range of font-family options neding in a generic font family. If users do not have Georgia, they may have Times or Times New Roman. If all else fails, they should at least have a serif font of some sort.

Any font family that has white space between words needs to be enclosed in quotation marks.

body
{
margin: 0;
padding: 0;
font-family: georgia, times, "times new roman", serif;
}

3.3 color

color and Background-color can be added into the body rule set.

body
{
margin: 0;
padding: 0;
color: #000;
background-color: #ddd;

}

Step 4. Styling the container

The success of this layout is based on this container. We will place a left aligned background image inside the container and set it to repeat down, but not across the page. The image will be as wide as the left column. This will give the appearance of a full colored column.

4.1 Margins

For this example, we will center the container and move it in from the edges of the viewport. To achieve this, we need to add margins on both sides of the container. We can use a shorthand declaration to define top and bottom margin (1em) as well as left and right margin (5%).

#container
{
margin: 1em 5%;
}

4.2 Adding color

To make the container a different color to the page, we will apply a background-color.

#container
{
margin: 1em 5%;
background-color: #FFF;
}

4.3 The background image

We need to be set a background image in this contaner. This image is 180px wide and will repeat down the page. To do this we set the background-repeat on the y axis only.

#container
{
margin: 1em 5%;
background-color: #FFF;
background-image: url(background.jpg);
background-repeat: repeat-y;

}

4.4 Adding a border

If you want, you can add a border to the container.

#container
{
margin: 1em 5%;
background-color: #FFF;
background-image: url(background.jpg);
background-repeat: repeat-y;
border: 1px solid #333;
}

Step 5. Styling the banner block

5.1 Adding color

We will now add background-color and border-bottom to the banner.

#banner
{
background-color: #666;
border-bottom: 1px solid #333;

}

5.2 Setting margin and padding on the heading

We also need to style the <h1> inside the banner <div>. To do this we will use a descendant selector - which will select only <h1> elements within a <div> styled with id="banner".

Browsers display headings in slightly different ways - both Opera and Internet Explore do not recognize margin-top correctly. One method to position the heading exactly within the containing <div> is to set the "margin: 0" and use padding to push the heading content into the desired position. Padding can be combined into one shorthand declaration.

#banner h1
{
margin: 0;
padding: .5em;

}

Step 6. Styling the left nav

6.1 Floating the nav

The nav <div> can be moved to the left of the screen by applying "float: left". All floated items must be given a width (apart from those elements with intrinsic width, such as images).

#nav
{
float: left;
width: 160px;

}

6.2 Margin

Margin-left is added to the <div> to push it away from the left edge of the containing box.

#nav
{
float: left;
width: 160px;
margin-left: 10px;
}

6.3 Padding

Padding is added to the top of the <div> to push it down from the banner.

#nav
{
float: left;
width: 160px;
margin-left: 10px;
padding-top: 1em;
}

6.4 Margin-top

Some browsers do not render margin-top correctly for the first item inside a container. As we want all items to line up correctly, we will turn off "margin-top" on the paragraph within this container using a descendant selector.

#nav p
{
margin-top: 0;
}

Step 7. Styling the content block

The content <div> needs to be styled so that it does not flow under the navigation <div>. To do this, we will set a left margin - slightly wider than the width of the nav. The left margin is the critical measurement. It gives the illusion that there is actually a column, even though it is statically positioned.

#content
{
padding-top: 1em;
margin: 0 2em 0 200px;

}

Step 8. Styling the footer block

8.1 clear: both

The footer needs "clear: both" to force it onto a new line, below any floated items.

#footer
{
clear: both;
}

8.2 Background color

We can set the background color to differentiate it from the content above.

#footer
{
clear: both;
background-color: #666;
}

8.3 Padding

Padding can be applied to move the text in from the edges of the container.

#footer
{
clear: both;
background-color: #666;
padding: 1em;
}

8.4 Text alignment

To move the text to the right edge of the containing box, "text-align: right" is used.

#footer
{
clear: both;
background-color: #666;
padding: 1em;
text-align: right;
}

8.5 Border-top

We can also set a border-top, using a shorthand rule.

#footer
{
clear: both;
background-color: #666;
padding: 1em;
text-align: right;
border-top: 1px solid #333;
}

We have now finished the basic layout.

Full-width variation

It is very easy to convert this layout into a full width version. All we do is change "margin: 1em 5%" to "margin: 0".

#container
{
margin: 0; /* changed from 1em 5% */
background-color: #FFF;
background-image: url(background.jpg);
background-repeat: repeat-y;
border: 1px solid #333;
}

Right nav variation

It is also very easy to convert this layout to a right nav version. All we need to do is change the nav to "float: right", and set the background image to the right as well.

#container
{
margin: 1em 5%;
background-color: #FFF;
background-image: url(background.jpg);
background-repeat: repeat-y;
border: 1px solid #333;
background-position: right; /* ADD THIS RULE */
}

#nav
{
float: right; /* WAS FLOAT LEFT */
width: 160px;
margin-right: 10px; /* WAS MARGIN-LEFT */
padding-top: 1em;
}

#content
{
padding-top: 1em;
margin: 0 200px 0 2em; /* SWAP ORDER */
}

Column borders variation

If you have tried to apply borders to any columns within 2 and 3 column layouts, you may have noticed that the borders will only extend as far as the content. One way around this is to use a background image that looks like a border. The image will extend to the depth of the longest column.

Liquid two column layout

January 27th, 2008

To lay out a page into two columns, you need to start with the basic page structure. In this case we will use some dummy content to create a two column template. The page has been grouped into five separate divs, and each of these divs has been given a unique ID selector. The divs are labeled; "container" (wrapped around the entire page’s content), "top" (for the top banner), "leftnav" (for the smaller, left column), "content" (for the main content) and "footer" (for the footer across the bottom of the page).

For this tutorial, obvious names have been used to help illustrate the point, but any name can be used. However, it is better to name classes and id’s based on their meaning, rather than their appearance.

CSS CODE
#container
{
width: 90%;
margin: 10px auto;
background-color: #fff;
color: #333;
border: 1px solid gray;
line-height: 130%;
}


#top
{
padding: .5em;
background-color: #ddd;
border-bottom: 1px solid gray;
}
#top h1
{
padding: 0;
margin: 0;
}

#leftnav
{
float: left;
width: 160px;
margin: 0;
padding: 1em;
}

#content
{
margin-left: 200px;
border-left: 1px solid gray;
padding: 1em;
max-width: 36em;
}

#footer
{
clear: both;
margin: 0;
padding: .5em;
color: #333;
background-color: #ddd;
border-top: 1px solid gray;
}

#leftnav p { margin: 0 0 1em 0; }
#content h2 { margin: 0 0 .5em 0; }

HTML CODE
<div id="container">
<div id="top">
<h1>Header</h1>
</div>
<div id="leftnav">
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut.
</p>
</div>
<div id="content">
<h2>Subheading</h2>
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
</p>
<p>
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
</p>
</div>
<div id="footer">
Footer
</div>
</div>