将文本居中于浮动元素之间

时间:2017-02-19 13:04:38

标签: html css center elements

我正在为我的网站制作导航栏。导航栏包含菜单图标,标题“评论”和搜索图标。图标需要距边框3%的距离。问题是我无法弄明白我是如何将文本置于中心位置的。我认为问题在于浮动元素,我不知道如何将浮动元素置于其他元素之间(在本例中为两个图标)。我希望有人能给我一个答案:)

结果如下:

the navigation bar

* {
  padding: 0px;
  margin: 0px;
}

#nav {
  width: 94%;
  background-color: blue;
  margin: 3%;
}

.menuButton {
  float: left;
}

#nav h1 {
  float: left;
}

.searchButton {
  float: right;
}
<!DOCTYPE html>
<html>

<head>
  <title> Reviews </title>
  <link rel="stylesheet" type="text/css" href="main.css">
  <meta charset="utf-8">
</head>

<div id="dropMenu"></div>

<body>
  <div id="nav">
    <img src="menu.jpg" class="menuButton">
    <h1> REVIEWS. </h1>
    <img src="search.jpg" class="searchButton">

  </div>

  <div id="content"></div>

  <div id="footer"></div>
</body>
</html>

2 个答案:

答案 0 :(得分:2)

您可以使用flex boxmodel和margin:

&#13;
&#13;
* {
  padding: 0px;
  margin: 0px;
}

#nav {
  width: 94%;
  background-color: blue;
  /* see center */
  background-image:linear-gradient(to right, transparent 50%, rgba(0,0,0,0.5) 50%);
  margin: 3%;
  display: flex;
  justify-content: center;
}

.menuButton {
  float: left;
  margin: auto auto auto 0;
}

#nav h1 {
  float: left;
}

.searchButton {
  margin: auto 0 auto auto;
}
&#13;
<!DOCTYPE html>
<html>
<head>
  <title> Reviews </title>
  <link rel="stylesheet" type="text/css" href="main.css">
  <meta charset="utf-8">
</head>
<div id="dropMenu"></div>
<body>
  <div id="nav">
    <img src="menu.jpg" class="menuButton">
    <h1> REVIEWS. </h1>
    <img src="search.jpg" class="searchButton">
  </div>
  <div id="content"></div>
  <div id="footer"></div>
</body>
</html>
&#13;
&#13;
&#13;

修改

仅考虑浮动,那么浮动元素应位于通量中,以便让其他非浮动元素介于其间,因此h1可以站立介于两者之间并获得text-align或通过displaymargin集中。

浮动不允许弹性所在的元素垂直居中。

  • text-align并且没有为h1
  • 浮动

&#13;
&#13;
* {
  padding: 0px;
  margin: 0px;
}

#nav {
  width: 94%;
  background-color: blue;
  margin: 3%;
}

.menuButton {
  float: left;
}

#nav h1 {
 display:table;/* shrinks on itself */
 margin:auto;
}

.searchButton {
  float: right;
}
&#13;
<div id="dropMenu"></div>


<div id="nav">
  <img src="menu.jpg" class="menuButton">
  <img src="search.jpg" class="searchButton">
  <h1> REVIEWS. </h1>

</div>

<div id="content"></div>

<div id="footer"></div>
&#13;
&#13;
&#13;

  • 或显示和没有浮动的边距为h1

&#13;
&#13;
* {
  padding: 0px;
  margin: 0px;
}

#nav {
  width: 94%;
  background-color: blue;
  margin: 3%;
}

.menuButton {
  float: left;
}

#nav h1 {
 text-align:center
}

.searchButton {
  float: right;
}
&#13;
<div id="dropMenu"></div>


<div id="nav">
  <img src="menu.jpg" class="menuButton">
  <img src="search.jpg" class="searchButton">
  <h1> REVIEWS. </h1>

</div>

<div id="content"></div>

<div id="footer"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

这个输出是否是你期待的

Check output in codepen.io

&#13;
&#13;
* {
  padding: 0;
  margin: 0;
}

#nav {
  width: 100%;
  background-color: blue;
  margin: 3%;
}

.menuButton {
  float: left;
}



.searchButton {
  float: right;
}

.a {
  text-align: center;
}
&#13;
<head>
  <title> Reviews </title>
  <link rel="stylesheet" type="text/css" href="main.css">
  <meta charset="utf-8">
</head>


<div id="dropMenu">



</div>

<body>

  <div id="nav">
    <img src="menu.jpg" class="menuButton">
    <h1 class="a"> REVIEWS. </h1>
    <img src="search.jpg" class="searchButton">


  </div>

  <div id="content">



  </div>

  <div id="footer">



  </div>


</body>
&#13;
&#13;
&#13;