Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the hover to work on mobile devices? On mobile devices I want what is displayed on hover on desktop to be automatically displayed

I wanted to use @media (hover: hover) but I could not get it to work. I have three images in a row that display a text overlay when they are hovered over with a mouse. Currently, whenever I add @media (hover: hover) and switch to a mobile view, the images remain unaltered. I either want to get @media (hover: hover) working or try something else entirely. This is my first time designing a website and using stack overflow, so I am a little out of my depth. I did not see any questions already posted that were similar enough to my problem to help me achieve my goal. My CSS is below.

    .card-container
{
    margin-top: 10%;
}

.card-container h1
{
    font-family: merriweather;
}

.card
{
    position: relative;
    overflow: hidden;
}

.card:before
{
    position: absolute;
    content: '';
    width: 80%;
    height: 220%;
    background: rgba(217, 153, 35, 0.7);
    top: -50%;
    left: -100%;
    z-index: 1;
    transform: rotate(25deg);
    transform-origin: center top 0;
    transition: .5s;
}

.card:hover:before
{
    left: 10%;
}

.card img
{
    width: 100%;
    height: auto;
}

.card-text
{
    width: 100%;
    padding: 0 20px;
    position: absolute;
    top: -100%;
    color: #ffff;
    left: 0;
    z-index: 2;
    transition: 1.1s;
}

.card-text h3
{
    font-family: merriweather;
    font-weight: 900;
}

.card-text h5
{
    font-family: lora;
}

.card:hover .card-text
{
    top: 80px;
}
like image 520
Beks Parker Avatar asked Sep 17 '25 21:09

Beks Parker


2 Answers

In mobile, you need a combination of hover and active to hover element.

So, you need to declare in your css like.

.card:hover, .card:active{
//your hover effect.
}
like image 103
Keyboard Corporation Avatar answered Sep 19 '25 11:09

Keyboard Corporation


You just need to use combine CSS selector properties in responsive media queries.

i.e.- .class:hover, .class:active, .class: focus

please refer this link for live demo to understand in details. https://jsfiddle.net/yudizsolutions/nc2fh7kp/

ol,
ul {
  list-style: none;
}

li {
  display: inline-block;
  padding: 20px 0 20px;
  vertical-align: middle;
}

a:hover,
a:focus,
a:active {
  color: #999;
  text-decoration: none;
}

a {
  text-decoration: none;
  transition: color 0.1s, background-color 0.1s;
  position: relative;
  display: block;
  padding: 16px 0;
  margin: 0 12px;
  letter-spacing: 1px;
  font-size: 12px;
  line-height: 16px;
  font-weight: 900;
  text-transform: uppercase;
  transition: color 0.1s, background-color 0.1s, padding 0.2s ease-in;
  color: #000;
}

a::before {
  content: '';
  display: block;
  position: absolute;
  bottom: 3px;
  left: 0;
  height: 3px;
  width: 100%;
  background-color: #000;
  transform-origin: right top;
  transform: scale(0, 1);
  transition: color 0.1s, transform 0.2s ease-out;
}

a:active::before {
  background-color: #000;
}

a:hover::before,
a:focus::before {
  transform-origin: left top;
  transform: scale(1, 1);
}
<nav>
  <ul>
    <li class="item"><a href="#">link 1</a></li>
    <li class="item"><a href="#">link 2</a></li>
    <li class="item"><a href="#">link 3</a></li>
    <li class="item"><a href="#">link 4</a></li>
    <li class="item"><a href="#">link 5</a></li>
  </ul>
</nav>
like image 34
Yudiz Solutions Avatar answered Sep 19 '25 11:09

Yudiz Solutions