简体   繁体   English

文字无法正确对齐到中心

[英]Texts wont align properly to center

So I got the following code. 所以我得到了以下代码。

HTML 的HTML

            <div id="container">
        <p class="title"> Social Media </p>
        <ul>
            <li><a href="#" class="twitter">Twitter</a></li>
            <li><a href="#" class="deviant">Deviantart</a></li>
            <li><a href="#" class="skype">Skype</a></li>
        </ul>
    </div>

CSS 的CSS

    #container {
background-color: #FFF;
background-image: url(images/footer.jpg);
height: 250px;
margin-top: 40px;
border-top: 3px solid #C6C6C6;
border-bottom: 3px solid #C6C6C6;
}

p.title {
color: #FFF;
font-family: "Open Sans", Helvetica, sans-serif;
text-align: center;
font-weight: bold;
text-transform: uppercase;
letter-spacing: 5px;
font-size: 24px;
text-shadow: 0px 1px 2px rgba(0, 0, 0, 0.5);
}


#container ul {
text-align: center;
word-spacing: 150px;
margin: 0;
padding: 0;
}

#container ul li {
display: inline;
font-family: "Open Sans", Helvetica, sans-serif;
font-size: 18px;
font-weight: bold;
}

#container > ul > li > a {
text-decoration: none;
color: #FFF;
letter-spacing: 5px;
text-transform: uppercase;
line-height: 3;
text-shadow: 0px 1px 2px rgba(0, 0, 0, 0.5);

}

Now, the problem is that the 3 links "TWITTER DEVIANTART SKYPE" wont align exactly as the text above saying "SOCIAL MEDIA" even though it's both aligned to center. 现在的问题是,“ TWITTER DEVIANTART SKYPE”这3个链接即使它们居中对齐,也不会与上面所说的“ SOCIAL MEDIA”完全对齐。

I don't know why though, I want the three links to be centered exactly as the SOCIAL MEDIA text above but it just isn't. 我不知道为什么,但我希望三个链接完全按照上面的“社交媒体”文本居中,但事实并非如此。 Any ideas? 有任何想法吗?

Here's a JSFiddle showing the issue: 这是显示问题的JSFiddle:

http://jsfiddle.net/cuLgC/ http://jsfiddle.net/cuLgC/

Not the nicest way but I put <br> after two of the list elements and that drops them down. 不是最好的方法,但是我在两个列表元素之后放置了<br> ,这使它们下降。

http://jsfiddle.net/cuLgC/7/ http://jsfiddle.net/cuLgC/7/

Change 更改

container ul li{
display: inline
}

To

container ul li{
display: block
}

http://jsfiddle.net/cuLgC/6/ http://jsfiddle.net/cuLgC/6/

Display inline puts them on one line block puts them on separate lines, hope this helps it works in the fiddle. Display inline将它们放在一个行块上,将它们放在单独的行上,希望这有助于它在小提琴中起作用。

just add <br> tag to the list.... here is the fiddle for that..[jsfiddle][1] 只需将<br>标记添加到列表中即可。...这是该提琴的提法。.[jsfiddle] [1]

[1]: http://jsfiddle.net/hh54D/7/ hope this is right... [1]: http//jsfiddle.net/hh54D/7/希望这是对的...

If I understand correctly: 如果我正确理解:

#container {
background-color: #FFF;
background-image: url(images/footer.jpg);
height: 250px;
margin-top: 40px;
border-top: 3px solid #C6C6C6;
border-bottom: 3px solid #C6C6C6;
}

p.title {
color: #FFF;
font-family: "Open Sans", Helvetica, sans-serif;
text-align: center;
font-weight: bold;
text-transform: uppercase;
letter-spacing: 5px;
font-size: 24px;
text-shadow: 0px 1px 2px rgba(0, 0, 0, 0.5);
}


#container ul {
text-align: center;
word-spacing: 150px;
margin: 0;
padding: 0;
}

#container ul li {
/*display: inline;*/
float: left; 
list-style: none;
width: 33.3%;    
font-family: "Open Sans", Helvetica, sans-serif;
font-size: 18px;
font-weight: bold;
}

#container > ul > li > a {
text-decoration: none;
color: #FFF;
letter-spacing: 5px;
text-transform: uppercase;
line-height: 3;
text-shadow: 0px 1px 2px rgba(0, 0, 0, 0.5);
}

FIDDLE 小提琴

Here's an updated fiddle with a fix: JSFiddle 这是带有修补程序的更新的小提琴: JSFiddle

The changes are noted below: 更改记录如下:

#container ul {
  /* text-align: center;  moved to li */
  /* word-spacing: 150px; removed, this was causing issue as some words are longer */
  width: 100%;
  display: table;        /* added  */
  table-layout: fixed;   /* added  */
  margin: 0;
  padding: 0;
}

#container ul li {
  display: table-cell;   /* added  */
  width: auto;           /* added  */
  text-align: center;    /* moved */
  /* display: inline; removed */
  font-family: "Open Sans", Helvetica, sans-serif;
  font-size: 18px;
  font-weight: bold;
}

The misalignment was being caused by: word-spacing: 150px; 对齐错误是由于word-spacing: 150px; and the fact that the menu options are all different widths. 菜单选项的宽度都不同。 If you used the same word 3 times they would align but different length words are spread according to their width. 如果您使用相同的单词3次,它们将对齐,但长度不同的单词将根据其宽度而分布。

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

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