How to remove the arrow from a select element in Firefox

后端 未结 30 1275
北恋
北恋 2020-11-22 15:58

I\'m trying to style a select element using CSS3. I\'m getting the results I desire in WebKit (Chrome / Safari), but Firefox isn\'t playing nicely (I\'m not ev

相关标签:
30条回答
  • 2020-11-22 16:29

    Use the pointer-events property.

    The idea here is to overlay an element over the native drop down arrow (to create our custom one) and then disallow pointer events on it. [see this post]

    Here is a working FIDDLE using this method.

    Also, in this SO answer I discussed this and another method in greater detail.

    0 讨论(0)
  • 2020-11-22 16:30

    You could increase the width of the box and move the arrow closer to the left of the arrow. this then allows you to cover the arrow with an empty white div.

    Have a look: http://jsbin.com/aniyu4/86/edit

    0 讨论(0)
  • 2020-11-22 16:32

    Further to Joao Cunha's answer, this problem is now on Mozilla's ToDo List and is targeted for ver 35.

    For those desiring, here is a workaround by Todd Parker, referenced on Cunha's blog, that works today:

    http://jsfiddle.net/xvushd7x/

    HTML:

    <label class="wrapper">This label wraps the select
        <div class="button custom-select ff-hack">
            <select>
                <option>Apples</option>
                <option>Bananas</option>
                <option>Grapes</option>
                <option>Oranges</option>
                <option>A very long option name to test wrapping</option>
            </select>
        </div>
    </label>
    

    CSS:

    /* Label styles: style as needed */
    label {
      display:block;
      margin-top:2em;
      font-size: 0.9em;
      color:#777;
    }
    
    /* Container used for styling the custom select, the buttom class below adds the bg gradient, corners, etc. */
    .custom-select {
      position: relative;
      display:block;
      margin-top:0.5em;
      padding:0;
    }
    
    /* These are the "theme" styles for our button applied via separate button class, style as you like */
    .button {
      border: 1px solid #bbb;
      border-radius: .3em;
      box-shadow: 0 1px 0 1px rgba(0,0,0,.04);
      background: #f3f3f3; /* Old browsers */
      background: -moz-linear-gradient(top, #ffffff 0%, #e5e5e5 100%); /* FF3.6+ */
      background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffffff), color-stop(100%,#e5e5e5)); /* Chrome,Safari4+ */
      background: -webkit-linear-gradient(top, #ffffff 0%,#e5e5e5 100%); /* Chrome10+,Safari5.1+ */
      background: -o-linear-gradient(top, #ffffff 0%,#e5e5e5 100%); /* Opera 11.10+ */
      background: -ms-linear-gradient(top, #ffffff 0%,#e5e5e5 100%); /* IE10+ */
      background: linear-gradient(to bottom, #ffffff 0%,#e5e5e5 100%); /* W3C */
    }
    
    /* This is the native select, we're making everything but the text invisible so we can see the button styles in the wrapper */
    .custom-select select {
      width:100%;
      margin:0;
      background:none;
      border: 1px solid transparent;
      outline: none;
      /* Prefixed box-sizing rules necessary for older browsers */
      -webkit-box-sizing: border-box;
      -moz-box-sizing: border-box;
      box-sizing: border-box;
      /* Remove select styling */
      appearance: none;
      -webkit-appearance: none;
      /* Font size must the 16px or larger to prevent iOS page zoom on focus */
      font-size:16px;
      /* General select styles: change as needed */
      font-family: helvetica, sans-serif;
      font-weight: bold;
      color: #444;
      padding: .6em 1.9em .5em .8em;
      line-height:1.3;
    }
    
    
    /* Custom arrow sits on top of the select - could be an image, SVG, icon font, etc. or the arrow could just baked into the bg image on the select. Note this si a 2x image so it will look bad in browsers that don't support background-size. In production, you'd handle this resolution switch via media query but this is a demo. */
    
    .custom-select::after {
      content: "";
      position: absolute;
      width: 9px;
      height: 8px;
      top: 50%;
      right: 1em;
      margin-top:-4px;
      background-image: url(http://filamentgroup.com/files/select-arrow.png);
      background-repeat: no-repeat;
      background-size: 100%;
      z-index: 2;
      /* These hacks make the select behind the arrow clickable in some browsers */
      pointer-events:none;
    }
    
    
    /* Hover style */
    .custom-select:hover {
      border:1px solid #888;
    }
    
    /* Focus style */
    .custom-select select:focus {
      outline:none;
      box-shadow: 0 0 1px 3px rgba(180,222,250, 1);
      background-color:transparent;
      color: #222;
      border:1px solid #aaa;
    }
    
    /* Set options to normal weight */
    .custom-select option {
      font-weight:normal;
    }
    
    0 讨论(0)
  • 2020-11-22 16:33

    Update: this was fixed in Firefox v35. See the full gist for details.


    Just figured out how to remove the select arrow from Firefox. The trick is to use a mix of -prefix-appearance, text-indent and text-overflow. It is pure CSS and requires no extra markup.

    select {
        -moz-appearance: none;
        text-indent: 0.01px;
        text-overflow: '';
    }
    

    Tested on Windows 8, Ubuntu and Mac, latest versions of Firefox.

    Live example: http://jsfiddle.net/joaocunha/RUEbp/1/

    More on the subject: https://gist.github.com/joaocunha/6273016

    0 讨论(0)
  • 2020-11-22 16:33

    I know this question is a bit old, but since it turns up on google, and this is a "new" solution:

    appearance: normal Seems to work fine in Firefox for me (version 5 now). but not in Opera and IE8/9

    As a workaround for Opera and IE9, I used the :before pseudoselector to create a new white box and put that on top of the arrow.

    Unfortunately, In IE8 this doesn't work. The box is rendered correctly, but the arrow just sticks out anyway... :-/

    Using select:before works fine in Opera, but not in IE. If I look at the developer tools, I see it is reading the rules correctly, and then just ignores them (they're crossed out). So I use a <span class="selectwrap"> around the actual <select>.

    select {
      -webkit-appearance: normal;
      -moz-appearance: normal;
      appearance: normal;
    }
    .selectwrap { position: relative; }
    .selectwrap:before {
      content: "";
      height: 0;
      width: 0;
      border: .9em solid red;
      background-color: red;
      position: absolute;
      right: -.1em;
      z-index: 42;
    }
    

    You may need to tweak this a bit, but this works for me!

    Disclaimer: I'm using this to get a good looking hardcopy of a webpage with forms so I don't need to create a second page. I'm not a 1337 haxx0r who wants red scrollbars, <marquee> tags, and whatnot :-) Please do not apply excessive styling to forms unless you have a very good reason.

    0 讨论(0)
  • 2020-11-22 16:33

    If you don't mind fiddling with JS, I wrote a small jQuery plugin that helps you do it. With it you don't need to worry about vendor prefixes.

     $.fn.magicSelectBox = function() {
      var $e = this;
    
      $e.each(function() {
        var $select = $(this);
    
        var $magicbox = $('<div></div>').attr('class', $select.attr('class')).attr('style', $select.attr('style')).addClass('magicbox');
        var $innermagicbox = $('<div></div>').css({
          position: 'relative',
          height: '100%'
        });
        var $text = $('<span></span>').css({
          position: 'absolute'
        }).text($select.find("option:selected").text());
    
        $select.attr('class', null).css({
          width: '100%',
          height: '100%',
          opacity: 0,
          position: 'absolute'
        }).on('change', function() {
          $text.text($select.find("option:selected").text());
        });
    
        $select.parent().append($magicbox);
        $innermagicbox.append($text, $select);
        $magicbox.append($innermagicbox);
      });
    
      return $e;
    };
    

    Fiddle here: JS Fiddle

    The condition is that you have to style the select from scratch (this means setting the background and border), but you probably want to do this anyway.

    Also since the function substitutes the original select with a div, you will lose any styling done directly on the select selector in your CSS. So give the select element a class and style the class.

    Supports most modern browsers, if you want to target older browsers, you can try an older version of jQuery, but perhaps have to replace on() with bind() in the function (not tested)

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