简体   繁体   English

如何使用背景图片向ul / li菜单添加填充?

[英]How to add padding to a ul/li menu with a background image?

I have the following menu: 我有以下菜单:

CSS: CSS:

            ul.menu {
                padding: 0;
                margin: 0;
                font-size: 16px;
            }

            ul.menu > li {
                display: block;
                width: 100%;
                margin: 0;
            }

            ul.menu > li:hover {
                color: red;
            }

            ul.menu > li a {
                display: block;
                background:transparent url("http://placehold.it/25x25") right center no-repeat;
                background-size: 15px 15px;
            }

            ul.menu > li > a:hover {
                background-color: #F7F7F7;
            }

HTML: HTML:

<div style="width: 200px; background-color: lightgrey;">
    <ul class="menu">
                        <li>
                            <a href="">Line 1</a>
                        </li>
                        <li>
                            <a href="">Line 2</a>
                        </li>
                        <li>
                            <a href="">Line 3</a>
                        </li>
                        <li>
                            <a href="">Line 4</a>
                        </li>
                    </ul>
</div>

JSFiddle: http://jsfiddle.net/8TzMc/ JSFiddle: http : //jsfiddle.net/8TzMc/

So far so good, but I want there to be padding on the left and right sides of the li's (about 10px) and I would also like the height of the li's to be a little greater (so that there's some space between the lines of text). 到目前为止,一切都很好,但是我希望在li的左右两侧都有填充(大约10像素),并且我也希望li的高度要大一些(以使li的线之间有一些空间)文本)。 I tried adding this line to the ul.menu > li CSS, but it messes up the menu in two ways: 我尝试将此行添加到ul.menu > li CSS,但是它以两种方式弄乱了菜单:

  1. There is now a non-clickable gap between the tops and bottoms of the menu items 现在,菜单项的顶部和底部之间没有不可单击的间隙
  2. The background image is messed up 背景图像混乱

JSFiddle: http://jsfiddle.net/HXrgq/ JSFiddle: http : //jsfiddle.net/HXrgq/

How can this be fixed? 如何解决?

Add the padding to the a element instead of the li element. padding添加到a元素而不是li元素。

ul.menu > li a { padding:10px;} 

for example 例如

Updated Fiddle link 更新了小提琴链接

而是在ul.menu > li a添加填充,以消除空白。

http://jsfiddle.net/HXrgq/1/ http://jsfiddle.net/HXrgq/1/

On the li element, do: 在li元素上,执行以下操作:

text-indent: 10px;
line-height: 1.5em;

seems this is what you searched: text indented (I suppose this is only one line?) and line-height . 似乎这是您搜索的内容:缩进的文本(我想这只是一行吗?)和line-height

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM