简体   繁体   English

在页面顶部连接标题,徽标,导航栏和文本

[英]Concatenate header, logo, navigation bar, and text on top of a page

I've been trying to get all those elements on the same grey background but I don't know why the header and navigation bar are separated, and the logo not being on top of the grey background. 我一直在尝试将所有这些元素设置在相同的灰色背景上,但是我不知道为什么标题和导航栏是分开的,并且徽标不在灰色背景之上。 当前标题

The code: 编码:

  .header { background-color: #666; text-align: center; font-size: 35px; color: white; margin:0; } #navigation { list-style-type: none; margin: 0; padding: 0; overflow: hidden; background-color: #666; } #navigation li { float: left; } #navigation li a { display: block; color: white; text-align: center; padding: 14px 16px; text-decoration: none; } #navigation li a:hover { background-color: #111; } 
 <div> <h2 class = "header" >Personal Portal</h2> </div> <div> <div> <nav> <a> <img src="logo.png" alt="Logo" style="float:right; position:relative; "> </a> <ul id = "navigation"> <li><a class="active" href="Home.htm">Home</a></li> <li><a href="OpenedTickets">Opened Tickets</a></li> <li><a href="FAQ.htm">FAQ</a></li> <li><a href="Stats.htm">Stats</a></li> </ul> </nav> </div> <div> <div id="loggedas" style = "float:right; padding-top:0; background-color:#666; color:#FFFFFF"> Logged in as </div> 

This is a very beginner-style question but I can't find the answers I want. 这是一个非常初学者风格的问题,但我找不到想要的答案。 Any help is appreciated thank you! 任何帮助表示感谢,谢谢!

You could try something like this: 您可以尝试这样的事情:

<div style="background-color: #666; height: 50px">  
    <nav style="clear: both">
        <ul id = "navigation">
            <li><a class="active" href="Home.htm">Home</a></li>
            <li><a href="OpenedTickets">Opened Tickets</a></li>
            <li><a href="FAQ.htm">FAQ</a></li>
            <li><a href="Stats.htm">Stats</a></li>
        </ul>

       <h2 class = "header" >Personal Portal</h2>

       <div class="rightInfo">
           <div id="loggedas" style = "float:right; padding-top:0; background-color:#666; color:#FFFFFF"> 
      Logged in as 
           </div>

       <a> 
           <img src="logo.png" alt="Logo" style="float:right; position:relative; ">       
       </a>
    </div>
</nav>

.header {
    background-color: #666;
    text-align: center;
    font-size: 35px;
    color: white;
    float: left;
    display: inline-block;
    margin-top: 0;
}

.rightInfo{
    float: right;
}

#navigation {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    background-color: #666;
    float: left;
}

#navigation li {
    float: left;
}

#navigation li a {
    display: block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

#navigation li a:hover {
    background-color: #111;
}

Your three display elements should have a common parent, otherwise, they will not display as you want them to, except if you use position: absolute to position them (I would not recommend that) 您的三个显示元素应该有一个共同的父元素,否则,它们将不会按您希望的那样显示,除非您使用position:absolute来放置它们(我不建议这样做)

To complete the previous answer (regarding the question why the title and nav bar are separated). 完成上一个答案(关于标题和导航栏为何分开的问题)。 You have placed those elements in separate divisions each with its own background color. 您已将这些元素放置在单独的区域中,每个区域都有其自己的背景色。 You could correct the situation by removing the margin between them, but: if you want two (or more) things to be on a common background, it is best to set that background on a common parent. 您可以通过消除它们之间的边距来纠正这种情况,但是:如果您希望两个(或多个)事物位于同一背景下,则最好将该背景设置为同一父背景。

Also, in any case when you're wondering why the browser rendered your code as it did: ask the browser. 另外,无论如何,如果您想知道浏览器为何如此渲染代码,请询问浏览器。 Press Ctrl-Shift-I (works on Chrome, Chromium and Firefox). 按Ctrl-Shift-I(适用于Chrome,Chromium和Firefox)。 It will show you all the applied styles (and exactly where they came from), plus the element size, padding, borders and margins. 它将向您显示所有应用的样式(以及它们的确切来源)以及元素大小,内边距,边框和边距。

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

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