> 웹 프론트엔드 > HTML 튜토리얼 > 用纯css改变下拉列表select框的默认样式 - dehua.Chen

用纯css改变下拉列表select框的默认样式 - dehua.Chen

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
풀어 주다: 2016-05-21 08:42:20
원래의
1443명이 탐색했습니다.

将浏览器默认的下拉框样式清除,然后应用上自己的,再附一张向右对齐小箭头的图片即可。

<span style="color: #008080;"> 1</span> <span style="color: #800000;">select </span>{
<span style="color: #008080;"> 2</span> <span style="color: #008000;">/*</span><span style="color: #008000;">Chrome和Firefox里面的边框是不一样的,所以复写了一下</span><span style="color: #008000;">*/</span>
<span style="color: #008080;"> 3</span> <span style="color: #ff0000;">  border</span>:<span style="color: #0000ff;"> solid 1px #000</span>;
<span style="color: #008080;"> 4</span> 
<span style="color: #008080;"> 5</span> <span style="color: #008000;">/*</span><span style="color: #008000;">很关键:将默认的select选择框样式清除</span><span style="color: #008000;">*/</span>
<span style="color: #008080;"> 6</span> <span style="color: #ff0000;">  appearance</span>:<span style="color: #0000ff;">none</span>;
<span style="color: #008080;"> 7</span> <span style="color: #ff0000;">  -moz-appearance</span>:<span style="color: #0000ff;">none</span>;
<span style="color: #008080;"> 8</span> <span style="color: #ff0000;">  -webkit-appearance</span>:<span style="color: #0000ff;">none</span>;
<span style="color: #008080;"> 9</span> <span style="color: #008000;">/*</span><span style="color: #008000;">在选择框的最右侧中间显示小箭头图片</span><span style="color: #008000;">*/</span>
<span style="color: #008080;">10</span> <span style="color: #ff0000;">  background</span>:<span style="color: #0000ff;"> url("http://ourjs.github.io/static/2015/arrow.png") no-repeat scroll right center transparent</span>;
<span style="color: #008080;">11</span> <span style="color: #008000;">/*</span><span style="color: #008000;">为下拉小箭头留出一点位置,避免被文字覆盖</span><span style="color: #008000;">*/</span>
<span style="color: #008080;">12</span> <span style="color: #ff0000;">  padding-right</span>:<span style="color: #0000ff;"> 14px</span>;
<span style="color: #008080;">13</span> }
<span style="color: #008080;">14</span> <span style="color: #008000;">/*</span><span style="color: #008000;">清除ie的默认选择框样式清除,隐藏下拉箭头</span><span style="color: #008000;">*/</span>
<span style="color: #008080;">15</span> <span style="color: #800000;">select::-ms-expand </span>{<span style="color: #ff0000;"> display</span>:<span style="color: #0000ff;"> none</span>; }
로그인 후 복사

IE8/9并不支持  appearance:none  CSS属性,想要支持的话可能需要非常特殊的方法,参考SF: 我们需要为其添加一个父容器,容器是用来覆盖小箭头的,然后为select添加一个向右的小偏移或者宽度大于父级元素。设置父级的CSS属性为超出部分不可见,即可覆盖即小箭头。然后再为父级容器添加背景图片即可。

<span style="color: #008080;">1</span> <span style="color: #0000ff;"><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="parent"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">2</span>   <span style="color: #0000ff;"><span style="color: #800000;">select</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">3</span>       <span style="color: #0000ff;"><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>what<span style="color: #0000ff;"></span><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">4</span>       <span style="color: #0000ff;"><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>the<span style="color: #0000ff;"></span><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">5</span>       <span style="color: #0000ff;"><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>hell<span style="color: #0000ff;"></span><span style="color: #800000;">option</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">6</span>   <span style="color: #0000ff;"></span><span style="color: #800000;">select</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">7</span> <span style="color: #0000ff;"></span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></span></span></span></span></span>
로그인 후 복사
<span style="color: #008080;">1</span> <span style="color: #800000;">#parent </span>{<span style="color: #ff0000;">background</span>:<span style="color: #0000ff;"> url('yourimage') no-repeat</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 100px</span>;
<span style="color: #008080;">2</span> <span style="color: #ff0000;">height</span>:<span style="color: #0000ff;"> 30px</span>;<span style="color: #ff0000;"> overflow</span>:<span style="color: #0000ff;"> hidden</span>;}
<span style="color: #008080;">3</span> <span style="color: #800000;">#parent select </span>{<span style="color: #ff0000;">background</span>:<span style="color: #0000ff;"> transparent</span>;<span style="color: #ff0000;"> border</span>:<span style="color: #0000ff;"> none</span>;<span style="color: #ff0000;"> padding-left</span>:<span style="color: #0000ff;"> 10px</span>;<span style="color: #ff0000;"> width</span>:<span style="color: #0000ff;"> 120px</span>;<span style="color: #ff0000;"> height</span>:<span style="color: #0000ff;"> 100%</span>;}
로그인 후 복사

 input 背景透明代码

background-color: transparent;

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