【问题标题】:Styling List Buttons样式列表按钮
【发布时间】:2011-09-28 22:41:02
【问题描述】:

使用列表,我想创建一个链接列表,如图所示

<div id="toolbarbottom" class="toolbar" style="position: fixed; clear: both; overflow: visible; bottom: 0px; left: 0px; width: 100%;">
     <ul>
         <li id="active"><span><a id="current" href="#add" class="button">News</a></span></li>
         <li><span> <a href="#Updates" class="button">Updates</a></span> </li>
         <li><span><a href="#Contact" class="button">Contact Us</a></span></li>
         <li><span><a href="#Website" class="button">Website</a> </span></li>
         <li><span><a href="#Refresh" id="#Refresh" class="button">Refresh</a></span> </li>
     </ul>
</div>

我有点卡在 CSS(按钮)上,可能是列表元素之间的间距。使列表以这种形式出现。请问有人知道我该如何解决这个问题吗?

【问题讨论】:

    标签: html css image hyperlink html-lists


    【解决方案1】:

    或者另一种方法是使用浮动,并使ul display: inline-block 包含浮动的li

    您需要稍微更改 HTML,以便 spana 内 - 这样您就可以隐藏跨区文本,但保留 a 元素的图像背景和可点击区域,我也是d 给每个链接一个唯一的引用(类或 ID),以便可以单独应用背景。

    示例 HTML:

    <div id="toolbarbottom" class="toolbar" style="position: fixed; top: 0px; left: 0px; width: 100%;">
         <ul>
             <li class="active"><a href="#add" id="madd"><span>News</span></a></li>
             <li><a href="#Updates" id="mupdates"><span> Updates</span></a></li>
             <li><a href="#Contact" id="mcontact"><span>Contact Us</span></a></li>
             <li><a href="#Website" id="mwebsite"><span>Website </span></a></li>
             <li><a href="#Refresh" id="mrefresh"><span>Refresh</span></a> </li>
         </ul>
    </div>
    

    然后您可以将整个背景放在 ul 上,并将单个图像放在每个链接上。

    #toolbarbottom ul {
       list-style: none; 
       margin: 0; 
       padding: 0; 
       display: inline-block; 
       width: 100%; 
       background: #ff0;
    }
    
    #toolbarbottom li {
       float: left; 
       width: 80px; 
       height: 80px; 
       border: 1px solid #000; /* not required, just to show where individual links are */
    } 
    
    #toolbarbottom li a { /* make link fill the li element */
       display: block; 
       height: 80px; 
    }
    
    #toolbarbottom li span { /* hide the text */
       position: absolute;
       clip: rect(1px, 1px, 1px, 1px);
       clip: rect (1px 1px 1px 1px);
    }
    
    /* couple examples of where to put individual backgrounds */   
    #toolbarbottom #mupdates {background: #dad;}
    #toolbarbottom #mcontact {background: #0f0;} 
    

    【讨论】:

    • 我应用了你上面的解决方案,我看到了边框。如何对齐我的图像,使其看起来好像恰好位于每个列表元素的中心,如果我将跨度移动到点击内部,则如何将点击区域缩小到该图像区域?本质上,我的 span 是为了封闭可点击区域,我想到了处理边距/填充属性以将其缩小到与最终图像完全相同的大小。
    • 使用background-position: 50% 50%center center 将图像居中。通过留出a 的边距来减少可点击区域 - 所以如果li 是80px x 80px 就像在我的例子中a 可能是display: block; height: 40px; margin: 20px; 那么a 只能在40px 方形区域上单击,但有边距每边 20px 以使其填充 80px 空间
    【解决方案2】:

    您应该首先将您的 css 设置为外部样式表,而不是将其硬编码到您的 html 中。 (有关更多信息,请参阅http://www.w3.org/TR/html4/present/styles.html)。要在 li 元素之间添加间距,您可以使用 css 级联添加一些底部填充,如下所示:

    #toolbarbottom ul li {
        padding-bottom:4px;
    }
    

    要使列表显示为内联,您可以使用:

    #toolbarbottom ul{
        list-style: none;
        display: inline;
        padding-left: 0;
        margin-left: 0;
    }
    

    这些按钮看起来像图像,因此您只需将它们包含在每个 li 元素中即可:

    <li><a href="example.com"><img src="/path/to/image.jpg"></a></li>
    

    【讨论】:

    • @Soumyah92 确实如此,但在这种情况下,对于如此简单的事情,他们有一个非常简单易懂的关于外部与内部样式表的教程。如果您有更好的教程,请随时发布链接:)。
    【解决方案3】:

    小提琴:http://jsfiddle.net/YaS9J/

    css

    #toolbarbottom li {
        display:inline-block;
        padding:0 10px;  
    }
    
    /* if you have one */
    #toolbarbottom li img {
        display:block;   
    }
    

    【讨论】:

    • 非常感谢您的评论。这是我除了已经得到的东西之外还需要的东西。
    猜你喜欢
    • 2010-09-06
    • 2012-04-27
    • 2023-03-21
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多