【问题标题】:No way to float right my input area in the nav bar - Why? [duplicate]无法在导航栏中向右浮动我的输入区域 - 为什么? [复制]
【发布时间】:2020-05-25 05:15:24
【问题描述】:

我找不到将我的输入字段(搜索框)及其相关图标(放大镜)设置为 float 的方法,位于我网站导航栏的右侧(同时将我的徽标留在左侧的导航)。有什么建议吗?

HTML:

 <nav>
        <div class="nav-container">
            <div class="logo">
                <a href="">
                    <h1>MyWebsiteName.com</h1>
                </a>
            </div>
            <form action="javascript:redirect()" autocomplete="off">
                <input type="search" name="search" id="search" class="form-control">
                <ul class=mylist list-group" id="result"></ul>  <!--this line is for JS-->
                <button type="submit">
                   <svg width="50" height="50" viewBox="0 0 77 77" fill="none" 
                           xmlns="http://www.w3.org/2000/svg">
                   </svg> </button>
            </form>

        </div>
</nav>

CSS:


.nav-container {
  display: flex;
  align-items: center;
  padding: 16px 0;
  position: relative;
}
.nav-container form {
  display: flex;
  align-items: center;
  justify-content: center;
}
nav input {
  font-size: 1.20em;
  border: solid 2px #E5E5E5;
  border-radius: 25px;
  background: none;
  color: #F4F4F4; 
  padding: 14px;
  margin: 0 20px 0 0;
  width: 260px;
}
form {
  float: right;
}

【问题讨论】:

  • 可能是......如果没有看到你完成的 css,我们不确定。
  • 抱歉,刚刚添加了CSS! @Manjuboyz
  • 感谢分享css,你有答案了:)

标签: html css input flexbox css-float


【解决方案1】:

在表单上使用 margin-left: auto; 而不是 float: right - 它位于弹性容器 (.nav-container) 内,浮动无效,但 margin-left: auto 会将其移动到尽可能远的位置。

.nav-container {
  display: flex;
  align-items: center;
  padding: 16px 0 16px 0;
  position: relative;
}

.nav-container form {
  display: flex;
  align-items: center;
  justify-content: center;
}

nav input {
  font-size: 1.20em;
  border: solid 2px #E5E5E5;
  border-radius: 25px;
  background: none;
  color: #F4F4F4;
  padding: 14px;
  margin: 0 20px 0 0;
  width: 260px;
}

form {
  margin-left: auto;
}
<nav>
  <div class="nav-container">
    <div class="logo">
      <a href="">
        <h1>MyWebsiteName.com</h1>
      </a>
    </div>
    <form action="javascript:redirect()" autocomplete="off">
      <input type="search" name="search" id="search" class="form-control">
      <ul class=mylist list-group " id="result "></ul>     <!-- this line is for JS -->
                <button type="submit ">
                   <svg width="50 " height="50 " viewBox="0 0 77 77 " fill="none " 
                           xmlns="http://www.w3.org/2000/svg ">
                   </svg> </button>
            </form>

        </div>
</nav>

【讨论】:

  • 太好了,谢谢你的解释!
【解决方案2】:

有多种方法可以做到这一点。

flexbox

grid

注意:

由于 sn-p 中的空间限制,您必须在 full screen 中查看它。

  1. 我刚刚使用了position:absolute;,这会将您的内容移至right。
  2. 您可以对这 2 个输入进行包装,并使用 flex 将这 2 个项目保持在右侧。
  3. 不要使用float,它已经过时了,并且在您的响应式设计中不太灵活。

.nav-container {
  display: flex;
  align-items: center;
  padding: 16px 0 16px 0;
  position: relative;
}

.nav-container form {
  display: flex;
  align-items: center;
  justify-content: center;
}

nav input {
  font-size: 1.20em;
  border: solid 2px #E5E5E5;
  border-radius: 25px;
  background: none;
  color: #F4F4F4;
  padding: 14px;
  margin: 0 20px 0 0;
  width: 260px;
}

.moveRight {
  position: absolute;
  right: 10px;
}
<nav>
  <div class="nav-container">
    <div class="logo">
      <a href="">
        <h1>MyWebsiteName.com</h1>
      </a>
    </div>
    <form action="javascript:redirect()" autocomplete="off" class="moveRight">
      <input type="search" name="search" id="search" class="form-control">
      <ul class="mylist list-group" id="result"></ul>
      <!-- this line is for JS -->
      <button type="submit">
                   <svg width="50" height="50" viewBox="0 0 77 77" fill="none" 
                           xmlns="http://www.w3.org/2000/svg">
                   </svg> </button>
    </form>

  </div>
</nav>

【讨论】:

  • 谢谢,它有效! :) 我不知道浮动被认为是旧的,你通常更喜欢什么响应式设计?
  • 很高兴它有效! position:absolute; 将适用于您的情况和响应能力,因为它将这两个控件始终保持在正确的方向。当您考虑进行任何响应式设计,尤其是布局设计时,请尝试使用 flexbox 或 grid 这两个对响应式设计非常有用,开始探索它们将为您提供非常好的信息。
  • 我已经添加了 flexbox 和 grid 布局文档,这可能会对您有所帮助。快乐编码。
  • 太棒了,我真的很感激 :)
【解决方案3】:

错过了

.nav-container {
justify-content: space-between;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-19
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多