3

その上のヘッダーに関連してaa ul要素を中央揃えにしようとしています。ul 要素は、連絡先情報 (電子メール、Twitter、RSS など) のリストで構成されます。これは私がこれまでに持っているものです:

整列していない

これは、次のコードで実現されます

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 クラスは、その上のヘッダーに対して中央に配置されていません。

margin-left: 425px;.intouch クラスに追加することで、中央揃えを実現できます。これが生成されます:

整列

私が知りたいのは、左マージンを追加するだけでなく、この中央揃えを実現するためのより良い方法があるかということです (li のテキストの長さに応じて変更する必要がありますか?)

4

1 に答える 1

3

@Sparky のコメントに続いて、「幅がないと、何かを自動的に中央に配置することはできません。」.intouch に幅を追加しました:

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

これにより、.intouch が完全に中央に配置されます。.intouch のコンテンツが変更されると、幅プロパティが再調整され、要素が中央に維持されます。

于 2013-04-01T15:49:31.357 に答える