Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I center this link

Tags:

css

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 href="http://someurl.com" id="d1link">Get D1</a>
    </div>
    <div id="d2">
        content of d2
    </div>
</div>
like image 725
silow Avatar asked Nov 17 '10 16:11

silow


People also ask

How do you center a link?

1) Centering links by putting it inside of a text aligned div. Place the HTML link inside of a div. In the styles for the div, apply text-align:center and make the anchor tag an inline-block (display:inline-block).

How do I center text in a link in HTML?

To center text using HTML, you can use the <center> tag or use a CSS property.

How do you center a link in an element?

Set “text-align: center” to the parent element's div to center it using a wrapper or container (also known as a container). In this case, the inside div should be set to display inline block.


2 Answers

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.

like image 147
Mark Steggles Avatar answered Oct 25 '22 16:10

Mark Steggles


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?

like image 32
Brandon Durham Avatar answered Oct 25 '22 15:10

Brandon Durham