Customizing the appearance of arrows in a drop-down menu option

I've been working on a dropdown menu box and an arrow, and for the most part it's looking good. However, I've run into an issue with the arrow icon not changing color when the user is focused on it. I'm using the :focus pseudo-class to turn the box green when selected, but the arrow isn't following suit. Can anyone provide some guidance on how to address this? Any help would be greatly appreciated.

I've tried searching for a solution online, but I haven't been able to fix the problem.

input[type=text],
input[type=tel],
input[type=email] {
  width: 100%;
  padding: 5px 5px;
  height: 40px;
  box-sizing: border-box;
  border: 2px solid #A9A9A9;
}


/* user input and dropdown turning to green color when active */

input[type=text]:focus,
input[type=tel]:focus,
input[type=email]:focus,
select:focus,
textarea:focus {
  width: 100%;
  padding: 5px 5px;
  height: 40px;
  box-sizing: border-box;
  border: 2px solid #42AC82;
}


/* dropdown box styling */

select {
  width: 100%;
  padding: 5px 5px;
  height: 40px;
  box-sizing: border-box;
  border: 2px solid #A9A9A9;
  -moz-appearance: none;
  -webkit-appearance: none;
  appearance: none;
}

select::-ms-expand {
  display: none;
}

.select-container {
  position: relative;
}

select:-moz-focusring {
  color: transparent;
  text-shadow: 0 0 0 #000;
}

.select-arrow {
  color: #A9A9A9;
  right: 0px;
  top: 7px;
  width: 30px;
  position: absolute;
  display: block;
  z-index: 10;
  margin: 0 0 0 0;
  pointer-events: none;
}

select:required:invalid {
  color: gray;
}

select[value=""][disabled] {
  display: none;
}
<div class='grid'>
  <div class='col col-1-of-4'>Is decision maker?<br>
    <div class="select-container">
      <span class="select-arrow fa fa-caret-down" style="font- 
              size: 1.5em;"></span>
      <select name="options" required>
        <option value="" disabled selected>Select an option</option>
        <option value="1">yes</option>
        <option value="2">no</option>
      </select>
    </div>
  </div>
  </div

Answer №1

The issue with the arrow color not changing is due to the arrow icon not being a child or sibling of the select element, and there is no CSS selector for targeting previous or parent elements. You have a couple of options to address this:

1) Move the arrow icon after the select element:

          <select name="options" required>
             <option value="" disabled selected>Select an 
             option</option>
             <option value="1">yes</option>
             <option value="2">no</option>
          </select>
          <span class="select-arrow fa fa-caret-down" style="font- 
      size: 1.5em;"></span>

This will allow you to use the + (next sibling) selector

select:focus + .select arrow { color: #42AC82 }

2) Use JavaScript to detect when the select element is focused and toggle a class on the select-container element, which will enable you to style all child elements as needed

Answer №2

Consider enhancing your CSS with the following styles:

input[type=text]:focus ~ span,
input[type=tel]:focus ~ span,
input[type=email]:focus ~ span,
select:focus ~ span,
textarea:focus ~ span {
    color: red;
}


You can also position your span with an icon after the select tag like this: 



    <select name="options" required>
         <option value="" disabled selected>Select an option</option>
         <option value="1">yes</option>                     
         <option value="2">no</option>
         </select>
         <span class="select-arrow fa fa-caret-down" style="font-size: 1.5em;">
       </span>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

jQuery animation that smoothly fades out from the left and fades in from the right

This survey is designed to assist individuals in determining where they should go with a specific type of ticket. Currently, everything is functioning smoothly, but I would like to add a sleek animation to each ul transition. Whenever a button is clicked ...

Tips for modifying hover effects using jQuerystylesheet changes

I attempted running the following code snippet var buttonElement = $('<button>Button</button>'); $('body').append(buttonElement); buttonElement.hover().css('background-color', 'red'); Unfortunately, the ...

What is the best way to create a CSS class for a list element in React?

I am facing an issue with styling buttons in my UI. These buttons represent different domains and are dynamically generated based on data fetched from the server using the componentDidMount() method. Since I do not know the quantity of buttons at the time ...

