【问题标题】:Vertically center an HTML header垂直居中 HTML 标头
【发布时间】:2016-06-08 02:03:44
【问题描述】:

我似乎无法让我的标题使标题居中在标题框的中间。它现在的样子,它有新闻、联系人和关于链接在框中的高处,而不是居中。

HTML:

<ul>
<li style="float:left"><a class="active" <a onclick="window.open(this.href, this.target);return false;" href="http://link/" target="link"> <img src="Logo.png" alt="Logo"    style="width:125px;height:75px;"></a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>

CSS:

ul {
    list-style-type: none;
    margin: 0;
    padding: 10px;
    overflow: hidden;
    background-color: black;
}

li {
    float: left;
}

li a {
    display: inline-block;
    color: white;
    text-align: center;
    padding: 14px 20px;
    text-decoration: none;
}

li a:hover:not(.active) {
    background-color: red;
}

.active {
    background-color: black;
} 

【问题讨论】:

  • 你能发布一个div网站的链接吗?
  • 澄清一下,您问的是垂直居中,不是水平居中?
  • 是垂直居中
  • 你可以在 jsfiddle 中发布你的代码吗?或者它是如何出现的截图?顺便说一句,你的 html 代码看起来不太好。

标签: html css


【解决方案1】:

&lt;li&gt; 上试试这个:

.li {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

how this works

编辑:清理了一些不平衡的 HTML 问题并清理了 css:

ul {
  list-style-type: none;
  margin: 0;
  padding: 50px; /* adjust as necessary */
  overflow: hidden;
  background-color: black;
}

li {
  float: left;
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

li a {
  display: inline-block;
  color: white;
  text-align: center;
  padding: 14px 20px;
  text-decoration: none;
}
<ul>
  <li>
    <a class="active" onclick="window.open(this.href, this.target);return false;" href="http://link/" target="link"> <img src="Logo.png" alt="Logo" style="width:125px;height:75px;"></a>
  </li>
  <li><a href="#news">News</a></li>
  <li><a href="#contact">Contact</a></li>
  <li><a href="#about">About</a></li>
</ul>

【讨论】:

  • 不完全是,不是将它们排成一行,而是将它们堆叠在一起
【解决方案2】:

这是一种更现代的方法:

https://jsfiddle.net/qq0t46pt/2/

flexbox 现在也得到了很好的支持,并且更容易实现。


HTML

<ul>
<li style="float:left"><a class="active"> <a onclick="window.open(this.href, this.target);return false;" href="http://link/" target="link"> <img src="Logo.png" alt="Logo"    style="width:125px;height:75px;"></a></a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>

CSS

ul {
    list-style-type: none;
    margin: 0;
    padding: 10px;
    overflow: hidden;
    background-color: black;

    display: -webkit-flexbox;
    display: -ms-flexbox;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-align: center;
    -ms-flex-align: center;
    -webkit-align-items: center;
    align-items: center;
    justify-content: center;
    height: 370px;
}

li {
    float: left;
}

li a {
    display: inline-block;
    color: white;
    text-align: center;
    padding: 14px 20px;
    text-decoration: none;
}

li a:hover:not(.active) {
    background-color: red;
}

.active {
    background-color: black;
} 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 2011-03-09
    • 2012-12-28
    • 2016-11-11
    • 1970-01-01
    相关资源
    最近更新 更多