M1X
M1X

Reputation: 5374

CSS :hover not working on iOS Safari and Chrome

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}

Upvotes: 16

Views: 40071

Answers (4)

Nastya
Nastya

Reputation: 11

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;

Upvotes: 0

Rounin
Rounin

Reputation: 29521

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:

Upvotes: 3

Andr&#233;le
Andr&#233;le

Reputation: 336

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.

Upvotes: 2

Wouter van Dijke
Wouter van Dijke

Reputation: 692

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>

Upvotes: 31

Related Questions