14

I have a rounded div which has a rounded image and a title at the bottom whith opacity: 0.5; On hover the opacity should become 1. It works fine on all desktop browsers and Firefox for iOS but it doesn't work on Safari nor Chrome for iOS.

Fiddle: https://jsfiddle.net/a10rLbnL/2/

HTML:

<div class="video_wrap update">
  <div class="content">
    <div class="img_wrap"><img src="https://i.ytimg.com/vi/0HDdjwpPM3Y/hqdefault.jpg"></div>
    <div class="title_wrap"><div class="title">bang bang</div></div>
  </div>
</div>

CSS:

.video_wrap {
    display: inline-block;
    width: 30%;
    padding-bottom: 30%;
    margin: 0 1%;
    position: relative;
    vertical-align: top;
}

.content {
    position: absolute;
    height: 100%;
    width: 100%;
}

.img_wrap {
    height: 100%;
    border-radius: 120px;
    overflow: hidden;
}

.title_wrap {
    line-height: 50px;
    top: -50px;
    height: 50px;
    position: relative;
    left: 0px;
    background: #fff;
    color: #f8008c;
    font-size: 12px;
    text-align: center;
    cursor: default;
    opacity: 0.5;
    transition: all .5s ease-in;
    min-height: 50px;
}

.img_wrap img {
    height: 100%;
    cursor: pointer;
}

.title_wrap:hover {opacity: 1}
M1X
  • 4,971
  • 10
  • 61
  • 123

4 Answers4

30

I found a workaround: if you add onclick="" to the div, the hover will work.

Your html would be:

<link rel="stylesheet" href="hover.css" type="text/css"/>

<div class="video_wrap update">
  <div class="content">
    <div class="img_wrap"><img src="https://i.ytimg.com/vi/0HDdjwpPM3Y/hqdefault.jpg"></div>
    <div class="title_wrap" onclick=""><div class="title">bang bang</div></div>
  </div>
</div>
Wouter van Dijke
  • 662
  • 1
  • 7
  • 17
  • 1
    When you tap and hold. It's the same as hover and it works in Firefox for iOS. – M1X Feb 03 '16 at 21:30
  • 1
    You're right. The problem I have when trying to hover in both Chrome and Safari is that I'm selecting the text when I tap and hold on the div. I think Safari puts 'selecting text' before 'applying hover'. – Wouter van Dijke Feb 03 '16 at 21:45
  • I found a workaround: if you add `onclick=""` to the div, the hover will work. See updated answer. – Wouter van Dijke Feb 03 '16 at 22:00
  • Yeah but you should click at it. it doesn't work if you tap and hold. – M1X Feb 03 '16 at 22:32
  • 1
    I have a risk like that, but i dont understand why i have two :hover on two
    different, the first div.class1:hover -> OK on Chrome & Safari Mobile. But the last div.class2:hover -> not working on Safari Mobile, it's only working on Chrome Mobile
    – kollein Nov 24 '16 at 02:46
  • 7
    If you add `cursor: pointer` to your CSS it has the same affect. – Gavin Jan 10 '17 at 16:24
  • You sir just saved my butt with a client project. =) – Mark Oct 15 '19 at 16:20
2

The iOS Browser needs an element that is clickable by default. If you use HTML5 you can change the wrapper div to an a-tag:

<a href="javascript:void(0);" class="title_wrap"><div class="title">bang bang</div></a>

and set it to an block element:

.title_wrap {
  ...
  display:block;
}

If you don't use HTML5 you have to change the <div class="title"> to an inline elment like <span class="title"> so the code is valid.

Andréle
  • 336
  • 3
  • 10
0

Solution:

If you want :hover and :focus to work, use tabindex="0"


Explanation:

The issue is that <div> is not, in its natural state, a focusable element.

Many elements commonly found in HTML Forms like <input> and <button> are focusable, but other elements commonly found outside forms like <div> and <li> are not.

If you want to make an element focusable which, normally, cannot be focused on, there are a number of ways you can achieve this result.

As one answer on this page states, onclick="" will make the element focusable.

But the standard way to make an element focusable is to use tabindex="0"


Further Reading:

Rounin
  • 27,134
  • 9
  • 83
  • 108
0

I have the same bag: when hovering over the parent-element, the child-button should change its style from opacity: 0 to opacity: 1 -it does not work only in Safari. onclick="" and tabindex="0" - don't work for me. Strange solution but works!!: add to parent position: relative;

Nastya
  • 11
  • 1
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 07 '23 at 20:31