移动端web,我踩过的坑之<img>...

第一个坑是不想背锅的img标签,明明是那时还是菜鸟,今天依旧菜鸟的我自己,没有逐字逐句认真看文档(・。・)

1. <img> 标签创建的是被引用图像的占位空间。所以请务必为img标签设置width。这涉及到用户体验(严肃脸(..•˘_˘•..)),您老板肯定不想因为糟糕的网页渲染性能而失去用户。

特别是移动端,为img设置一个em单位的宽度,图片才能随着用户屏幕的尺寸弹性伸缩。

  1.1  昨天在PC端发现,同一个页面,同一张图,图片有时候会出现宽度变形高度不变。 把设置的height取消掉就不会有这种bug出现。原因不明,找不出是哪里的样式冲突。

  1.2  <img> 标签创建的是被引用图像的占位空间。指定宽高尺寸可改善网站性能。通常情况下,指定了img的宽度,高度也会随之按比例缩放。

    注意:如果提供了一个百分比形式的 width 值而忽略了 height,那么不管是放大还是缩小,浏览器都将保持图像的宽高比例。这意味着图像的高度与宽度之比将不会发生变化,图像也就不会发生扭曲。

  1.3  今天的一个移动端页面,在chrome浏览器手机模式预览下,有两张图因为只指定宽度,没有指定height而产生变形。调试了一番,终于发现: 父容器布局的样式 display: flex, align-items 属性。因为这个属性的默认值导致图片拉伸到容器高度:

    •   stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。

    而在火狐的手机模式下预览,就没有这个问题。不管怎么说,既然有这个潜在风险,那就再指定下弹性高度,或者设置 align-items 属性为center。

    为什么要用display:flex 呢。水平居中的三个办法,justify-content: center; text-align: center , margin: 0 auto; 区别和用法在哪里呢。什么情况下会无效呢。

  1.4 翻了MDN上的 img api 英文原文,没有看到推荐只指定宽度的说法。点击这个链接查看:MDN上的HTML element reference: img

  1.5 继续翻w3上的英文教程:the-img-element。也并未推荐只写宽度。

The IDL attributes width and height must return the rendered width and height of the image, in CSS pixels, if the image is being rendered, and is being rendered to a visual medium; or else the intrinsic width and height of the image, in CSS pixels, if the image is available but not being rendered to a visual medium; or else 0, if the image is not available[CSS]

2. <a>包含的<img> 设置max-width:100%无效.

先来看下max-width可能的值%概念: 

max-width:% 定义基于包含它的块级对象的百分比最大宽度。

100%则是如果指定元素的宽度不超过父元素的宽度,则大小不变,如果超过了父元素的宽度,则将宽度收缩为父元素的宽度。

max-width overrides width, but min-width overrides max-width

所以,这个时候要给<a>加个 - "display:block;"- 等等这些样式:能让<a>从行内摇身一变,变为块状元素。

ps:

img { max-width: 100%;}

这行代码对于大多数嵌入网页的视频也有效,所以可以写成:

img, object { max-width: 100%;}

3. <img> 在不同浏览器下的表现差异。

这种差异,即便是像下面一样初始化设置样式也于事无补。有兴趣可以试试看。不得不说,chrome和FF这些浏览器,有时候有点贴心得过头。

img{
    margin: 0;
    padding: 0;       
}

解决方案有N种:

①. 简单粗暴的:把<img>间的空格注释掉,像这样:

    <div class='img-dad'>
        <img src="1.jpg" alt=""><!-- 
         --><img src="2.jpg" alt=""><!-- 
         --><img src="3.jpg" alt=""><!-- 
          --><img src="4.jpg">
    </div>

这种办法能解决<img>左右边距的问题,但是上下边距还在呀,而且那么多注释标签,看着也不美观对吧~

②. 依旧给<img>设置样式:

    img{
        vertical-align: top;
    }

这个方法,和第一种一样,也只能去掉上下边距。

③. 给<img>他爸元素加个样式:

  办法A:  .img-dad{
        font-size: 0;
    }

啊,perfect!上下左右边距都不见了,我们可以想要多少边距就要多少边距了。

④. 给<img>设置样式:

方法A:img{
        float: left;
    }
方法B:img{
        display: block;/table/box/list-item (等等这些可以让它变成块的东西)
    }
根据实际需要结合使用。

 4. <img>的border-radius,在安卓机出现圆角变形,像是秃了一部分头顶一样。解决办法:img外嵌套一层div,设定border-radius

 

