创建具有背景色的自定义选择

时间:2018-11-27 10:24:42

标签: html css

this is the selected Nav with select that i want

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

enter image description here 请忽略该视图,因为该按钮稍后会更改

我使用此html代码创建了select,

.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>

1 个答案:

答案 0 :(得分:0)

由于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>