Ways to reduce the size of the background image in a select element?

Utilizing the bootstrap form-select select element : <select class="form-select listbox_length_menu_datatable" <option value='5'>5</option> <option value='10'>10</option> <option value ...

Having trouble importing Google Fonts using Styled Components and Next.js?

I encountered an issue while attempting to load Google Fonts. I came across a solution that suggests adding the following code snippet in _document.js to import it within a head tag: import React from 'react'; import Document, { Html, Head, Main, ...

"Maximizing battery life: Efficient video playback on iOS in low power

Summary: I am currently working on a website that features a video set as the background which autoplays. However, I have encountered an issue on iOS devices when "Low Power Mode" is activated - the video fails to play and instead displays a large "Play" i ...

Harness the power of JavaScript to generate a dynamic overlay with a see-through image that can be expanded

Within different sections of my website, we display banner ads that are loaded in real-time from third-party sources and come in various sizes. I'm interested in adding a transparent overlay image to each ad which would allow me to trigger a click ev ...

Adjust the input value with CSS when hovering

Whenever I click on a button, the name of that button is displayed on the site. However, I am looking to change this button name when hovering over it but I am not sure how to do it. echo '<form id="button" method="POST" >&ap ...

The CSS3 animations using transit do not occur at the same time in Internet Explorer and Firefox

I am currently developing a presentation library to simplify my work. Naturally, animations play a significant role in this project. The library leverages Transit for smooth CSS3 animations. One specific animation involves an element sliding into or out o ...

If the font size exceeds a certain value in SCSS, then apply a different value

Can SCSS handle this scenario? I am looking to set the font size to 22px in case it is greater than 30px, especially within a media query for a max width of 480px. ...

Send JavaScript variable using a query parameter

I am working on passing a JavaScript variable across all pages in my project. The goal is to calculate the total value from various pages and display it at the end of navigation. Below is an example of my JS code: <script> $(document).ready ...

The value within the style.setProperty function does not get applied

I have a progress bar that dynamically increases based on user input: <div class="progressBarContainer percentBar"> <div class="progressBarPercent" style="--width:${gPercent}" id="${gName}-pbar">< ...

What methods are available to generate dynamic shapes using HTML?

Looking to create an interactive triangle where users can move vertices or sides, updating angles in real-time. I'm struggling with how to accomplish this task. My initial attempt was to manually draw the triangle using the code below. <!DOCTYPE ht ...

Exploring the ins and outs of webpage loading speed

I am working on writing JavaScript code that includes a button to open a webpage of my choice. I now want to understand how to detect when the page I called is finished loading. Any suggestions or ideas on this topic? I apologize if my explanation was no ...

Equal size images displayed within cards in Material UI

Is there a way to create a list of Material UI components with images that have uniform height, even if the original images vary in size? I want to make all image heights responsive and consistent across all cards. Any suggestions on how to achieve this? ...

Is there a way to prevent the Material UI Chip component from appearing when there is no content present?

I have developed an application that pulls content from an API and displays it on the screen. Within this app, I have implemented a Material UI Card component to showcase the retrieved information, along with a MUI Chip component nested within the card. &l ...

Issue with the Styled Components Color Picker display

For the past 6 months, I have been using VSCode with React and Styled Components without any issues. However, recently I encountered a problem where the color picker would not show up when using CSS properties related to color. Usually, a quick reload or r ...

The columns in the table are all displaying varying widths even though they have been defined with fixed table layout

I have a nested table displayed in my HTML, and I am attempting to set each column to be 50% width, but for some reason it's not working. In the past, whenever I've needed to accomplish this, applying table-layout: fixed has usually done the tri ...

The divs contained are misaligned and not properly centered on the page

My container div contains 6 inner divs, but they are not properly centered on the page. Although the divs are equally spaced apart, they seem to be shifted to the left. I tried using 'justify-content: center' without success, and I suspect it mi ...

What is the best way to utilize a mask in an inline SVG that is compatible with Chrome browser?

I am looking to visually crop my element using an SVG shape that is defined within the same HTML file (inline SVG). Using clip-path works perfectly: div { width: 200px; height: 200px; background-color: red; clip-path: url("#c"); } <div> ...