Using the Chrome browser, I implemented a dropdown menu in HTML that includes custom background colors for each option

Struggling to create a dropdown list for users to choose colors with each option displaying the selected color as its background. To showcase this concept, I've put together a simple HTML page:

<!DOCTYPE html>
<html>
<head>

    <style media="screen" type="text/css">
        .white  {background-color:white;}
        .red    {background-color:red;}
        .yellow {background-color:yellow;}
        .green  {background-color:green;}
    </style>

</head>
<body>

    <h2>Choose Color</h2>
    <select>
        <option class="white">This should have a WHITE background</option>
        <option class="red">This should have a RED background</option>
        <option class="yellow">This should have a YELLOW background</option>
        <option class="green">This should have a GREEN background</option>
    </select>

</body>
</html>

Viewing this on Firefox in either PC or Mac looks fantastic, but Chrome on Mac fails to display the background colors properly. Check out the screenshot after it's rendered in Chrome and I click on the select object:

https://i.sstatic.net/GwPoF.png

The list appears usual with gray backgrounds instead of the designated colors, despite having the correct classes assigned. Though I've referenced various resources on this topic like links, none seem to solve my problem.

Where could I be going wrong? Appreciate any help!

Answer №1

There seems to be an issue with the styling options across various operating systems. Have you considered utilizing a plugin such as bootstrap-select? Utilizing this for the li element should effectively achieve your desired outcome.

Answer №2

The solution presented in the hyperlink you shared involves utilizing JavaScript and CSS adjustments, which appears to be functioning correctly on my device:

<!DOCTYPE html>
<html>
<head>

    <style media="screen" type="text/css">
        .white  {background-color:white;color:red;}
        .red    {background-color:red;color:black;}
        .yellow {background-color:yellow;color:black;}
        .green  {background-color:green;color:white;}
    </style>


</head>
<body>

    <h2>Choose Color</h2>
    <select>
        <option class="white">This should have a WHITE background</option>
        <option class="red">This should have a RED background</option>
        <option class="yellow">This should have a YELLOW background</option>
        <option class="green">This should have a GREEN background</option>
    </select>

    <script>
        var select = document.querySelector('select');
        var colorChange = function(){
            var color = select.options[select.selectedIndex].className;
            select.className = color;
            select.blur();
        };
        select.addEventListener('change', colorChange);
        colorChange();
    </script>

</body>
</html>

Furthermore, I included the color attribute within certain classes to demonstrate that additional styling elements can be modified apart from just the background color.

Answer №3

@Randal Make sure to run the code snippet below to confirm that your code is functioning correctly.

<head>

    <style media="screen" type="text/css">
        .white  {background-color:white;}
        .red    {background-color:red;}
        .yellow {background-color:yellow;}
        .green  {background-color:green;}
    </style>
<body>

    <h2>Choose Color</h2>
    <select>
        <option class="white">This should have a WHITE background</option>
        <option class="red">This should have a RED background</option>
        <option class="yellow">This should have a YELLOW background</option>
        <option class="green">This should have a GREEN background</option>
    </select>

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

In search of a CSS selector that can target elements based on specific text contained within them

Query: <div class="btn btn-second-in-pair-not-desired btn-tall">Clear search</div> <div class="btn btn-second-in-pair-not-desired btn-tall">Raw Search</div> <div class="btn btn-second-in-pair-not-desired btn-tall">Basic Searc ...

Whenever I move my cursor over the NavList, the bottom list expands in width and pushes the top list downward

I am facing an issue with the width of the ul elements in my navigation bar. The first ul has a width equal to the width of the words plus a margin-right of 50px, while the bottom ul has a fixed width of 200px. The problem arises when the top ul's wid ...

JavaScript isn't cooperating with a straightforward .click() command

Having some trouble clicking a div using JavaScript in my project. I am able to utilize jQuery, and have tried selecting the element with QuerySelector as well as XPath, followed by utilizing .click() on the element. However, it seems that the div is not b ...

Combine floating left with a centered element

I am trying to figure out how to center the second element in a div without knowing the width of the first one. My aim is to have "foo" align to the left and "bar" centered within the original div. I have managed to achieve this by setting a static width ...

