将div中的navbar元素相对于

时间:2015-06-16 13:40:24

标签: html css

我希望像这样的div中有一个导航栏

enter image description here

我在div的中间有一个图像,我想编写css,这样我的导航元素(home,about ...)有一个相对边距而不在图像中。

HTML

<div class="nav">
  <img id="diamond" src="{% static "blog/img/Diamond.png" %}">
  <a id="nav_left" href="{% url 'index' %}">Home</a>               
  <a id="nav_left" href="{% url 'guides' %}">Guides</a>                                        
  <a id="nav_right" href="{% url 'stream' %}">Stream</a>                                       
  <a id="nav_right" href="{% url 'about' %}">About</a>                                         
</div>

当前的css

.nav {
  left: 0;
  right: 0;
  background-color: #FFFFFF;
  position: fixed;
  bottom 0px;
  height: 20vh;
  margin: 5px;
}

#nav_left {
  float: left;
  color: black;
}

#nav_right {
  float: right;
  color: black;
}

#diamond {
  display: block;
  margin-left: auto;
  margin-right: auto;
  max-width: 100%;
  max-height: 100%;
}

1 个答案:

答案 0 :(得分:1)

只需更改源订单将是最简单的方法...然后inline-blockvertical-align:middle

&#13;
&#13;
.nav {
  left: 0;
  right: 0;
  background-color: lightblue;
  position: fixed;
  bottom 0px;
  height: 20vh;
  margin: 5px;
  text-align: center;
}
#diamond {
  max-height: 100%;
  width: auto;
}
.nav a,
.nav img {
  display: inline-block;
  vertical-align: middle;
  margin: 0 1em;
}
&#13;
<div class="nav">

  <a class="nav_left" href="#">Home</a> 
  <a class="nav_left" href="#">Guides</a> 
  <img id="diamond" src="http://lorempixel.com/output/abstract-q-c-100-100-5.jpg" />
  <a class="nav_right" href="#">Stream</a> 
  <a class="nav_right" href="#">About</a> 
</div>
&#13;
&#13;
&#13;

flexbox已经被提及,并且可以保留问题的原始顺序,但内部订单需要重新进行一些重复。

&#13;
&#13;
.nav {
  left: 0;
  right: 0;
  background-color: lightblue;
  position: fixed;
  bottom 0px;
  height: 20vh;
  margin: 5px;
  text-align: center;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.nav a {
  order: 3
}
.nav a:nth-of-type(1) {
  order: -1
}
.nav a:nth-of-type(2) {
  order: 1
}
#diamond {
  max-height: 100%;
  width: auto;
  order: 2;
}
&#13;
<div class="nav">
  <img id="diamond" src="http://lorempixel.com/output/abstract-q-c-100-100-5.jpg" />
  <a class="nav_left" href="#">Home</a> 
  <a class="nav_left" href="#">Guides</a> 
  <a class="nav_right" href="#">Stream</a> 
  <a class="nav_right" href="#">About</a> 
</div>
&#13;
&#13;
&#13;