Jump to content

How To Have The Div Aligned In The Middle, And Relative To Its Parent?


itran

Recommended Posts

The HTML:

 

<div class="parent">
<div>
 <a href="">First Page</a><a href="">Second Page</a><a href="">Third Page</a>
</div>
</div>

 

 

The CSS:

 

div.parent > div > a {
margin-right: 2em;		
}
div.parent {
border: 1px solid red;
height: 100px;
background-color: #bbb;
}
div.parent > div {
border: 1px solid lightblue;
background-color: #fff;

height: 40px;
width: 900px;

display: block;
float: default;
margin: 0 auto 0;
}

 

The Example:

The result of the stylesheet can be seen on JSFiddle: http://jsfiddle.net/BMEXR/2/

 

Notice: The colors and spacings are set for better differentiating.

 

 

The Question Again:

How can the div be aligned in the middle, and that relative to its parent?

 

So that the div will move relative to the height setting of the parent div?

Edited by itran
Link to comment
Share on other sites

Just played around with your code and it seems to be working now how you want (I think).

 

Check it out: http://jsfiddle.net/BMEXR/44/

 

You can add any width to the parent div now and your links will always align to the center. This is because your unordered list is spanning 100% of the parent but what really makes it work is setting the text-align: center style. However large your unordered list area is, setting text-align to center will ensure it is always centered within the div.

 

Hope this is your desired result!

 

Kind regards,

 

AotB.

Edited by AoTBuNgLe
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.