2

I apologize if this has been answered time and time again. I remember searching thoroughly for an answer a couple years ago when I first wrote up my website script, but I couldn't ever find one. The same for now.

Recently I reworked my website's script so I can host it onto Weebly. Here is one of the four pages of my site that I need help with. As you can see, the images that pop up when the thumbnail is hovered over are absolutely positioned. For most computer resolutions and/or browsers, this will have the image appear out of the designated box. How could I position them to the inner top left corner of the div? Or better yet, horizontally and vertically centered within it?

<section id="Sizes" style="float: left">
<a href="#Space">
<img class="Small" src="/files/theme/SampleD_Fun_Icon.png" width="150" height="150" alt="Sample 1: Day of Fun" />
<img class="Large" src="/files/theme/SampleD_Fun.png" width="150" height="150" alt="Sample 1: Day of Fun" />
</a>
...
</section>
<a id="Space"></a>
<span class="Popup">Hover over thumbnail to display sample artwork.</span>
<br style="clear: left" />

a:hover img.Small
{
    border: 5px solid #21568b;
    margin: 10px;
    text-decoration: none;
}
section#Sizes a img.Large
{
    border-width: 0;
    height: 0;
    left: 438px;
    position: absolute;
    top: 326px;
    width: 0;
}
section#Sizes a:hover img.Large
{
    height: 526px;
    left: 438px;
    position: absolute;
    top: 326px;
    width: 520px;
}
.Popup
{
    border: 3px solid;
    float: left;
    height: 272px;
    margin: 8px 20px 0px 0px;
    padding-top: 254px;
    text-align: center;
    width: 520px;
}

Thank you for your time. :)

Akshay Gundewar
  • 864
  • 11
  • 30

4 Answers4

0

TLDR: You can't do this in pure CSS.

You can easily position the image inside the container div if you place the image element inside the div element, and then use absolute positioning like top: 0; left: 0; (or with a number of other methods). But then you'd need JavaScript to correlate the hovered thumbnail with the popup full-size image.

Alternatively, you can have the full-size image be nested in the thumbnail element (like you currently have), but then you'd need JavaScript to position the full-size popup image inside the container div.

Of the two alternatives, I recommend the first: put all the popup images inside the target container, and use JavaScript to show or hide them when a thumbnail is hovered. Correlating the thumbnail and the full size image via JavaScript is going to be easier then writing positioning code.

Avish
  • 4,516
  • 19
  • 28
  • Hmm... Ok. I was hoping to avoid it, but I guess I'll just havta use Java. I have no idea how to write Javascript, I can only put already written code into my own. I'd like to use the second solution, but only because I'm biased to code that's similar to my own. I'll just take your advice and use the first one. It seems simpler in the long run. Thank you for your advice! :) Now to get someone to write me a JavaScript code... – ShoulderMonster Apr 18 '15 at 19:24
0

Your whole design is a bit fragile, and I wouldn't recommend building this this way in the first place, but you're looking for practical answers, so here's the smallest change I can think of that fixes your problem:

1) Add this to your style sheet:

body { position: relative; }

2) On line 40 from your main_style.css, change top: 326px to top: 316px and left: 438px to left: 428px, so that it becomes like this:

section#Sizes a:hover img.Large {position: absolute; top: 316px; left: 428px; width: 520px; height: 526px;}

How does that work?

Your images are place using absolute positioning. By default, that works relative to the viewport (the window). But by turning the body into position relative, it becomes a containing block, and position absolute is relative to the nearest containing block ancestor.

So now, your images are fixed within the body element, instead of being fixed relative to the window. Since the margins of the body element is what's changing size when you resize the window, that makes the various pieces of your content fixed relative to each other. You then just need to remove 10px from the top and left side, since that's the size of the border of your body element, and we're now measuring from inside the border.

Florian Rivoal
  • 491
  • 4
  • 8
  • Yeah, my entire website is built using only one class' worth of knowledge. Very basic, haha. But I don't mind, as long as it looks fine on most browsers. I'll probably commission someone to fix it up some one day, if it ever gets that serious. I remember trying relative positioning when I first made it, but I guess I didn't do it right. X'D I gave up and just used absolute. Thank you so much! This fixed it right up. :3 However, I noticed that when the browser zooms in, it isn't aligned perfectly anymore. I'll look at the other solutions and see if there's a fix for that. – ShoulderMonster Apr 18 '15 at 22:51
0

I see you're using jQuery already so why not do something like this?

$('.Small').on('mouseover', function(){
$('.Popup').empty().html($(yourtarget).attr('img' , 'src'));
});

$('.Small').on('mouseout', function(){
$('.Popup').empty().html('Hover over thumbnail to display sample artwork.');
});
Gary Hayes
  • 1,728
  • 1
  • 15
  • 23
0

Just because everyone was saying it can't be done with pure css, I wanted to demonstrate that it can, and it is even quite easy. Have a look at the folowing example:

http://jsfiddle.net/aafa2zp5/

<div id='images-wrapper'>
    <ul>
        <li>
            <img class='small' src='http://placehold.it/50/ff0000'/>
            <img class='big' src='http://placehold.it/300/ff0000'/>
        </li>
        <!-- and some more similar thumb / image groups -->
    </ul>
    <div class='preview-area'></div>
</div>

CSS (or the relevant part at least)

#images-wrapper {
    position: relative;
}
.big {
    display: block;
    position: absolute;
    top: 54px;
    right: 54px;
    opacity: 0;
    transition: opacity .5s;
}  
.preview-area {
    width: 350px;
    height: 350px;
    border: 4px solid blue;
    position: absolute;
    top: 21px;
    right: 21px;
}
li:hover .big {
    opacity: 1;
}

The key is to set a position relative to the wrapper (and keep all of the descendants as their default static). Then you can use this to position the preview area and the big images against by setting them to postion absolute and carefully calculating the correct postion. I even added a cross fade, just because it is so easy, but you could just as well work with display block / none if you prefer.

For smaller screens you may want to alter the dimensions and positioning inside a media query, but it still should be doable (though depending on the hover state is perhaps not the best idea on a touch device)

I hope you get the idea and you can figure out how to apply this technique to your own site. Feel free to ask if you want me to explain further or when you get stuck.

Pevara
  • 14,242
  • 1
  • 34
  • 47
  • Someone else gave a solution that's easier to use, but I'll keep this one in mind because it's really nice! Especially the transparent effect, Imma yoink that, thank you. :D – ShoulderMonster Apr 18 '15 at 23:34