Các thuộc tính Shorthand cho phép viết nhiều thuộc tính trên một dòng ngắn gọn hơn. Chúng hữu ích vì cung cấp code rõ ràng và giảm số dòng code (LOC). Các thuộc tính Shorthand chúng ta sẽ đề cập:
- Background
- Font
- Border
- Outline
- Margin
- Padding
- List
Background: Thuộc tính CSS Background dùng để thiết lập background cho trang web. Background có thể áp dụng cho mọi phần tử như body, h1, p, div... Có nhiều thuộc tính background như color, image, position. Một số thuộc tính được dùng trong code dưới đây.
- Longhand way:
background-color:#000;
background-image: url(images/bg.png);
background-repeat: no-repeat;
background-position:left top;
- Shorthand way:
background:#000 url(images/bg.png) no-repeat left top;
- Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1.0" />
<title>Document</title>
<style>
body {
background: #000 url(images/bg.png) no-repeat left top;
}
</style>
</head>
<body></body>
</html>
- Output:
Font: Thuộc tính CSS font dùng để áp dụng các font khác nhau cho text trên trang web. Các thuộc tính có thể thiết lập bằng font là font-family, font-size, font-weight. Một vài thuộc tính sẽ được dùng trong code bên dưới.
- Longhand way:
font-style:italic;
font-weight:bold;
font-size:18px;
line-height:150%;
font-family:Arial,sans-serif;
- Shorthand way:
font: italic bold 18px/150% Arial, sans-serif;
- Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1.0" />
<title>Document</title>
<style>
h1 {
font: italic bold 18px/150% Arial, sans-serif;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
</body>
</html>
- Output:
Border: Thuộc tính CSS border dùng để áp dụng border cho các phần tử khác nhau của trang web. Border có nhiều thuộc tính như width, style, color.
- Longhand way:
border-width: 1px;
border-style: solid;
border-color: #000;
- Shorthand way:
border: 1px solid #000;
- Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1.0" />
<title>Document</title>
<style>
h1 {
border: 1px solid #000;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
</body>
</html>
- Output:
Outline: Thuộc tính CSS outline dùng để áp dụng outline cho các phần tử trên trang web.
- Longhand way:
outline-width: 1px;
outline-style: solid;
outline-color: #000;
- Shorthand way:
outline: 1px solid #000;
- Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1.0" />
<title>Document</title>
<style>
h1 {
outline: 10px solid #000;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
</body>
</html>
- Output:
Margin: Các thuộc tính CSS margin dùng để tạo khoảng trắng xung quanh phần tử, bên ngoài border. Chúng ta có thể định nghĩa margin cho cả 4 cạnh: trên, dưới, trái, phải.
- Longhand way:
margin-top: 10px;
margin-right: 5px;
margin-bottom: 10px;
margin-left :5px;
- Shorthand way:
margin : 10px 5px 10px 5px;
- Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1.0" />
<title>Document</title>
<style>
h1 {
margin: 100px 50px 100px 50px;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
</body>
</html>
- Output:
Padding: Các thuộc tính CSS padding dùng để tạo khoảng trắng xung quanh nội dung, bên trong border. Padding có thể áp dụng cho top, bottom, left và right.
- Longhand way:
padding-top: 10px;
padding-right: 5px;
padding-bottom: 10px;
padding-left :5px;
- Shorthand way:
padding : 10px 5px 10px 5px;
- Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1.0" />
<title>Document</title>
<style>
h1 {
padding: 100px 50px 100px 50px;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
</body>
</html>
- Output:
Margin và Padding cũng chấp nhận hai và ba giá trị trong dạng shorthand. Ví dụ:
margin: 50px 75px; // margin trên-dưới là 50px và margin phải-trái là 75px. Điều tương tự áp dụng cho padding.
margin: 50px 35px 75px; // margin trên là 50px margin phải-trái là 35px và margin dưới là 75px. Điều tương tự áp dụng cho padding.
Nó áp dụng theo chiều kim đồng hồ bắt đầu từ trên, phải, dưới, trái.
List: Có hai loại list chính trong CSS: 1. Ordered list <ol> 2. Unordered list <ul>. Unordered list có dấu chấm đầu dòng, ordered list có số.
- Longhand way:
list-style-type: disc;
list-style-position: inside;
list-style-image: url(disc.png);
- Shorthand way:
list-style: disc inside url(disc.png);
- Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width,
initial-scale=1.0" />
<title>Document</title>
<style>
li {
list-style: disc inside url(assets/hole.png);
}
</style>
</head>
<body>
<li>GeeksforGeeks</li>
</body>
</html>
- Output: