如何使用Tailwind更改<select>上chevron的位置?

huangapple go评论52阅读模式
英文:

How to change position of chevron on <select> with Tailwind?

问题

I have a classic select input which receives an array of strings.

问题是,chevron 位于 select 的绝对右侧,靠近边框。
如何使用Tailwind更改<select>上chevron的位置?

我想将其位置向左移动一点。

如何在 Tailwind 中实现?

这是一个 Tailwind playground

这是代码:

<div class="m-8">
  <select class="h-10 w-full rounded border border-solid border-neutral-300 px-4 text-sm">
    <option value="none">None</option>
    {list?.map((item) => (
    <option key="{item}" value="{item}">{item}</option>
    ))}
  </select>
</div>
英文:

I have a classic select input which receive an array of strings.

Problem, the chevron is on absolute right of the select, against the border.
如何使用Tailwind更改<select>上chevron的位置?

I want to move its poition to the left a little.

How to achieve that with tailwind ?

Here is a Tailwind playground

Here is the code :

&lt;div class=&quot;m-8&quot;&gt;
  &lt;select class=&quot;h-10 w-full rounded border border-solid border-neutral-300 px-4 text-sm&quot;&gt;
    &lt;option value=&quot;none&quot;&gt;None&lt;/option&gt;
    {list?.map((item) =&gt; (
    &lt;option key=&quot;{item}&quot; value=&quot;{item}&quot;&gt;{item}&lt;/option&gt;
    ))}
  &lt;/select&gt;
&lt;/div&gt;

答案1

得分: 2

We can't add padding to the default select arrow. The workaround you can do is use outline property. You can achieve this using the following code.

<div class="m-8">
  <select class="h-10 w-full rounded border-r-8 border-transparent px-4 text-sm outline outline-neutral-700">
    <option value="none">Non précisée</option>
    {list?.map((item) => (
    <option key="{item}" value="{item}">{item}</option>
    ))}
  </select>
</div>

Here, we are giving a border to just the right side and setting it to 8px of width. This will work as padding after we add the outline. Don't forget to set the border color to transparent. Then add the outline property and set the color you want to outline. This will create the same effect that you wanted with border and padding.

Here's the tailwind playground

英文:

We can't add padding to the default select arrow. The workaround you can do is use outline property. You can achieve is using following code.

&lt;div class=&quot;m-8&quot;&gt;
  &lt;select class=&quot;h-10 w-full rounded border-r-8 border-transparent px-4 text-sm outline outline-neutral-700&quot;&gt;
    &lt;option value=&quot;none&quot;&gt;Non pr&#233;cis&#233;&lt;/option&gt;
    {list?.map((item) =&gt; (
    &lt;option key=&quot;{item}&quot; value=&quot;{item}&quot;&gt;{item}&lt;/option&gt;
    ))}
  &lt;/select&gt;
&lt;/div&gt;

Here, we are giving border to just the right side and setting it to 8px of width. This will work as a padding after we add the outline. Don't forget to set border colour to transparent. Then add outline property and set the colour you want to outline. This will create the same effect that you wanted with border and padding.

Here's the tailwind playground

答案2

得分: 0

以下是翻译好的内容:

"select" 元素很难进行样式化。为了获得更多自由,我会选择自制下拉菜单。

另外,总是有一些 CSS 技巧:

HTML:

<select class="form-select appearance-none pr-8 pl-2 bg-no-repeat">
  <option>选项 1</option>
  <option>选项 2</option>
  <option>选项 3</option>
</select>

CSS:

.form-select {
  background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAQCAYAAAAMJL+VAAAABGdBTUEAALGPC/xhBQAAAQtJREFUOBG1lEEOgjAQRalbGj2OG9caOACn4ALGtfEuHACiazceR1PWOH/CNA3aMiTaBDpt/7zPdBKy7M/DCL9pGkvxxVp7KsvyJftL5rZt1865M+Ucq6pyyF3hNcI7Cuu+728QYn/JQA5yKaempxuZmQngOwEaYx55nu+1lQh8GIatMGi+01NwBcEmhxBqK4nAPZJ78K0KKFAJmR3oPp8+Iwgob0Oa6+TLoeCvRx+mTUYf/FVBGTPRwDkfLxnaSrRwcH0FWhNOmrkWYbE2XEicqgSa1J0LQ+aPCuQgZiLnwewbGuz5MGoAhcIkCQcjaTBjMgtXGURMVHC1wcQEy0J+Zlj8bKAnY1/UzDe2dbAVqfXn6wAAAABJRU5ErkJggg==');
  background-size: 0.7rem;
  background-position: right 0.7rem center;
}
英文:

The "select" elements are complicated to style. For more freedom I would opt for a home made dropdown.

Otherwise there are always some css tricks:

https://play.tailwindcss.com/rOgId32XZx

HTML :

&lt;select class=&quot;form-select appearance-none pr-8 pl-2 bg-no-repeat&quot;&gt;
  &lt;option&gt;Option 1&lt;/option&gt;
  &lt;option&gt;Option 2&lt;/option&gt;
  &lt;option&gt;Option 3&lt;/option&gt;
&lt;/select&gt;

CSS:

  .form-select {
    background-image: url(&#39;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAQCAYAAAAMJL+VAAAABGdBTUEAALGPC/xhBQAAAQtJREFUOBG1lEEOgjAQRalbGj2OG9caOACn4ALGtfEuHACiazceR1PWOH/CNA3aMiTaBDpt/7zPdBKy7M/DCL9pGkvxxVp7KsvyJftL5rZt1865M+Ucq6pyyF3hNcI7Cuu+728QYn/JQA5yKaempxuZmQngOwEaYx55nu+1lQh8GIatMGi+01NwBcEmhxBqK4nAPZJ78K0KKFAJmR3oPp8+Iwgob0Oa6+TLoeCvRx+mTUYf/FVBGTPRwDkfLxnaSrRwcH0FWhNOmrkWYbE2XEicqgSa1J0LQ+aPCuQgZiLnwewbGuz5MGoAhcIkCQcjaTBjMgtXGURMVHC1wcQEy0J+Zlj8bKAnY1/UzDe2dbAVqfXn6wAAAABJRU5ErkJggg==&#39;);
    background-size: 0.7rem;
    background-position: right 0.7rem center;
  }

huangapple
  • 本文由 发表于 2023年5月10日 23:06:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定