简体   繁体   English

如何使用HTML和CSS内联显示列表

[英]how to display a list inline using HTML and CSS

I am trying to create a menu using html, I have added my link in an unordered list (ul) has shown below. 我正在尝试使用html创建菜单,我已在下面显示的无序列表(ul)中添加了我的链接。 In my css i added a display:inline; 我在CSS中添加了display:inline; to the links so that they would display in a link like a menu but for some reason it doesn't seem to work. 链接,以便它们可以像菜单一样显示在链接中,但由于某种原因,它似乎不起作用。

 #menu a { text-decoration: none; } #menu ul { list-style: none; } #menu ul li a { display: inline; } 
 <div id="menu"> <ul> <li><a href="index.php">Home</a> </li> <li><a href="about.php">About Us</a> </li> <li><a href="offers.php">Special Offers</a> </li> <li><a href="staff.php">Meet Our Staff</a> </li> <li><a href="contact.php">Contact</a> </li> </ul> </div> 

You are targeting the anchors, which are already inline by default. 您将定位锚定为默认情况下已经内联的锚。 I believe you mean to target the list items: 我相信您的意思是针对列表项:

#menu ul li {
    display: inline;
}

JSFiddle 的jsfiddle

Try this: ul li { float: left; padding-right:10px; } 试试这个: ul li { float: left; padding-right:10px; } ul li { float: left; padding-right:10px; }

https://jsfiddle.net/n4aak3nk/1/ https://jsfiddle.net/n4aak3nk/1/

You were very close! 亲密!

The only thing wrong with your code, is that display: inline; 您的代码唯一的错误是display: inline; should be on your <li> elements instead of your <a> elements : 应该在您的<li>元素上,而不是在<a>元素上:

 #menu a { text-decoration: none; } #menu ul { list-style: none; } #menu ul li { display: inline; } 
 <div id="menu"> <ul> <li><a href="index.php">Home</a> </li> <li><a href="about.php">About Us</a> </li> <li><a href="offers.php">Special Offers</a> </li> <li><a href="staff.php">Meet Our Staff</a> </li> <li><a href="contact.php">Contact</a> </li> </ul> </div> 

(see also this Fiddle ) (另请参阅此小提琴

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

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