css 如何在select中放置元素?

yrdbyhpb  于 2022-12-30  发布在  其他
关注(0)|答案(3)|浏览(165)

尝试在选择部分插入SVG图像,如下所示:In FIGMA
不明白我的代码中有什么错误。

.contacts__select {
  margin: 0;
  margin-top: 95px;
  margin-left: 64px;
  position: relative;
  height: 50px;
  width: 450px;
  background-color: #D6E7D2;
  border: none;
  box-shadow: 0px 4px 4px 0px #00000040;
  align-self: flex-start;

  -webkit-appearance: none;
  -moz-appearance: none;
  -ms-appearance: none;
  appearance: none;

  font-family: 'Inter';
  color: #717171;
  font-size: 20px;
  font-weight: 400;
  line-height: 40px;
  letter-spacing: 0.05em;
  text-align: left;
}

.contacts__select::after {
  position: absolute;
  content: '';
  background-image: url('./assets/img/svg/accordion_btn.svg');
  bottom: 0;
  right: 0;
}

我尝试了不同的组合,如果左/右上/下。无论如何不工作。my version

sdnqo3pr

sdnqo3pr1#

.custom-select {
    position: relative;
    border: 1px solid #ccc;
    overflow: hidden;
    background-color: #fff;
  }

  .custom-select:before {
    content: "";
    width: 34px;
    height: 34px;
    position: absolute;
    right: 14px;
    top: 50%;
    transform: translateY(-50%);
    pointer-events: none;
    background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-down-circle-fill" viewBox="0 0 16 16"><path d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM8.5 4.5a.5.5 0 0 0-1 0v5.793L5.354 8.146a.5.5 0 1 0-.708.708l3 3a.5.5 0 0 0 .708 0l3-3a.5.5 0 0 0-.708-.708L8.5 10.293V4.5z"/></svg>')
      no-repeat center;
  }

  .custom-select select {
    padding: 10px;
    width: 100%;
    border: none;
    box-shadow: none;
    appearance: none;
  }
<div class="custom-select">
  <select>
    <option value="">Select an option</option>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
  </select>
</div>
mfpqipee

mfpqipee2#

你可以用CSS Flex来实现,它非常强大。
我们所要做的就是将标题和SVG(子对象)放在一个显示Flex(父对象)中,然后告诉子对象居中对齐,并将它们彼此隔开

.dropdown {
    display: flex;
    justify-content: space-between;
    align-items: center;

    background-color: #d6e7d2;
    box-shadow: 0px 4px 4px 0px #00000040;
    height: 50px;
    padding: 0 16px;
}

.dropdown__title {
    font-family: 'Inter';
        color: #717171;
    font-size: 20px;
    font-weight: 400;
}

.dropdown__icon {
  width: 24px;
  height: 24px;
}
<div class="dropdown">
    <span class="dropdown__title">City</span>
    <span class="dropdown__icon">
        <img src="/assets/img/svg/accordion_btn.svg" alt="icon">
    </span>
</div>
vfh0ocws

vfh0ocws3#

必须为伪元素提供尺寸,例如

width: 20px;
height: 20px;

由于内容为空,因此维度默认为0。

相关问题