简体   繁体   English

试图获取菜单以内联html和css

[英]Trying to get a menu to go inline with html and css

I have created a menu list in html using a list, i have added the list to a div called menu. 我已经使用列表创建了html菜单列表,我将该列表添加到了名为menu的div中。 In my css I have added display:inline to try and make the list to display the list inline, normally this works but for some reason it doesn't seem to work. 在我的CSS中,我添加了display:inline来尝试使列表内联显示列表,通常这可以正常工作,但是由于某种原因它似乎不起作用。

Here is my Code 这是我的代码

 #menu ul { width: 100px; position: absolute; z-index: 1; background: white; padding-top: 20px; margin: 0; padding: 0; list-style: none; display: inline; } 
 <div id="menu"> <ul> <li><a href="index.php">Home</a> </li>&nbsp; &nbsp; &nbsp; <li><a href="about.php">About Us</a> </li>&nbsp; &nbsp; &nbsp; <li><a href="Specials.php">Specials</a> </li>&nbsp; &nbsp; &nbsp; <li><a href="terms.php">Terms</a> </li>&nbsp; &nbsp; &nbsp; <li><a href="contact.php">Contact Us</a> </li>&nbsp; &nbsp; &nbsp; </ul> </div> 

if you want the list items to be inline: 如果您希望列表项是内联的:

#menu ul li{display:inline-block;}

or 要么

#menu ul li{float:left;}

or 要么

#menu ul li{display:inline;}

But don't forget to clearfix the float for the 2n option. 但是不要忘了为2n选项清除浮点数。

You need to put display inline on your ul li not ul since you only have one :) 因为您只有一个,所以您需要在内联显示而不是ul :)

So change your css to this: 因此,将您的CSS更改为:

    #menu ul li
{

 width: 100px;
 position: absolute;
 z-index: 1;
 background: white;
 padding-top: 20px;
 margin: 0;
 padding: 0;
 list-style:none;
 display:inline;
}

You would need to apply the display: inline to li . 您将需要将display: inline应用于li Also remove the position: absolute from ul 还要删除position: absolute ul position: absolute

 #menu ul { width: 100px; z-index: 1; background: white; padding-top: 20px; margin: 0; padding: 0; list-style: none; display: inline; } #menu ul li { display: inline; } 
 <div id="menu"> <ul> <li><a href="index.php">Home</a> </li>&nbsp; &nbsp; &nbsp; <li><a href="about.php">About Us</a> </li>&nbsp; &nbsp; &nbsp; <li><a href="Specials.php">Specials</a> </li>&nbsp; &nbsp; &nbsp; <li><a href="terms.php">Terms</a> </li>&nbsp; &nbsp; &nbsp; <li><a href="contact.php">Contact Us</a> </li>&nbsp; &nbsp; &nbsp; </ul> </div> 

Please try the next code, it will work for you. 请尝试下一个代码,它将为您服务。 I've removed the &nbsp and update the css to apply the rules to li element. 我已经删除了&nbsp并更新了CSS以将规则应用于li元素。

 #menu li { width: 100px; z-index: 1; background: white; padding-top: 20px; margin: 0; padding: 10px; list-style:none; 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="Specials.php">Specials</a></li> <li><a href="terms.php">Terms</a></li> <li><a href="contact.php">Contact Us</a></li> </ul> </div> 

I hope that helps :D 希望对您有所帮助:D

You have to put select the li instead of the ul. 您必须选择li而不是ul。 Also you have a width of 100px in your ul that doesn't let the li's to spread. 另外,您的ul中有100px的宽度,不会让li扩散。

#menu ul {
     position: absolute;
     z-index: 1;
     background: white;
     padding-top: 20px;
     margin: 0;
     padding: 0;
     list-style:none;
}

#menu ul li { display: inline; }

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

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