导航样式问题

时间:2018-02-08 12:20:02

标签: jquery html css sass

我非常接近完成我的响应式导航栏,它似乎在移动设备上正常工作,但是在桌面上我无法让图像与其他导航系统一致?我可以让它显示宽度:100%然后占用其余导航链接的整个空间是内联的吗?

我尝试过左/右浮动,直到在移动视图中它才会消失 - 任何帮助都会感激不尽!在下面找到我的HTML,CSS和截图 - 我希望你能帮忙!

谢谢!

@import url('https://fonts.googleapis.com/css?family=Ubuntu');
@import url("https://fonts.googleapis.com/css?family=Open+Sans");


body { 
  margin: 0;
  font-family: Arial;
}

.logo{
  background-image: url("../images/placeholder-image.jpg");
  background-repeat: no-repeat;
  height: 50px;
  width: 100%;
  display: inline-block;
  vertical-align: top;
}.search-logo{
  border: none;
  background-color: transparent;
  outline: none;
}


.nav-links a {
  display: inline-block;
}

.search-bar{
  display: none;
}

.search-icon{
  font-size: 25px !important; //Due to icon inheriting from external css
  cursor: pointer;
}

* {
  box-sizing: border-box;
}


.header {
  overflow: hidden;
  background-color: #f1f1f1;
  padding: 20px 100px;
}

.header a {
  float: left;
  color: black;
  text-align: center;
  padding: 12px;
  text-decoration: none;
  font-size: 18px; 
  line-height: 25px;
  border-radius: 4px;
}

.header a.logo {
  font-size: 25px;
  font-weight: bold;
}

.nav-header{
  position: absolute;

  &--header-left{
    float: left;
    left: 15%;
  }

  &--header-right{
    float: right;
    right: 15%;
  }
}

.header-right {
  float: right;
  position: absolute;
  right: 15%;
}

.header-left {
  float: left;
}

@media screen and (max-width: 768px) {
  .header a {
    float: none;
    display: block;
    text-align: center;
  }

  .header a.logo {
    text-align: center;
    width: 100%;
  }

  .nav-header--header-right,
  .nav-header--header-left {
    float: none;
    position: static;
  }

  .logo{
    background-position: center;
  }
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<div class="header">
  <div class="nav-header--header-left">
    <div class="logo"></div>
  </div>
  <div class="nav-header--header-right">
    <a href="#">ABOUT US</a>
    <a href="#">OUR WORK</a>
    <a href="#">NEWS</a>
    <a href="#">CAREER</a>
    <a href="#">CONTACT</a>
    <a>
      <button type="submit" class="search-logo"><i class="fa fa-search search-icon"></i></button>
      <input type="text" placeholder="Search.." name="search" class="search-bar">
    </a>
  </div>
</div>

Desktop image (with image not showing on the left) Mobile devices where image shows correctly

2 个答案:

答案 0 :(得分:0)

您的.logo DIV定义为display: inline-block;width: 100%,但没有任何内容(背景图片不计入内容)。

width: 100%引用父元素,因为它也没有设置宽度而没有其他内容,所以宽度为零,因此保持不可见。

为避免这种情况,您可以在min-width上使用px设置.logo

答案 1 :(得分:0)

请参阅此代码

&#13;
&#13;
@import url('https://fonts.googleapis.com/css?family=Ubuntu');
@import url("https://fonts.googleapis.com/css?family=Open+Sans");


body { 
  margin: 0;
  font-family: Arial;
}
.header{
  display: flex;
    justify-content: space-between;
}
@media screen and (max-width: 767px){
  .header{    
    flex-direction: column;
    align-items: center;
  }
}
.logo{
  background-image: url("http://via.placeholder.com/350x150");
  background-repeat: no-repeat;
  height: 50px;
  width: 200px;
  display: inline-block;
  vertical-align: top;
}.search-logo{
  border: none;
  background-color: transparent;
  outline: none;
}


.nav-links a {
  display: inline-block;
}

.search-bar{
  display: none;
}

.search-icon{
  font-size: 25px !important; //Due to icon inheriting from external css
  cursor: pointer;
}

* {
  box-sizing: border-box;
}


.header {
  overflow: hidden;
  background-color: #f1f1f1;
  padding: 20px 100px;
}

.header a {
  float: left;
  color: black;
  text-align: center;
  padding: 12px;
  text-decoration: none;
  font-size: 18px; 
  line-height: 25px;
  border-radius: 4px;
}

.header a.logo {
  font-size: 25px;
  font-weight: bold;
}

.nav-header{
  position: absolute;

  &--header-left{
    float: left;
    left: 15%;
  }

  &--header-right{
    float: right;
    right: 15%;
  }
}

.header-right {
  float: right;
  position: absolute;
  right: 15%;
}

.header-left {
  float: left;
}

@media screen and (max-width: 768px) {
  .header a {
    float: none;
    display: block;
    text-align: center;
  }

  .header a.logo {
    text-align: center;
    width: 100%;
  }

  .nav-header--header-right,
  .nav-header--header-left {
    float: none;
    position: static;
  }

  .logo{
    background-position: center;
  }
}
&#13;
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

<div class="header">
  <div class="nav-header--header-left">
    <div class="logo"></div>
  </div>
  <div class="nav-header--header-right">
    <a href="#">ABOUT US</a>
    <a href="#">OUR WORK</a>
    <a href="#">NEWS</a>
    <a href="#">CAREER</a>
    <a href="#">CONTACT</a>
    <a>
      <button type="submit" class="search-logo"><i class="fa fa-search search-icon"></i></button>
      <input type="text" placeholder="Search.." name="search" class="search-bar">
    </a>
  </div>
</div>
&#13;
&#13;
&#13;