如何使用CSS在选择下拉菜单中缩进文本 [英] How to indent text in a select drop down menu using CSS

查看:221
本文介绍了如何使用CSS在选择下拉菜单中缩进文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用CSS在选择下拉菜单中缩进类别和无限子类别?

How can I indent categories and endless sub categories that I have in a select drop down menu using CSS?

   1. Apple
   2. Arts & Entertainment
         1. Amusement
         2. Art
         3. Artists
               1. A
                     1. a1
                     2. a2
               2. B
               3. C
               4. D
   3. Automotive
   4. Network
   5. Server
   6. Web Design
         1. CSS
         2. HTML


推荐答案

使用< select> 元素执行此操作。你不能有无限的嵌套子类,像你建议的,只有一个,与< optgroup> 。样式也会很困难,因为样式表单元素的能力因浏览器而异。

You can't do this with the <select> element. You can't have endless nested subcategory like you suggest, only one, with <optgroup>. Styling will also be difficult as the ability to style form elements differs from browser to browser.

但是,根据您的需要,以下解决方案可能有效:

However, depending on your needs, the following solution might work:

使用HTML列表选择元素。标记将如下所示:

For this, we are recreating the select element using HTML lists. The markup would look like this:

<div id="select">
    <p>Select your Answer</p>
    <ul>
        <li><a href="#">Apple</a></li>
        <li><a href="#">Arts and Entertainment</a>
            <ul>
                <li><a href="#">Amusement</a></li>
                <li><a href="#">Art</a></li>
                <li><a href="#">Artist</a>
                    <ul>
                        <li><a href="#">A</a></li>
                        <li><a href="#">B</a></li>
                    </ul></li>
            </ul>
        </li>
    </ul>
</div>

然后,对CSS进行风格调整,使其符合您的目的:

Then, with CSS, style it such that it fits your purpose:

#select {
    border: 1px solid #999;
    background-color: rgba(0, 0, 0, 0.1);
    position: absolute;
    width: 300px;
    font-family: Arial, sans-serif;
    font-size: 11px;
}

#select:hover {
    background-color: #efefef;
}

#select > ul {
    display: none;
    list-style: none;
}

#select:hover > ul {
    display: block;
}

#select > ul ul {
    margin-left: 20px;
}

#select > ul a, #select > p {
    display: block;
    padding: 3px 8px 4px;
    color: #333;
    text-decoration: none;
}

#select > ul a:hover {
    background-color: #ddd;
}

在这里使用它: http://jsfiddle.net/thHFS/

这篇关于如何使用CSS在选择下拉菜单中缩进文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