Sample code sharing for a simple introduction to CSS custom drop-down list styles

Source: Internet
Author: User
Default style for drop-down list:


Here are two ways to customize a drop-down list:

Method One:

Customize the style of the drop-down list with a pure CSS.

Principle: Clears the Default drop-down list style, customizes the style, and attaches a small arrow right-aligned picture.

<!doctype html>


Problem: Modify the width and height of option to be invalid.

Method Two:

Use P+ul+jquery to implement a custom style drop-down list select.

HTML code:

<p id= "container" ><form action= "" method= "post" ><p><ul><li class= "active" > Please select </li ><li> Beijing </li><li> Shanghai </li><li> Guangzhou </li></ul></p></form> </p>

CSS code:

#container {background:grey;width:300px;height:200px;padding:20px;} Form p{    width:236px;    height:34px;} Form P{font-family:microsoft Yahei;    Background: #FFFFFF;} Form p:hover{    border:1px solid #E74F4D;} Form ul{margin:0;padding:0;} Form ul li:first-child{    height:34px;    line-height:34px;} Form ul li{    width:236px;    height:24px;    line-height:24px;    font-size:15px;    Color: #323333;    opacity:0.7;    Background: #e3e3e5;    Text-indent:12px;display:none;} Form ul li.active{    Display:block;    Background:url ("Images/arrows_active_down.gif") no-repeat scroll right center;    Opacity:1;} Form UL Li:not (. Active): hover{    background: #E74F4D;    Color:white;}

jquery Code:

$ (document). Ready (function () {var p = $ ("form"). Find ("P");        P.mouseover (function (e) {var event = e | | window.event; var target = Event.target | |        Event.srcelement;        var _this = $ (this);            if (target.nodeName.toLowerCase () = = ' Li ') {_this.find (' li '). CSS (' Display ', ' block ');                _this.find (' Li '). Click (function () {var li = $ (this);            _this.find ('. Active '). Text (Li.text ());        });            } _this.mouseout (function (e) {var event = e | | window.event; var target = Event.target | |            Event.srcelement;        if (target.nodeName.toLowerCase () = = ' Li ') _this.find (' Li '). Not ('. Active '). CSS (' Display ', ' none ');    }); });});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.