【问题标题】:Customize a select with font-awesome使用 font-awesome 自定义选择
【发布时间】:2015-08-27 06:08:13
【问题描述】:

这是我实际拥有的:

HTML

<label class="select">
  <select name="email" id="email">
    <option>aaaa</option>
    <option>aaaa</option>
    <option>aaaa</option>
    <option>aaaa</option>
    <option>aaaa</option>
    <option>aaaa</option>
  </select>
</label>

CSS

.cforms select {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;   
}


.select {
    position:relative;   
    display: -moz-inline-stack;
    display: inline-block;
    vertical-align: middle;
    zoom: 1;
    *display: inline;
    margin-top:40px;
}

.select:after {
  content: "\f0dc";
  font-family: FontAwesome;
  color: #000;
  padding:8px;
  position:relative;
  right:35px;
  top:0px;
  background:red;
  z-index:-1;
  width:10%;
  line-height:10%;
}

问题

实际上,我的选择附近没有出现箭头。

你能帮我解决这个问题吗?

我在网上搜索了示例,但无法使其工作。

谢谢。

【问题讨论】:

  • inputs 如select 元素不能有伪元素AFAIK

标签: css select font-awesome


【解决方案1】:

也许是这样

1) 字体真棒 4

.select {
    border: 1px solid #ccc;
    overflow: hidden; 
    height: 40px;    
    width: 240px;
    position: relative;
    display: block;
}

select{       
    height: 40px;
    padding: 5px;
    border: 0;
    font-size: 16px;       
    width: 240px;
   -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
}
.select:after {
    content:"\f0dc";
    font-family: FontAwesome;
    color: #000;
    padding: 12px 8px;
    position: absolute; right: 0; top: 0;
    background: red;
    z-index: 1;
    text-align: center;
    width: 10%;
    height: 100%;      
    pointer-events: none;
    box-sizing: border-box;   
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet"/>
<label class="select">
    <select name="email" id="email">
        <option>aaaa1</option>
        <option>aaaa2</option>
        <option>aaaa3</option>
        <option>aaaa4</option>
        <option>aaaa5</option>
        <option>aaaa6</option>
    </select>
</label>

2) 字体真棒 5

.select {
    border: 1px solid #ccc;
    overflow: hidden; 
    height: 40px;    
    width: 240px;
    position: relative;
    display: block;
}

select{       
    height: 40px;
    padding: 5px;
    border: 0;
    font-size: 16px;       
    width: 240px;
   -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
}
.select:after {
    content:"\f35a";
    font-family: "Font Awesome 5 Free";
    color: #fff;
    padding: 12px 8px;
    position: absolute; right: 0; top: 0;
    background: red;
    z-index: 1;
    text-align: center;
    width: 10%;
    height: 100%;      
    pointer-events: none;
    box-sizing: border-box;   
}
<link href="https://use.fontawesome.com/releases/v5.13.0/css/all.css" rel="stylesheet">
<label class="select">
    <select name="email" id="email">
        <option>aaaa1</option>
        <option>aaaa2</option>
        <option>aaaa3</option>
        <option>aaaa4</option>
        <option>aaaa5</option>
        <option>aaaa6</option>
    </select>
</label>

【讨论】:

  • 在这个答案中对我来说真正重要的是pointer-events: none;,因为它可以防止点击事件中断!
  • 我们不能在&lt;select&gt; 上使用pseudo-elements。我们必须使用label 来包装它。这是个好主意和你的答案
  • 如果使用 Font Awesome 5,您需要使用不同的字体系列。很可能是font-family: "Font Awesome 5 Free",但现在有 4 种不同的选项。更多详情请看这里:fontawesome.com/how-to-use/on-the-web/advanced/…
  • 我用它来自定义 Contact Form 7 (WordPress) 上的表单,而不是在表单上添加标签(它破坏了我的布局),我使用了周围的类CF7 在 select 周围附加的 span 以在其上应用 after 样式。太好了!
猜你喜欢
  • 1970-01-01
  • 2014-11-22
  • 2020-02-01
  • 1970-01-01
  • 2023-03-06
  • 2012-06-18
  • 1970-01-01
  • 2016-06-06
  • 2019-06-22
相关资源
最近更新 更多