float
下面的代码是如何在 CSS 中使用菜单的基本表示。可以看到<li>
在 HTML 中只是尝试添加另一个<li>
,它会在右侧的菜单中自动调整,复制这个 HTML 和 CSS 并尝试理解忍受它。
<ul>
存在列表的标签<li>
is ,其中float: right
as 。在这种情况下,菜单是右对齐的,所有新的将在左侧对齐,因为.<li>
's
float: left;
<li>'s
<ul>
float: left;
我推荐https://www.w3schools.com/html/default.asp以获取有关 HTML 和 CSS 的详细知识。祝你好运!
输出截图
文件
<!DOCTYPE html>
<html>
<!-- This is the head section it's where the title of the page and links are given-->
<head>
<link rel="stylesheet" href="menu.css">
<title>
Menu Example
</title>
</head>
<!-- This is the body section it's where you write things that you want to show on the screen -->
<body>
</body>
<div class="header">
<h1 src="logo.png" height="60" class="logo">Logo</h1>
<div class="menu">
<ul class="ul">
<li class="line"><a id="active" class="link" href="home.html">HOME</a></li>
<li class="line"><a class="link" href="#">PAGES</a></li>
<li class="line"><a class="link" href="#">SERVICES</a></li>
<li class="line"><a class="link" href="#">CASE STUDIES</a></li>
<li class="line"><a class="link" href="#">CAREERS</a></li>
<li class="line"><a class="link" href="#">CONTACT</a></li>
</ul>
</div>
</div>
</html>
CSS文件
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* The above part is css reset you can get it
from the internet it resets the css so your code
is compatible with all the browsers */
.header{
width: 100%;
height: 120px;
}
.logo{
float: left;
margin-top: 20px;
width: 24%;
font-size: 50px;
}
.menu{
float: right;
width: 75%;
}
.ul{
float: right;
list-style-type: none;
margin-top: 60px;
margin-right: 60px;
}
.line{
float: left;
color: #000000;
font-size: 12px;
font-family: openSans;
font-weight: bold;
}
.link{
color: black;
text-decoration: none;
height: 100%;
padding-right: 10px;
padding-top: 15px;
padding-bottom: 15px;
padding-left: 10px;
border-right: 2px solid #d2d2d2;
}
.link:hover{
color: #d7002e;
border-color: #d7002e;
}
#active{
color: #d7002e;
border-color: #d7002e;
}