简体   繁体   English

CSS:如何使无序列表之后的元素不从新行开始?

[英]CSS: How can I keep the element after an unordered list from starting on a new line?

I'm trying to make a simple navigation bar that has a input box. 我正在尝试制作一个具有输入框的简单导航栏。

    <!DOCTYPE html>
<html>
    <head>
        <title>Website</title>
        <link rel="stylesheet" href="./main2.css">
    </head>
    <body>
        <div id="container">
            <div id="nav">
                <h1>Userfind</h1>
                    <ul>
                        <li><a href="#home">Home</a></li>
                        <li><a href="#contact">Contact</a></li>
                        <li><a href="#about">About</a></li>
                    </ul>

                <div id="main-search">
                    <input type="text" id="usersearch" name="usersearch" placeholder="Search for User here. Must be atleast 5 characters long." required
                            minlength="5" maxlength="30" size="60">              
                 </div>
            </div>
        </div>
    </body>
</html>

But no matter what I try, I can't figure out how to keep the input box on the same line.The CSS looks like this: 但是无论如何尝试,我都无法弄清楚如何将输入框保持在同一行.CSS看起来像这样:

body {
  background-color: black;
}

#container {
  background-color: purple;
  margin-left: auto;
  margin-right: auto;
}

#nav {
  background-color: #404040;
  color: white;
  height: 39px;
  line-height: 39px;
  padding: 0px 10px 39px;
}
#nav h1 {
  text-align: left;
  float: left;
}
#nav ul {
  list-style-type: none;
  margin: 0;
  padding: 10px;
  overflow: hidden;
}
#nav li {
  float: left;
}
#nav a {
  display: block;
  text-align: center;
  padding: 14px 16px;
  background-color: #404040;
  color: white;
  text-decoration: none;
}
#nav p {
  float: left;
}
#nav #main-search {
  display: block;
  float: left;
}

If I put the div "main-search" in the unordered list it stays on the same line. 如果我将div“ main-search”放在无序列表中,它将保持在同一行。 But how can I get it it to stay on the same line without putting in between the <ul> tags? 但是,如何在不放在<ul>标记之间的情况下使其保持同一行呢?

This is not a full answer by any means. 无论如何,这不是一个完整的答案。 But it includes some of the suggestions from the comments. 但这包括评论中的一些建议。 I changed #nav li to display: inline-block, along with removing some of the conflicting css that you had. 我将#nav li更改为显示:inline-block,同时删除了一些您有冲突的CSS。

I suggest doing the search feature as an li, or doing all nav links as separate divs using display: flex (aka flexbox). 我建议使用li来进行搜索,或者使用display:flex(又名flexbox)将所有的nav链接作为单独的div进行。

There's a short tutorial below that you can adapt to your case: https://www.freecodecamp.org/news/how-to-create-a-fully-responsive-navbar-with-flexbox-a4435d175dd3/ 下面有一个简短的教程,您可以根据自己的情况进行调整: https : //www.freecodecamp.org/news/how-to-create-a-fully-sensitive-navbar-with-flexbox-a4435d175dd3/

Your code that I added display: inline-block and removed some conflicting css: 我添加的代码显示:inline-block并删除了一些冲突的CSS:

   <!DOCTYPE html>
<html>
    <head>
        <title>Website</title>
        <link rel="stylesheet" href="./main2.css">
    </head>
    <body>
        <div id="container">
            <div id="nav">
                <h1>Userfind</h1>
                    <ul>
                        <li><a href="#home">Home</a></li>
                        <li><a href="#contact">Contact</a></li>
                        <li><a href="#about">About</a></li>
                    </ul>

                <div id="main-search">
                  <div>
                    <input type="text" id="usersearch" name="usersearch" placeholder="Search for User here. Must be atleast 5 characters long." required
                            minlength="5" maxlength="30" size="60"> 
                  </div>           
                 </div>
            </div>
        </div>
    </body>
</html>

And: 和:

body {
  background-color: black;
}

#container {
  background-color: purple;
  margin-left: auto;
  margin-right: auto;
}

#nav {
  background-color: blue;
  color: white;
  height: 39px;
  line-height: 39px;
  padding: 0px 10px 39px;
}
#nav h1 {
  text-align: left;
  float: left;
}
#nav ul {
  list-style-type: none;
  margin: 3px;
  overflow: hidden;
}
#nav li {
  display: inline-block;
}
#nav a {
  text-align: center;
  background-color: orange;
  color: white;
  text-decoration: none;
}

#nav p {
}
#nav #main-search {
  margin-left: 27%;
  margin-bottom: 50px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用CSS在嵌套的无序列表中保持第二行的缩进? - How to keep indent for second line in nested unordered list using CSS? 使用CSS flex,如何在标题之后到行尾之间获得一行(如hr元素) - Using CSS flex, how can I get a line (like the hr element) starting after my title to the end of the line 我如何保持水平无序列表不被破坏? - How I keep a horizontal unordered list from breaking? 如何从无序列表中删除列表元素? - How do I remove a list element from an unordered list? 如何在 flex 元素之后创建新行? - How can I create a new line after a flex element? 如何调整 html 和 css 中的图像、无序列表和按钮 - How can i adjust images , unordered list and buttons in html and css Bootstrap CSS-导航栏列表从新行开始换行。 怎么做 <ul> 从当前行开始换行? - Bootstrap CSS - navbar list wraps starting on a new line. How to make <ul> wrap starting on the current line? 如何从其值访问无序列表项? - How can I access an unordered list item from its value? 如何通过 contenteditable div 输入保留附加到列表的元素的换行符(格式)? - How can I keep the line breaks (formatting) of an element appended to a list via a contenteditable div input? 从中心开始后的伪元素-CSS - Pseudo element in after starting from center - CSS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM