简体   繁体   中英

HTML Div not appearing in nav bar

I am trying to make a navigation bar in the top of my website and my logo appears but the "home" doesn't when i try to float left. What am I doing wrong?

When I make the site as small as it will go, it appears left of the logo, but I want it to be to the right.

<html>
<head>

</head>

<style>
body{
margin: 0px;
padding: 0px;
}

#topBar{
background-color: #242424;
height: 63px;
}

#logo{
height: 40px;

}
#logo-item{
float: left;
margin-left: 90px;
padding-top: 10px;
}
.menu-item{
float: left;
margin-top: -28px;
font-size: 110%
margin-right: 20px;
font-color: white;
}
.topBarItems{

}

</style>

<body>

<div id="topBar">

<div id="logo-item"> <img id="logo" src="backflip-logo.png"> </div>

<div class="menu-item">HOME</div>

</div>
</body>
</html>

There are several problems with this HTML

  • First you have the style tag between the head and body tag, it should go inside the head tag - which may cause strange behavior or simply not work depending on the browser
  • You are using a margin of -28px, which effectively moves the inner div above the outer div which in turn renders it invisible (as it is above the visible page).
  • font-color should be color

Here is your code with a few edits:

<body>
    <div id="topBar">
        <img id="logo" class="menu-item logo-item" src="backflip-logo.png"><!-- Updated the class -->
        <div class="menu-item">HOME</div><!-- Updated the tag placement to fall within top bar -->
    </div>
</body>

Also edited your CSS a bit:

#topBar{
background-color: #242424;
height: 63px;
}

#logo{
height: 40px;

}
.logo-item{
float: left;
margin-left: 90px;
}
.menu-item{
padding-top: 10px;
float: left;
font-size: 110%
margin-right: 20px;
color: #ffffff;
}
.topBarItems{

}

But, instead of doing everything from scratch, you may just want to use a framework such as Bootstrap : http://getbootstrap.com

Here is a fiddle for it: https://jsfiddle.net/windrunn3r1990/fck4zsue/

You can solve your issue by using display:inline-block; rather than floating components. https://jsfiddle.net/oxycm856/

HTML

<body> 
<div id="topBar">
<div id="logo-item"> <img id="logo" src="backflip-logo.png"> </div>
<div class="menu-item">HOME</div> 
</div>
</body>

CSS

#topBar{
  width:100%;
  background-color:#242424;
  height: 63px;
}

#logo-item, .menu-item{
  display:inline-block;
  color:#fff;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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