在我的HTML中,我有一個無序的鏈接列表。在我的CSS中,我試圖將網(wǎng)頁上的鏈接列表水平居中。以下是我的HTML和CSS代碼:
@import url("https://fonts.googleapis.com/css2?family=Roboto&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box
}
body {
font-size: 2rem;
font-family: "Roboto", sans-serif;
}
ul {
display: inline-block;
margin: 0px, auto;
list-style: none;
padding: 0.5rem;
background-color: black;
}
li {
display: inline-block;
margin-inline: 0.5rem;
}
li a {
color: white;
text-decoration: none;
}
li a:hover, li a:focus {
text-decoration: underline;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Display</title>
<link rel="stylesheet" href="practice.css">
</head>
<body>
<main>
<nav>
<ul>
<li><a href="#">Intro</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">Projects</a></li>
</ul>
</nav>
</main>
</body>
</html>
你需要做兩件事:
1 .顯示:伸縮到你的導(dǎo)航
nav{
display: flex
}
2 .為ul提供的邊距不正確,應(yīng)該沒有逗號
margin: 0px auto;
工作代碼如下:
@import url("https://fonts.googleapis.com/css2?family=Roboto&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box
}
nav {
display: flex;
}
body {
font-size: 2rem;
font-family: "Roboto", sans-serif;
}
ul {
display: inline-block;
margin: 0 auto;
list-style: none;
padding: 0.5rem;
background-color: black;
}
li {
display: inline-block;
margin-inline: 0.5rem;
}
li a {
color: white;
text-decoration: none;
}
li a:hover,
li a:focus {
text-decoration: underline;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Display</title>
<link rel="stylesheet" href="practice.css">
</head>
<body>
<main>
<nav>
<ul>
<li><a href="#">Intro</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">Projects</a></li>
</ul>
</nav>
</main>
</body>
</html>
更改此行:
margin: 0px, auto;
對此:
margin: 0px auto;
要解決這個問題,您可以將逗號改為空格。這將使你的無序列表在網(wǎng)頁上水平居中。
卡米爾是正確的。你的語法有錯誤。從你的保證金聲明中刪除逗號。
要使列表居中,您應(yīng)該刪除display:inline-block;并添加小于100%的寬度。這將允許您的列表在頁面上居中。
內(nèi)嵌塊元素沒有固有的寬度。因此,保證金:自動;不會起作用。
@import url("https://fonts.googleapis.com/css2?family=Roboto&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box
}
body {
font-size: 2rem;
font-family: "Roboto", sans-serif;
}
ul {
width: 80%;
margin: 0px auto;
list-style: none;
padding: 0.5rem;
background-color: black;
}
li {
display: inline-block;
margin-inline: 0.5rem;
}
li a {
color: white;
text-decoration: none;
}
li a:hover, li a:focus {
text-decoration: underline;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Display</title>
<link rel="stylesheet" href="practice.css">
</head>
<body>
<main>
<nav>
<ul>
<li><a href="#">Intro</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">Projects</a></li>
</ul>
</nav>
</main>
</body>
</html>