html - 如何使左浮和右浮在同一行上?

标签 html css css-float

问题:

     **The left part** (#nav ul li) which float: left
     and **the right part** (#nav .search) which float: right
            **are not in a line**.

it should be like this: enter image description here

but mine: enter image description here

The HTML:

<div id="nav">
   <ul>
     <li><a href="#">Home</a></li>
     <li><a href="#">Portfolio</a></li>
     <li><a href="#">Blog</a></li>
     <li><a href="#">Contact</a></li>
   </ul>
   <div class="search">
     <input type="text" name="search" id="search" value="search">
   </div>       

CSS:

#nav ul li{
float: left;
list-style: none;
margin: 0 20px;
}

#nav .search{
float: right;
}


我的解决方案:

解决方案 1:使用 bootsrap 构建布局,而不是自己做,我在页脚上使用它,它完全在同一行上!但我仍然不知道它是如何工作的 enter image description here

解决方案 2:我使用 ma​​rgin-top: -20px 来拉起搜索框,但我认为这不是一个好的做法。

有人可以帮忙吗?非常感谢!


最佳答案

已经提到的解决方案的替代方案是翻转标记的顺序,以便右侧 float 的元素排在第一位。

#nav ul li {
  float: left;
  list-style: none;
  margin: 0 20px;
}
#nav .search {
  float: right;
}
<div id="nav">
   <div class="search">
     <input type="text" name="search" id="search" value="search">
   </div>       
   <ul>
     <li><a href="#">Home</a></li>
     <li><a href="#">Portfolio</a></li>
     <li><a href="#">Blog</a></li>
     <li><a href="#">Contact</a></li>
   </ul>
</div>

您无需执行任何其他操作。不需要其他 CSS 或标记。

关于html - 如何使左浮和右浮在同一行上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14471662/

相关文章:

javascript - 悬停到不同的 div 后显示 div 内容

javascript - 当 slider 中的图像处于事件状态时显示 div

javascript - 我无法将 css 文件连接到 node.js 服务器上的 html 文件

javascript - HTML 表单检查空字段

html - div 内的文本和图像顶部对齐

html - 在 HTML 和 CSS 中实现完全对齐 : works in limited-quirks mode but no-quirks mode messes up the height

html - 在 bootstrap 列中包含 mp4 视频

html - CSS子清除父 float

css - 使用 float 网格模式内容将页脚贴在底部

html - 为什么显示:block not doing what I expect on my nav items?