DEV Community

Sekti Wicaksono
Sekti Wicaksono

Posted on • Edited on

2

Custom Select for Form

Hello guys, now I want to create a custom form select/dropdown.

Let start with the html

<div class="wrapper">

  <label for="country">Country</label>
  <select name="country" id="country"> 
    <option value="US">United States</option>
    <option value="UK">United Kingdom</option>
    <option value="ID">Indonesia</option>
    <option value="SG">Singapore</option>
    <option value="MY">Malaysia</option>
  </select>

</div>
Enter fullscreen mode Exit fullscreen mode

The result:
Html

Now let's give it a style to the dropdown.

@import url('https://fonts.googleapis.com/css2?family=Nunito:wght@300;700&display=swap');

* { box-sizing: border-box; }

body, html {
  padding: 0;
  margin: 0;
  font-family: 'Nunito', sans-serif;
  color: #181820;
  height: 100%;
}

.wrapper {
  height: 100%;
  padding: 2em;
  display: grid;
  place-content: center;
}
Enter fullscreen mode Exit fullscreen mode

The style above will set the font to Nunito and put the element in the centre of the page, like the image below.
CSS-1

Let's style the label first.

label {
  margin-top: 1.5em;
  margin-bottom: .5em;
  font-weight: bold;
  font-size: 1.2em;
}
Enter fullscreen mode Exit fullscreen mode

From the code above, the label will have a white space from top and bottom with font-size 1.2em and font-weight bold.

Result:
CSS-2

Now give style to dropdown/select.

select {
  padding: 1em;
  width: 130%;
  border-radius: .2em;
  border: 1px solid #acacac;
  color: #181820;
}
Enter fullscreen mode Exit fullscreen mode

Result:
CSS-2

As you can see the dropdown not exactly have white space on the right side after I give it a padding by 1em;
To fix this we need to customise the dropdown.

First, I need to add appearance of the select to none.

select {
  /* previous css code*/

  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  -ms-appearance: none;
}
Enter fullscreen mode Exit fullscreen mode

Result:
CSS-3

Second, add the dropdown icon from an image as a background with no-repeat and set the size around 14px-18px. Also set position to right alignment, and origin based on the content-box;

select {
  /* previous css code*/

  background: url('https://cdn1.iconfinder.com/data/icons/arrows-vol-1-4/24/dropdown_arrow-512.png');
  background-repeat: no-repeat;
  background-size: 15px 15px;
  background-position: right;
  background-origin: content-box;
}
Enter fullscreen mode Exit fullscreen mode

The full code

That's all. I hope you enjoyed it.
Happy coding! :)

Jetbrains image

Is Your CI/CD Server a Prime Target for Attack?

57% of organizations have suffered from a security incident related to DevOps toolchain exposures. It makes sense—CI/CD servers have access to source code, a highly valuable asset. Is yours secure? Check out nine practical tips to protect your CI/CD.

Learn more

Top comments (0)

Image of Stellar post

🚀 Stellar Dev Diaries Series: Episode 1 is LIVE!

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Dive into this informative piece, backed by our vibrant DEV Community

Whether you’re a novice or a pro, your perspective enriches our collective insight.

A simple “thank you” can lift someone’s spirits—share your gratitude in the comments!

On DEV, the power of shared knowledge paves a smoother path and tightens our community ties. Found value here? A quick thanks to the author makes a big impact.

Okay