fxxqq / 6fedcom.github.io

frank的前端养成记(hexo博客)
https://6fed.com
22 stars 5 forks source link

h5中progress标签的CSS样式总结 #41

Open fxxqq opened 6 years ago

fxxqq commented 6 years ago

HTML5中新增了progress标签,用来表示进度条。

<progress value="100" max="100" class="hot">

显示效果如下: image

progress {
    width: 168px;
    height: 5px;
}

progress::-webkit-progress-bar {
    background-color: #d7d7d7;
}

progress::-webkit-progress-value {
    background-color: orange;
}

解释下,在Chrome浏览器中

progress是以如下结构渲染的

progress

::-webkit-progress-bar 全部进度

::-webkit-progress-value 已完成进度

通过这两个伪元素为其添加样式。

但在别的浏览器中又有所不同,如IE10,这两个伪元素不起作用,直接用color样式可以修改已完成进度的颜色,而全部进度为background

FireFox中progress-bar为已完成进度,background为全部进度,而Opera中对这个样式只能为浏览器默认样式。

因此兼容性写法可以考虑如下:

progress {
    color: orange;
 /*兼容IE10的已完成进度背景*/
    border: none;
    background: #d7d7d7;
/*这个属性也可当作Chrome的已完成进度背景,只不过被下面的::progress-bar覆盖了*/
}

progress::-webkit-progress-bar {
    background: #d7d7d7;
}

progress::-webkit-progress-value,
progress::-moz-progress-bar {
    background: orange;
}