css中可使用background-size屬性來設(shè)置背景圖大小,語法“background-size:數(shù)值|百分比|cover|contain;”;其中cover代表等比擴(kuò)展圖片來填滿元素,contain代表等比縮小圖片來適應(yīng)元素的尺寸。
本教程操作環(huán)境:windows7系統(tǒng)、CSS3&&HTML5版、Dell G3電腦。
css3設(shè)置背景圖片的大小
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> body { background:url(img_flwr.gif); background-size:80px 60px; background-repeat:no-repeat; padding-top:40px; } </style> </head> <body> <p> Lorem ipsum,中文又稱“亂數(shù)假文”, 是指一篇常用于排版設(shè)計(jì)領(lǐng)域的拉丁文文章 ,主要的目的為測試文章或文字在不同字型、版型下看起來的效果。 </p> <p>原始圖片: <img src="/try/demo_source/img_flwr.gif" alt="Flowers" width="224" height="162"></p> </body> </html>
css3 background-size 屬性
background-size屬性指定背景圖片大小;在firefox,chrome,以及ie9上都可以使用。
具體使用方法如下:
-
背景圖尺寸(數(shù)值表示方式):
#background-size{ background-size:200px 100px; }
-
背景圖尺寸(百分比表示方式):
#background-size2{ background-size:30% 60%; }
-
背景圖尺寸(等比擴(kuò)展圖片來填滿元素,即cover值):
#background-size3{ background-size:cover; }
-
背景圖尺寸(等比縮小圖片來適應(yīng)元素的尺寸,即contain值):
#background-size4{ background-size:contain; }
-
背景圖尺寸(以圖片自身大小來填充元素,即auto值):
#background-size5{ background-size:auto; }
(學(xué)習(xí)視頻分享:css視頻教程)