居中对齐 ul 元素

3

我正在尝试使一个ul元素相对于其上方的标题居中对齐。该ul元素包含联系信息列表(电子邮件、Twitter、RSS等)。目前为止,这是我的代码:

unaligned

使用以下代码实现:

html

<html>
<head>
    <meta charset="utf-8" />
    <title>Jing Xue - Portfolio</title>
    <link rel="stylesheet" href="css/normalize.css" />
    <link rel="stylesheet" href="css/style.css" />
    <link href='http://fonts.googleapis.com/css?family=Delius+Swash+Caps' rel='stylesheet' type='text/css'>
    <link href='http://fonts.googleapis.com/css?family=Playfair+Display+SC:700' rel='stylesheet' type='text/css'>
</head>
<body>

<div class="header">
    <div class="wrapper">
    <h1>Portfolio of...</h1>    
    <h2>Jing Xue</h2>
    <ul class="intouch">
        <li class="twitter"><a href="">Twitter</a></li>
        <li class="rss"><a href="">RSS</a></li>
        <li class="email"><a href="">Email</a></li>
    </ul>
    </div>
</div>
</body>

css

body {
    background-color: rgb(255,255,255);
    color: rgb(59,67,68);
    margin: 0;
    padding: 0;
    font: 1em "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Verdana, Tahoma, sans-serif;
}

h1, h2, h3 {
    text-shadow: 1px 1px 2px rgba(0,0,0,0.3);
}

.wrapper {
    width: 940px;
    margin: 0 auto 0 auto;
}

/* header ------------------------------------------------------------------ */

.header {
    text-align: center;
    padding: 40px 0 0 0;
    margin-bottom: 40px;
}

.header h1, .header h2 {
     margin: 0;
}

.header h1 {
    font-family: 'Delius Swash Caps', cursive;
    font-size: 250%;
    color: rgb(200,50,50);
}

.header h2 {
    font-family: 'Playfair Display SC', serif;
    font-size: 450%;
    color: rgb(59,67,68);
}


.intouch {
    list-style: none;
    margin: 0 auto;
    padding: 0;
    width: 0;
}

.intouch li a:link, .intouch li a:visited {
  padding: 0.5em 0 0.5em 40px;
  display: block;
  font-weight: bold;
  background-repeat: no-repeat;
  background-image: url(../img/sprite-roll.png);
  text-decoration: none;
  color: rgb(136,136,136);
}

.intouch li.twitter a:link, .intouch li.twitter a:visited {
  background-position: 0 6px;
}

.intouch li.twitter a:hover {
  background-position: 0 -90px;
  color: rgb(105,210,231);
}

.intouch li.rss a:link, .intouch li.rss a:visited {
  background-position: 0 -30px;
}

.intouch li.email a:link, .intouch li.email a:visited {
  background-position: 0 -60px;
}

.intouch li.twitter a:hover {
  background-position: 0 -90px;
  color: rgb(105,210,231);
}

.intouch li.rss a:hover {
  background-position: 0 -126px;
  color: rgb(243,134,48);
}

.intouch li.email a:hover {
  background-position: 0 -156px;
  color: rgb(56,55,54);
}

正如您所看到的,.intouch ul类与上面的标题没有居中对齐。

我可以通过向.intouch类添加margin-left: 425px;来实现居中对齐。效果如下:

aligned

我想知道是否有更好的方法来实现这种居中对齐,而不仅仅是添加左边距(这取决于li中文本的长度)?


你能提供一个实时网站或JSFiddle吗?你尝试过margin: 0 auto;吗? - Michael
1
没有指定 宽度,无法自动居中某个元素。 - Sparky
这是一个非常笼统的说法,而且并不完全正确。在text-align:center的父元素中,内联块元素可以很好地居中。 - Dawson
1个回答

3
继@Sparky的评论“没有宽度,您无法自动居中某些内容”之后,我已经为.intouch添加了一个宽度:
.intouch {
   list-style: none;
   margin: 0 auto;
   padding: 0;
  width: 110px;
}

这将完美地将.intouch居中对齐。当.intouch的内容改变时,宽度属性将被重新调整,保持元素居中。


我很高兴能够帮忙。请不要忘记在系统允许的时候接受您自己的答案。 - Sparky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接