如何使用CSS在页面中央放置导航栏

亚历克斯96

嗨,我正在尝试创建一个网站,除了导航栏不会移动到屏幕的中心之外,其他一切都进行得很好,当我让导航栏占据整个页面时,它确实转到了中心,但是我只想要导航酒吧要尽可能大,但现在我无法将其放到页面的中央。

这是我的代码如下:

body {
display: block;
margin: 0;
padding: 0px;
}

#header {
background-color: #b9fee2;
width: 1920px;
height: 200px;
display: inline-block;
}

h1 {
font-family: Optima, Segoe, 'Segoe UI', Candara, Calibri, Arial, sans-serif;
font-size: 75px;
font-weight: 500;
text-align: center;
margin: 0px;
color: #000000;
text-decoration: none;
}

h1 a{
text-decoration: none;
}

h1 a:visited {
color: #000000;
text-decoration: none;
}

#nav {
display: inline-block;
-webkit-padding-start: 0px;
text-align: center;
margin: auto;
width: 35%;
}

#nav li {
display: inline-block;
vertical-align: top;
}

#nav a {
-webkit-transition: background-color 0.2s linear;
}

#nav a:link {
display: block;
text-align: center;
color: #000000;
text-decoration: none;
font-family: Georgia, Times, 'Times New Roman', serif;
font-size: 30px;
padding-top: 10px;
padding-bottom: 10px;
padding-left: 50px;
padding-right: 50px;
}

#nav a:visited {
color: #000000;
}

#nav a:hover {
display: block;
text-align: center;
color: #000000;
background-color: #FFFFFF;
text-decoration: none;
padding-left: 50px;
padding-right: 50px;
font-size: 30px;
}
#circle {
padding-top: 25px;
}

#webInfo {
padding-top: 10px;
text-align: center;
font-family: Georgia, Times, 'Times New Roman', serif;
font-size: 25px;
}

#webInfo p{
padding: 0px;
margin: 
}


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML         4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>My Portfolio - Home</title>
    <link rel="stylesheet" type="text/css" href="css/style.css" />
</head>
<body>
    <div id="header">
        <h1><a href="index.html">Alex Trotter</a></h1>
        <ul id="nav">
            <li><a href="index.html">Home</a></li>
            <img id="circle" src="images/circle.png"/>
            <li><a href="about.html">About Me</a></li>
            <img id="circle" src="images/circle.png"/>
            <li><a href="contact.html">Contact</a></li>
        </ul>
    </div>
    <div id="content">
        <div id="webInfo">
            <p>Hi, my name is Alex Trotter and this is my portfolio website</p>
            <p>below you will find some of work that I have created.</p>
            <p>Above you can navigate to different pages to learn more about me.</p>
        </div>
        <div id="exampleWork1"></div>
        <div id="exampleWork2"></div>
        <div id="exampleWork3"></div>
    </div>
</body>

片段:

body {
  display: block;
  margin: 0;
  padding: 0px;
}
#header {
  background-color: #b9fee2;
  width: 1920px;
  height: 200px;
  display: inline-block;
}
h1 {
  font-family: Optima, Segoe, 'Segoe UI', Candara, Calibri, Arial, sans-serif;
  font-size: 75px;
  font-weight: 500;
  text-align: center;
  margin: 0px;
  color: #000000;
  text-decoration: none;
}
h1 a {
  text-decoration: none;
}
h1 a:visited {
  color: #000000;
  text-decoration: none;
}
#nav {
  display: inline-block;
  -webkit-padding-start: 0px;
  text-align: center;
  margin: auto;
  width: 35%;
}
#nav li {
  display: inline-block;
  vertical-align: top;
}
#nav a {
  -webkit-transition: background-color 0.2s linear;
}
#nav a:link {
  display: block;
  text-align: center;
  color: #000000;
  text-decoration: none;
  font-family: Georgia, Times, 'Times New Roman', serif;
  font-size: 30px;
  padding-top: 10px;
  padding-bottom: 10px;
  padding-left: 50px;
  padding-right: 50px;
}
#nav a:visited {
  color: #000000;
}
#nav a:hover {
  display: block;
  text-align: center;
  color: #000000;
  background-color: #FFFFFF;
  text-decoration: none;
  padding-left: 50px;
  padding-right: 50px;
  font-size: 30px;
}
#circle {
  padding-top: 25px;
}
#webInfo {
  padding-top: 10px;
  text-align: center;
  font-family: Georgia, Times, 'Times New Roman', serif;
  font-size: 25px;
}
#webInfo p {
  padding: 0px;
  margin:
}
<div id="header">
  <h1><a href="index.html">Alex Trotter</a></h1>
  <ul id="nav">
    <li><a href="index.html">Home</a>
    </li>
    <img id="circle" src="images/circle.png" />
    <li><a href="about.html">About Me</a>
    </li>
    <img id="circle" src="images/circle.png" />
    <li><a href="contact.html">Contact</a>
    </li>
  </ul>
