CSS 我怎样才能把这个链接居中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4206348/
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 23:11:40  来源:igfitidea点击:

How can I center this link

css

提问by silow

I have a link labeled Get D1which I need to center in the middle of the page. I tried text-align:center, but that works on the text itself and doesn't actually center the link in the middle of the page. Does anyone know the css trick for this?

我有一个标记的链接Get D1,我需要将其置于页面中间。我尝试过 text-align:center,但这适用于文本本身,实际上并没有将链接置于页面中间。有谁知道这个 css 技巧吗?

<div id="allds">
    <div id="d1">
        <a href="http://someurl.com" id="d1link">Get D1</a>
    </div>
    <div id="d2">
        content of d2
    </div>
</div>

采纳答案by meder omuraliev

#d1 { width:whatever; margin:0 auto; }

If you don't want to specify a width, you need to use an alternate technique.

如果不想指定宽度,则需要使用替代技术。

回答by Mark Steggles

As simple as:

就这么简单:

#d1link {display:block;text-align:center}

text-align:center doesn't work on inline elements. An anchor is an inline element by default.

text-align:center 不适用于内联元素。默认情况下,锚点是内联元素。

回答by Brandon Durham

Here ya go:

给你:

#link-container {
    text-align: center;
}
#link {
    background: black;
    color: white;
    display: inline-block;
    padding: 10px;
}

<div id="link-container">
    <a href="#" id="link">This is my link!</a>
</div>

That what you need?

那你需要什么?

回答by stoeffel

you can set margin-left of d1 to 50%... but allds must have a spec width

您可以将 d1 的 margin-left 设置为 50%...但 allds 必须具有规格宽度