html - 一个元素不能居中,因为有些东西看起来不对

标签 html css user-interface alignment

我正在尝试制作一个网页,其中所有内容都位于中心 40%,但顶部标题似乎没有以水平导航栏为中心。看看左边的空间似乎比右边多,文本比导航栏长?

screen shot

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Sample test page</title>
<style>
#navigation li {
    font 34px Arial;
    display: inline-block;
    list-style-type: none;

}

#navigation a {
    background: #140913;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    border-radius: 3px;
    display: inline-block;
    cursor: pointer;
    color: #ffffff;
    font-size: 18px;
    padding: 5px 19px;
    text-decoration: none;
    text-shadow: 0px 1px 0px #2f6627;
}

#navigation a:hover {
    background: #4a3a4a;
    text-decoration: none;
}





#text {
    width: 90%;
    margin: 0 auto;
    text-align: left;
}

#background {
    background-size: 100% auto;
}

.stretch {
    width:100%;
    height:100%;
}

body {
    margin-top: auto;
    margin-right: auto;
    margin-left: auto;
    margin-bottom: auto;
    text-align: center;
    width: 50%

}

footer { 
    margin: auto; 
    text-align: center;
}


</style>
</head>

<body>
<nav>
  <div id="navigation">
  <ul>
    <li><a href="#">AAAAAAA</a></li>
    <li><a href="#">BBBBB</a></li>
    <li><a href="#">CCCCCCC</a></li>
    <li><a href="#">DDDDDDD</a></li>
    <li><a href="#">EEEEEEE</a></li>
    <li><a href="#">FFFFF</a></li>
    <li><a href="#">GGGGGGG</a></li>
  </ul>
  </div>
</nav>
<div id="background"> <br />
</div>
<div id="wordcloud">
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit quisque malesuada erat sed sagittis gravida<br /> suspendisse in sem sed magna ultrices.</p>
</div>
<div id="wordclouddescription">
  <h2>The sentence will be aproximitely the same size here as on the final product, thankyou very much.</h2>
  <p>&nbsp;</p>
  <div id="text">  </div>
</div>
</body>
</html>

最佳答案

我想你必须重新设置你的 ul,它仍然有 40px 的填充。

#navigation ul {
  padding: 0;
  margin: 0;
}

关于html - 一个元素不能居中,因为有些东西看起来不对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31239146/

相关文章:

jQuery 使用 $(selector).each 在前面添加多个 div

html - 使用关键帧动画化 CSS 按钮边框

css - Gulp 生成空的 css 文件(编译 sass 到 css)

java - 带数字键盘的按键监听器?

javascript - jQuery 加载窗口,同时等待长函数完成

javascript - 调整容器内的字体文本(在多个 div 内)以完美适应

javascript-单个onclick事件来突出显示不同表格中的单元格

php - 像 DIV 背景一样裁剪图像

html - CSS Calc() 使用两个像素数量来获得百分比

java - 在 GUI 中显示一些可选择的字符串