bmcswee
bmcswee

Reputation: 107

CSS Positioning element relative to grandparent?

I'm trying to position an element (a button) relative to the element 2 elements before it (a picture). There is a varying amount of text between the picture and the button. Take a look at my site:

http://gorilla-gym.com/product-category/fitness-attachments/

What I'm trying to achieve is having the "Shop Now" buttons align horizontally for each product listing regardless of how much text is underneath the picture.

It seemed to me the most logical way to do this way to position the button relative to the picture, but I can't figure out how to do this. Let me know if you guys have an idea of how to do this, or if there's a better way to achieve what I want to do.

Thanks in advance.

Upvotes: 3

Views: 9632

Answers (4)

Matt
Matt

Reputation: 115

This question is better answered here How to set relative position with Grandfather! element? simply setting position: relative on the grandfather element and position: absolute on the subject element.

That solution does rely on there being no positioning set on intermediate elements.

Upvotes: 0

Manish Sharma
Manish Sharma

Reputation: 1720

check this one i think you want something like this

http://jsfiddle.net/FWzzR/1/

css

ul.products { 
    display:table; 
    width:100%; 
    table-layout:fixed; 
    border-collapse:separate; 
    border-spacing:10px; 
}

.products > li { 
    background-color: #4F81BD; 
    border:2px solid #385D8A; 
    position: relative; 
    width: 22.05%; 
    display: table-cell; 
    padding:10px; 
    padding-bottom:50px; 
    text-align:center; 
    vertical-align:top; 
}

.products > li >a { 
    display:block; 
}

.products a.button { 
    position:absolute; 
    bottom:10px; 
    left:50%; 
    margin-left:-40px; 
    font-size: 100%; 
    line-height: 1em; 
    cursor: pointer; 
    text-decoration: none; 
    padding: 6px 10px; 
    font-family: inherit; 
    font-weight: bold; 
    color: #FFF; 
    text-shadow: 0 1px 0 #FF6311; 
    text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.8); 
    border: 1px solid #973100; 
    -webkit-border-radius: 2px; 
    -moz-border-radius: 2px; 
    border-radius: 2px; 
    background: #FD5200; 
    background: -webkit-gradient(linear, left top, left bottom, from(#FD5200), to(#CA4100)); 
    background: -webkit-linear-gradient(#FD5200, #CA4100); 
    background: -moz-linear-gradient(center top, #FD5200 0%, #CA4100 100%); 
    background: -moz-gradient(center top, #FD5200 0%, #CA4100 100%); 
    -webkit-box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.075), inset 0 1px 0 rgba(255, 255, 255, 0.3), 0 1px 2px rgba(0, 0, 0, 0.1); 
    -moz-box-shadow: inset 0 -1px 0 rgba(0,0,0,0.075), inset 0 1px 0 rgba(255,255,255,0.3), 0 1px 2px rgba(0,0,0,0.1); 
    box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.075), inset 0 1px 0 rgba(255, 255, 255, 0.3), 0 1px 2px rgba(0, 0, 0, 0.1); 
}

Upvotes: 1

Adam Jenkins
Adam Jenkins

Reputation: 55613

If there is a varying amount of text underneath the picture, then the elements will all be of varying height and you cannot align the "Shop Now" button horizontally beneath the picture. The only way to accomplish this is by making sure that all the divs are the same height, then you just position the shop now button as follows:

<div class="shop-now-div">
 <img src="yourimage.jpg">
 Lorem ipsum....
 <a class="button" href="#">Shop Now</a>
</div>


.button { position: absolute; bottom: 5px; right: 5px; }
.shop-now-div { position: relative; }

There are two ways to make your div's the same height 1) JavaScript (not recommended, it's a pain) 2) A table (do it in CSS so you aren't messing with semantics)

UNFORTUNATELY, some modern browsers (Firefox, I believe) will not support position: relative on table-cell's (which you will need), so you are stuck with having to use JS to make your div's the same height....

Easiest solution: Stick your shop now button on top of the image - that way you can easily align them horizontally. :)

Upvotes: 0

Mudassir Ali
Mudassir Ali

Reputation: 8041

If all you want is to center align the "Shop Now" button at the bottom, then

.shopnow_button{
  display: block;
  margin: 0 auto; //something was overriding so I had to do !important here
  width: 57px; // can be any value < the width of the parent container(Ofcourse !)
}

Upvotes: 0

Related Questions