简体   繁体   English

如何在列表项中居中放置文本和子行?

[英]How to center text and a subline in a list item?

I had a list that was structured like this: 我有一个结构如下的列表:

<ul>
  <li>
    <a href="#">Link</a>
  </li>
</ul>

I wanted each of my list items to have a height of 50px, so I set the line-height of <a> to 50px and it worked fine. 我希望每个列表项的高度都为50px,因此我将<a>的行高设置为50px,它可以正常工作。

However, now I'm trying to add a sub-line to each <a> tag so that it looks like this: 但是,现在我试图在每个<a>标记中添加一个子行,使其看起来像这样:

<ul>
  <li>
    <a href="#">
      Link
      <span class="sub-line">Link Info</span>
    </a>
  </li>
</ul>

The sub-line would be a smaller font and would show up comfortably under the link text. 子行将是较小的字体,并会舒适地显示在链接文本下。 I've been messing around with display and line-height and absolute positioning, but I've had no luck coming up with a clean solution. 我一直在搞乱显示,行高和绝对位置,但是我没有运气想出一个干净的解决方案。 Help would be greatly appreciated! 帮助将不胜感激!

You mean something like this? 你的意思是这样的吗?

a {
    position: relative;
}

a span {
    position: absolute;
    font-size: 0.5em;
    bottom: -1em;
    left: 0;
    width: 100%;
}

JSFiddle JSFiddle

This is what I understand from your question: 这是我从您的问题中了解到的:

Fiddle 小提琴

HTML: HTML:

<a href="#">
    Link
    <sub>Link Info</sub>
</a>

CSS: CSS:

a     { text-decoration: none; }
a sub { font-size: 8px; }
li    { text-align: center; }

You can just use display: block to put the sub line below the link text with a smaller font 您可以只使用display: block将子行放在链接文本下方,并使用较小的字体

.sub-line {
    display: block;
    font-size: 80%;
}

To reduce the distance 减少距离

    line-height: 50%;

and to center both horizontally 并同时水平居中

li {
    text-align: center;
}

See JSFiddle 参见JSFiddle

DEMO 演示

Try this: 尝试这个:

li {
  display: inline-block;
}
a {
  line-height: 50px;
  display: block;
}
span {
  display: block;
  line-height: 1px;
  font-size: 75%;
  //Here you can play with negative margin-top for better positioning 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM