你們好,今天與你們一塊兒學習分享css3中的background-size,background-clip使用與實踐。css
通常狀況下,咱們設置的背景圖與背景爲徹底匹配,但也有狀況是可能不匹配的,或者大或者小,那麼當尺寸不匹配時,你但願如何控制尺寸呢?這就是background-size的價值所在。html
可能取值:px|percentage|cover|contain,詳細說明以下:css3
取值 | 說明 |
---|---|
px | 設置背景圖像的寬度和高度,若是隻設置一個,第二個被認爲auto |
percentage | 設置背景圖像的寬度和高度,若是隻設置一個,第二個被認爲auto |
contain | 縮放背景圖像,讓其能顯示完整 |
cover | 縮放其圖像,讓其能徹底覆蓋區域,但可能背景顯示不全 |
背景裁剪通常用於控制其背景的顯示策略,顯示覆蓋區域,常規默認是覆蓋所有也就是border-box的。瀏覽器
可能取值:padding-box|content-box|border-box,與box-sizing一致的取值範圍。bash
在下面的實踐中咱們主要實現一個全屏背景中的註冊窗,效果有如下幾點是須要完成的:學習
<!--特別說明,由於filter效果會影響到子元素,因此咱們背景層單獨用了一個mask樣式元素-->
<div class="mask">
</div>
<form class="m-login">
<h2>當即註冊</h2>
</form>
<style>
*{
margin:0;
padding:0;
border:none;
box-sizing:border-box;
}
html,body{
height:100%;
}
.mask{
height:100%;
background:url(https://study.miaov.com/img/pc/remote_chapter/bannerBg.png) no-repeat center;
background-size:cover;
position:relative;
filter: blur(6px);
}
.m-login{
position:absolute;
width:500px;
height:250px;
border-radius:10px;
border:1px solid #fff;
top:50%;
left:50%;
margin-top:-100px;
margin-left:-250px;
padding:5px;
background:#fff;
background-clip:content-box;
font-size:14px;
h2{
text-align:center;
line-height:40px;
}
}
</style>
複製代碼