You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使Navbar、主内容区和Footer居中并设置80%宽度

问题描述

我是网页开发新手,正在给本地运动队做网页——他们现有的页面效果差,WordPress又不好用,所以决定自己写代码。

现在遇到的问题:没法让Navbar、主内容区和Footer在页面居中,希望这三个元素宽度设为80%,这样能露出部分球员照片背景。试过float、margin、flex、grid这些CSS样式,都没解决。

附上相关代码:

<style>
#footerWrapper { 
    max-width: 80%;
    max-height: 5%;
    margin-bottom: 20px;
    line-height: 30px;
    font-size: 0.9em;
    border: 0 solid #ddd;
    border-bottom-color: #3F8444;
    border-width: 1px 0;
}
/*Positions and Margins*/
.image {
    position: absolute; 
    top:100px; 
    left:100px;
}
.image-1 {
    position: absolute;
    top:20px;
    right: 100px;
}
header{
    text-align: center;
    margin-top: 100px;
    margin-bottom: 100px;
}
main{ 
    width: 80%;
    display: flex;
    flex-direction: row;
    justify-content: space-evenly;
    align-items: center;
}
footer{ 
    position: static;
    left: 0;
    bottom: 0;
    width: 80%;
    height: 5%;
}
p {
    margin-left: 5px;
    margin-right: 5px;
}
/*Navigation*/
ul { 
    display: flex;
    flex-direction: row;
    align-items: center;
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #04AA6D;
    width: 80%;
}

/*li {float: left;}*/

li a, .dropdown li a {
    display: block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

li a:hover, .dropdown:hover .dropbtn {background-color: #111;}

li.dropdown {
    display: inline-block;
}

.dropdown-content { 
    display:none;
    position: absolute;
    background-color: #04AA6D;
    color: white;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0,.2);
    z-index: 1;
}
.dropdown-content a
{
    color: white;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}
.dropdown-content a:hover {background-color: #111;color: white;}
.dropdown:hover .dropdown-content
{
    display: block;
}
</style>    
</div>
</head>
<body>
    <header>
        <div class="image" id="Sports Team logo">
        <img src="Sport Teams logo">
    </div>
    <div class="image-1" id="Club logo">
        <img src="Club logo" width="246" height="237">
    </div>
        <h1>Sports Team</h1>
    </header>
    <nav>
    <ul>
        <li><a href="Sports Team url link">Home</a></li>
        <li class="dropdown">
          <a class="dropbtn">Club Info</a>
          <div class="dropdown-content">
            <a href="Sports Team url link">About Us</a>
            <a href="Sports Team url link">Committee</a>
            <a href="Sports Team url link">Contact Us</a>
            <a href="Sports Team url link">Game Rules</a>
            <a href="Sports Team url link">Uniforms</a>
            <a href="Sports Team url link">Fair Play</a>
            <a href="Sports Team url link">Terms and Conditions</a>
          </div>
        <li class="dropdown">
            <a class="dropbtn">Winter Season 2023</a>
            <div class="dropdown-content">
          <a href="Sports Team url link">Training Information</a>
          <a href="Sports Team url link">Game Details</a>
              <a href="Sports Team url link">Registration</a>
              <a href="Sports Team url link">Season Info Pack</a>
              <a href="Sports Team url link">Handbook</a>
              <a href="Sports Team url link">Privacy Policy</a>
                </div>
        <li class="dropdown">
            <a class="dropbtn">Summer Season 2023</a>
            <div class="dropdown-content">
              <a href="Sports Team url link">Training Information</a>
              <a href="Sports Team url link">Game Details</a>
              <a href="Sports Team url link">Registration</a>
              <a href="Sports Team url link">Season Info Pack</a>
                  </div>
          <li><a href="Sports Team url link">Umpire Information</a>
      </ul>
    </nav>
<Div></Div>
<main>
  <div class="main">
   <h2>Contact Us</h2>
   <p>Sports Team email</p>
   </main>
   </body>
</html>
解决方案

核心问题是你已经给这些元素设置了80%宽度,但没加上水平居中的关键样式:margin: 0 auto;——块级元素设置固定/百分比宽度后,左右margin设为auto就会自动水平居中。

另外注意几个小问题:你的HTML里有未闭合的标签(比如li和main里的div),这些也可能导致布局异常,先修复。

修改后的关键CSS代码

/* 导航栏ul居中 */
ul { 
    display: flex;
    flex-direction: row;
    align-items: center;
    list-style-type: none;
    margin: 0 auto; /* 新增这行 */
    padding: 0;
    overflow: hidden;
    background-color: #04AA6D;
    width: 80%;
}

/* 主内容区main居中 */
main{ 
    width: 80%;
    display: flex;
    flex-direction: row;
    justify-content: space-evenly;
    align-items: center;
    margin: 0 auto; /* 新增这行 */
}

/* Footer居中,去掉多余的left:0 */
footer{ 
    position: static;
    bottom: 0;
    width: 80%;
    height: 5%;
    margin: 0 auto; /* 新增这行 */
}

/* 如果footerWrapper是包裹footer的容器,也加上居中 */
#footerWrapper { 
    max-width: 80%;
    max-height: 5%;
    margin: 0 auto 20px; /* 修改margin,上下和左右分开设置 */
    line-height: 30px;
    font-size: 0.9em;
    border: 0 solid #ddd;
    border-bottom-color: #3F8444;
    border-width: 1px 0;
}

修复后的HTML结构片段

<nav>
    <ul>
        <li><a href="Sports Team url link">Home</a></li>
        <li class="dropdown">
            <a class="dropbtn">Club Info</a>
            <div class="dropdown-content">
                <a href="Sports Team url link">About Us</a>
                <a href="Sports Team url link">Committee</a>
                <a href="Sports Team url link">Contact Us</a>
                <a href="Sports Team url link">Game Rules</a>
                <a href="Sports Team url link">Uniforms</a>
                <a href="Sports Team url link">Fair Play</a>
                <a href="Sports Team url link">Terms and Conditions</a>
            </div>
        </li> <!-- 新增闭合标签 -->
        <li class="dropdown">
            <a class="dropbtn">Winter Season 2023</a>
            <div class="dropdown-content">
                <a href="Sports Team url link">Training Information</a>
                <a href="Sports Team url link">Game Details</a>
                <a href="Sports Team url link">Registration</a>
                <a href="Sports Team url link">Season Info Pack</a>
                <a href="Sports Team url link">Handbook</a>
                <a href="Sports Team url link">Privacy Policy</a>
            </div>
        </li> <!-- 新增闭合标签 -->
        <li class="dropdown">
            <a class="dropbtn">Summer Season 2023</a>
            <div class="dropdown-content">
                <a href="Sports Team url link">Training Information</a>
                <a href="Sports Team url link">Game Details</a>
                <a href="Sports Team url link">Registration</a>
                <a href="Sports Team url link">Season Info Pack</a>
            </div>
        </li> <!-- 新增闭合标签 -->
        <li><a href="Sports Team url link">Umpire Information</a></li> <!-- 新增闭合标签 -->
    </ul>
</nav>
<Div></Div>
<main>
    <div class="main">
        <h2>Contact Us</h2>
        <p>Sports Team email</p>
    </div> <!-- 新增闭合标签 -->
</main>

这样修改后,Navbar、main和Footer就能水平居中,同时保持80%宽度,露出背景图了。

内容的提问来源于stack exchange,提问作者NewbieWebCoder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 05:07:36