如何在导航栏的末尾放置输入文本?

时间:2018-09-30 05:00:23

标签: html css

nav {
  background-color: #e1e1e1;
  height: 200px;
}

nav ul li a {
  color: #fff;
  padding-right: 20px;
}

nav ul {
  display: inline-flex;
  list-style: none;
}
<nav>
  <ul>
    <li><a href="">Home</a></li>
    <li><a href="">About</a></li>

    <div class="col-sm-12 col-md-12 col-lg-3 my-1 divSearch">
      <form>
        <div>
          <input placeholder="Search">
          <button type="submit">
            <img>
          </button>
        </div>
      </form>
    </div>
  </ul>
</nav>

我需要将input放在nav的右侧(末尾)。我该怎么办?

我尝试放入float: right;justify-content: flex-end;align-content: flex-end,但没有用。

3 个答案:

答案 0 :(得分:0)

尝试一下...

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

.topnav {
  overflow: hidden;
  background-color: #e9e9e9;
}

.topnav a {
  float: left;
  display: block;
  color: black;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover {
  background-color: #ddd;
  color: black;
}

.topnav a.active {
  background-color: #2196F3;
  color: white;
}

.topnav .search-container {
  float: right;
}

.topnav input[type=text] {
  padding: 6px;
  margin-top: 8px;
  font-size: 17px;
  border: none;
}

.topnav .search-container button {
  float: right;
  padding: 6px;
  margin-top: 8px;
  margin-right: 16px;
  background: #ddd;
  font-size: 17px;
  border: none;
  cursor: pointer;
}

.topnav .search-container button:hover {
  background: #ccc;
}

@media screen and (max-width: 600px) {
  .topnav .search-container {
    float: none;
  }
  .topnav a, .topnav input[type=text], .topnav .search-container button {
    float: none;
    display: block;
    text-align: left;
    width: 100%;
    margin: 0;
    padding: 14px;
  }
  .topnav input[type=text] {
    border: 1px solid #ccc;  
  }
}
<body>

<div class="topnav">
  <a class="active" href="#home">Home</a>
  <a href="#about">About</a>
  <div class="search-container">
    <form action="/action_page.php">
      <input type="text" placeholder="Search.." name="search">
      <button type="submit">Submit</button>
    </form>
  </div>
</div>

<div style="padding-left:16px">
  <h2>Responsive Input Field in Navbar</h2>
  <p>Navigation bar with a search box and a submit button inside of it.</p>
  <p>Resize the browser window to see the responsive effect.</p>
</div>

</body>

答案 1 :(得分:0)

我已经记录了CSS代码中的更改

nav {
  background-color: #e1e1e1;
  height: 200px;
}

nav ul li a {
  color: #fff;
  padding-right: 20px;
}

nav ul {
  display: flex; /* Instead of inline-flex */
  list-style: none;
}

.divSearch {
  margin-left: auto; /* Pushes it to the right */
}
<nav>
  <ul>
    <li><a href="">Home</a></li>
    <li><a href="">About</a></li>
    <div class="col-sm-12 col-md-12 col-lg-3 my-1 divSearch">
      <form>
        <div>
          <input placeholder="Search">
          <button type="submit">
            <img>
          </button>
        </div>
      </form>
    </div>
  </ul>
</nav>

答案 2 :(得分:0)

这是我的解决方法。

.divSearch将div li换行并添加margin-left: auto; 同时将inline-flex的{​​{1}}更改为flex

ul
nav {
  background-color: #e1e1e1;
  height: 200px;
}

nav ul li a {
  color: #fff;
  padding-right: 20px;
}

nav ul {
  display: flex;
  list-style: none;
}

.input-parent {
  margin-left: auto;
}