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

Kis*_*Kis 7 css anchor vertical-alignment css3 html-lists

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

HTML

<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> 
Run Code Online (Sandbox Code Playgroud)

CSS

nav
{
    height: 30%;
    width: 100%;
}

nav ul
{
    height: 100%;
    margin: 0px;
}

nav ul li
{
    height: 33%;
    width: 100%;
    vertical-align: middle;
}
Run Code Online (Sandbox Code Playgroud)

G-C*_*Cyr 10

您可以使用显示为内联框的伪元素,使用li的全高和垂直对齐midlle.DEMO

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;
}
Run Code Online (Sandbox Code Playgroud)

编辑

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

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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)


Elm*_*lmo 5

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

CSS

ul li a {
    display:flex; // Enables flexbox 
    justify-content: center; // Center on main axis
    align-items: center; // Center on cross axis
}
Run Code Online (Sandbox Code Playgroud)

更新(使用自动边距

你也可以这样做:

ul li { display:flex }
li a { margin: auto }
Run Code Online (Sandbox Code Playgroud)

/* 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;
  */
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)