I want to code a navigation bar for my website. It should support a mobile and a desktop view. Now I want to add a div in nav and it doesn't work, but when I analyse it with inspecting element in firefox and refresh the site it works. Can anyone help me?
Here is a code-snippet of the nav:
/* Here is the css declaration of the drop class: */
.drop {
width: 50px;
height: 50px;
background-color: #FFFFFF;
cursor: pointer;
display: block;
position: absolute;
right: 0;
top: 0;
}
<nav class="nav" id='navigation'>
<ul style="font: normal 14px Tauri, serif ">
<li style="float: left; border: none ">
<a href='index.php'>test</a>
</li>
<button class="drop"></button>
Actually there is a "nav" but it got no height.
I did a Fiddle where I added some height and a background color, also removing margin to the body, check this out!
https://jsfiddle.net/tu9rh3sg/
This for the HTML (I've just closed the nav):
<nav class="nav" id='navigation'>
<ul style="font: normal 14px Tauri, serif ">
<li style="float: left; border: none ">
<a href='index.php'>test</a>
</li>
<button class="drop"></button>
</nav>
And this for the CSS (height + background-color+margin);
.drop {
width: 50px;
height: 50px;
background-color: #FFFFFF;
cursor: pointer;
display: block;
position: absolute;
right: 0;
top: 0;
}
.nav {
background-color: black;
height: 50px;
}
body {
margin: 0 auto;
margin-top: -14px;
}
Tell me if it help ! ☻
Related
This question already has an answer here:
Why position:sticky is not working when the element is wrapped inside another one?
(1 answer)
Closed 1 year ago.
I'm new to HTML/CSS and am building a simple test site. Currently, I am trying to get sticky positioning to work. Specifically, I have a title/logo area with a toolbar underneath. As you scroll down, I want the title to scroll away, but the toolbar to scroll until it reaches the top of the page, where it'll remain.
I've tried removing all size adjustments, padding, and margins. I read that height adjustments can cause problems, so I thought I'd try all of those. I don't have any overflow either. I also tried putting the sticky position on all of the different elements. It's still not working and I don't understand why.
Any advice would be greatly appreciated. Thanks!
My code:
h1 {
background-color: lightblue;
text-align: center;
}
.toolbar {
border: 3px double black;
background-color: coral;
padding: 5px;
text-align: center;
width: 80%;
max-width: 650px;
margin: auto;
position: sticky;
top: 0px;
margin: auto;
}
main {
height: 200vh;
}
a {
display: inline-block;
color: black;
background-color: lightblue;
list-style-type: none;
border: 1px solid black;
width: 100px;
font-size: 20px position:sticky;
}
a:link {
text-decoration: none;
color: red;
}
<header>
<h1>Title Area</h1>
<h2>"Awesome Tagline!"</h2>
<hr>
</header>
<nav>
<ul class="toolbar">
<a href="">
<li>Home</li>
</a>
<a href="">
<li>About</li>
</a>
<a href="">
<li>Menu</li>
</a>
<a href="">
<li>Other</li>
</a>
</ul>
</nav>
<main></main>
position: sticky; will scroll for the parents height, so for you it will be for as long as nav is present on the screen. So in your case you need to put the position: sticky; on the <nav class="some-class">...</nav>.
Not sure if that was your correct html but you are missing the <body> element.
This doesnt work because the .toolbar is a child element of the <nav> tag. A sticky element it must stay within its parent. if the parent leaves the screen, so does the child element (.toolbar). Move the sticky property to the <nav> tag:
Delete this line:
.toolbar { position: sticky; top: 0; }
Add this line:
nav { position: sticky; top: 0; }
h1 {
background-color: lightblue;
text-align: center;
}
.toolbar {
border: 3px double black;
background-color: coral;
padding: 5px;
text-align: center;
width: 80%;
max-width: 650px;
margin: auto;
margin: auto;
}
nav {
position: sticky;
top: 0px;
}
main {
height: 200vh;
}
a {
display: inline-block;
color: black;
background-color: lightblue;
list-style-type: none;
border: 1px solid black;
width: 100px;
font-size: 20px position:sticky;
}
a:link {
text-decoration: none;
color: red;
}
<header>
<h1>Title Area</h1>
<h2>"Awesome Tagline!"</h2>
<hr>
</header>
<nav>
<ul class="toolbar">
<a href="">
<li>Home</li>
</a>
<a href="">
<li>About</li>
</a>
<a href="">
<li>Menu</li>
</a>
<a href="">
<li>Other</li>
</a>
</ul>
</nav>
<main></main>
This is a solution-way question to another question I asked before (Open 100% width Navigation on hover over navigation item)
I am trying to build a navigation bar that shows more in depth search terms as soon as you hover over the respective navigation bar item.
Now I think I am close to a solution: I created this separate div that should be visible as soon as one hovers over "Produkte" / #navigation-item2. Now with the way I am acustomed to :hover this does not work. How can I make this work?
.navigation {
width: 100%;
height: auto;
max-height: 20vh;
background: white;
display: flex;
;
align-items: center;
justify-content: center;
}
.navigation-item {
position: relative;
height: 10%;
width: 15%;
font-size: 1.2em;
color: black;
font-weight: bold;
padding: 0.5em 1em 0.5em 1em;
margin: 0px 1% 0px 1%;
text-align: center;
text-transform: uppercase;
transition: background 200ms linear;
text-decoration: none;
}
.navigation-item:hover {
background: lightgrey;
}
.nav-menu {
height: 200px;
position: absolute;
width: 100%;
max-height: 25vh;
opacity: 0;
}
#navigation-item2:hover #nav-menu2 {
opacity: 1
}
<header>
<nav class="navigation">
<a href="https://www.amazon.com" style="flex: 0.5; margin-left: 20px">
<img src="/images/Logo.png" alt="Logo" style="height:100%; width:auto; max-height: 16vh; margin-top: 3px;">
</a>
<a class="navigation-item" href="https://www.amazon.com">
Home</a>
<a class="navigation-item" href="https://www.amazon.com">
Portfolio</a>
<a class="navigation-item" id="navigation-item2" href="https://www.amazon.com">
Produkte</a>
<a class="navigation-item" href="https://www.amazon.com">
Über uns</a>
</nav>
<div class="nav-menu" id="#nav-menu2">
<ul class="dropdown">
<li>
<ul>
<li>Kochmesser</li>
<li>Küchenmesser</li>
</ul>
<li>Scheren</li>
<li>Klingen</li>
</ul>
</div>
</header>
What #navigation-item2:hover #nav-menu2 means, is that it targets the child element #nav-menu2 when the parent #navigation-item2 is hovered.
What you need to do is to move the #nav-menu2 inside the nav-tag, because you can only target siblings (with + or ~) or child elements.
I moved #nav-menu2 so it's directly follows the relevant a tag. Then you can use #navigation-item2:hover + #nav-menu2 to increase the opacity to 1.
By positioning every single sub-menu in the same order in the DOM, you can even go a step further, which I show-case in the code below, using only the classes.
For the sake of demonstration, I changed the #nav-menu2 to a starting opacity of 0.1. I also positioned #nav-menu2 to align at the bottom of .navigation (hence, adding position: relative to .navigation), and then translated it's full height (100%) to have the #nav-menu2 position itself under the navbar.
I would even go one step further and place #nav-menu2 inside #navigation-item2, so you automatically get the correct x-position. It would also solve the issue that you look the hover state if you got outside the .navigation-item. But I leave that up to you to figure out.
.navigation {
position: relative; /* ADDED */
width: 100%;
height: auto;
max-height: 20vh;
background: white;
display: flex;
;
align-items: center;
justify-content: center;
}
.navigation-item {
height: 10%;
width: 15%;
font-size: 1.2em;
color: black;
font-weight: bold;
padding: 0.5em 1em 0.5em 1em;
margin: 0px 1% 0px 1%;
text-align: center;
text-transform: uppercase;
transition: background 200ms linear;
text-decoration: none;
}
.navigation-item:hover {
background: lightgrey;
}
.nav-menu {
height: 200px;
bottom: 0px; /* ADDED */
transform: translateY(100%); /* ADDED */
position: absolute;
width: 100%;
max-height: 25vh;
opacity: 0.1; /* CHANGED */
}
.navigation-item:hover + .nav-menu { /* CHANGED */
opacity: 1;
}
<header>
<nav class="navigation">
<a href="https://www.amazon.com" style="flex: 0.5; margin-left: 20px">
<img src="/images/Logo.png" alt="Logo" style="height:100%; width:auto; max-height: 16vh; margin-top: 3px;">
</a>
<a class="navigation-item" href="https://www.amazon.com">
Home</a>
<a class="navigation-item" href="https://www.amazon.com">
Portfolio</a>
<a class="navigation-item" id="navigation-item2" href="https://www.amazon.com">
Produkte</a>
<div class="nav-menu" id="nav-menu2">
<ul class="dropdown">
<ul>
<li>Kochmesser</li>
<li>Küchenmesser</li>
</ul>
<li>Scheren</li>
<li>Klingen</li>
</ul>
</div>
<a class="navigation-item" href="https://www.amazon.com">
Über uns</a>
</nav>
</header>
This is my html code below:-
.header {
background-color: #b6b4b4;
padding: 5px;
}
.logo {
border-radius: 30px;
float: left;
}
#social {
width: 50px;
border-radius: 100%;
float: right;
}
.navigatbar {
margin-top: -16px;
}
#navigat {
display: inline;
color: #b6b4b4;
font-size: 21px;
font-family: 'Poppins', sans-serif;
margin: 0 10px;
padding: 0 3px 0 3px;
}
.topnav {
background: #ffffff;
}
a {
color: #2ad2c9;
}
.active {
background-color: #e8e8e8;
}
.droplinks {
position: absolute;
background-color: #ffffff;
min-width: 140px;
display: none;
}
.droplinks a {
padding: 10px;
display: block;
}
.dropbutton:hover .droplinks {
display: block;
}
<body link="#008080" vlink="#66b2b2">
<div class="header">
<img src="images/logo.jpg" class="logo">
<img src="slike/yt.png" id="social">
<img src="slike/ig.png" id="social">
<img src="slike/fb.png" id="social">
</div>
<div class="navigatbar">
<ul class="topnav">
<li class="active" id="navigat">Početna</li>
<li class="dropbutton" id="navigat">Fitnes
<div class="droplinks">
Treninzi
Dijagnostika
</div>
</li>
<li id="navigat">Školica sporta</li>
<li id="navigat">Boks</li>
<li id="navigat">Personalni treninzi</li>
<li id="navigat">Ishrana i zdravlje</li>
<li class="dropbutton" id="navigat">Prevencija i rehabilitacija
<div class="droplinks">
Prevencija
Rehabilitacija
Kiropraktika
Kinezitejping
</div>
</li>
<li id="navigat">Kontakt</li>
</ul>
</div>
</body>
Now, here is the problem: my dropdown menus work, but both of them open on the left side of the navigation bar, under the first element. Where have I gone wrong?
I've tried to add some margin-left and it works, butthe problem is that it moves both of them for the same amount of pixels, and they're still opening on the same place. I could give different classes to them and add them a different margin-left, but I'm kind of sure that that is not the only possible solution.
Can anyone help me, please?
Change your CSS in 2 places:
#navigat {
display: inline;
color: #b6b4b4;
font-size: 21px;
font-family: 'Poppins', sans-serif;
margin: 0 10px;
padding: 0 3px 0 3px;
position: relative; /* add this line */
}
.droplinks {
position: absolute;
background-color: #ffffff;
min-width: 140px;
display: none;
left: 0; /* add this line */
z-index: 1; /* add this line */
}
Add this to your css
.dropbutton { position: relative; }
Absolutely positioned elements are positioned relative to their first ancestor that have a non-static position. position: static is the default for all elements, unless otherwise supplied.
That means in your case, the drop down menus are both positioned relative to the body of the document. What you want, is to position them relative to the button that triggers them. The above suggestion should take care of that.
Also, use left, top, right and bottom instead of margin to position your dropdowns.
This is my image-button for my website. http://puu.sh/cK7Sf/6309c39cdb.jpg When I re-size my browser it goes over here http://puu.sh/cK7VU/f17dafcc41.jpg
Here is my code
HTML
<div class="Nav">
<div id="buttons">
<div id="home_button"></div>
CSS
#home_button {
background-image: url("home.png");
background-repeat:no-repeat;
background-size: 100%;
width: 150px;
height: 60px;
position: absolute;
top: 196px;
left: 502px;
z-index: 10;
}
Keep in mind i am new to css and html, please dont hate
You should not use absolute position for this. Change it to position: relative; or position: static; The absolute positioning is causing the button to shift by the coordinates (top: 196px; left: 502px;) from the edge of the browser window.
I suggest researching the float property as well, because it's very useful in positioning things so that they flow nicely, especially for navigation like this.
You really should try to use lists for navigation. Here I made a quick-hand example. Further, you could easily recreate your button with CSS. So no need to use background-image
.nav-container {
background: #CCC;
width: 80%;
position: relative;
margin-left: auto;
margin-right: auto;
}
.nav {
padding: 10px;
}
.nav-item {
display: inline-block;
padding: 10px;
background: linear-gradient(#68B6E7,#3349D3);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#68B6E7,endColorstr=#3349D3);
-ms-filter:"progid:DXImageTransform.Microsoft.gradient(startColorstr=#68B6E7,endColorstr=#3349D3)";
font-family: Verdana, sans-serif;
border: 2px solid #000;
border-radius: 4px;
}
.nav-item a {
text-decoration: none;
color: #000;
}
<div class="nav-container">
<ul class="nav">
<li class="nav-item">Item 1
</li>
<li class="nav-item">Item 1
</li>
<li class="nav-item">Item 1
</li>
<li class="nav-item">Item 1
</li>
</ul>
</div>
I'm trying to fit a few navigation tabs onto an <article> tab but it doesn't work properly on firefox and when on different screens, it messes up. Does anyone know an alternative that works with all browsers and doesn't change as the screen is resized?
Here is the result I want:
Exactly that except I don't want to use a fixed margin-top
EDIT:
Code:
Navigation:
nav {
width: 100%;
height: 4%;
}
nav ul {
list-style-type: none;
}
nav li {
width: 100%;
background-color: #9A9489;
display: inline-block;
text-align: center;
width: 11%;
font: 1.4em/1.3em Bonveno, Open Sans, Sans-Serif;
float: right;
cursor: pointer;
margin-right: 0.5%;
border-top-left-radius: 0.3125em;
border-top-right-radius: 0.3125em;
transition: background-color 0.2s ease-in-out;
}
Article (without margin-top):
article {
width: 99.5%;
margin-left: auto;
margin-right: auto;
box-shadow: 0 0.3125em 0 #9A9489;
padding: 0 0 0.5% 1.5%;
}
HTML:
<nav>
<ul>
<a href="contact.php">
<li>Contact</li>
</a>
<a href="pictures.php">
<li>Pictures</li>
</a>
<a href="about.php">
<li class="selected">About</li>
</a>
<a link="black" href="index.php">
<li>Home</li>
</a>
</ul>
</nav>
<article>
<h1>Hello World</h1>
<p>Hello world</p>
</article>
Well you can always try position as an alternative to margin.
In this case, I think you would have to use a relative position, so do the following:
.YOUR_TABS_SELECTOR{
position: relative;
top: 10px;
}