在移动设备上隐藏DIV

时间:2019-02-28 01:36:33

标签: html mobile responsive

下面是我的代码,我想知道如何在移动设备上隐藏图标栏DIV。我尝试添加以下@media,但它仍显示在较小的屏幕上。任何帮助或建议,将不胜感激。

@media only screen and (max-width: 980px) {
#icon-bar {
top: 100px !important;
}
}

@media only screen and (max-width: 980px) { /*adjust the max width value to hide the button on the screen sizes of your choice*/
#icon-bar {
display: none;
}
}

body {margin:0;height:2000px;}

.icon-bar {
  position: fixed;
  top: 50%;
  -webkit-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
}

.icon-bar a {
  display: block;
  text-align: center;
  padding: 16px;
  transition: all 0.3s ease;
  color: white;
  font-size: 20px;
}

.icon-bar a:hover {
  background-color: #000;
}

.facebook {
  background: #3B5998;
  color: white;
}

.twitter {
  background: #55ACEE;
  color: white;
}

.google {
  background: #dd4b39;
  color: white;
}

.linkedin {
  background: #007bb5;
  color: white;
}

.youtube {
  background: #bb0000;
  color: white;
}

.content {
  margin-left: 75px;
  font-size: 30px;
}
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<body>

<div class="icon-bar">
  <a href="#" class="facebook"><i class="fa fa-facebook"></i></a> 
  <a href="#" class="twitter"><i class="fa fa-twitter"></i></a> 
  <a href="#" class="google"><i class="fa fa-google"></i></a> 
  <a href="#" class="linkedin"><i class="fa fa-linkedin"></i></a>
  <a href="#" class="youtube"><i class="fa fa-youtube"></i></a> 
</div>

<div class="content">
  <h3>Howdy</h3>
  <p>Your content goes here.....</p>
   <p>Your content goes here.....</p>  <p>Your content goes here.....</p>  <p>Your content goes here.....</p>  <p>Your content goes here.....</p>  <p>Your content goes here.....</p>  <p>Your content goes here.....</p>  <p>Your content goes here.....</p>  <p>Your content goes here.....</p>  <p>Your content goes here.....</p>  <p>Your content goes here.....</p>  <p>Your content goes here.....</p>  <p>Your content goes here.....</p>  <p>Your content goes here.....</p>  <p>Your content goes here.....</p>  

</body>
</html> 

下面是我的代码,我想知道如何在移动设备上隐藏图标栏DIV。我尝试添加以下@media,但它仍显示在较小的屏幕上。任何帮助或建议,将不胜感激。

1 个答案:

答案 0 :(得分:0)

您的icon-bar不是id。它是class。因此,您需要使用.icon-bar而不是#icon-bar

在CSS中定位它

此外,您的第一个媒体查询是多余的,因为它改变了图标栏的高度,但是在第二个媒体查询中您将其隐藏了,因此高度变化将永远不会被看到,因此您可以删除第一个媒体查询。

请参见下面的工作示例:

body {
  margin: 0;
  height: 2000px;
}

@media only screen and (max-width: 980px) {
  .icon-bar {
    display: none;
  }
}

.icon-bar {
  position: fixed;
  top: 50%;
  -webkit-transform: translateY(-50%);
  -ms-transform: translateY(-50%);
  transform: translateY(-50%);
}

.icon-bar a {
  display: block;
  text-align: center;
  padding: 16px;
  transition: all 0.3s ease;
  color: white;
  font-size: 20px;
}

.icon-bar a:hover {
  background-color: #000;
}

.facebook {
  background: #3B5998;
  color: white;
}

.twitter {
  background: #55ACEE;
  color: white;
}

.google {
  background: #dd4b39;
  color: white;
}

.linkedin {
  background: #007bb5;
  color: white;
}

.youtube {
  background: #bb0000;
  color: white;
}

.content {
  margin-left: 75px;
  font-size: 30px;
}
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<body>
  <div class="icon-bar">
    <a href="#" class="facebook"><i class="fa fa-facebook"></i></a>
    <a href="#" class="twitter"><i class="fa fa-twitter"></i></a>
    <a href="#" class="google"><i class="fa fa-google"></i></a>
    <a href="#" class="linkedin"><i class="fa fa-linkedin"></i></a>
    <a href="#" class="youtube"><i class="fa fa-youtube"></i></a>
  </div>

  <div class="content">
    <h3>Howdy</h3>
    <p>Your content goes here.....</p>
    <p>Your content goes here.....</p>
    <p>Your content goes here.....</p>
    <p>Your content goes here.....</p>
    <p>Your content goes here.....</p>
    <p>Your content goes here.....</p>
    <p>Your content goes here.....</p>
    <p>Your content goes here.....</p>
    <p>Your content goes here.....</p>
    <p>Your content goes here.....</p>
    <p>Your content goes here.....</p>
    <p>Your content goes here.....</p>
    <p>Your content goes here.....</p>
    <p>Your content goes here.....</p>
    <p>Your content goes here.....</p>
  </div>
</body>
</html>