锚元素嵌套在无序列表中,如何垂直对齐锚元素中的文本



我已经广泛搜索并看到了许多关于如何使用vertical-align属性和line-height属性来vertical-align文本的例子。然而,我所有的努力似乎都没有结果,因为我无法让我的文字垂直对齐。我如何做垂直对齐文本居中?height属性不固定,所以我不能使用line-height

<nav>
    <ul>
        <li><a href="html/login.html">Login</a></li>
        <li><a href="html/user_registration.html">Register</a></li>
        <li><a href="#">Programmes Offered</a></li>
    </ul>
</nav> 
CSS

nav
{
    height: 30%;
    width: 100%;
}
nav ul
{
    height: 100%;
    margin: 0px;
}
nav ul li
{
    height: 33%;
    width: 100%;
    vertical-align: middle;
}

您可以使用一个伪元素显示为一个内联框,使用全高为li,垂直对齐到中间。

body, html {
    height:100%; /* needed for demo */
}
nav {
    height: 50%; /* increased for demo */
    width: 100%;
}
nav ul {
    height: 100%;
    margin: 0px;
}
nav ul li {
    height: 33%;
    box-shadow:inset 0 0 0 1px; /* show me li , for demo */
}
nav ul li:before {
    content:'';
    height:100%;
    display:inline-block;
    vertical-align: middle;
}

编辑

如果<a>上重置displayvertical-align,链接可以分散在几行(下面的演示):

body,
html {
  height: 100%;
}
nav {
  height: 70%; /* height set for snippet demo purpose, could be really too much  */
  width: 100%;
}
nav ul {
  height: 100%; /* will follow height, inherit height value , set in nav if any avalaible  */
  margin: 0px;
}
nav ul li {
  height: 33%;
  /* see me and my center*/
  box-shadow: inset 0 0 0 1px;
  background:linear-gradient(to top, rgba(0,0,0,0.1) 50%, rgba(0,0,0,0.2) 50%);
}
nav ul li:before {
  content: '';
  height: 100%;
  display: inline-block;
  vertical-align: middle;
}
a {
  display: inline-block;
  vertical-align: middle;
}
<nav>
  <ul>
    <li><a href="html/login.html">Login</a>
    </li>
    <li><a href="html/user_registration.html">Register</a>
    </li>
    <li><a href="#">Programmes<br/> Offered</a>
    </li>
  </ul>
</nav>

如果你可以使用flexbox,你可以使用下面的css:

CSS

ul li a {
    display:flex; // Enables flexbox 
    justify-content: center; // Center on main axis
    align-items: center; // Center on cross axis
}

更新(使用自动边距)

你也可以这样做:

ul li { display:flex }
li a { margin: auto }

/* These rules are just to make things easier to see. */
nav {
  margin: 0 auto;
  margin-top: 5rem;
  border: 1px dotted green;
  width: 100%;
}
ul {
  padding: 0;
  display: flex;
  justify-content: space-around;
}
li {
  height: 3rem;
  padding: 2rem;
  border: 1px dotted red;
}
/* Here are what I am trying to illustrate */
ul li {
  display: flex;
}
a {
  margin: auto;
  /* or adjust them one by one, by targeting 
     the ones you want and setting
     using each margin like this:
  
    margin-top: auto;
    margin-right: auto;
    margin-bottom: auto;
    margin-left: auto;
  */
}
<nav>
  <ul>
    <li><a href="html/login.html">Login</a></li>
    <li><a href="html/user_registration.html">Register</a></li>
    <li><a href="#">Programmes Offered</a></li>
  </ul>
</nav>

vertical align将内联元素与其兄弟元素对齐。除非在表格单元格中使用。

我不认为有一个照本的垂直对齐的方法…但这应该可以工作:

D E M O

nav ul li
{
    background:#f1f1f1;
    height: 33%;
    width: 100%;
    border-top:1px solid #e0e0e0;
}
nav ul li a
{
    display:block;
    position:relative;
    top:50%;
    -webkit-transform:translate(0,-50%);
    -moz-transform:translate(0,-50%);
    transform:translate(0,-50%);
}

你试过了吗

nav ul li a
{
    height: 33%;
    width: 100%;
    vertical-align: 5px; //(you can make it 10px or -10px, just so it fits your style)
}

你的文本在a标签内,所以在css中添加a可能会解决你的问题:)

最新更新