How can I center an anchor link?
I have a link labeled Get D1
which 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?
<div id="allds">
<div id="d1">
<a开发者_如何学C href="http://someurl.com" id="d1link">Get D1</a>
</div>
<div id="d2">
content of d2
</div>
</div>
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.
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?
#d1 { width:whatever; margin:0 auto; }
If you don't want to specify a width, you need to use an alternate technique.
you can set margin-left of d1 to 50%... but allds must have a spec width
精彩评论