Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 在页面上垂直和水平居中放置CSS菜单_Html_Css - Fatal编程技术网

Html 在页面上垂直和水平居中放置CSS菜单

Html 在页面上垂直和水平居中放置CSS菜单,html,css,Html,Css,在我网站的第一页上,我只想让导航菜单垂直和水平居中。我尝试了很多不同的方法,这是我最近的一次尝试 我不确定如何水平居中链接1到4,我也不确定菜单是否在蓝色框中垂直居中 (另外,这不是实际的配色方案。我添加了彩色背景以显示每个元素的边界。) 以下是HTML: <body> <div id="centered"> <ul> <li><h1><a href="link1.html">LINK1</a></

在我网站的第一页上,我只想让导航菜单垂直和水平居中。我尝试了很多不同的方法,这是我最近的一次尝试

我不确定如何水平居中链接1到4,我也不确定菜单是否在蓝色框中垂直居中

(另外,这不是实际的配色方案。我添加了彩色背景以显示每个元素的边界。)

以下是HTML:

<body>
<div id="centered">
<ul>
    <li><h1><a href="link1.html">LINK1</a></h1></li>
    <li><h1><a href="link2.html">LINK2</a></h1></li>
    <li><h1><a href="link3.html">LINK3</a></h1></li>
    <li><h1><a href="link4.html">LINK4</a></h1></li>
</ul>     
</div>
</body>

此处演示:

CSS更改

html, body {
    background-color: black;
    height: 100%;
    margin: 0;
    padding: 0;
    color: white;
    text-align: center;
}
a {
    text-decoration: none;
    color: #fff;
}
h1 {
    font-size: 1.5em;
    color: #000;
}
#centered {
    border: solid black;
    background-color: blue;
    height: 20%;
    width: 100%;
    position: relative;
    top: 40%;
    vertical-align:middle;
}
#centered ul {
    list-style-type:none;
    text-align:center;
    display:inline-block;
    margin-right:auto;
}
#centered li {
    float:left;
    padding: 3px 6px;
}

首先不要在链接中使用标题,其次…这里有一个


#居中{ 边界:无; 背景:蓝色; 高度:150像素; 宽度:100%; 文本对齐:居中; 位置:绝对位置; 最高:50%; 利润上限:-75px; } #中心ul{ 列表样式:无; 边缘顶部:58px; 填充:0; } #李正中{ 显示:内联块; 利润率:0.20px 0.20px; } #李娜{ 文字装饰:无; 字体系列:Verdana; 字体大小:24px; 颜色:#fff; 过渡:颜色0.2s线性; } #李娜:停下来{ 颜色:#888; }
谢谢!这很接近,但看起来链接1左侧的空间比链接4右侧的空间大。你知道为什么会这样吗?试着去掉
#centered li
html, body {
    background-color: black;
    height: 100%;
    margin: 0;
    padding: 0;
    color: white;
    text-align: center;
}
a {
    text-decoration: none;
    color: #fff;
}
h1 {
    font-size: 1.5em;
    color: #000;
}
#centered {
    border: solid black;
    background-color: blue;
    height: 20%;
    width: 100%;
    position: relative;
    top: 40%;
    vertical-align:middle;
}
#centered ul {
    list-style-type:none;
    text-align:center;
    display:inline-block;
    margin-right:auto;
}
#centered li {
    float:left;
    padding: 3px 6px;
}
<div id="centered">
  <ul>
    <li><a href="link1.html">LINK 1</a></li>
    <li><a href="link2.html">LINK 2</a></li>
    <li><a href="link3.html">LINK 3</a></li>
    <li><a href="link4.html">LINK 4</a></li>
  </ul>     
</div>


#centered { 
  border: none; 
  background: blue; 
  height: 150px; 
  width: 100%;
  text-align: center;
  position: absolute; 
  top: 50%;
  margin-top: -75px;
}

#centered ul {
  list-style: none;
  margin-top: 58px;
  padding: 0;
} 

#centered li {
  display: inline-block;
  margin: 0 20px 0 20px;
}

#centered li a { 
  text-decoration: none; 
  font-family: Verdana;
  font-size: 24px;
  color: #fff;
  transition: color 0.2s linear;
}
#centered li a:hover {
  color: #888;
}