img标签src不给路径就会出现边框
2016-08-12 20:15
507 查看
<img/>在
src加载失败或没有给的,浏览器会自动给
img加上边框。
如下图这样:
产品觉得影响美观,一定要pass掉。
原码是这样:
.ctn{ position: relative; width: 2.8rem; height: 2.8rem; border-radius: 3px; overflow: hidden; background: #FFF; } .ctn .title{ position: absolute; top: 0; width: 2.8rem; height: 2.8rem; background:rgba(0,0,0,.35) ; color: #FFF; font-size: .52rem; font-weight: bold; padding:0 .4rem; } .ctn img{ width: 2.6rem; height: 2.2rem; margin: .3rem auto; object-fit: cover; background: url(images/120X120.jpg?201608012) no-repeat center; background-size: 2.2rem; }
<div class="ctn"> <div class="title sn-flex center"> <p>休闲西装</p> </div> <img src=""/> </div>
百度一下,在知乎上找到了解决方案,链接在这https://www.zhihu.com/question/27426689
基于能用
css实现就不用
js的原则,选择了以下的解决方案:
给
img包个
div
<div class="ctn"> <div class="title sn-flex center"><p>收腰款</p></div> <div class="img-ctn"> <img src="temp/app_200x200.jpg"/> </div> </div>
.ctn .img-ctn{ width: 2.6rem; height: 2.2rem; margin: .3rem auto; overflow: hidden; } .ctn .img-ctn img{ width: -webkit-calc(2.6rem + 2px); height: -webkit-calc(2.2rem + 2px); width: calc(2.6rem + 2px); height: calc(2.2rem + 2px); background: url(images/120X120.jpg?201608012) no-repeat center; background-size: 1.8rem; margin: -1px; object-fit: cover; }
but,有问题,无图片时上下的
border是隐藏了,左右无论怎么样都隐藏不了,暂时没查出来问题,于是改成了这样:
.ctn .img-ctn{ width: 2.6rem; height: 2.2rem; margin: .3rem auto; overflow: hidden; background: url(images/120X120.jpg?201608012) no-repeat center; background-size: 1.8rem; } .ctn .img-ctn img{ width: inherit; height: inherit; object-fit: cover; } /*无src隐藏*/ .ctn .img-ctn img[src='']{ visibility: hidden; }
后来,在控制台调试时,忽然灵光乍现,FUCK,是
reset样式的问题。
原来,base.css
对
img做了这个
img { max-width: 100%; }
hehe,重新又改成这样:
.ctn .img-ctn{ width: 2.6rem; height: 2.2rem; margin: .3rem auto; overflow: hidden; } .ctn .img-ctn img{ width: -webkit-calc(2.6rem + 2px); height: -webkit-calc(2.2rem + 2px); width: calc(2.6rem + 2px); height: calc(2.2rem + 2px); background: url(images/120X120.jpg?201608012) no-repeat center; background-size: 1.8rem; margin: -1px; /*就是这货*/ max-width: none; object-fit: cover; }
ok,提交给开发,终于可以偷懒一会了。
however,改变来的太快。开发发来了一张图:
去开发机上调试一下,瞬间感受到了深深的恶意。
原来图片的背景图层是透明的,盒子模型的渲染层级是
color>src>background-image>background-color,图片空白区域透明自然就显示背景图片了。
img{ background: red url(images/120X120.jpg?201608012) no-repeat center; }
<img src=".png">
感觉自己的洪荒之力已经用完了。。。。
at the end,为了规避这种图片出现,直接不给背景图片了,还是通过模板引擎来判断吧
<img src="{$src||'images/120X120.png'}"/>
多好,一下子就解决了。
白白绕了这么一大圈
相关文章推荐
- 移动端---img标签设置了宽高但是src不给路径会出现边框,怎么去掉边框呢
- java 正则表达式替换img标签的src路径
- a标签里面的img出现边框
- 怎么用正则表达式获取文章内容中的&lt;img标签和 src的路径
- HTML中IMG标签的SRC属性路径问题
- <img src="">标签中的src 找不到 Servlet的路径
- 关于img标签的src的绝对路径问题
- 我在使用ng-src时图片加载不出来后,Chrome会自动给img标签增边框,解决办法
- HTML:img标签--src属性:相对路径和绝对路径
- img标签src路径不变,读取不同图片时,图片不刷新甚至不显示
- 判断(img标签)中src的路径中照片文件是否存在
- img标签--src属性:相对路径和绝对路径
- html <img>标签 src=""内的图片路径问题
- img标签去间隙以及src为空时的默认边框
- img标签使用onload进行src更改时出现的内存溢出问题
- html<img>标签中src=“图片路径”,怎么用变量替换路径
- 正则表达式提取字符串内所有的img标签下的src路径
- 提取img标签src属性中的图片路径正则 (php)
- jsp页面img标签src路径问题
- javascript修改IMG标签的SRC