Select input в css

Рейтинг: 0Ответов: 2Опубликовано: 19.07.2023

Подскажите пожалуйста один момент, мне нужно сделать с помощью select и option выпадающий список, но в нем нужно перед каждым выбором значения сделать статичный текст Порядок: как на скрине, так же этого текста не должно быть в вариантах выбора так же как на скрине78

Ответы

▲ 0

Для такого эффекта можно добавлять выбранному пункту класс который имеет свойство display: none. Демо (код взят отсюда и дополнен):

let selectContainer = document.querySelector(".select-container");
let select = document.querySelector(".select");
let input = document.getElementById("input");
let options = document.querySelectorAll(".option");

select.onclick = () => {
    selectContainer.classList.toggle("active");
};

options.forEach((e) => {
    e.addEventListener("click", () => {
        input.value = e.innerText;
        selectContainer.classList.remove("active");
        options.forEach((e) => {
            e.classList.remove("selected");
        });
        e.classList.add("selected");
    });
});
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: "Courier New";
}

body {
  background: #edeeff;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center
}

.select-container {
  position: relative;
  margin: 0 auto;
  width: 400px;
  display: flex;
}

.select-container .select {
  position: relative;
  background: #0f0e11;
  height: 60px;
}

.select-container .select::after {
  position: absolute;
  content: "";
  width: 15px;
  height: 15px;
  top: 50%;
  right: 15px;
  transform: translateY(-50%) rotate(45deg);
  border-bottom: 2px solid white;
  border-right: 2px solid white;
  cursor: pointer;
  transition: border-color 0.4s;
}

.select-container.active .select::after {
  border: none;
  border-left: 2px solid white;
  border-top: 2px solid white;
}
.select-container .select input {
  position: relative;
  width: 100%;
  height: 100%;
  padding: 0 15px;
  background: none;
  outline: none;
  border: none;
  font-size: 1.4rem;
  color: white;
  cursor: pointer;
}
.select-container .option-container {
  position: relative;
  background: #6e6477;
  height: 0;
  overflow-y: scroll;
  transition: 0.4s;
}

.select-container.active .option-container {
  height: 120px;
}

.select-container .option-container::-webkit-scrollbar {
  border-left: 1px solid rgba(0, 0, 0, 0.2);
  width: 10px;
}

.select-container .option-container::-webkit-scrollbar-thumb {
  background: #0f0e11;
}

.select-container .option-container .option {
  position: relative;
  padding-left: 15px;
  height: 60px;
  border-top: 1px solid rgba(0, 0, 0, 0.3);
  cursor: pointer;
  display: flex;
  align-items: center;
  transition: 0.2s;
}

.select-container .option-container .option.selected {
  background: rgba(0, 0, 0, 0.5);
  pointer-events: none;
  display: none;
}

.select-container .option-container .option:hover {
  background: rgba(0, 0, 0, 0.2);
  padding-left: 20px;
}

.select-container .option-container .option label {
  font-size: 1.1rem;
  color: white;
  cursor: pointer;
}

.text {
  width: 100px;
  margin: 20px 10px 0;
}

.select-options {
  width: 100%;
}
<div class="select-container">
  <p class="text">Порядок: </p>
  <div class="select-options">
    <div class="select">
        <input type="text" id="input" placeholder="Квартиры">
    </div>
    <div class="option-container">
        <div class="option">
            <label>Сперва новые</label>
        </div>
        <div class="option">
            <label>Сначала дорогие</label>
        </div>
        <div class="option">
            <label>Сначала дешевые</label>
        </div>
    </div>
  </div>
</div>

▲ 0

html,
body {
  height: 100%;
  margin: 0;
  background: white;
  font-family: "Source Sans Pro", sans-serif;
}

select {
  font-family: "Source Sans Pro", sans-serif;
  font-size: 1rem;
}

.select-group {
  background: #fff;
  padding: 5px 15px;
  width: 240px;
  margin: 5px;
  display: flex;
  align-items: center;
  border: 1px solid #c1c1c1;
  border-radius: 25px;
  position: relative;
}

#select {
  border: 0;
  padding: 0px 10px;
  outline: 0;
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  display: block;
  height: 40px;
  width: 100%
}

.select-group__arrow {
  position: absolute;
  right: 20px;
  width: 0;
  height: 0;
  pointer-events: none;
  border-style: solid;
  border-width: 8px 5px 0 5px;
  border-color: #7b7b7b transparent transparent transparent;
}
<div class="select-group">
  <span>Порядок:</span>
  <select name="" id="select">
    <option value="">сперва новые</option>
    <option value="">сначала дорогие</option>
    <option value="">сначала дешевые</option>
  </select>
  <span class="select-group__arrow"></span>
</div>
</div>