Show button on hover only

前端 未结 4 1941
后悔当初
后悔当初 2021-02-19 00:36

I\'ve read around and tried other solutions to my problem but none seem to work.

I have 3 images, each within their own 4-column div. I have a css transition set up so t

相关标签:
4条回答
  • 2021-02-19 00:51

    You can set class to the image container and show the button on hover the image container.

    Please check this link with working example:

    .img-container:hover .button {
        display: block;
        position: absolute;
        left: 0;
        right: 0;
        bottom: 20px;
        width: 100%;
        text-align: center;
    }

    0 讨论(0)
  • 2021-02-19 00:56

    You could use a simple img:hover + .button to select the link (the + selects the next element if it matches the .button selector)

    .button {
      display: none;
    }
    img:hover + .button {
      display: inline-block;
    }
    <div class="small-4 columns">
      <img src="http://placehold.it/350x150">
      <a class="button" href="/jane/">View Jane's Profile</a>
    </div>

    Alternatively if the button isn't over the image, you could use :hover on a wrapper element, which avoids the problem of the image no longer being hovered when you want to click the button (the button would disappear when you try to click it, as seen in the above example)

    .button {
      display: none;
    }
    .wrapper:hover img {
      /* Change the filter in here */
    }
    .wrapper:hover .button {
      display: inline-block;
    }
    <div class="small-4 columns">
      <div class="wrapper">
        <img src="http://placehold.it/350x150">
        <a class="button" href="/jane/">View Jane's Profile</a>
      </div>
    </div>

    0 讨论(0)
  • 2021-02-19 00:59

    here you go my friend:

    div.small-4.columns img ~ a  {display:none;}
    div.small-4.columns img:hover ~ a {display:block;}
    

    UPDATE:

    if you want the button to be clickable and not dissapear until you remove the move from the picture, use the following instead:

    a.button {display: none;}
    div.small-4.columns:hover > a.button {display:block;}
    

    EXPLANATION:

    a.button is to select the a with class .button
    div.small-4.columns:hover selecting the div that has both classes .small-4 and .columns (parent of image)

    > means child and ~ means sibling, in this case div.small-4.columns:hover > a.button {display:block;} we're telling it to display the child element which is a.button, when we hover div.small-4.columns

    0 讨论(0)
  • 2021-02-19 01:03

    Here you go:

    .button {
        display: none;
    }
    
    img:hover + .button, .button:hover {
        display: inline-block;
    }
    

    By doing this, we're using the adjacent sibling css selector +. The selector is pretty simple: on image "hovering", you select .button (its sibling) and display it. Here, I added .button:hover so that when the user "hovers" the button, it keeps it visible (prevent a blinking effect as the user moves the mouse over the button).

    0 讨论(0)
提交回复
热议问题