How to style the option of an html “select” element?

后端 未结 18 2553
迷失自我
迷失自我 2020-11-21 05:09

Here\'s my HTML:


                        
    
提交评论

  • 2020-11-21 05:24

    Seems like I can just set the CSS for the select in Chrome directly. CSS and HTML code provided below :

    .boldoption {
    	font-weight: bold;
    }
    <select>
    	<option>Some normal-font option</option>
    	<option>Another normal-font option</option>
    	<option class="boldoption">Some bold option</option>
    </select>

    0 讨论(0)
  • 2020-11-21 05:24

    Actually you can add :before and :after and style those. At least it's something

    option{
        font-size:18px;
        background-color:#ffffff;
    }
    option:before{
        content: ">";
        font-size:20px;
        display:none;
        padding-right:10px;
        padding-left:5px;
        color:#fff;
    }
    option:hover:before{
        display:inline;
    }

    0 讨论(0)
  • 2020-11-21 05:25

    It's 2017 and it IS possible to target specific select options. In my project I have a table with a class="variations", and the select options are in the table cell td="value", and the select has an ID select#pa_color. The option element also has a class option="attached" (among other class tags). If a user is logged in as a wholesale customer, they can see all of the color options. But retail customers are not allowed to purchase 2 color options, so I've disabled them

    <option class="attached" disabled>color 1</option>
    <option class="attached" disabled>color 2</option>
    

    It took a little logic, but here is how I targeted the disabled select options.

    CSS

    table.variations td.value select#pa_color option.attached:disabled {
    display: none !important;
    }
    

    With that, my color options are only visible to wholesale customers.

    0 讨论(0)
  • 2020-11-21 05:25

    Is this what youre looking for? I did it with jQuery! :D

    Run Code Snippet :)

    $(".custom-select").each(function() {
        var classes = $(this).attr("class"),
            id      = $(this).attr("id"),
            name    = $(this).attr("name");
        var template =  '<div class="' + classes + '">';
        template += '<span class="custom-select-trigger">' + $(this).attr("placeholder") + '</span>';
        template += '<div class="custom-options">';
        $(this).find("option").each(function() {
            template += '<span class="custom-option ' + $(this).attr("class") + '" data-value="' + $(this).attr("value") + '">' + $(this).html() + '</span>';
        });
        template += '</div></div>';
    
        $(this).wrap('<div class="custom-select-wrapper"></div>');
        $(this).hide();
        $(this).after(template);
    });
    $(".custom-option:first-of-type").hover(function() {
        $(this).parents(".custom-options").addClass("option-hover");
    }, function() {
        $(this).parents(".custom-options").removeClass("option-hover");
    });
    $(".custom-select-trigger").on("click", function() {
        $('html').one('click',function() {
            $(".custom-select").removeClass("opened");
        });
        $(this).parents(".custom-select").toggleClass("opened");
        event.stopPropagation();
    });
    $(".custom-option").on("click", function() {
        $(this).parents(".custom-select-wrapper").find("select").val($(this).data("value"));
        $(this).parents(".custom-options").find(".custom-option").removeClass("selection");
        $(this).addClass("selection");
        $(this).parents(".custom-select").removeClass("opened");
        $(this).parents(".custom-select").find(".custom-select-trigger").text($(this).text());
    });
    body {
    font-family: 'Roboto', sans-serif;
    }
    
     .custom-select-wrapper {
            position: relative;
            display: inline-block;
            user-select: none;
        }
        .custom-select-wrapper select {
            display: none;
        }
        .custom-select {
            position: relative;
            display: inline-block;
        }
        .custom-select-trigger {
            position: relative;
            display: block;
            width: 170px;
            padding: 0 84px 0 22px;
            font-size: 19px;
            font-weight: 300;
            color: #5f5f5f;
            line-height: 50px;
            background: #EAEAEA;
            border-radius: 4px;
            cursor: pointer;
            margin-left:20px;
            border: 1px solid #5f5f5f;
            transition: all 0.3s;
        }
    
        .custom-select-trigger:hover {
            background-color: #d9d9d9;
            transition: all 0.3s;
        }
    
        .custom-select-trigger:after {
            position: absolute;
            display: block;
            content: '';
            width: 10px; height: 10px;
            top: 50%; right: 25px;
            margin-top: -3px;
            border-bottom: 1px solid #5f5f5f;
            border-right: 1px solid #5f5f5f;
            transform: rotate(45deg) translateY(-50%);
            transition: all 0.4s ease-in-out;
            transform-origin: 50% 0;
        }
        .custom-select.opened .custom-select-trigger:after {
            margin-top: 3px;
            transform: rotate(-135deg) translateY(-50%);
        }
        .custom-options {
            position: absolute;
            display: block;
            top: 100%; left: 0; right: 0;
            margin: 15px 0;
            border: 1px solid #b5b5b5;
            border-radius: 4px;
            box-sizing: border-box;
            box-shadow: 0 2px 1px rgba(0,0,0,.07);
            background: #fff;
            transition: all 0.4s ease-in-out;
            margin-left: 20px;
            opacity: 0;
            visibility: hidden;
            pointer-events: none;
            transform: translateY(-15px);
        }
        .custom-select.opened .custom-options {
            opacity: 1;
            visibility: visible;
            pointer-events: all;
            transform: translateY(0);
        }
        .custom-options:before {
            position: absolute;
            display: block;
            content: '';
            bottom: 100%; right: 25px;
            width: 7px; height: 7px;
            margin-bottom: -4px;
            border-top: 1px solid #b5b5b5;
            border-left: 1px solid #b5b5b5;
            background: #fff;
            transform: rotate(45deg);
            transition: all 0.4s ease-in-out;
        }
        .option-hover:before {
            background: #f9f9f9;
        }
        .custom-option {
            position: relative;
            display: block;
            padding: 0 22px;
            border-bottom: 1px solid #b5b5b5;
            font-size: 18px;
            font-weight: 600;
            color: #b5b5b5;
            line-height: 47px;
            cursor: pointer;
            transition: all 0.15s ease-in-out;
        }
        .custom-option:first-of-type {
            border-radius: 4px 4px 0 0;
        }
        .custom-option:last-of-type {
            border-bottom: 0;
            border-radius: 0 0 4px 4px;
        }
        .custom-option:hover,
        .custom-option.selection {
            background: #f2f0f0;
        }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <select name="sources" id="sources" class="custom-select sources" placeholder="My Categories">
        <option value="categoryOne">Category 1</option>
        <option value="categoryTwo">Category 2</option>
        <option value="categoryThree">Category 3</option>
        <option value="categoryFour">Category 4</option>
    
    </select>

    0 讨论(0)
  • 2020-11-21 05:27

    There are only a few style attributes that can be applied to an <option> element.

    This is because this type of element is an example of a "replaced element". They are OS-dependent and are not part of the HTML/browser. It cannot be styled via CSS.

    There are replacement plug-ins/libraries that look like a <select> but are actually composed of regular HTML elements that CAN be styled.

    0 讨论(0)
  • 提交回复
    热议问题