Computer >> Máy Tính >  >> Lập trình >> CSS

Tạo kiểu nền của các phần tử trong CSS


Chúng ta có thể chỉ định thuộc tính nền của một phần tử bằng cách sử dụng thuộc tính nền. Nó có thể nhận các giá trị như sau:background-color, background-image, background-repeat, background-position, background-clip, background-size, background-origin và background-attachment.

Cú pháp

Cú pháp của thuộc tính nền CSS như sau -

Selector {
   background: /*value*/
}

Ví dụ

Các ví dụ sau minh họa thuộc tính nền CSS -

<!DOCTYPE html>
<html>
<head>
<style>
body {
   background: gray url("https://www.tutorialspoint.com/pytorch/images/pytorch.jpg") no-repeat;
   font-size: 1.2em;
   color: black;
}
h2 {
   -webkit-text-fill-color: transparent;
   -webkit-text-stroke: 1px black;
}
p {
   box-shadow: 0 0 5px 5px black;
}
</style>
</head>
<body>
<h2><i>PYTORCH Tutorial</i></h2>
<p>This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. This is demo text. </p>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
<li>Four</li>
<li>Five</li>
</ul>
</body>
</html>

Đầu ra

Điều này cho kết quả sau -

Tạo kiểu nền của các phần tử trong CSS

Ví dụ

<!DOCTYPE html>
<html>
<head>
<style>
h2 {
   background-color: bisque;
}
div {
   margin: auto;
   background-color:orange;
   width: 40%;
}
</style>
</head>
<body>
<h2>Demo Text</h2>
<div>demo demo demo demo</div>
</body>
</html>

Đầu ra

Điều này cho kết quả sau -

Tạo kiểu nền của các phần tử trong CSS