CSS,将 div 内的链接居中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3399216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 22:30:58  来源:igfitidea点击:

CSS, centering links inside div

css

提问by webmasters

how can I center my links like this? They all are centered inside a div, but they begin from the same distance.

我怎样才能像这样将我的链接居中?它们都在 a 内居中div,但它们从相同的距离开始。

i am link 1
i am a longer link than link 1
i am a short link
we are all centered in div

If I apply text-align: center, I get:

如果我申请text-align: center,我会得到:

         i am link 1
i am a longer link than link 1
      i am a short link
   we are all centered in div

Thank you ;)

谢谢 ;)

I have a table:

我有一张桌子:

<td valign='top'><a href='{url}' title='{title}' target='_blank'>{anchor}</a></td>

<td valign='top'><a href='{url}' title='{title}' target='_blank'>{anchor}</a></td>

The links need to be centered in my way, each in its own td. Not sure it's possible.

链接需要以我的方式居中,每个链接都有自己的td. 不确定是否有可能。

回答by meder omuraliev

If you need shrinkwrapped centering, apply inline-blockon the things you want centered. Then text-align:center;on the parent.

如果您需要收缩包装居中,请应用inline-block在您想要居中的东西上。然后text-align:center;在父母身上。

You'll need to probably have a workaround for IE, but as you haven't posted code I won't go into that.

您可能需要为 IE 找到一个解决方法,但由于您尚未发布代码,因此我不会深入探讨。

回答by Ian Wetherbee

Wrap the links in another <div style="width: 300px; margin: 0 auto;">to group them together.

将链接包装在另一个中<div style="width: 300px; margin: 0 auto;">以将它们组合在一起。

http://jsfiddle.net/NcEpe/1/

http://jsfiddle.net/NcEpe/1/

Editfor dynamic widths: http://matthewjamestaylor.com/blog/beautiful-css-centered-menus-no-hacks-full-cross-browser-support

编辑动态宽度:http: //matthewjamestaylor.com/blog/beautiful-css-central-menus-no-hacks-full-cross-browser-support

回答by Paul Sheldrake

Try text-align: left;

尝试 text-align: left;

回答by kbrimington

Do this: nest a left-aligned div in a center-aligned div.

这样做:在居中对齐的 div 中嵌套一个左对齐的 div。

<div style="text-align:center">
    <div style="text-align:left; width:300px;">
        Content<br />
        Longer Content<br />
        Really long Content<br />
        Content<br />
        Content
    </div>
</div>

Addition:

添加:

CSS lovers everywhere will condemn me for suggesting that you could replace the inner div with a table, which would remove the need to specify a fixed (or percentage) width.

各地的 CSS 爱好者都会谴责我建议您可以用表格替换内部 div,这将消除指定固定(或百分比)宽度的需要。