转载于:https://www.cnblogs.com/dodocie/p/6674355.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对下面的代码加注释:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <style> #backg { width: 919px; height: 272px; } div img { width: 300px; height: 222px; margin-top: 20px; margin-left: 10px; } #h3 { color: rgb(132, 106, 90); font-weight: 600; font-size: 23px; margin-top: -224px; margin-left: 340px; } #p1 { font-size: 14px; color: rgb(20, 111, 158); font-weight: 550; margin-top: 13px; margin-left: 340px; } #p2 { width: 575px; font-size: 13.2px; color: black; text-indent: 2em; margin-top: -3px; margin-left: 340px; } #p2 span { color: rgb(20, 111, 158); font-style: italic; text-decoration: underline; } #p3 { font-size: 14px; color: rgb(20, 111, 158); font-weight: 550; margin-top: 10px; margin-left: 340px; } #p3 span { color: red; font-size: 20px; font-weight: 800; } hr { width: 560px; margin-top: -2px; margin-left: 340px; } #p4 { color: rgb(132, 106, 90); font-weight: 540; font-size: 23px; margin-top: 12px; margin-left: 340px; } #p4 span { color: red; font-weight: 800; } </style> <body> <div id="backg"> <img src="./img/bk01.png" alt=""> <div id="h3">Web前端开发工程师</div> <p id="p1">技术要求:</p> <p id="p2">对常用的一些Js框架了解,如jQuery、YUuI等。掌握最基本的JavaScript计算方法编写。对目前互联网流行的网页制作方法(Web2.0) HTML+CSS,以及各大浏览器兼容性有很大的了解。对前沿技术(HTML5+CSS3)的基本掌握。<span>Web前端技术</span>你究竟掌握了多少...</p> <p id="p3">更新时间:2015年05月19日20点(已有<span>325</span>人点赞)</p> <hr> <p id="p4">相关技术文章 <span>8</span> 篇</p> </div> </body> </html>
05-24
``` <!DOCTYPE html> <!--声明文档类型为HTML5--> <html lang="en"> <!--HTML文档的基本结构,lang属性表示使用的语言为英语--> <head> <!--网页头部,包含元数据信息和外部资源引用--> <meta charset="UTF-8"> <!--指定字符编码为UTF-8--> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <!--告诉IE浏览器采用最新的渲染方式--> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!--设置页面在移动端显示时的视口大小--> <title>Document</title> <!--网页标题--> </head> <style> <!--样式表--> #backg { <!--id选择器,对应下面的div标签--> width: 919px; <!--宽度为919像素--> height: 272px; <!--高度为272像素--> } div img { <!--对应上面的图片,设置其样式--> width: 300px; <!--宽度为300像素--> height: 222px; <!--高度为222像素--> margin-top: 20px; <!--上边距为20像素--> margin-left: 10px; <!--左边距为10像素--> } #h3 { <!--id选择器,对应下面的h3标签--> color: rgb(132, 106, 90); <!--文字颜色为rgb(132, 106, 90)--> font-weight: 600; <!--字体加粗--> font-size: 23px; <!--字体大小为23像素--> margin-top: -224px; <!--设置上边距,将h3标签向上提升224像素--> margin-left: 340px; <!--左边距为340像素--> } #p1 { <!--id选择器,对应下面的p标签--> font-size: 14px; <!--字体大小为14像素--> color: rgb(20, 111, 158); <!--文字颜色为rgb(20, 111, 158)--> font-weight: 550; <!--字体加粗--> margin-top: 13px; <!--上边距为13像素--> margin-left: 340px; <!--左边距为340像素--> } #p2 { <!--id选择器,对应下面的p标签--> width: 575px; <!--宽度为575像素--> font-size: 13.2px; <!--字体大小为13.2像素--> color: black; <!--文字颜色为黑色--> text-indent: 2em; <!--段落首行缩进2个字符--> margin-top: -3px; <!--上边距为-3像素--> margin-left: 340px; <!--左边距为340像素--> } #p2 span { <!--对应上面的span标签,设置其样式--> color: rgb(20, 111, 158); <!--文字颜色为rgb(20, 111, 158)--> font-style: italic; <!--字体样式为斜体--> text-decoration: underline; <!--添加下划线--> } #p3 { <!--id选择器,对应下面的p标签--> font-size: 14px; <!--字体大小为14像素--> color: rgb(20, 111, 158); <!--文字颜色为rgb(20, 111, 158)--> font-weight: 550; <!--字体加粗--> margin-top: 10px; <!--上边距为10像素--> margin-left: 340px; <!--左边距为340像素--> } #p3 span { <!--对应上面的span标签,设置其样式--> color: red; <!--文字颜色为红色--> font-size: 20px; <!--字体大小为20像素--> font-weight: 800; <!--字体加粗--> } hr { <!--水平线--> width: 560px; <!--宽度为560像素--> margin-top: -2px; <!--上边距为-2像素--> margin-left: 340px; <!--左边距为340像素--> } #p4 { <!--id选择器,对应下面的p标签--> color: rgb(132, 106, 90); <!--文字颜色为rgb(132, 106, 90)--> font-weight: 540; <!--字体加粗--> font-size: 23px; <!--字体大小为23像素--> margin-top: 12px; <!--上边距为12像素--> margin-left: 340px; <!--左边距为340像素--> } #p4 span { <!--对应上面的span标签,设置其样式--> color: red; <!--文字颜色为红色--> font-weight: 800; <!--字体加粗--> } </style> <body> <!--网页主体部分--> <div id="backg"> <!--div标签,id为backg,用作背景--> <img src="./img/bk01.png" alt=""> <!--图片,引用地址为./img/bk01.png--> <div id="h3">Web前端开发工程师</div> <!--h3标题,id为h3--> <p id="p1">技术要求:</p> <!--段落,id为p1--> <p id="p2">对常用的一些Js框架了解,如jQuery、YUuI等。掌握最基本的JavaScript计算方法编写。对目前互联网流行的网页制作方法(Web2.0) HTML+CSS,以及各大浏览器兼容性有很大的了解。对前沿技术(HTML5+CSS3)的基本掌握。<span>Web前端技术</span>你究竟掌握了多少...</p> <!--段落,id为p2,包含一个span标签--> <p id="p3">更新时间:2015年05月19日20点(已有<span>325</span>人点赞)</p> <!--段落,id为p3,包含一个span标签--> <hr> <!--水平线--> <p id="p4">相关技术文章 <span>8</span> 篇</p> <!--段落,id为p4,包含一个span标签--> </div> </body> </html> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值