3

I'm learning html and css, but I have some troubles. Right now I'm making a site that has a small images with different w sizes. The point is that, when you hover on them they show up clickable elements, and I can't get the right position on them.

What I have:

enter image description here

What I want:

enter image description here

Part of code for this:

            <div class="photo">
                <img src="http://placekitten.com/400/300" alt="image"/>
                <div class="zoom">
                </div>
                <div class="all">
                </div>
                <div class="link">
                </div>
                <div class="info">
                </div>
                <div class="like">
                </div>
            </div>

CSS:

.photo img {
    float:left;
    width:auto;
    height:auto;
}

.photo:hover {
    display: block;
    opacity:0.6;
}
.photo:hover .zoom {
    position: absolute;
    background-image:url(http://www.kolazhgostar.com/images/small-img05.png);
    background-repeat:no-repeat;
    width:46px;
    height:50px;
    background-position:center;

http://jsfiddle.net/zzu87/

4 Answers 4

3

You need to add some positioning to each image if you use position: absolute. Try something like this:

.photo:hover .zoom {
  position: absolute;
  top: 50%;
  left: 200px;
  background-image: url(http://www.kolazhgostar.com/images/small-img05.png);
  background-repeat: no-repeat;
  width: 46px;
  height: 50px;
  background-position: center;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ok that works, but what If I want to have another picture next to it with same hover? jsfiddle.net/eW7Lc Do I have to make own class with hover for each image?
Yes, you'll have to position each individually.
1

This should get you where you want to go. (JS fiddle)

css

.photo {
    display:block;
    position:absolute;
    background-image: url('//placekitten.com/400/300');
    background-repeat: no-repeat;
    width:400px;
    height:300px;
}

.photo>.container {
    display:none;
}
.photo>.container>div {
    display:inline;
}

.photo:hover>.container {
    display:block;
    margin-left: 85px;
    margin-top: 200px;
}

html

<div class="photo">
    <div class="container">
        <div class="zoom">
            <img src="//www.kolazhgostar.com/images/small-img05.png"/>
        </div>
        <div class="all">
            <img src="//www.kolazhgostar.com/images/small-img05.png"/>
        </div>
        <div class="link">
            <img src="//www.kolazhgostar.com/images/small-img05.png"/>
        </div>
        <div class="info">
                        <img src="//www.kolazhgostar.com/images/small-img05.png"/>
        </div>
        <div class="like">
            <img src="//www.kolazhgostar.com/images/small-img05.png"/>
        </div>
    </div>
</div>

Comments

1

First float the parent div left and set the position to relative. Then you'll have better control over the positioning of any child elements.

.photo {
   float:left;
   position:relative;
}

After, padding, margin, bottom, left, right, and top can be used to achieve the specific location desired inside the parent div. Here I used left and top...

.photo:hover .zoom {
   position: absolute;
   background-image:url(http://www.kolazhgostar.com/images/small-img05.png);
   background-repeat:no-repeat;
   width:46px;
   height:50px;
   background-position:center;

   left:50%;
   top:50%;
}

Here is the FIDDLE.

Comments

0

Interesting question. I solved the problem by making more div containers for the photo and its contents. Also, I worked under assumption that your photo images are 400x300. Modify the code as you like! :)

I think the interesting part about my solution is that I used only position: relative; which lifts up the hover menu above your images so it plays together nicely:

.photo-menu {
    position: relative;
    left: 0px;
    top: -300px;
}

Thus, most of the horizontal position is accomplished using margin: 0 auto; instead of playing too much with absolute or relative position. Generally speaking, those can be avoided most of the time. It depends.

The result can be also viewed from the following: js fiddle example or from this jsfiddle example if cat images are removed sometime later.

Linking also relevant code below:

HTML:

<div class="photo-container">

    <div class="photo">
        <img src="http://placekitten.com/400/300" alt="image"/>
        <div class="photo-menu">
              <div class="upper-menu"></div>
              <div class="lower-menu">
                   <div class="all"></div>
                   <div class="link"></div>
                   <div class="info"></div>
                   <div class="like"></div>
              </div>
        </div>
    </div>

    <div class="photo">
        <img src="http://placekitten.com/400/300" alt="image"/>
        <div class="photo-menu">
              <div class="upper-menu"></div>
              <div class="lower-menu">
                   <div class="all"></div>
                   <div class="link"></div>
                   <div class="info"></div>
                   <div class="like"></div>
              </div>
        </div>
    </div>

</div>

CSS:

body {
    margin: 0px;
    padding: 0px;
}

.photo-container {
    width: 800px;
}

.photo {
    float: left;
    width: 400px;
}

.photo, .photo-menu {
    width: 400px;
    height: 300px;
}

.photo:hover {
    display: block;
    opacity: 0.6;
}

.photo-menu {
    position: relative;
    left: 0px;
    top: -300px;
}

.photo .photo-menu {
    display: none;
}

.photo:hover .photo-menu {
    display: block;
}

.photo-menu .upper-menu {   
    background-image: url("http://www.kolazhgostar.com/images/small-img05.png");
    background-repeat: no-repeat;
    background-position: center;
    width: inherit;
    height: 200px;
    margin: 0 auto;
}

.photo-menu .lower-menu {   
    width: 280px;
    margin: 0 auto;
    height: 100px;
}

.photo-menu .lower-menu div {
    min-width: 40px;
    width: 24.9999%;
    height: 40px;
    background-repeat: no-repeat;
    background-position: center;
    float: left;
}

.photo-menu .lower-menu .all {  
    background-image: url("http://placehold.it/40/ff0000");
}

.photo-menu .lower-menu .link { 
    background-image: url("http://placehold.it/40/00ff00");
}

.photo-menu .lower-menu .info { 
    background-image: url("http://placehold.it/40/0000ff");
}

.photo-menu .lower-menu .like { 
    background-image: url("http://placehold.it/40/c0ff33");
}

Note: I used placehold.it to place dummy images for the icons.

Cheers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.