简体   繁体   English

h1和nav在同一行

[英]h1 and nav on the same line

I searched Stack overflow and google and tried all the suggestions to getting my h1 and nav on the same line. 我搜索了Stack Overflow和Google,并尝试了所有建议以使我的h1和nav处于同一行。 I tried inline, inline-block, setting the header itself to 100%. 我尝试了内联,内联块,将标头本身设置为100%。 It's just not aligning. 只是不对齐。 On top of that my li posted backwards when I set it to float left so the home that was on the top of the list is now on the outer end instead of the beginning. 最重要的是,当我将其设置为向左浮动时,我的li向后发布,因此位于列表顶部的房屋现在位于最外面,而不是最开始。 here's my code 这是我的代码

  .header{ background-color: #00001a; height: 40px; width: 100%; } ul{ list-style-type: none; } .header h1{ float: left; color: #ffffff; font-size: 15px; display: inline-block; } .nav li{ float: right; display: inline-block; color: #ffffff; } 
 <div class="header"> <div class="nav"> <h1>EaTogeter</h1> <ul> <li>home</li> <li>About</li> <li>Couples</li> <li>family</li> </ul> </div> </div> <div class="Maincontent"> <div class="container"> <h2>Try It</h2 <p>Today's Try It Recipe!<p> </div> </div> 

display: flex; justify-content: space-between; will put them on the same line and separate them with the available space. 将它们放在同一行上,并用可用空间分隔它们。

 .header { background-color: #00001a; padding: 0 1em; } .nav { display: flex; justify-content: space-between; align-items: center; } ul { list-style-type: none; } .header h1 { color: #ffffff; font-size: 15px; } .nav li { color: #ffffff; display: inline-block; } 
 <div class="header"> <div class="nav"> <h1>EaTogeter</h1> <ul> <li>home</li> <li>About</li> <li>Couples</li> <li>family</li> </ul> </div> </div> 

Put the heading and the navigation in their own containers. 将标题和导航放在各自的容器中。 Float one left, the other right, and make sure to clear them afterwards. 左右浮动一个,然后确保清除它们。

 header { background-color: #00001a; padding: 0px 10px; box-sizing: border-box; } h1 { color: white; margin: 5px 0; padding: 0; } .left { float: left; } .right { float: right; } .clear { clear: both; } ul { list-style-type: none; } ul li { display: inline-block; color: white; margin-left: 20px; } 
 <header> <div class="left"> <h1> EaTogether </h1> </div> <div class="right"> <ul> <li>Home</li> <li>About</li> <li>Couples</li> <li>Family</li> </ul> </div> <div class="clear"></div> </header> 

Note: I've changed "Togeter" to "Together", assuming it was a typo. 注意:我已经将“ Togeter”更改为“ Together”,假设它是拼写错误。

I am not sure if you want this thing but I just gave a try, 我不确定您是否要这个东西,但我只是尝试了一下,

For this, set float:right to ul element and not on li elements. 为此,请将float:right设置为ul元素,而不要设置为li元素。 Since you want to align h1 and li content set line-height to 0.5 for ul element 由于要对齐h1和li内容,因此将ul元素的line-height设置为0.5

please check this fiddle: https://jsfiddle.net/hz0104mp/ 请检查此小提琴: https : //jsfiddle.net/hz0104mp/

<div class="header">
       <div class="nav">
        <h1>EaTogeter</h1>
        <ul>
         <li>home</li>
         <li>About</li>
         <li>Couples</li>
         <li>family</li>
        </ul>   
        </div>
       </div>
        <div class="Maincontent">
          <div class="container">
          <h2>Try It</h2>
          <p>Today's Try It Recipe!<p>
         </div>
       </div>


.header{
        background-color: #00001a;
        height: 40px;
        width: 100%;
       }

        ul{
         list-style-type: none;
        }

        .header h1{
         color: #ffffff;
         font-size: 15px;
         display: inline-block;
        }

       .nav ul{
          float:right;
          line-height:0.5;
        }

       .nav li{
        display: inline-block;
        color: #ffffff;
       }

I like the flexbox method mentioned by @Michael Coker but here's a solution using floats as the OP attempted to do. 我喜欢@Michael Coker提到的flexbox方法,但是这是在OP尝试执行时使用浮点数的解决方案。

You were on the right track but might have been applying some of your CSS to the wrong elements for the wrong reasons. 您的工作方向正确,但由于错误的原因,可能会将某些CSS应用于错误的元素。

On top of that my li posted backwards when I set it to float left so the home that was on the top of the list is now on the outer end instead of the beginning. 最重要的是,当我将其设置为向左浮动时,我的li向后发布,因此位于列表顶部的房屋现在位于最外面,而不是最开始。

The reasons for this are not obvious until you break things down. 除非您分解,否则原因并不明显。 The reason this happens is because float: right is applied to each element separately and in the order they appear in the markup, not as a whole. 发生这种情况的原因是因为float: right分别按其在标记中的顺序而不是整体应用于每个元素。 The browser will float Home as far to the right as it can. 浏览器将“ 主页”尽可能向右浮动。 After that, it will move About as far to the right as it can. 在此之后,它会尽可能的权利,因为它可以走动 Rinse and repeat for any other li . 冲洗和重复任何其他li

I rectified this by floating the ul instead of individual li and setting those to display: inline; 我通过浮动ul而不是单独的li并将其设置为display: inline;来纠正此问题display: inline; . Floating the li to the left would also work. li浮动到左侧也可以。

 header { padding: 0 0.5rem; height: 40px; color: #ffffff; background-color: #00001a; } ul, li { margin: 0; padding: 0; list-style: none; } header h1 { margin: 0; font-size: 15px; display: inline-block; } header h1, .nav li { line-height: 40px; } .nav { float: right; } .nav li { padding: 0 0 0 0.25rem; display: inline; } 
 <header> <h1>Eat Together</h1> <ul class="nav"> <li>Home</li> <li>About</li> <li>Couples</li> <li>Family</li> </ul> </header> <main> <h2>Try It</h2> <p>Today's Try It Recipe!<p> </main> 

Please note that I took a few liberties with your markup to help provide an example of how it can be more semantic and achieved with less markup (along with a few choice styles to make it a little more "pretty" ). 请注意,我为您的标记保留了一些自由,以帮助您提供一个示例,说明如何通过更少的标记实现更多的语义和实现(以及一些选择样式,使其更“漂亮” )。

删除float: left .nav li float: left ,然后添加到ul

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM