【问题标题】:Create a Custom select with a Background-color使用背景颜色创建自定义选择
【发布时间】:2018-11-27 21:36:21
【问题描述】:

我想用这些选择标签创建这个导航,col-md-9,问题是当我向 div 添加背景颜色时,箭头的绿色背景消失了,

请忽略视图,因为按钮稍后会更改它们

我使用这个 html 代码来创建选择,

.select-style {
  position: relative;
  padding: 0;
  margin: 0;
  border: 1px solid #ccc;
  width: 120px;
  height: 30px;
  overflow: hidden;
  background: transparent url("https://i.imgur.com/qJolQ2D.png") no-repeat 93% 50%;
}

.select-style select {
  padding: 5px 8px;
  width: 150%;
  border: none;
  background-color: transparent;
  background-image: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
}

.select-style select:focus {
  outline: none;
}

.select-style:after {
  position: absolute;
  content: "";
  width: 30px;
  height: 100%;
  background-color: #90c322!important;
  top: 0;
  right: -1px;
  z-index: -1;
  border-radius: 0 1px 1px 0;
}
<div class="col-md-9 headerdiv">

  <span id="sortby">  Sort By </span>
  <div class="select-style" id="style1">
    <select>
      <option>Position</option>
      <option>Position</option>
    </select>
  </div>
  <span id="show">  Show </span>
  <div class="select-style" id="style2">
    <select>
      <option> 5 per page </option>
      <option>3 per page </option>
    </select>
  </div>

  <button class="right" value="MORE">1</button>
  <button class="right" value="LESS">2</button>
  <div id="viewas"> View As</div>

</div>

【问题讨论】:

  • 如果您仔细观察,箭头指针就在那里,但它不是绿色的,因为您无法像这样在 css 中更改图像的颜色。
  • 假设我想删除这张图片并用箭头替换它,你能帮我吗?
  • 或者告诉我如何将它的背景更改为#90c322
  • 您可以尝试使用字体很棒的图标,因为它们在技术上是一种您可以编辑它们的字体(更改颜色、大小等)
  • 什么浏览器不能用?在 chrome 中看起来不错

标签: html css


【解决方案1】:

绿色框没有出现是因为 z-index:-1,如果你给它 z-index:1 它肯定会出现,但问题是你必须在绿色框外点击才能进入下拉菜单上班。

.headerdiv{
background-color:lightgray;
}
.select-style {
  position: relative;
  padding: 0;
  margin: 0;
  border: 1px solid #ccc;
  width: 120px;
  height: 30px;
  overflow: hidden;
  background-color:pink;
}
.select-style i{
color:#fff;
position:absolute;
top:8px;
right:10px;
z-index:9999;
font-size:20px;
}

.select-style select {
  padding: 5px 8px;
  width: 150%;
  border: none;
  background-color: transparent;
  background-image: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
}

.select-style select:focus {
  outline: none;
}

.select-style:after {
  position: absolute;
  content: "";
  width: 30px;
  height: 100%;
  background-color: #90c322!important;
  top: 0;
  right: -1px;
  z-index: 1;
  border-radius: 0 1px 1px 0;
}
<link href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" rel="stylesheet"/>
<head>
<meta charset="utf-8>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
</head>
<body>
<div class="col-md-9 headerdiv">

  <span id="sortby">  Sort By </span>
  <div class="select-style" id="style1">
  <i class="fas fa-angle-down"></i>
    <select>
      <option>Position</option>
      <option>Position</option>
    </select>
  </div>
  <span id="show">  Show </span>
  <div class="select-style" id="style2">
  <i class="fas fa-angle-down"></i>
    <select>
      <option> 5 per page </option>
      <option>3 per page </option>
    </select>
  </div>

  <button class="right" value="MORE">1</button>
  <button class="right" value="LESS">2</button>
  <div id="viewas"> View As</div>

</div>
</body>

你也可以试试这个。 (这个解决方案的灵感来自https://codepen.io/vkjgr/pen/VYMeXp

.headerdiv{
  background-color: lightgray;
}
select {

  /* styling */
  background-color: white;
  border: thin solid blue;
  border-radius: 4px;
  display: inline-block;
  font: inherit;
  line-height: 1.5em;
  padding: 0.5em 3.5em 0.5em 1em;

  /* reset */

  margin: 0;      
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  -webkit-appearance: none;
  -moz-appearance: none;
}


/* arrows */

select.classic {
  background-image:
    linear-gradient(45deg, transparent 50%, white 50%),
    linear-gradient(135deg, white 50%, transparent 50%),
    linear-gradient(to right, green, green);
  background-position:
    calc(100% - 20px) calc(1em + 2px),
    calc(100% - 15px) calc(1em + 2px),
    100% 0;
  background-size:
    5px 5px,
    5px 5px,
    2.5em 2.5em;
  background-repeat: no-repeat;
}

select.classic:focus {
  background-image:
    linear-gradient(45deg, white 50%, transparent 50%),
    linear-gradient(135deg, transparent 50%, white 50%),
    linear-gradient(to right, green, green);
  background-position:
    calc(100% - 15px) 1em,
    calc(100% - 20px) 1em,
    100% 0;
  background-size:
    5px 5px,
    5px 5px,
    2.5em 2.5em;
  background-repeat: no-repeat;
  border-color: grey;
  outline: 0;
}
<div class="col-md-9 headerdiv">

  <span id="sortby">  Sort By </span>
  <div class="select-style" id="style1">
    <select class="classic">
      <option>Position</option>
      <option>Position</option>
    </select>
  </div>
  <span id="show">  Show </span>
  <div class="select-style" id="style2">
    <select class="classic">
      <option> 5 per page </option>
      <option>3 per page </option>
    </select>
  </div>

  <button class="right" value="MORE">1</button>
  <button class="right" value="LESS">2</button>
  <div id="viewas"> View As</div>

</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-14
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多