{"componentChunkName":"component---src-components-tutorials-js","path":"/tutorials/select-input-styling/","result":{"data":{"markdownRemark":{"html":"<h2>Contents</h2>\n<ol>\n<li><a href=\"#about\">Why would I need this?</a></li>\n<li>\n<p><a href=\"#work\">How do I get this working</a></p>\n<ol>\n<li><a href=\"#work-ui\">Preparing the UI</a></li>\n<li><a href=\"#work-css\">Adding CSS</a></li>\n</ol>\n</li>\n<li><a href=\"#further-thoughts\">Futher Thoughts</a></li>\n<li><a href=\"#run\">How do I run the example</a></li>\n</ol>\n<!-- endexcerpt -->\n<p><span id=\"about\"></span></p>\n<h2>Why would I need this?</h2>\n<p>This example was developed in response to the R community post titled, <a href=\"https://community.rstudio.com/t/selectinput-css-customization/12664\">selectInput -- CSS Customization</a>, which asked the following:</p>\n<blockquote>\n<p>Hi everyone, do you know is there a way to configure selectInput in a more meta way using CSS? For example, I want the background of the first option to be green; is there a way to do it?</p>\n</blockquote>\n<p>The beauty about the shiny package is that there are a lot of \"out-of-the-box\" features and tools. You really don't have to mess around with styling or adding interactivity, which allows you to focus on the data (the most important part of your app). As you progress as a shiny developer, you may find that you need to design pages that meet specific branding guidelines (using corporate css for example) or you want certain elements to look and behave in certain way. Under the hood, shiny uses <a href=\"https://getbootstrap.com\">bootstrap</a>, which is a popular UI library for building websites and applications. This means that if you want shiny elements to have specific appearances, then you will have to overwrite bootstrap styles, which can get very messy. This is were having knowledge in html, css, and javascript come in handy.</p>\n<p>To address the original question, we will create our own select input with html and style it with css.</p>\n<blockquote>\n<p>I would use caution before implementing this approach in any production application without extensive testing. This example uses the css property <code>appearance</code> which is still fairly experimental. This may cause elements to behave and look differently across browsers and devices. Check a CSS coverage site such as caniuse.com for browser support and implementation guidelines. Also, conduct tests on the target browsers and devices before moving your app to production.</p>\n</blockquote>\n<p>In the future, I plan on developing tutorials that provide more alternatives so stay tuned! </p>\n<p>For now, we will create an application with a custom select input. We will also display the selection in an output element just to demonstrate that the input is working with shiny. Here is what the final app will look like.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 600px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/a5e127e7c149ab997b08626aaf6ca49b/10ab7/select_input_preview.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 61.33333333333334%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAABYklEQVQoz6WSy07CQBSGZ4cglxoVbys3GgyauPUVUIwPAImJz8SGrpigjoEFhEjxUQiEhLDkJiJJO505nqklVIIY8CRf/86l35w0Q473D64Oo3vsNBbPncTi9Oz8km7v7NJIRKOatkWDwbBDKDQj6GY4rFGfb4P6/QEaCGw+4fsFoTn6wJ4Z5POPoPK1VIZKqQTPjAFjL2AYNYdq1fiBYRhQq71BuYz7KxWHbFa/I91uNzUcDqHX65uC2xYIwSXn3LZtPplMuGmaHACWYbkAuhKk1WqlB4N3GH2MBSJH40/o9QcwxkQvoNABD1gI7pGY0rIsqNfr16TT6aSFECClFPK7AAfgpKfmx555tSCVo9lszoRY6iHnJdIjl4sP+l047fAvyUpCWLGWCW25ZqlmvMKU2xh3/+M62K4wQdrt9r26HmpCXQOVqzD9RjkajUaS6Lp+VCgUbpGb/1AsFpOZTCb6BR5eMOz0Y7CXAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"select input shiny application\"\n        title=\"select input shiny application\"\n        src=\"/static/a5e127e7c149ab997b08626aaf6ca49b/0a47e/select_input_preview.png\"\n        srcset=\"/static/a5e127e7c149ab997b08626aaf6ca49b/8a4e8/select_input_preview.png 150w,\n/static/a5e127e7c149ab997b08626aaf6ca49b/5a46d/select_input_preview.png 300w,\n/static/a5e127e7c149ab997b08626aaf6ca49b/0a47e/select_input_preview.png 600w,\n/static/a5e127e7c149ab997b08626aaf6ca49b/1cfc2/select_input_preview.png 900w,\n/static/a5e127e7c149ab997b08626aaf6ca49b/c1b63/select_input_preview.png 1200w,\n/static/a5e127e7c149ab997b08626aaf6ca49b/10ab7/select_input_preview.png 1552w\"\n        sizes=\"(max-width: 600px) 100vw, 600px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n  </a>\n    </span></p>\n<blockquote>\n<p>This tutorial uses a bit of html and css. Some knowledge will help, but it is not required. I'll try to explain things as I go and provide links to additional resources. As always, questions and suggestions are welcome!</p>\n</blockquote>\n<blockquote>\n<p>Also, I'm the process of developing an new example that demonstrates how to build a completely custom input element. Stay tuned! </p>\n</blockquote>\n<p><span id=\"work\"></span></p>\n<h2>How do I get this working?</h2>\n<p>I will cover two steps in this tutorial. </p>\n<ol>\n<li>Prepare the UI: learn how to structure our input in shiny</li>\n<li>Add CSS: define the appearance of the input</li>\n</ol>\n<p>Let's get started!</p>\n<p><span id=\"work-ui\"></span></p>\n<h3>Preparing the UI</h3>\n<p>First, let's figure out how we are going to write our input element. </p>\n<p>If we take a look at the docs for the <a href=\"https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select\">select</a> element, we can see that there are a few elements that are standard practice. </p>\n<div class=\"gatsby-highlight\" data-language=\"html\"><pre class=\"language-html\"><code class=\"language-html\"><span class=\"token comment\">&lt;!-- from the docs: 04 Nov 2019 ---></span>\n<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>label</span> <span class=\"token attr-name\">for</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>pet-select<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>Choose a pet:<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>label</span><span class=\"token punctuation\">></span></span>\n\n<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>select</span> <span class=\"token attr-name\">name</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>pets<span class=\"token punctuation\">\"</span></span> <span class=\"token attr-name\">id</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>pet-select<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>option</span> <span class=\"token attr-name\">value</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span><span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>--Please choose an option--<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>option</span><span class=\"token punctuation\">></span></span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>option</span> <span class=\"token attr-name\">value</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>dog<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>Dog<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>option</span><span class=\"token punctuation\">></span></span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>option</span> <span class=\"token attr-name\">value</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>cat<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>Cat<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>option</span><span class=\"token punctuation\">></span></span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>option</span> <span class=\"token attr-name\">value</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>hamster<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>Hamster<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>option</span><span class=\"token punctuation\">></span></span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>option</span> <span class=\"token attr-name\">value</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>parrot<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>Parrot<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>option</span><span class=\"token punctuation\">></span></span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>option</span> <span class=\"token attr-name\">value</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>spider<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>Spider<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>option</span><span class=\"token punctuation\">></span></span>\n    <span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>option</span> <span class=\"token attr-name\">value</span><span class=\"token attr-value\"><span class=\"token punctuation attr-equals\">=</span><span class=\"token punctuation\">\"</span>goldfish<span class=\"token punctuation\">\"</span></span><span class=\"token punctuation\">></span></span>Goldfish<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>option</span><span class=\"token punctuation\">></span></span>\n<span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>select</span><span class=\"token punctuation\">></span></span></code></pre></div>\n<p>The <code>&#x3C;label></code> element is required as it acts as a title for the input. The <code>for</code> attribute is also required as it links the label with the input element (this important for individuals who use screen readers or other web assistive technologies). The <code>for</code> always references the <code>id</code> of the input element. </p>\n<p>The main element is <code>&#x3C;select></code>, which acts as a control mechanism for a series of choices, or <code>&#x3C;options></code>. It's good practice to use the <code>name</code> attribute and an <code>id</code>. To create our list of options (i.e., choices to choose from), each option is wrapped in an <code>&#x3C;option></code> tag and given a unique value. The option and value should relate to each other (the title <code>Dog</code> relates to the value <code>dog</code>). It's also a good idea to have a blank option as demonstrated in the example above as this will allow you to run functions when a valid selection is made.</p>\n<p>This how you would write a select input in HTML, how do we write this in shiny?</p>\n<p>In shiny, writting html is bit different. In this example, we will use the <code>tagList</code> aproach. The select element in shiny can be used by typing:</p>\n<div class=\"gatsby-highlight\" data-language=\"r\"><pre class=\"language-r\"><code class=\"language-r\">tags<span class=\"token operator\">$</span>select<span class=\"token punctuation\">(</span><span class=\"token ellipsis\">...</span><span class=\"token punctuation\">)</span></code></pre></div>\n<p>A full list of all HTML5 shiny tags can be found in the <a href=\"https://shiny.rstudio.com/articles/tag-glossary.html\">Shiny Tags Glossary</a>.</p>\n<p>Let's take a look at the example from the original post.</p>\n<div class=\"gatsby-highlight\" data-language=\"r\"><pre class=\"language-r\"><code class=\"language-r\"><span class=\"token ellipsis\">...</span>\nselectInput<span class=\"token punctuation\">(</span><span class=\"token string\">\"state\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"Choose a state:\"</span><span class=\"token punctuation\">,</span>\n    list<span class=\"token punctuation\">(</span>\n        `East Coast` <span class=\"token operator\">=</span> c<span class=\"token punctuation\">(</span><span class=\"token string\">\"NY\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"NJ\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"CT\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n        `West Coast` <span class=\"token operator\">=</span> c<span class=\"token punctuation\">(</span><span class=\"token string\">\"WA\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"OR\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"CA\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n        `Midwest` <span class=\"token operator\">=</span> c<span class=\"token punctuation\">(</span><span class=\"token string\">\"MN\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"WI\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"IA\"</span><span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">)</span>\n<span class=\"token ellipsis\">...</span></code></pre></div>\n<p>The select input was given an input id of <code>state</code> and given the label <code>Choose a state:</code>. We won't really change anything to these elements. </p>\n<p>Now the use <code>list</code> is a bit interesting. This is grouping similar options together by a similar title. In shiny, we aren't expose to what is going on behind the scenes, but here's what is happening. </p>\n<ol>\n<li>Create an <code>&#x3C;option></code> for each state (i.e., NY, NJ, etc.)</li>\n<li>It will group options into three groups i.e., East Coast, West Coast, Midwest</li>\n<li>Create labels for each group of options</li>\n</ol>\n<p>Groups of options can be created by wrapping the desired <code>&#x3C;options></code> in an <a href=\"https://developer.mozilla.org/en-US/docs/Web/HTML/Element/optgroup\">option group</a>: <code>&#x3C;optgroup></code>. We will use the property <code>label</code> to give a title for the groups of options.</p>\n<p>Let's start building our input. First, the label.</p>\n<div class=\"gatsby-highlight\" data-language=\"r\"><pre class=\"language-r\"><code class=\"language-r\"><span class=\"token comment\"># &lt;label> via tags$label</span>\ntags<span class=\"token operator\">$</span>label<span class=\"token punctuation\">(</span><span class=\"token string\">\"for\"</span><span class=\"token operator\">=</span><span class=\"token string\">\"state\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"Select a State\"</span><span class=\"token punctuation\">,</span> class<span class=\"token operator\">=</span><span class=\"token string\">\"input-label\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span></code></pre></div>\n<p>Second, we can start creating the shell for the select input. We are using the id provided in the example and let's add the default option too.</p>\n<div class=\"gatsby-highlight\" data-language=\"r\"><pre class=\"language-r\"><code class=\"language-r\"><span class=\"token comment\"># &lt;select> via tags$select</span>\ntags<span class=\"token operator\">$</span>select<span class=\"token punctuation\">(</span>id<span class=\"token operator\">=</span><span class=\"token string\">\"state\"</span><span class=\"token punctuation\">,</span>\n    \n    <span class=\"token comment\"># default</span>\n    tags<span class=\"token operator\">$</span>option<span class=\"token punctuation\">(</span>value <span class=\"token operator\">=</span> <span class=\"token string\">\"none\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n    <span class=\"token ellipsis\">...</span>\n<span class=\"token punctuation\">)</span></code></pre></div>\n<p>Now, let's add the options. Remember, we are wrapping similar options in the <code>&#x3C;optgroup></code> tag. In shiny, it is <code>tags$optgroup(...)</code>. We will also assign a label for each group. Here's what that looks like.</p>\n<div class=\"gatsby-highlight\" data-language=\"r\"><pre class=\"language-r\"><code class=\"language-r\"><span class=\"token comment\"># input</span>\ntags<span class=\"token operator\">$</span>select<span class=\"token punctuation\">(</span>id<span class=\"token operator\">=</span><span class=\"token string\">\"state\"</span><span class=\"token punctuation\">,</span>\n            \n            <span class=\"token comment\"># default</span>\n            tags<span class=\"token operator\">$</span>option<span class=\"token punctuation\">(</span>value <span class=\"token operator\">=</span> <span class=\"token string\">\"none\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n            \n            <span class=\"token comment\"># east coast</span>\n            tags<span class=\"token operator\">$</span>optgroup<span class=\"token punctuation\">(</span>label <span class=\"token operator\">=</span> <span class=\"token string\">\"East Coast\"</span><span class=\"token punctuation\">,</span>\n                tags<span class=\"token operator\">$</span>option<span class=\"token punctuation\">(</span>value <span class=\"token operator\">=</span> <span class=\"token string\">\"NY\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"NY\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n                tags<span class=\"token operator\">$</span>option<span class=\"token punctuation\">(</span>value <span class=\"token operator\">=</span> <span class=\"token string\">\"NJ\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"NJ\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n                tags<span class=\"token operator\">$</span>option<span class=\"token punctuation\">(</span>value <span class=\"token operator\">=</span> <span class=\"token string\">\"CT\"</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"CT\"</span><span class=\"token punctuation\">)</span>\n            <span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n            \n            <span class=\"token comment\"># west coast</span>\n            tags<span class=\"token operator\">$</span>optgroup<span class=\"token punctuation\">(</span>label <span class=\"token operator\">=</span> <span class=\"token string\">\"West Coast\"</span><span class=\"token punctuation\">,</span>\n                tags<span class=\"token operator\">$</span>option<span class=\"token punctuation\">(</span>value <span class=\"token operator\">=</span> <span class=\"token string\">\"WA\"</span><span class=\"token punctuation\">,</span><span class=\"token string\">\"WA\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n                tags<span class=\"token operator\">$</span>option<span class=\"token punctuation\">(</span>value <span class=\"token operator\">=</span> <span class=\"token string\">\"OR\"</span><span class=\"token punctuation\">,</span><span class=\"token string\">\"OR\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n                tags<span class=\"token operator\">$</span>option<span class=\"token punctuation\">(</span>value <span class=\"token operator\">=</span> <span class=\"token string\">\"CA\"</span><span class=\"token punctuation\">,</span><span class=\"token string\">\"CA\"</span><span class=\"token punctuation\">)</span>\n            <span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n            \n            <span class=\"token comment\"># midwest</span>\n            tags<span class=\"token operator\">$</span>optgroup<span class=\"token punctuation\">(</span>label <span class=\"token operator\">=</span> <span class=\"token string\">\"Midwest\"</span><span class=\"token punctuation\">,</span>\n                tags<span class=\"token operator\">$</span>option<span class=\"token punctuation\">(</span>value <span class=\"token operator\">=</span> <span class=\"token string\">\"MN\"</span><span class=\"token punctuation\">,</span><span class=\"token string\">\"MN\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n                tags<span class=\"token operator\">$</span>option<span class=\"token punctuation\">(</span>value <span class=\"token operator\">=</span> <span class=\"token string\">\"WI\"</span><span class=\"token punctuation\">,</span><span class=\"token string\">\"WI\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">,</span>\n                tags<span class=\"token operator\">$</span>option<span class=\"token punctuation\">(</span>value <span class=\"token operator\">=</span> <span class=\"token string\">\"IA\"</span><span class=\"token punctuation\">,</span><span class=\"token string\">\"IA\"</span><span class=\"token punctuation\">)</span>\n            <span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">)</span></code></pre></div>\n<p>That's it! Start the app and take a look. </p>\n<p>It's kind of boring isn't it? Let's start adding our styles.</p>\n<p><span id=\"work-css\"></span></p>\n<h3>Adding CSS</h3>\n<p>In order to add styling to the input, we will use the css property <code>appearance</code> and set it to <code>none</code>. (webkit and moz are <a href=\"https://developer.mozilla.org/en-US/docs/Glossary/Vendor_Prefix\">vender prefixes</a> for other browsers). I set the width of the input to 90% of the parent element (sidebarPanel). This will allow the input to be resized accordingly when the browser dimensions are changed (resized). Again, this is still a bit experimental so use with caution.</p>\n<p>The rest of the styles are used for giving the input some more space (<a href=\"https://developer.mozilla.org/en-US/docs/Web/CSS/padding\">padding</a>), setting the <a href=\"https://developer.mozilla.org/en-US/docs/Web/CSS/font-size\">font size</a> and <a href=\"https://developer.mozilla.org/en-US/docs/Web/CSS/color\">color</a>, removing rounded corners with <a href=\"https://developer.mozilla.org/en-US/docs/Web/CSS/border-radius\">border radius</a>, and defining the <a href=\"https://developer.mozilla.org/en-US/docs/Web/CSS/border\">border</a> style. </p>\n<p>Since, the property <code>appearance: none</code> removes all styles of our input, it will also remove the dropdown icon (which is one on the reasons you shouldn't really override input styles as you are removing browser defaults). However, you can use your own icon as a replacement and set it as a background element.</p>\n<p>In the file <code>www/css/menu-chevron-down.svg</code>, I wrote an svg icon that can be used as a replacement. We will load it in using the <a href=\"https://developer.mozilla.org/en-US/docs/Web/CSS/background\">background</a> property. We will position the location of the icon to the right hand side and somewhat in the middle of our input using the <a href=\"https://developer.mozilla.org/en-US/docs/Web/CSS/background-position\">background-position</a> property. We will also specify that we will only want to render the icon once (hence the use of <code>no-repeat</code>; <a href=\"https://developer.mozilla.org/en-US/docs/Web/CSS/background-repeat\">background-repeat</a>).</p>\n<p>Here's the markup. </p>\n<div class=\"gatsby-highlight\" data-language=\"css\"><pre class=\"language-css\"><code class=\"language-css\"><span class=\"token comment\">/* primary styling for &lt;select> + manually add menu button */</span>\n<span class=\"token selector\">#state</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token property\">-webkit-appearance</span><span class=\"token punctuation\">:</span>none<span class=\"token punctuation\">;</span>\n    <span class=\"token property\">-moz-appearance</span><span class=\"token punctuation\">:</span>none<span class=\"token punctuation\">;</span>\n    <span class=\"token property\">appearance</span><span class=\"token punctuation\">:</span>none<span class=\"token punctuation\">;</span>\n    <span class=\"token property\">width</span><span class=\"token punctuation\">:</span> 90%<span class=\"token punctuation\">;</span>\n    <span class=\"token property\">padding</span><span class=\"token punctuation\">:</span> 15px<span class=\"token punctuation\">;</span>\n    <span class=\"token property\">font-size</span><span class=\"token punctuation\">:</span> 14pt<span class=\"token punctuation\">;</span>\n    <span class=\"token property\">border-radius</span><span class=\"token punctuation\">:</span> 0<span class=\"token punctuation\">;</span>\n    <span class=\"token property\">outline</span><span class=\"token punctuation\">:</span> none<span class=\"token punctuation\">;</span>\n    <span class=\"token property\">border</span><span class=\"token punctuation\">:</span> 2px solid #3A506B<span class=\"token punctuation\">;</span>\n    <span class=\"token property\">color</span><span class=\"token punctuation\">:</span> #3A506B<span class=\"token punctuation\">;</span>\n    <span class=\"token property\">background</span><span class=\"token punctuation\">:</span> <span class=\"token url\"><span class=\"token function\">url</span><span class=\"token punctuation\">(</span>menu-chevron-down.svg<span class=\"token punctuation\">)</span></span><span class=\"token punctuation\">;</span>\n    <span class=\"token property\">background-repeat</span><span class=\"token punctuation\">:</span> no-repeat<span class=\"token punctuation\">;</span>\n    <span class=\"token property\">background-position</span><span class=\"token punctuation\">:</span> 95% 20px<span class=\"token punctuation\">;</span>\n    <span class=\"token property\">background-color</span><span class=\"token punctuation\">:</span> white<span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Since the element we want to style the select input. We can select the element in our css file a couple of ways. In the above code, we are using the id selector. In our ui, we applied the id <code>state</code> to our input element. In css, we can reference that id using <code>#</code> + <code>name</code> (or <code>#state</code>). There are many other types of css selectors that your can use and you can even combine them as well. See the <a href=\"https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors\">CSS Selectors</a> for more information and examples.</p>\n<p>Now, let's revisit the second part of the question. </p>\n<blockquote>\n<p>For example, I want the background of the first option to be green; is there a way to do it?</p>\n</blockquote>\n<p>For the first <code>&#x3C;option></code> in each <code>&#x3C;optgroup></code>, set the background to green. For setting the background color, I used descendent selector paths to select the first element in each group.</p>\n<div class=\"gatsby-highlight\" data-language=\"css\"><pre class=\"language-css\"><code class=\"language-css\"><span class=\"token selector\">#state optgroup option:nth-child(1)</span><span class=\"token punctuation\">{</span>\n    <span class=\"token property\">background-color</span><span class=\"token punctuation\">:</span> #31E981 <span class=\"token important\">!important</span><span class=\"token punctuation\">;</span>\n    <span class=\"token property\">color</span><span class=\"token punctuation\">:</span> white<span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>This says within the element with the id of state select all option groups, and then select the first child in each group. If you wanted the second item in each group, it would be <code>:nth-child(2)</code>. For the third, it would <code>:nth-child(3)</code> and so on.</p>\n<p>Save the css file and refresh the app. How does it look? Nice, right?</p>\n<p><span id=\"further-thoughts\"></span></p>\n<h2>Futher Thoughts</h2>\n<p>Before you begin, I would use caution in using this approach as overriding browser defaults can cause things to break or not function properly. This can also result in inconsistencies across browsers and devices. Check site such as caniuse.com for browser support before implementing into your production app.</p>\n<p>At the time of writing this post, the browsers I tested the app (Safari, Chrome and FireFox) with rendered the select input the same. However, the differences occurred when the select input was opened. FireFox allowed the background color of the first option to be set whereas Safari and Chrome did not. </p>\n<p>This is due to the complex structure of the select input element and the behaviors that are associated with it. If it were another \"easier\" element, you wouldn't have much of an issue. However, if you want the base structure of the select input to follow a certain look, then this is for you. </p>\n<p>In a future tutorial, I will cover another way to create and style this element so check back soon!</p>\n<p><span id=\"run\"></span></p>\n<h2>How do I run this example?</h2>\n<p>You can run this demo by cloning the <a href=\"https://github.com/davidruvolo51/shinyAppTutorials\">github repository</a> and opening the Rproject file in <code>select_input_styling</code> directory. Alternatively, you can run the app through the console using:</p>\n<div class=\"gatsby-highlight\" data-language=\"r\"><pre class=\"language-r\"><code class=\"language-r\">install.packages<span class=\"token punctuation\">(</span>shiny<span class=\"token punctuation\">)</span>\nshiny<span class=\"token operator\">::</span>runGithub<span class=\"token punctuation\">(</span>repo<span class=\"token operator\">=</span><span class=\"token string\">\"shinyAppTutorials\"</span><span class=\"token punctuation\">,</span> username<span class=\"token operator\">=</span><span class=\"token string\">\"davidruvolo51\"</span><span class=\"token punctuation\">,</span> subdir<span class=\"token operator\">=</span><span class=\"token string\">\"select_input_styling\"</span><span class=\"token punctuation\">)</span></code></pre></div>","frontmatter":{"title":"Custom Select Inputs","subtitle":"Creating your own select inputs with style","abstract":"For some apps, you may need to style the selectInput widget. Styling the element can be tricky. Learn how to use standard html elements and css to create your own input.","date":"2019-11-04","updated":"2020-04-23","keywords":["css","html"]},"fields":{"readingTime":{"minutes":9.215}}}},"pageContext":{"slug":"/tutorials/select-input-styling/"}},"staticQueryHashes":["63159454"]}