</div>
<div id="content">
  <div id="webInfo">
    <p>Hi, my name is Alex Trotter and this is my portfolio website</p>
    <p>below you will find some of work that I have created.</p>
    <p>Above you can navigate to different pages to learn more about me.</p>
  </div>
  <div id="exampleWork1"></div>
  <div id="exampleWork2"></div>
  <div id="exampleWork3"></div>
</div>

提前致谢!:)

亚历克斯·查尔

您可以设置width: 100%#nav但是您的html无效:

ul:允许的内容零个或多个li元素,最终与和元素混合。

我在中移动img元素liid必须是唯一使用类,而不是(我改变的元素id #circle.circle也更新您的CSS)和img元素必须有标题和alt属性。正确的版本:

body {
  display: block;
  margin: 0;
  padding: 0px;
}
#header {
  background-color: #b9fee2;
  width: 1920px;
  height: 200px;
  display: inline-block;
}
h1 {
  font-family: Optima, Segoe, 'Segoe UI', Candara, Calibri, Arial, sans-serif;
  font-size: 75px;
  font-weight: 500;
  text-align: center;
  margin: 0px;
  color: #000000;
  text-decoration: none;
}
h1 a {
  text-decoration: none;
}
h1 a:visited {
  color: #000000;
  text-decoration: none;
}
#nav {
  display: inline-block;
  -webkit-padding-start: 0px;
  text-align: center;
  margin: auto;
  width: 100%;
}
#nav li {
  display: inline-block;
  vertical-align: top;
}
#nav a {
  -webkit-transition: background-color 0.2s linear;
}
#nav a:link {
  display: block;
  text-align: center;
  color: #000000;
  text-decoration: none;
  font-family: Georgia, Times, 'Times New Roman', serif;
  font-size: 30px;
  padding-top: 10px;
  padding-bottom: 10px;
  padding-left: 50px;
  padding-right: 50px;
}
#nav a:visited {
  color: #000000;
}
#nav a:hover {
  display: block;
  text-align: center;
  color: #000000;
  background-color: #FFFFFF;
  text-decoration: none;
  padding-left: 50px;
  padding-right: 50px;
  font-size: 30px;
}
.circle {
  padding-top: 25px;
}
#webInfo {
  padding-top: 10px;
  text-align: center;
  font-family: Georgia, Times, 'Times New Roman', serif;
  font-size: 25px;
}
#webInfo p {
  padding: 0px;
  margin:
}
<div id="header">
  <h1><a href="index.html">Alex Trotter</a></h1>
  <ul id="nav">
    <li><a href="index.html">Home</a>
      <img class="circle" title="circle" alt="circle" src="images/circle.png" />
    </li>
    <li><a href="about.html">About Me</a>
      <img class="circle" title="circle" alt="circle" src="images/circle.png" />
    </li>
    <li><a href="contact.html">Contact</a>
    </li>
  </ul>
</div>
<div id="content">
  <div id="webInfo">
    <p>Hi, my name is Alex Trotter and this is my portfolio website</p>
    <p>below you will find some of work that I have created.</p>
    <p>Above you can navigate to different pages to learn more about me.</p>
  </div>
  <div id="exampleWork1"></div>
  <div id="exampleWork2"></div>
  <div id="exampleWork3"></div>
</div>

