Beau
Beau

Reputation: 87

Hover on one div affects another

I researched other questions similar to mine and have been pointed in the right direction, but I believe my case is a bit different. I am new to CSS so bear with me please! I am trying to make a logo that is interactive on hover. I have had some success but want to make it better. The logo is comprised of two images, a rake and another image that is text on top of it. On hover, I want the rake to make a raking motion, but have the text remain static. I have successfully made the rake (bottom image) move on hover but it is hard to get your cursor on just the rake (bottom image) is covered by the text (top image) I have this HTML:

<div id="container">
<div class="logo">
    <img src="rake-logo-top.png" width="214" height="170" />
</div>
<div class="vertpan pic">
    <img src="rake-logo-bottom.png" >
</div>
</div>

CSS:

#container {
height:304px;
width: 246px;
margin: 20px;
cursor: pointer;
}

.logo {
    position: absolute;
    z-index: 2;
    padding-top: 105px;
    padding-left: 15px;
}

.pic {
  height: 304px;
  width: 246px;
  overflow: hidden;
}

/*VERTPAN*/
.vertpan img {
  position: relative;   
  margin-top: 100px;
  -webkit-transition: margin 1s ease;
     -moz-transition: margin 1s ease;
       -o-transition: margin 1s ease;
      -ms-transition: margin 1s ease;
          transition: margin 1s ease;
  z-index: 1;
}

.vertpan img:hover {
  margin-top: 0px;
}

I know this can be done with something along the lines of this:

#container:hover + vertpan img {
  margin-top: 100px;
  -webkit-transition: margin 1s ease;
     -moz-transition: margin 1s ease;
       -o-transition: margin 1s ease;
      -ms-transition: margin 1s ease;
          transition: margin 1s ease;
  z-index: 1;
}

But that is not quite correct. The "vertpan" code was pre-formatted and I am changing it. Basically I want to hover on either #container or .logo and have the same reaction as I get when hover on just the .pic. I have tried a few times and am stuck...is it because I already have the hover effect on .pic?

Any help is appreciated!

Upvotes: 0

Views: 1321

Answers (1)

laconbass
laconbass

Reputation: 17824

Your question does not fit the Q&A format, but I felt altruist and maked an example to you

HTML Observations

  • You should use background-images instead <img> tags for this effect, the image is not, semantically speaking, content.
  • If the logo has a text layer then you should definitively use text for it.
  • I assume you are using cursor: pointer because you are linking to somewhere - maybe the site home - so I recommend using an <a> tag for the container.

    <a id="logo" href="#">
        <div class="text">Logo text</div>
        <div class="background"></div>
    </a>
    

CSS observations

  • You are going too complex with positioning, your example will be difficult to integrate.
  • Try using absolute positioning for the background image rather than for the container, see my example.
  • If you define the transition behaviour for the :hover state, this behaviour will not affect the element on other state.

    /**
     * Make logo width responsive to its text,
     * while staying as block.
     * Any absolute content will be relative to this
     * Remove the default underline for the <a> tag
     */
    #logo {
        display: inline-block;
        position: relative;
        text-decoration: none;
    }
    /**
     * place the background layer on 0,0
     * make them responsive to parent width and height
         * correct the z-index to display it behind the text
     * set transition time and easing
     */
    #logo .background {
        position: absolute;
        top: 0; left: 0; 
        width: 100%;
        height: 100%;
        z-index: -1;
        -webkit-transition: all .5s ease;
    }
    /**
     * Just decoration for prettyness:
     * - Background image
     * - Text color
     * - Set a size which fits the bg image aspect ratio
     */
    #logo .background {
        background: url("http://lorempixel.com/g/214/170/abstract/1") no-repeat;
        background-size: contain;
    }
    #logo .text {
        color: gold;
        width: 214px; height: 170px;
    }
    #logo:hover .background {
        margin-top: -20px;
    }
    

I hope my answer helps you

Upvotes: 1

Related Questions