Display different content based on screen width
P粉904450959
P粉904450959 2024-04-01 22:53:42
0
1
394

For one element, I have two different texts based on the screen width. I want to display narrow when the screen width is less than or equal to 400px. I want to display large when the screen width is larger than 400px.

Here is the code: https://jsbin.com/qagetoseva/edit?html,css,output

But the problem is that when the width is 400px, this code displays nothing. Does anyone know how to modify it?

<!DOCTYPE html>
<html>
<head>
    <script src="https://code.jquery.com/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
    <style>
        .me {
          height: 100vh;
        }
    </style>
</head>
<body>
  <div class="hide-if-large">narrow</div>
  <div class="hide-if-narrow">large</div>
</body>
</html>
@media screen and (min-width: 400px) {
    .hide-if-large {
        display:none
    }    
}

@media screen and (max-width: 400px) {
    .hide-if-narrow {
        display: none;
    }
}

P粉904450959
P粉904450959

reply all(1)
P粉642436282

Dear friend, there is a problem with your CSS media queries.

@media screen and (max-width: 400px) {
.hide-if-large {
    display:none
}

}

@media screen and (max-width: 1024px) {
    .hide-if-narrow {
        display: none;
    }
}

I changed the second media query to 1024px to fit the tablet size. You should create a new media query for larger screens, for example min-width 1025px

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template