我該如何居中浮動元素,你可以看到這個photo?我想從網頁的左側將它放到中間,但將它保留在頁面的頂部?如何將CSS中的浮動元素居中?
下面是HTML的代碼:
<html>
<head>
<title>Batch File Generator | Home</title>
</head>
<link href="style.css" rel="stylesheet" >
<ul>
<li><a>Home</a></li>
<li><a>Download</a>
<ul>
<li><a href="32-bit version.exe" download="download">32-bit version</a></li>
<li><a href="64-bit version.exe" download="download">64-bit version</a></li>
</ul>
</li>
<li><a>Discussion</a>
<ul>
<li><a>Community forums</li></a>
<li><a>Ask the developers</li></a>
</ul>
</li>
<li><a>News</a></li>
</ul>
</html>
這裏是CSS的代碼:
body{
background: url("background.jpg") no-repeat;
background-size: cover;
font-family: Arial;
color: white;
}
ul{
margin: 0px;
padding: 0px;
list-style: none;
}
ul li {
float: left;
width: 200px;
height: 40px;
background-color: black;
opacity: .8;
line-height: 40px;
text-align: center;
font-size: 20px;
}
ul li a {
text-decoration: none;
color: white;
display: block;
}
ul li a:hover {
background-color: green;
}
ul li ul li {
display: none;
}
ul li:hover ul li {
display: block;
}
爲什麼你把它漂浮到左側時,要居中呢?刪除浮動並添加'margin:0 auto;' –
謝謝。 「我必須寫這個,否則我不能發表評論'因爲它太短...」 – ange
Dawid Zbinski它沒有工作,因爲它是一個下拉菜單,並使用「邊距:0自動;」白色書寫是,在頁面頂部的中間,但它垂直對齊,下拉效果不起作用。 – ange