html - 对齐标题菜单上的搜索栏和菜单栏

标签 html css

我试图找出为什么它无法正确对齐。我尝试过:显示、对齐、更改边距、更改填充、更改位置。事件顶部或在其上 float 。

Photo of what it looks like

.headerMenu {
  background-color: #272626;
  Height: 56px;
  border-bottom: 0px;
  padding-left: auto;
  padding-right: auto;
  width: 100%;
}
#wrapper {
  margin-left: auto;
  margin-right: auto;
  width: 1000px;
  padding-top: 0px;
  padding-bottom: 0px;
}
.logo {
  padding-top: 9px;
  padding-bottom: 9px;
  width: 38px;
  transition-timing-function: linear;
  transition: all 2s;
}
.logo .img {
  background-image: url(../img/logo-white-p.png);
  width: 38px;
  height: 38px;
  transition-timing-function: linear;
  transition: all 0.5s;
}
.logo:hover {
  background-color: rgba(255, 255, 255, 1.00);
}
.logo .img:hover {
  background-image: url(../img/logo-black-p.png);
}
.search_box {
  position: absolute;
  top: 13px;
  margin-left: 155px;
}
#search input[type="text"] {
  background: url(../img/search-icon-white.png) no-repeat 10px 6px #000;
  outline: none;
  border: 0 none;
  font: bold 12px;
  color: #fff;
  width: 350px;
  padding: 6px 15px 6px 35px;
  text-shadow: 0 2px 2px rgba(255, 255, 255, 0.30);
  -webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 3px rgba(0, 0, 0, 0.2) inset;
  -moz-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 3px rgba(0, 0, 0, 0.2) inset;
  -box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 3px rgba(0, 0, 0, 0.2) inset;
  -webkit-transition: all 0.7s ease 0s;
  -moz-transition: all 0.7s ease 0s;
  -o-transition: all 0.7s ease 0s;
  -transition: all 0.7s ease 0s;
}
#search input[type="text"]:focus {
  background: url(../img/search-icon.png) no-repeat 10px 6px #fff;
  color: #6a6f75;
  width: 350px;
  -webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.9) inset;
  -moz-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.9) inset;
  -box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.9) inset;
  text-shadow: 0 2px 3px rgba(0, 0, 0, 0.1);
}
@media screen and (max-width: 1280px) {
  #menu {
    postion: absolute;
    top: 0px;
    right0px;
    height: 37px;
    padding-top: 19px;
  }
}
#menu a {
  color: #000;
  text-decoration: none;
  font-size: 14px;
  padding-top: 19px;
  padding-bottom: 22px;
  padding-left: 10px;
  padding-right: 10px;
}
#menu a:hover {
  border-bottom: 2px solid #000000;
}
<div class="headerMenu">
  <div id="wrapper">
    <div class="logo">
      <div class="img"></div>
    </div>
    <div class="serch_box">
      <form action="serch.php" method="get" id="search">
        <input type="text" name="q" size="60px" placeholder="Search..." />
      </form>
    </div>

    <div id="menu">
      <a href="#">Home</a>
      <a href="#">About</a>
      <a href="#">Login</a>
      <a href="#">Register</a>
    </div>
  </div>
</div>

最佳答案

您的问题在于 div 元素是 block 元素,因此会强制自身换行。

如果我们告诉系统将#wrapper下的每个div设为内联元素,那么它们就会按预期排列。

它还需要 vertical-align: middle 使所有内容都与标题的中间对齐。如果不存在,则将其设置为标题元素的底部。

.headerMenu {
  background-color: #272626;
  Height: 56px;
  border-bottom: 0px;
  padding-left: auto;
  padding-right: auto;
  width: 100%;
}
#wrapper {
  margin-left: auto;
  margin-right: auto;
  width: 1000px;
  padding-top: 0px;
  padding-bottom: 0px;
}
div#wrapper > div {
  display: inline-block;
  vertical-align: middle;
}
.logo {
  padding-top: 9px;
  padding-bottom: 9px;
  width: 38px;
  transition-timing-function: linear;
  transition: all 2s;
}
.logo .img {
  background-image: url(../img/logo-white-p.png);
  width: 38px;
  height: 38px;
  transition-timing-function: linear;
  transition: all 0.5s;
}
.logo:hover {
  background-color: rgba(255, 255, 255, 1.00);
}
.logo .img:hover {
  background-image: url(../img/logo-black-p.png);
}
.search_box {
  position: absolute;
  top: 13px;
  margin-left: 155px;
}
#search input[type="text"] {
  background: url(../img/search-icon-white.png) no-repeat 10px 6px #000;
  outline: none;
  border: 0 none;
  font: bold 12px;
  color: #fff;
  width: 350px;
  padding: 6px 15px 6px 35px;
  text-shadow: 0 2px 2px rgba(255, 255, 255, 0.30);
  -webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 3px rgba(0, 0, 0, 0.2) inset;
  -moz-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 3px rgba(0, 0, 0, 0.2) inset;
  -box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 3px rgba(0, 0, 0, 0.2) inset;
  -webkit-transition: all 0.7s ease 0s;
  -moz-transition: all 0.7s ease 0s;
  -o-transition: all 0.7s ease 0s;
  -transition: all 0.7s ease 0s;
}
#search input[type="text"]:focus {
  background: url(../img/search-icon.png) no-repeat 10px 6px #fff;
  color: #6a6f75;
  width: 350px;
  -webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.9) inset;
  -moz-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.9) inset;
  -box-shadow: 0 1px 0 rgba(255, 255, 255, 0.1), 0 1px 0 rgba(0, 0, 0, 0.9) inset;
  text-shadow: 0 2px 3px rgba(0, 0, 0, 0.1);
}
@media screen and (max-width: 1280px) {
  #menu {
    postion: absolute;
    top: 0px;
    right0px;
    height: 37px;
    padding-top: 19px;
  }
}
#menu a {
  color: white;
  text-decoration: none;
  font-size: 14px;
  padding-top: 19px;
  padding-bottom: 22px;
  padding-left: 10px;
  padding-right: 10px;
}
#menu a:hover {
  border-bottom: 2px solid #000000;
}
<div class="headerMenu">
  <div id="wrapper">
    <div class="logo">
      <div class="img"></div>
    </div>
    <div class="serch_box">
      <form action="serch.php" method="get" id="search">
        <input type="text" name="q" size="60px" placeholder="Search..." />
      </form>
    </div>

    <div id="menu">
      <a href="#">Home</a>
      <a href="#">About</a>
      <a href="#">Login</a>
      <a href="#">Register</a>
    </div>
  </div>
</div>

关于html - 对齐标题菜单上的搜索栏和菜单栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34970187/

相关文章:

javascript - 如何不使用 jQuery 来设置 div 的宽度动画?

html - 底部的 CSS DIV 内联

HTML5/CSS - 将大图像放入较小的 DIV

html - 伪元素上的 Font Awesome 5 显示正方形而不是图标

css - 在单行中显示标题和内容

css - Chrome 中的 float 错误? 1px 额外填充,但它不是填充

javascript - 我决定不支持IE7;我现在可以使用哪些 JS/CSS/HTML 功能?

javascript - 是否可以在非服务页面上使用 JS/HTML5 FileReader 加载文件?

css - 如何设置一个div的最小高度来调整它的内容

html - 带文本的居中图标