使用OP代码的解决方案:

body {
  display: block;
  margin: 0;
  padding: 0px;
}
#header {
  background-color: #b9fee2;
  width: 1920px;
  height: 200px;
  display: inline-block;
}
h1 {
  font-family: Optima, Segoe, 'Segoe UI', Candara, Calibri, Arial, sans-serif;
  font-size: 75px;
  font-weight: 500;
  text-align: center;
  margin: 0px;
  color: #000000;
  text-decoration: none;
}
h1 a {
  text-decoration: none;
}
h1 a:visited {
  color: #000000;
  text-decoration: none;
}
#nav {
  display: inline-block;
  -webkit-padding-start: 0px;
  text-align: center;
  margin: auto;
  width: 100%;
}
#nav li {
  display: inline-block;
  vertical-align: top;
}
#nav a {
  -webkit-transition: background-color 0.2s linear;
}
#nav a:link {
  display: block;
  text-align: center;
  color: #000000;
  text-decoration: none;
  font-family: Georgia, Times, 'Times New Roman', serif;
  font-size: 30px;
  padding-top: 10px;
  padding-bottom: 10px;
  padding-left: 50px;
  padding-right: 50px;
}
#nav a:visited {
  color: #000000;
}
#nav a:hover {
  display: block;
  text-align: center;
  color: #000000;
  background-color: #FFFFFF;
  text-decoration: none;
  padding-left: 50px;
  padding-right: 50px;
  font-size: 30px;
}
#circle {
  padding-top: 25px;
}
#webInfo {
  padding-top: 10px;
  text-align: center;
  font-family: Georgia, Times, 'Times New Roman', serif;
  font-size: 25px;
}
#webInfo p {
  padding: 0px;
  margin:
}
<div id="header">
  <h1><a href="index.html">Alex Trotter</a></h1>
  <ul id="nav">
    <li><a href="index.html">Home</a>
    </li>
    <img id="circle" src="images/circle.png" />
    <li><a href="about.html">About Me</a>
    </li>
    <img id="circle" src="images/circle.png" />
    <li><a href="contact.html">Contact</a>
    </li>
  </ul>
</div>
<div id="content">
  <div id="webInfo">
    <p>Hi, my name is Alex Trotter and this is my portfolio website</p>
    <p>below you will find some of work that I have created.</p>
    <p>Above you can navigate to different pages to learn more about me.</p>
  </div>
  <div id="exampleWork1"></div>
  <div id="exampleWork2"></div>
  <div id="exampleWork3"></div>
</div>

参考

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用CSS放置横幅和导航栏

CSS 如何在整个导航栏周围放置边框

将导航项(或徽标)居中放置在导航栏的中央

引导程序3-如何将品牌放置在导航栏的中央?

如何使用引导程序4将导航栏放置在粘性导航栏下方?

导航栏下的页面内容使用 CSS 动画移动

我如何在导航栏上当前正在查看的页面下方放置一行?

如何将导航栏及其背景图像放置在页面的背景颜色上

使用导航栏时如何刷新页面

如何在导航栏的右侧放置徽标?

如何将 flexbox 放置在页面中央?

如何在导航页的导航栏部分放置图标

如何使用:使用CSS检查响应式导航栏

我如何使用CSS使导航栏背景图像缩放到导航栏边框

使用CSS定位导航栏

在使用页面锚点导航时,如何设置“激活”类在Angular 2中引导导航栏?

如何使用JS和CSS制作浮动导航栏?

如何使用 CSS 在导航栏下添加阴影?

如何使用CSS和HTML将导航栏居中

如何使用CSS将文本导航栏居中

如何使用CSS选择样式的导航栏链接?

如何使用 CSS 将导航栏的文本居中?

如何使用HTML和CSS在页面中间放置按钮

如何在jetpack compose中使用底部导航栏在屏幕底部放置标签

如何使用Flexbox在导航栏中均匀放置<li>项目?的HTML

如何使导航栏跨页面伸展

使用引导程序4.3.1时,如何在页面中央放置卡片列?

CSS:无法将元素正确放置在粘性导航栏下方

CSS-水平放置我的导航栏