Is it possible to adjust the scroll top position using inline style in angularJS/CSS?

I am working on storing the scrollTop value of a vertical menu in my controller so that I can set it up each time I return to the page for persistent scrolling. Does anyone know how I can achieve this? This is the code snippet I am currently using: < ...

Is there a way to use CSS to prevent a user from being able to click on a table?

My CSS table appears standard: <table class="form grid"> <thead> <tr> <th>EId</th> <th>Number</th> ...

"Accessing the HTMLDivElement object through document.getElementById will result in [object HTMLDivElement]

When inserting this code snippet into my HTML div: <script> document.getElementById("mArray").appendChild = '<%= mArray %>' console.log('<%= mArray %>') </script> <div id="mArray&qu ...

Ways of distinguishing the variance between vertical align: -0.125em and vertical align: middle explained

Upon coming across the code vertical-align: -0.125em;, I begin to ponder about the contrasts between this and vertical-align: middle. ...

Having trouble getting StencilJS Bottomsheet to work properly? Looking for a way to smoothly slide up your content?

I've been working on creating a Bottomsheet in Stencil, but I'm facing an issue where it shows up suddenly when activated. My goal is to display the overlay when the active property is set, and then smoothly slide up the content. Below is my comp ...

I'm looking to position the vibrant red box at the bottom of the screen, right under the navigation bar. However, every time I try to

I'm struggling to align the full-screen red box under the navigation bar. I've tried using flex properties but it's not working as expected. My goal is to have the red box at 100% width and be responsive. Could it be something with my nav b ...

What is the proper jQuery traversal method to display a single templated element?

Utilizing handlebars as the view engine for node.js, the subsequent html content is dynamically produced and repeated: http://jsfiddle.net/4Q5PE/ <div class="btnsContainer"> <div class="btn-group" data-toggle="buttons"> <label cla ...

There is a noticeable gap at the top of the scroll area in my Bootstrap modal

Hello everyone, I have encountered an issue with the modals on my website where the scroll area does not fit the page properly. Please refer to the screenshot below for reference. I have not applied any extra styles to the modals and I am unable to pinpo ...

The command `python3 manage.py findstatic` is unable to locate the static file

I have been struggling to incorporate static files into my project, but the CSS is not loading. I tried using findstatic but Django is unable to locate any static directory: https://i.sstatic.net/jRgzx.png https://i.sstatic.net/7VG0x.png In a different pr ...

JavaScript XMLHttpRequest is always essential

I am currently working on a chat application that needs to constantly receive server information. After the request finishes, I have added an additional call to the function within the http.onreadystatechange=function() like this: request(); This sets ev ...

Having trouble with Python Bottle <br /> not functioning properly?

I encountered a perplexing issue and I am unsure of how to resolve it: There is a text area where users can input their text. For example, let's say the following is entered : This is the user's text After storing this in my database and ret ...

Looking for assistance with PHP if statement and troubleshooting iFrame issues

Using the PHP if statement below on my website: <?php if(empty($_GET['tid'])) echo '<iframe src="http://tracking.mydomain.com/aff_goal?a=33&goal_id=47" scrolling="no" frameborder="0" width="1" height="1"></iframe>'; ...

How to create a full-page image background using Bootstrap 4?

I am struggling to implement a full-page image background using Bootstrap 4, but unfortunately it's not working and I am unsure how to troubleshoot this issue. Despite conducting extensive research online, I have been unable to find a solution that ad ...

What is the predefined value for a multi-select generated by the ng-for directive in Angular?

I am having trouble setting default selected values for the multi-select. Despite trying various methods such as initializing the ngModel to bind the variable and using [selected] = "selectedSegment == 'S1'", none of them seem to be effective fo ...

"Learn the best way to showcase images within Pusher's real-time chat application

Having trouble displaying images in real-time chat using Pusher. I am storing the images as BLOB but cannot display them on the client side using JavaScript. When the person who sends the message types, the image shows up fine, but on the receiver's s ...

Using JavaScript to implement responsive design through media queries

The code seems to be having some issues as it only works when I reload the page. I am looking to display certain code if "size < 700" and other code if "size > 699". I also tried this code from here: <script> function myFunction(x) { if ( ...