- background-size
- background-origin
- background-clip
- background-image
- background
1. CSS3 Multiple backgrouds
background-image
.
1
| background : url (link-to-file.png) |
1
2
3
4
| background : url (link-to-file 1 .png), url (link-to-file 2 .png) ...; |
1
2
| background-position : top left , right bottom ; background-repeat : no-repeat , no-repeat |
1
2
3
4
5
6
7
8
9
10
| div { width : 620px ; height : 350px ; background-image : background-position : center center , center center ; background-repeat : no-repeat , no-repeat } |
backgroud
như ví dụ sau:
1
2
3
4
5
6
7
8
| div { width : 620px ; height : 350px ; background : url (http://freetuts.net/upload/tut_post/images/ 2015 / 10 / 23 / 478 /background.jpg) no-repeat center center ; } |
2. Cấu hình kích thước cho background (background-size)
background-size
.
1
2
3
4
5
6
7
8
9
| div { width : 620px ; height : 350px ; background : url (http://freetuts.net/upload/tut_post/images/ 2015 / 10 / 23 / 478 /background.jpg) no-repeat center center ; background- size : 200px 200px , 300px 300px ; } |
- contain: background sẽ có tác dụng như thẻ
img
, nghĩa là nó sẽ co giãn theo chiều rộng và chiều cao theo đúng tỉ lệ của hình ảnh. - cover: Nếu chiều rộng và chiều cao của thẻ HTML lớn hơn hình ảnh thì nó sẽ giãn ra (full)
1
2
3
4
5
6
7
8
| div { width : 800px ; height : 800px ; background : url (http://freetuts.net/upload/tut_post/images/ 2015 / 10 / 23 / 478 /background.jpg) no-repeat center center ; background- size : cover; } |
1
2
3
4
5
6
7
8
9
| div { width : 800px ; height : 100px ; border : solid 1px ; background : url (http://freetuts.net/upload/tut_post/images/ 2015 / 10 / 23 / 478 /background.jpg) no-repeat center center ; background- size : contain; } |
3. Thuộc tính background-origin trong CSS3
background-origin
trong CSS3 dùng để xác đinh nơi mà background hình ảnh sẽ hiển thị. Nó có ba giá trị sau đây:- border-box: biên của background tính luôn border ngoài cùng
- padding-box: biên của background tính từ vị trí padding (sát lề border)
- content-box: biên của backgroudn tính từ vị trí có thể sử dụng
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| div{ width : 250px ; height : 100px ; margin : 20px auto ; border : solid 20px ; padding : 50px ; background : url (http://freetuts.net/upload/tut_post/images/ 2015 / 10 / 23 / 478 /hoa-hong-dep.gif) no-repeat top left ; } #div 1 { background-origin: border-box; } #div 2 { background-origin: padding-box; } #div 3 { background-origin: content-box; } |
4. Thuộc tính background-clip trong CSS3
- border-box: biên của background tính luôn border ngoài cùng
- padding-box: biên của background tính từ vị trí padding (sát lề border)
- content-box: biên của backgroudn tính từ vị trí có thể sử dụng
background-origin
nhưng nó có tác dụng với background color còn background-origin
thì có tác dụng với background image.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| div{ width : 250px ; height : 100px ; margin : 20px auto ; border : dotted 10px ; padding : 50px ; background : blue ; } #div 1 { background- clip : border-box; } #div 2 { background- clip : padding-box; } #div 3 { background- clip : content-box; } |
5. Lời kết
background-origin
và backgroud-clip
cũng giúp cho coder có nhiều sự lựa chọn hơn.
No comments:
Post a Comment