使用媒体查询删除div

时间:2018-06-21 10:41:37

标签: css bootstrap-4 media-queries

当我使用媒体查询将屏幕尺寸增加到超过768像素时,我正在尝试删除.navbar-brand>img,但是我没有任何运气。我应用于img的样式已删除,但是图像保留在同一位置。 如果有帮助,我正在使用BS4

<body>
    <div class="container-fluid">
        <nav class="navbar-expand-lg navbar navbar-light">
            <!-- Image and text -->
             <a class="navbar-brand" href="#"><img alt="" class="d-inline-block align-top" height="30" src="img/SpartanHead.png" width="30"></a> <button aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler" data-target="#navbarNav" data-toggle="collapse" type="button"><span class="navbar-toggler-icon"><i class="fas fa-bars"></i></span></button>

            <div class="collapse navbar-collapse justify-content-center" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item active">
                        <a class="nav-link" href="about.html">ABOUT</a>
                    </li>
                </ul>
            </div>
        </nav>
    </div>
</body> 

CSS

.navbar {
  background-color: #2A2F35;
  padding: 0 !important;
}
/*Navbar Links*/

.navbar-nav a {
  font-family: 'Open Sans', sans-serif;
  font-style: normal;
  font-size: 14px;
  font-weight: 100;
  letter-spacing: 1px;
  color: #ffffff !important;
  padding: 25px;
  margin: 0px 25px 0px 25px;
}

@media only screen and (max-width: 768px) {
  .navbar-brand>img {
      width: auto;
      max-height: 90px;
      margin: 10px 0px 10px 30px;
  }
}

@media screen and (min-width: 768px) {
  .navbar-brand>img {
    display: none;
  }
}

2 个答案:

答案 0 :(得分:2)

使用!important来防止覆盖 display: none!important;(请参见小提琴:https://jsfiddle.net/wtyzqkrp/1/

.navbar {
  background-color: #2A2F35;
  padding: 0 !important;
}
/*Navbar Links*/

.navbar-nav a {
  font-family: 'Open Sans', sans-serif;
  font-style: normal;
  font-size: 14px;
  font-weight: 100;
  letter-spacing: 1px;
  color: #ffffff !important;
  padding: 25px;
  margin: 0px 25px 0px 25px;
}

@media only screen and (max-width: 768px) {
  .navbar-brand>img {
      width: auto;
      max-height: 90px;
      margin: 10px 0px 10px 30px;
  }
}

@media screen and (min-width: 768px) {
  .navbar-brand>img {
    display: none!important;
  }
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>

    <div class="container-fluid">
        <nav class="navbar-expand-lg navbar navbar-light">
            <!-- Image and text -->
             <a class="navbar-brand" href="#"><img alt="" class="d-inline-block align-top" height="30" src="https://material.angular.io/assets/img/examples/shiba1.jpg" width="30"></a> <button aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation" class="navbar-toggler" data-target="#navbarNav" data-toggle="collapse" type="button"><span class="navbar-toggler-icon"><i class="fas fa-bars"></i></span></button>

            <div class="collapse navbar-collapse justify-content-center" id="navbarNav">
                <ul class="navbar-nav">
                    <li class="nav-item active">
                        <a class="nav-link" href="about.html">ABOUT</a>
                    </li>
                </ul>
            </div>
        </nav>
    </div>

答案 1 :(得分:2)

仅使用显示实用程序类的响应版本。您使用display:inline-block将图像设置为class="d-inline-block",只需添加d-md-none即可将其自动设置为> 768px的display:none

更改

<img alt="" class="d-inline-block align-top" height="30" src="img/SpartanHead.png" width="30">

<img alt="" class="d-inline-block d-md-none align-top" height="30" src="img/SpartanHead.png" width="30">

,您不必添加任何自定义CSS。

有关更多信息,请参见https://getbootstrap.com/docs/4.1/utilities/display/

相关问题