Home  >  Article  >  Web Front-end  >  css3怎么让背景宽为100%时,高度跟这个背景图等比例拉伸呢_html/css_WEB-ITnose

css3怎么让背景宽为100%时,高度跟这个背景图等比例拉伸呢_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:42:091431browse

*{ margin:0; padding:0;} .kech{ background-image: url(tu.jpg) ;background-size:100% auto;background-repeat: no-repeat; width:100%;} .kech:before{content: ""; display: block; padding-top: 100%;} .kech1 img{ width:100%;}



css3怎么让背景宽为100%时,高度跟这个背景图等比例拉伸呢_html/css_WEB-ITnose



我在网上找的,用:before,padding-top:100%;但是不对,就是想要kech跟kech1里的效果一样,当宽设置为100%时,背景等比例拉伸,就像把图片宽设置100%,高度自适应一样的效果


回复讨论(解决方案)

background-size:100% auto;


就是这句 宽度100% 高度等比例

background-size设置图片。。。
div高度需要js解决。。。

background-size:100% auto;这个没什么用的,试过了,div高度需要js解决。这个JS怎么写

这个我知道,你需要做一个高宽比固定的容器,
这里有一个比较偏的小技巧,就是padding-bottom是按照父元素的宽度比计算的,而不是按高度算,所以这里可以用溢出的padding-bottom来做
width:100%;
height::0;
padding-bottom:100%//这里可以设定比例
然后加上背景图
参考下这篇文章   http://zihua.li/2013/12/keep-height-relevant-to-width-using-css/

   http://zihua.li/2013/12/keep-height-relevant-to-width-using-css/

这个文章不错,我用我的图片高除以宽,得到一个百分比46%;再padding-bottom:46%;完美解决了这个问题

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn