如何在固定导航中放置标题?

时间:2018-08-19 21:50:23

标签: html css header navigation fixed

我遇到了一个问题,我不确定该如何解决,我认为这可能与职位有关?我试图查找解决方案,但仍然不知道为什么会这样。

因此,我试图将标题(名称)放在固定的导航栏中。当我尝试执行此操作时,标题位于灰色背景色的后面。 如何将标题置于背景上方?我希望标题固定在导航栏中。.非常感谢!

html {
    margin: 0;
    background: #ffffff;
}

body {
    margin: 0;
    background: #ffffff;
}

/*---font---*/
@font-face {
    font-family: open-sans;
    src: url('open-sans.regular.ttf');
}

@font-face {
    font-family: Prata-Regular;
    src: url('Prata-Regular.ttf');
}

@font-face {
    font-family: Frontage-Outline;
    src: url('Frontage-Outline.otf');
}

/*---nav bar---*/
h1 {
    display: inline-block;
    float: left;
    position: fixed;
    z-index: -1;
}

ul {
    list-style-type: none;
    background-color: #F5F5F5;
    margin: 0;
    padding: 0;
    overflow: hidden;
    position: fixed;
    top: 0;
    width: 100%;
    display: inline-block;
}

li {
    float: right;
}

li a {
    display: block;
    color: #000;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-family: open-sans;
}

li a:hover:not(.active) {
    color: #555555;
}

.active {
    color: #000;
}
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
    <head>
        <link rel="stylesheet" href="main.css"> 
    </head>
    
    <body>  
        
<!--nav bar-->
    <div class="navbar">
        <div class="bar">
            <h1>cindy le</h1>
            <ul>
                <li><a href="#about">about</a></li>
                <li><a href="#work">work</a></li>
                <li><a href="#contact">contact</a></li>
            </ul>
        </div>
    </div>
        
    </body>



</html>

2 个答案:

答案 0 :(得分:0)

将h1的z-index更改为1

html {
    margin: 0;
    background: #ffffff;
}

body {
    margin: 0;
    background: #ffffff;
}

/*---font---*/
@font-face {
    font-family: open-sans;
    src: url('open-sans.regular.ttf');
}

@font-face {
    font-family: Prata-Regular;
    src: url('Prata-Regular.ttf');
}

@font-face {
    font-family: Frontage-Outline;
    src: url('Frontage-Outline.otf');
}

/*---nav bar---*/
h1 {
    display: inline-block;
    float: left;
    position: fixed;
    z-index: 1; /* Change to 1*/
}

ul {
    list-style-type: none;
    background-color: #F5F5F5;
    margin: 0;
    padding: 0;
    overflow: hidden;
    position: fixed;
    top: 0;
    width: 100%;
    display: inline-block;
}

li {
    float: right;
}

li a {
    display: block;
    color: #000;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-family: open-sans;
}

li a:hover:not(.active) {
    color: #555555;
}

.active {
    color: #000;
}
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
    <head>
        <link rel="stylesheet" href="main.css"> 
    </head>
    
    <body>  
        
<!--nav bar-->
    <div class="navbar">
        <div class="bar">
            <h1>cindy le</h1>
            <ul>
                <li><a href="#about">about</a></li>
                <li><a href="#work">work</a></li>
                <li><a href="#contact">contact</a></li>
            </ul>
        </div>
    </div>
        
    </body>



</html>

答案 1 :(得分:0)

执行此操作的最佳方法是修复导航栏div,然后将div浮动到左侧并包含h1标签,将div右侧添加并包含ul。我不会浮动h1和ul。如下所示:

.navbar{
position: fixed;
width: 100%;
top: 100%;
left: 100%;
background-color: #f5f5f5;
}

.navbar-left{
float left;
text-align: left;
}

.navbar-right{
float: right;
text-align: right;
}

.navbar ul{
display: inline-block;
list-style-type: none;
margin: 0px;
padding: 0px;
}

.navbar ul li{
float: left;
}

.navbar ul li a{
display: block;
padding: 14px 16px;
color: #555555;
font-family: open-sans;
}

.navbar ul li a:hover{
color: #000;
}

.clear{
clear: both;
}  


<div class="navbar">
    <div class="navbar-left">
        <h1>header text</h1>
   </div>
   <div class="navbar-left">
        <ul>
            <li><a href="#about">about</a></li>
            <li><a href="#work">work</a></li> 
            <li><a href="#contact">contact</a></li>
        </ul>
    </div>
    <div class="clear"></div>
</div>

很抱歉格式化。用手机发帖。