比如我做一个高度、宽度百分比的按钮,背景用sprite的大图的一部分,用background-size或者其他属性可以做到按钮适应屏幕的大小时,背景图的那一部分也同时适应屏幕大小吗?
伊谢尔伦2017-04-17 11:07:53
In browsers that better support CSS3, it is necessary to display the same image in different sizes proportionally at different sizes. First of all, we need to unify the units
The following is part of the scss code
.mainwarp{font-size:12px;}
@include min-screen(480px){
.mainwarp{ font-size: 24px; }
}
@include min-screen(640px){
.mainwarp{ font-size: 32px; }
}
Set the size of the background image, the width and height are 486px * 389px. Here, use the custom method of scss to convert to em. Here, the maximum font size of 32 is used as the benchmark.
.phone_icon{ background: url(/static/images/common/phone_icon.png) no-repeat; background-size: pxToEm(486,32) pxToEm(389,32); }
//活动专享
.ac_icon_byzx{background: #2cc0da;
.m_icon{background-position: pte(4,32) pte(-136,32);}
}
//爱国教育
.ac_icon_agjy{background: #ff867a;
.m_icon{background-position: pte(-59,32) pte(-132,32);}
}
//科普学习
.ac_icon_kpxx{background: #87bcf7;
.m_icon{background-position: pte(-122,32) pte(-136,32);}
}
//运动休闲
.ac_icon_ydxx{background: #ad7ece;
.m_icon{background-position: pte(-186,32) pte(-136,32);}
}
高洛峰2017-04-17 11:07:53
I wonder if the questioner is asking how to adapt the background image to the entire screen. If so, if you use background-size, you need to consider the compatibility issues under various browsers
Fullscreen backgrounds with centered content
Or try using img as a background image
阿神2017-04-17 11:07:53
If you need a button that takes up both height and width, you can use a separate image. Write background-size:cover directly like this; pay attention to compatibility.
Generally speaking, sprites use a certain position. For example, your button is also a picture when it is hovered. At this time, just change the position.