简体   繁体   English

造型 <li> 与背景图像 <ul>

[英]Styling <li> with background images in an <ul>

I'm setting up a footer for a site and using an unordered list with three list items as follows: 我正在为网站设置页脚,并使用具有三个列表项的无序列表,如下所示:

<ul id="socialnav">
    <li id="facebook"></li>
    <li id="twitter"></li>
    <li id="googleplus"></li>
</ul>

I am trying to use background images for the list items to display the social media icons and then I would like to apply a hover state to the list item for the button's 'on-state'. 我试图将背景图像用于列表项以显示社交媒体图标,然后将悬停状态应用于按钮的“打开状态”的列表项。 However, my three icons won't appear horizontally in the list. 但是,我的三个图标不会在列表中水平显示。 Here is my css: 这是我的CSS:

#socialnav {
display: inline;
line-height: 42px;
}

#facebook {
background-image: url("images/fb.png");
background-repeat: no-repeat;
list-style-type: none;
height: 42px;
}

#twitter {
background-image: url("images/twt.png");
background-repeat: no-repeat;
list-style-type: none;
height: 42px;
}

#googleplus {
background-image: url("images/gplus.png");
background-repeat: no-repeat;
list-style-type: none;
height: 42px;
}

Two questions - why is this not working? 两个问题-为什么这不起作用? And is there a better/easier way to do this? 有没有更好/更简便的方法可以做到这一点?

To make your li elements appear in a horizontal fashion, you can make two changes. 要使li元素以水平方式显示,可以进行两项更改。

First, remove the display: inline from the ul : 首先,从ul删除display: inline

#socialnav {
    line-height: 42px;
}

Second, style the li elements to float and use display: block : 其次,设置li元素的样式以使其浮动并使用display: block

#socialnav li {
    display: block;
    float: left;
}

Note that the width and border directives were added to allow the elements to appear on the page which the images are unavailable. 请注意,添加了width和border指令以允许元素显示在图像不可用的页面上。 You can remove them as needed. 您可以根据需要将其删除。

JSFiddle here . JSFiddle在这里

  1. Probably you don't see images because your list-items are empty so they're not wide enough to see images, or the path to the images is incorrect. 您可能看不到图像,因为列表项为空,因此它们的宽度不足以查看图像,或者图像的路径不正确。

  2. Use classes instead of id's. 使用类而不是ID。 So you can re-use the icons again in the same page if needed. 因此,您可以根据需要在同一页面中再次使用这些图标。 Use also list-style : none once, applied to <ul> element and background-position: no-repeat; height: 42px; 也可以使用list-style : none一次,不应用于<ul>元素和background-position: no-repeat; height: 42px; background-position: no-repeat; height: 42px; once, applied to every <li> . 一次,应用于每个<li> Another improvement could be using a sprite image, containing all the icons in a single file (so to avoid too many server requests and reduce the overall image size) 另一个改进是使用Sprite图像,将所有图标包含在一个文件中(这样可以避免过多的服务器请求并减小整体图像大小)

For a start I'd make sure your CSS is looking for images in the right place. 首先,我将确保您的CSS在正确的位置查找图像。 The url needs to be relative to the CSS file, not the site root. 网址必须相对于CSS文件,而不是网站根目录。

Secondly I'd put a &nbsp; 其次,我会加一个&nbsp; in your <li> tags to make sure they're not seens as being empty. 在您的<li>标记中,以确保它们不会被视为空白。

The code works otherwise. 该代码否则起作用。

 #facebook {
     background-image: url("http://www.moifacebook.ru/wp-content/themes/atenta12/sections/branding/facebook.png");
     background-repeat: no-repeat;
     list-style-type: none;
     height: 42px;
}  

<ul id="socialnav">
        <li id="facebook">&nbsp;</li>
        <li id="twitter">&nbsp;</li>
        <li id="googleplus">&nbsp;</li>
    </ul>​

http://jsfiddle.net/spacebeers/yQzDy/ http://jsfiddle.net/spacebeers/yQzDy/

Update your code as below 如下更新代码

<ul id="socialnav">
<li id="facebook">&nbsp;</li>
<li id="twitter">&nbsp;</li>
<li id="googleplus">&nbsp;</li>
</ul>​

Change CSS to 将CSS更改为

#socialnav {
display: inline;
 line-height: 42px;
   }

  #facebook {
  background: url("images/fb.png") no-repeat;
  list-style-type: none;
   height: 42px;
    }

   #twitter {
   background: url("images/twt.png") no-repeat;
  list-style-type: none;
   height: 42px;
   }

 #googleplus {
  background: url("images/gplus.png") no-repeat;
  list-style-type: none;
  height: 42px;
  }

First of all not sure why you are making #socialnav display:inline... that really makes no sense. 首先,不确定为什么要使用#socialnav display:inline ...这真的没有意义。

Second of all you should place an anchor inside your li's to make these social links clickable. 第二,您应该在li的内部放置锚点,以使这些社交链接可点击。 Then you should make those anchors display:block and use them as a way to handle the sizing of the icons. 然后,应将这些锚点显示为:block,并将其用作处理图标大小的一种方法。

Make sure there is some width specified! 确保指定了一些宽度!

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

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