CSS Background

Khi bạn vào bất kỳ một trang web nào đó, bạn sẽ thấy màu của các trang web có thể sẽ khác nhau và bạn có bao giờ nghĩ sao họ lại làm được như thế không? Hôm nay, eLib.VN sẽ giới thiệu đến bạn đọc cách làm thay đổi màu của các trang web qua bài viết dưới đây, bạn đọc cùng tham khảo nhé!

CSS Background

1. CSS background-color

Sau đây là ví dụ thể hiện cách thiết lập màu nền cho một phần tử.

<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<style>
		h2, p {
		  background-color: #b0d4de;
		}
	</style>
</head>

<body>
	<h2>CSS background-color</h2>
	<p>Đây là ví dụ về CSS background-color.</p>
</body>

</html>

Nó sẽ tạo ra kết quả sau:

2. CSS background-image

Chúng ta có thể thiết lập hình nền bằng cách gọi các hình ảnh được lưu trữ cục bộ như thể hiện dưới đây

<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<style>
		body {
		  background-image: url("paper1.gif");
		  margin-left: 100px;
		}
	</style>
</head>

<body>
	<h1>Hello eLib.VN</h1>
</body>

</html>

3. CSS background-repeat

Theo mặc định, thuộc tính background-repeat lặp lại hình nền theo chiều ngang và chiều dọc. Một số hình ảnh được lặp lại chỉ theo chiều ngang hoặc chiều dọc.

Dưới đây là ví dụ về lặp hình nền theo chiều ngang: background-repeat: repeat-x;

<html>
   <head>
      <style>
         body {
            background-image: url("../css/images/css.jpg");
            background-repeat: repeat;
         }
      </style>
   </head>
   <body>
      <p>Vi du ve gia tri mac dinh cua thuoc tinh background-repeat trong CSS.</p>
   </body>
</html>

Nó sẽ tạo ra kết quả sau:

Ví dụ sau minh họa cách lặp lại hình nền theo chiều dọc: background-repeat: repeat-y;

<html>
   <head>
      <style>
         body {
            background-image: url("../css/images/css.jpg");
            background-repeat: repeat-y;
         }
      </style>
   </head>
   <body>
      <p>Vi du ve gia tri repeat-y cua thuoc tinh background-repeat trong CSS.</>
   </body>
</html>

Nó sẽ tạo ra kết quả sau:

Ví dụ sau minh chứng cách lặp lại hình nền theo mặc định.

<html>
   <head>
      <style>
         body {
            background-image: url("../css/images/css.jpg");
            background-repeat: repeat-x;
         }
      </style>
   </head>
   <body>
      <p>Vi du ve gia tri repeat-x cua thuoc tinh background-repeat trong CSS.</>
   </body>
</html>
Nó sẽ tạo ra kết quả sau:

Kết quả là:

4. CSS background-position

Thuộc tính background-position được sử dụng để xác định vị trí ban đầu của hình nền. Theo mặc định, hình nền được đặt ở phía trên bên trái của trang web.

Thuộc tính background-position có những giá trị sau:

  • center
  • top
  • bottom
  • left
  • right

Dưới đây là ví dụ về vị trí của hình nền trong CSS.

<!DOCTYPE html>
<html>

<head>
	<meta charset="UTF-8">
	<style>
		body {
		  background: white url('good-morning.jpg');
		  background-repeat: no-repeat;
		  background-attachment: fixed;
		  background-position: center;
		}
	</style>
</head>

<body>
	<p>This is a fixed background-image. Scroll down the page.</p>
	<p>This is a fixed background-image. Scroll down the page.</p>
	<p>This is a fixed background-image. Scroll down the page.</p>
	<p>This is a fixed background-image. Scroll down the page.</p>
	<p>This is a fixed background-image. Scroll down the page.</p>
	<p>This is a fixed background-image. Scroll down the page.</p>
	<p>This is a fixed background-image. Scroll down the page.</p>
	<p>This is a fixed background-image. Scroll down the page.</p>
	<p>This is a fixed background-image. Scroll down the page.</p>
	<p>This is a fixed background-image. Scroll down the page.</p>
	<p>This is a fixed background-image. Scroll down the page.</p>
	<p>If you do not see any scrollbars, Resize the browser window.</p>
</body>

</html>

Nó sẽ tạo ra kết quả sau:

5. CSS background-attachment

Tệp đính kèm nền xác định liệu một hình nền được cố định hay cuộn với phần còn lại của trang.

Ví dụ dưới đây thể hiện cách thiết lập hình nền cố định.

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
body {
  background: white url('bg2.gif');
  background-repeat: no-repeat;
  background-attachment: fixed;
  margin-left: 200px;
}
</style>
</head>
<body>
  <p>This is a fixed background-image. Scroll down the page.</p>
  <p>This is a fixed background-image. Scroll down the page.</p>
  <p>This is a fixed background-image. Scroll down the page.</p>
  <p>This is a fixed background-image. Scroll down the page.</p>
  <p>This is a fixed background-image. Scroll down the page.</p>
  <p>This is a fixed background-image. Scroll down the page.</p>
  <p>This is a fixed background-image. Scroll down the page.</p>
  <p>This is a fixed background-image. Scroll down the page.</p>
  <p>This is a fixed background-image. Scroll down the page.</p>
  <p>This is a fixed background-image. Scroll down the page.</p>
  <p>This is a fixed background-image. Scroll down the page.</p>
  <p>This is a fixed background-image. Scroll down the page.</p>
  <p>This is a fixed background-image. Scroll down the page.</p>
  <p>This is a fixed background-image. Scroll down the page.</p>
  <p>This is a fixed background-image. Scroll down the page.</p>
  <p>If you do not see any scrollbars, Resize the browser window.</p>
</body>
</html>
Nó sẽ tạo ra kết quả sau:

6. CSS background

Bạn có thể sử dụng thuộc tính background để thiết lập tất cả các thuộc tính nền cùng một lúc. Ví dụ:

<p style="background:url(/images/bg1.gif) repeat fixed;">
   This parapgraph has fixed repeated background image.
</p>
Trên đây là bài viết của eLib.VN về CSS Background. Chúng tôi hy vọng qua phần này mọi người có thể hiểu hơn về thuộc tính background trong CSS. Chúc các bạn thành công!
Ngày:09/11/2020 Chia sẻ bởi:Nguyễn Minh Duy

CÓ THỂ BẠN QUAN TÂM