Results 1 to 5 of 5

Thread: how do I center links within a <div> element?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    how do I center links within a <div> element?

    Here is my css:

    body {
    }
    #sidebar {
    background-color:lightgray;
    width:200px;
    position:fixed;
    height:1000px;
    float:left;
    }

    li {
    list-style-type:none;
    }
    a {
    display:block;
    background-color:white;
    width:150px;
    text-align:center;
    }

    And here is my markup:

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>
    <link href="StyleSheet1.css" rel="stylesheet" />
    </head>
    <body>
    <form id="form1" runat="server">
    <header>Header</header>
    <div id="sidebar">
    <ul>
    <li>
    <a href="WebForm1.aspx" class="btn">Link1</a>

    </li>
    <li>
    <a href="WebForm1.aspx" class="btn">Link2</a>

    </li>



    </ul>
    </div>
    <div>
    <asp:ContentPlaceHolder ID="ContentPlaceHolderMain" runat="server">
    </asp:ContentPlaceHolder>
    </div>
    </form>
    </body>
    </html>

    Currently, it's not centered. It's currently to the right.

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: how do I center links within a <div> element?

    To Center your links inside your Div first you need to wrap them in a Paragraph, then set the paragraph test align style to center!!

    So change your Div markup to this -

    Code:
    <div id="sidebar">
    <ul>
    <li>
    <p class="Links"><a href="WebForm1.aspx" class="btn">Link1</a></p>
    
    </li>
    <li>
    <p class="Links"><a href="WebForm1.aspx" class="btn">Link2</a></p>
    </li>
    </ul>
    </div>
    <div>
    And add a new item to your styles like this -

    Code:
    .Links{
       text-align:center;
    }
    Last edited by NeedSomeAnswers; May 21st, 2015 at 02:29 AM.
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: how do I center links within a <div> element?

    Thanks!

  4. #4
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657

    Re: how do I center links within a <div> element?

    no problem, mark your thread as resolved using the Thread tools if that has fixed your problem
    Please Mark your Thread "Resolved", if the query is solved & Rate those who have helped you



  5. #5
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: how do I center links within a <div> element?

    You can also set a class so that the div is centered:

    HTML Code:
    1. <div class="centered">Hi, from the middle</div>
    CSS Code:
    1. div.centered {
    2.     display: table-cell;
    3.     width: 500px;
    4.     text-align: center;
    5. }
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width