How to remove the arrow from a select element in Firefox

后端 未结 30 1348
北恋
北恋 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:34

    Since Firefox 35, "-moz-appearance:none" that you already wrote in your code, finally remove arrow button as desired.

    It was a bug solved since that version.

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

    Okay, I know this question is old, but 2 years down the track and mozilla have done nothing.

    I've come up with a simple workaround.

    This essentially strips all formatting of the select box in firefox and wraps a span element around the select box with your custom style, but should only apply to firefox.

    Say this is your select menu:

    <select class='css-select'>
      <option value='1'> First option </option>
      <option value='2'> Second option </option>
    </select>
    

    And lets assume the css class 'css-select' is:

    .css-select {
       background-image: url('images/select_arrow.gif');
       background-repeat: no-repeat;
       background-position: right center;
       padding-right: 20px;
    }
    

    In firefox, this would display with the select menu, followed by the ugly firefox select arrow, followed by your nice custom looking one. Not ideal.

    Now to get this going in firefox, add a span element around with the class 'css-select-moz':

       <span class='css-select-moz'>
         <select class='css-select'>
           <option value='1'> First option </option>
           <option value='2'> Second option </option>
         </select>
       </span>
    

    Then fix the CSS to hide mozilla's dirty arrow with -moz-appearance:window and throw the custom arrow into the span's class 'css-select-moz', but only get it to display on mozilla, like this:

    .css-select {
       -moz-appearance:window;
       background-image: url('images/select_arrow.gif');
       background-repeat: no-repeat;
       background-position: right center;
       padding-right: 20px;
    }
    
    @-moz-document url-prefix() {
    .css-select-moz{
         background-image: url('images/select_arrow.gif');
         background-repeat: no-repeat;
         background-position: right center;
         padding-right: 20px;
      }
    } 
    

    Pretty cool for only stumbling across this bug 3 hours ago (I'm new to webdesign and completely self-taught). However, this community has indirectly provided me with so much help, I thought it was about time I give something back.

    I have only tested it in firefox (mac) version 18, and then 22 (after I updated).

    All feedback is welcome.

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

    A lot of Discussions Happening here & there but I don't see some proper solution for this problem. Finally Ended up by writing a small Jquery + CSS code for doing this HACK on IE & Firefox.

    Calculate Element Width (SELECT Element) using Jquery. Add a Wrapper Around Select Element and Keep overflow hidden for this element. Make sure that Width of this wrapper is appox. 25px less as that of SELECT Element. This could be easily done with Jquery. So Now Our Icon is Gone..! and it is time for adding our image icon on SELECT element...!!! Just add few simple lines for adding background and you are all Done..!! Make sure to use overflow hidden for outer wrapper,

    Here is a Sample of Code which was done for Drupal. However could be used for others also by removing few lines of code which is Drupal Specific.

    /*
     * Jquery Code for Removing Dropdown Arrow.
     * @by: North Web Studio
     */
    (function($) {
      Drupal.behaviors.nwsJS = {
        attach: function(context, settings) {
          $('.form-select').once('nws-arrow', function() {
            $wrap_width = $(this).outerWidth();
            $element_width = $wrap_width + 20;
            $(this).css('width', $element_width);
            $(this).wrap('<div class="nws-select"></div>');
            $(this).parent('.nws-select').css('width', $wrap_width);
          });
        }
      };
    })(jQuery);
    
    /*
     * CSS Code for Removing Dropdown Arrow.
     * @by: North Web Studio
     */
    
    .nws-select {
      border: 1px solid #ccc;
      overflow: hidden;
      background: url('../images/icon.png') no-repeat 95% 50%;
    }
    .nws-select .form-select {
      border: none;
      background: transparent;
    }
    

    Solution works on All Browsers IE, Chrome & Firefox No need of Adding fixed Widths Hacks Using CSS. It is all being handled Dynamically using JQuery.!

    More Described at:- http://northwebstudio.com/blogs/1/jquery/remove-drop-down-arrow-html-select-element-using-jquery-and-css

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

    The appearance property from CSS3 does not allow none value. Take a look at the W3C reference. So, what you is trying to do isn't valid (indeed Chrome shouldn't accept too).

    Then unfortunatelly we really don't have any cross-browser solution to hide that arrow using pure CSS. As pointed, you will need JavaScript.

    I suggest you to consider using selectBox jQuery plugin. It's very lightweight and nicely done.

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

    The other answers didn't seem to work for me, but I found this hack. This worked for me (July 2014)

    select {
    -moz-appearance: textfield !important;
        }
    

    In my case, I also had a woocommerce input field so I used this

    .woocommerce .quantity input.qty {
    -moz-appearance: textfield !important;
     }
    

    Updated my answer to show select rather than input

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

    hackity hack ... a solution that works in every browser I've tested (Safari, Firefox, Chrome). Don't have any IEs lying around, so it would be nice if you could test and comment:

    <div class="wrapper">
      <select>
        <option>123456789</option>
        <option>234567890</option>
      </select>
    </div>
    

    CSS, with url-encoded image:

    .wrapper { position:relative; width:200px; }
    .wrapper:after {
      content:"";
      display: block;
      position: absolute;
      top:1px; height:28px;
      right:1px; width:16px;
      background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABEAAAAcCAYAAACH81QkAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA2hpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOk9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZDowODgwMTE3NDA3MjA2ODExOEE2RENENTU2MTFGMEQ1RCIgeG1wTU06RG9jdW1lbnRJRD0ieG1wLmRpZDpGNDE5NDQ3Nzc5ODIxMUU0OEU0M0JFMzgzMkUxOTk3MiIgeG1wTU06SW5zdGFuY2VJRD0ieG1wLmlpZDpGNDE5NDQ3Njc5ODIxMUU0OEU0M0JFMzgzMkUxOTk3MiIgeG1wOkNyZWF0b3JUb29sPSJBZG9iZSBQaG90b3Nob3AgQ1M2IChNYWNpbnRvc2gpIj4gPHhtcE1NOkRlcml2ZWRGcm9tIHN0UmVmOmluc3RhbmNlSUQ9InhtcC5paWQ6MDk4MDExNzQwNzIwNjgxMThBNkRDRDU1NjExRjBENUQiIHN0UmVmOmRvY3VtZW50SUQ9InhtcC5kaWQ6MDg4MDExNzQwNzIwNjgxMThBNkRDRDU1NjExRjBENUQiLz4gPC9yZGY6RGVzY3JpcHRpb24+IDwvcmRmOlJERj4gPC94OnhtcG1ldGE+IDw/eHBhY2tldCBlbmQ9InIiPz4o7anbAAAAjklEQVR42uzUsQ3EIAwFUPty7MBOsAoVC7EVYgyUSFcdzn0iJYquAZGSLxnLzatsWERWGsvGP0QGkc+LxvN9AqGJTKQJMcYQM/+VtbZdiTGKUgr3cxbmlJI0ZiW83vsbgrkjB5JzFq11BdAxdyNICKEi6J25kFKKOOdq70We+JS2ufYTacjyxrKMLtsuwAAznzqGLHX9BwAAAABJRU5ErkJggg==);
    
      pointer-events: none;
    }
    
    select {
      width: 100%;
      padding:3px;
      margin: 0;
      border-radius: 0;
      border:1px solid black;
      outline:none;
      display: inline-block;
      -webkit-appearance:none;
      -moz-appearance:none;
      appearance:none;
      cursor:pointer;
      float:none!important;
      background:white;
    
      font-size:13px;
      line-height: 1em;
      height: 30px;
      padding:6px 20px 6px 10px;
    }
    

    http://codepen.io/anon/pen/myPEBy

    I'm using the :after-element to cover the ugly arrow. Since select doesn't support :after, i need a wrapper to work with. Now, if you would click on the arrow, the dropdown won't register it ... unless your browser supports pointer-events: none, which everyone except IE10- does: http://caniuse.com/#feat=pointer-events

    So for me it's perfect - a nice, clean, low-headache solution, at least compared to all the other options which include javascript.

    tl;dr:

    If IE10 (or lower) Users click the arrow, it won't work. Works good enough for me...

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