Using CSS to customize the dropdown text styling of a select box within a custom wrapper

I have a specific customized dropdown menu using the styled-select wrapper:

HTML:

<div class="styled-select">
<select name="example_length" aria-controls="example" class="">
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
</div>

CSS:

.styled-select {
      background: url(http://i62.tinypic.com/15xvbd5.png) no-repeat 84% 0;
      height: 29px;
      overflow: hidden;
      width: 70px;
      -webkit-border-radius: 5px;
      -moz-border-radius: 5px;
      border-radius: 5px;
      background-color: #1abc9c;
      display: inline-block;
      vertical-align: middle;
    }

 .styled-select select {
       background: transparent;
       border: none;
       font-size: 14px;
       color: #000;
       height: 29px;
       padding: 5px; 
       width: 90px;
    }

Link to JsFiddle

I am attempting to change the color of the selected value in the dropdown menu (10 is initially shown as default) to white, while keeping other items in the dropdown dark.

To achieve this effect, I changed the color to white in the select css, but it also made other items in the dropdown white, making them invisible when placed on a white background. If I change the background of the dropdown to a darker color, it overrides the color of the styled select box.

Any assistance would be greatly appreciated!

Answer №1

To achieve this effect in your CSS, you can include the following code:

option:not(:checked) {
  color: #000;
}

Additionally, update the default color of .styled-select select to color:#fff. This will ensure that unselected options appear with black text while the selected option displays white text.

Learn more about the :not() selector at this link (w3schools)

For further information on the :checked selector, visit this page (w3schools)

I hope this explanation helps!

Answer №2

Do you think a solution like this could work for your needs? You might want to consider incorporating some jQuery for added functionality:

<div class="styled-select">
  <select id="source" name="example_length" aria-controls="example">
    <option selected="selected" value="10">10</option>
    <option value="25">25</option>
    <option value="50">50</option>
    <option value="100">100</option>
  </select>

Here's an example of using jQuery to style and create a dropdown effect:

    $(document).ready(function() {
   createDropDown();

   $(".dropdown dt a").click(function() {
     $(".dropdown dd ul").toggle();
   });

   $(document).bind('click', function(e) {
     var $clicked = $(e.target);
     if (!$clicked.parents().hasClass("dropdown"))
       $(".dropdown dd ul").hide();
   });

   $(".dropdown dd ul li a").click(function() {
     var text = $(this).html();
     $(".dropdown dt a").html(text);
     $(".dropdown dd ul").hide();

     var source = $("#source");
     source.val($(this).find("span.value").html())
   });
 });

 function createDropDown() {
   var source = $("#source");
   var selected = source.find("option[selected]");
   var options = $("option", source);

   $("body").append('<dl id="target" class="dropdown"></dl>')
   $("#target").append('<dt><a href="#">' + selected.text() +
     '<span class="value">' + selected.val() +
     '</span></a></dt>')
   $("#target").append('<dd><ul></ul></dd>')

   options.each(function() {
     $("#target dd ul").append('<li><a href="#">' +
       $(this).text() + '<span class="value">' +
       $(this).val() + '</span></a></li>');
   });
 }

Take a look at the live demo here: http://jsfiddle.net/kybernaut/bbnso2og/4/

If you're interested in more detailed information and tutorials, check out this resource:

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

Select a random class from an array of classes in JavaScript

I have a collection of Classes: possibleEnemies: [ Slime, (currently only one available) ], I am trying to randomly pick one of them and assign it to a variable like this (all classes are derived from the Enemy class): this.enemy = new this.possibleEn ...

How can I add information into a nested div element?

I encountered an issue when attempting to insert data into my block. The div structure is as follows: <div class="1"> <div class="2"> <div class="3"> <div class="4"></div> </div> ...

Utilizing Beautifulsoup to extract elements from Selenium

When utilizing BeautifulSoup with Selenium, I usually start by parsing the entire page using soup = BeautifulSoup(driver.page_source). But what if I only want to parse a specific element from Selenium in BeautifulSoup? If you try the following code snipp ...

Loading screen for specific content within the current WordPress theme

I am trying to display a preloader only in the 'content' div, but it ends up hiding the entire page. The structure of the site is as follows: Title Menu Content (where I want the preloader) Footer I'm having trouble figuring out where exa ...

Tips for instructing Vimeo on the recommended video dimensions for responsive delivery

Embedding Vimeo's iframe in a responsive way is quite straightforward, allowing the player to adjust its size accordingly. However, the real question is: Does Vimeo actually deliver the video in the correct size? In the past, I mistakenly assumed th ...

There seems to be an error with JVectorMap: the <g> attribute transform is expecting a number but is instead receiving "scale(NaN) translate(N..."

Hey there! I'm currently working on creating a form with a map to select the country of birth using JVectorMap. However, when checking the Google Chrome developer console, I encountered the following error message: jquery-jvectormap-2.0.5.min.js:1 Err ...

The correlation between dynamic pricing and surge pricing in relation to PX

I'm exploring the translation of measurements from dp and sp to pixels within Google's new material design for a website I'm working on. Usually, my web designs have used pixel-based measurements for both grid and font sizing over the four y ...

Preserving chosen options in a dropdown menu using PHP and HTML

I'm facing an issue with my code where the selected values in a dropdown menu revert back to default every time the page refreshes. How can I ensure that the selected values remain unchanged? Unfortunately, I am unable to utilize AJAX as per the user ...

HTML: Issue with H1 alignment in class

I'm a beginner with HTML and I've been attempting to center the H1 using a CSS class, but it doesn't seem to be working for me. Here's my code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8> ...

showcasing the picture in the email is consistently

I have created an email system that sends a message to users when they are active in the database, but I am experiencing an issue with the image not displaying. It is important for the image to appear automatically, just like Facebook's logo always sh ...

Enhancing the appearance of the active menu item with HTML/CSS/JS/PHP

Here's the HTML code I have: <ul class="navigation"> <li><a href="index.php">Home</a></li> <li><a href="index.php?content=about">About us</a></li> </ul> As you can see, the content of the " ...

Send the image link as a parameter

I've been trying to pass an image link through two child components, but I'm having trouble. I added the link to the state and passed it down, but it doesn't work. Strangely, when I manually input the link in the child component, it works pe ...

Having trouble bringing in SVG file into my React project

I am currently using React version 15.4.1 and I have tried implementing a solution from this link. However, instead of the component being rendered, I only see a string like this https://localhost:3001/dist/7553ed8d42edb0d73754cd9a5dea2168.svg This is how ...

JS Unable to get scrollIntoView function to work with mousewheel event

I have been working with the script provided above. It correctly displays the id in the browser console, but unfortunately, it is not triggering the scrolling function as expected. var divID = null; var up = null; var down = null; function div(x) { s ...

The show more/show less link for a long jQuery paragraph is malfunctioning

I encountered an issue while coding where the "read more" link works correctly, but the "show less" link does not. Despite my efforts, I cannot seem to identify the error. Within this code snippet, there is an anchor tag with class="show-less" that I am u ...

Adding HTML components to an image with adjustable dimensions

A graphic designer has provided us with an image featuring three selection boxes. I implemented the necessary HTML elements and CSS to display three overlapped selection boxes on top of the image, using pixel values for positioning. However, the original ...

What is the best way to store items in localStorage within Angular version 4.4.6?

I have been working on implementing a basic authentication system in Angular 4.4 with MongoDB as the backend database. login.component.ts import { Component, OnInit } from '@angular/core'; import { AuthService } from 'app/services/auth.ser ...

Activate the initial tab in JQuery UI accordion upon initialization

Hello, I have implemented a simple sidenav menu on my website. It consists of parent items and child items structured according to the h3 > div format as suggested by JQuery's documentation. My challenge now is to automatically open the "active" t ...

Incorporating input fields into an HTML form

I'm looking to enhance my form by adding input fields dynamically through the JavaScript function when I click on the add button: let i = 0; function increment() { i += 1; } function addFieldFunction() { let newDiv = document.createElement(&apos ...

Adding an image to an HTML page

I am working on an HTML page and need to insert an image. Here is the code I currently have: <img src="../Images/logo.jpg" alt="Logo" height="50"> I chose "../Images/logo.jpg" as the source because my logo.jpg is located two levels up from the curr ...