Fix a cascading css menu to the top - html

I've been trying to get a menu to stick to the top of the page no matter where you scroll, but I am unable to do so. Here is my code:
body#beta a#beta,
body#zte a#zte,
body#honor a#honor,
body#samsung a#samsung,
body#market a#market,
body#beta a#beta,
active {
background-color: #0D47A1;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
}
active {
background-color: #0D47A1;
}
.active li {
background: #fff;
}
ul a,.dropbtn {
display: inline-block;
color: #fff;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
ul.dropdown {
display: inline-block;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: fixed;
top: 40px;
background-color: #fff;
min-width: 120px;
box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2);
}
.dropdown-content a {
color: #000;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1;
}
.dropdown:hover .dropdown-content {
display: block;
}
<ul>
<ul class="dropdown">
Progam 1
<div class="dropdown-content">
<ul class="active" id=menu position=fixed>
<li><a href="" style="color:black; " id=home>Home Page</a></li>
<li><a href="" style="color:black; " id=zte>ZTE</a></li>
<li><a href="" style="color:black; " id=honor>Honor</a></li>
<li><a href="" style="color:black; " id=samsung>Samsung</a></li>
<li><a href="" style="color:black; " id=market>Newsletter</a></li>
</div>
</ul>
<ul class="dropdown">
<li class="active">Progam 2 BETA</li>
<div class="dropdown-content">
<ul class="active" id=menu position=fixed>
<li><a href="" style="color:black; " id=beta> Progam 2 Home Page</a></li>
</div>
</ul>
</ul>
I've tried doing
position:fixed;
and
top:0px;
but it will only become a jumbled mess.
Can anyone help me figure out how to get the entire menu to be fixed at the top?

position:fixed;
top:0;
is all you need.
This gets the element outside the normal flow of content and places it at the top of the viewport no matter where you scroll in the content (by default the viewport is the browser window - but it can be a parent iframe or any parent element that has a few select 3d transforms).
You have to make sure the body element doesn't have any margins (body{margin:0;} will take care of that).
Your problem is that you do not have a normal flow of content (at least in this example).
Another rather important thing to add is that IoS devices ignore position:fixed; and replace it with position:absolute;. Apparently, they consider position:fixed; a liability to online security (citation needed). It's a bad design decision, but it's how things are. Anyway, in order for your element to remain "fixed" on IoS, you need to make sure it doesn't have a parent with a set position (other than static), which kind of beats the whole purpose of using position:fixed in the first place.
Yeah, I know. Talk to the apple.
Note: in some newer versions of IoS, the position:fixed; property is only ignored in special states, such as when the keyboard is opened.

Related

CSS filter property disabling fixed position on navigation bar [duplicate]

This question already has an answer here:
Why does clip-path (and other properties) affect the stacking order (z-index) of elements later in DOM?
(1 answer)
Closed 2 years ago.
I made my navigation bar and positioned it (fixed), and it works fine. I was able to scroll down and all. As soon has I added filter (brightness) to it the image on my page, the navigation bar disappeared. I have tried using pseudo-elements and setting the position (absolute/relative), I set the filter property to the container of the child element of the image, it still didn't work. Can someone help me on how to have my navigation bar display on fixed position and still have the image filtered. Thanks in Advance.
nav {
position: fixed;
background-color: #fff;
}
nav ul {
margin: 0;
padding: 0;
}
.navbar-brand {
padding-right: 20px;
}
nav li {
display: inline-block;
padding: 10px;
}
nav a {
color: #000;
text-decoration: none;
}
nav a:hover {
color: #ff6600;
text-decoration: none;
}
.title-image img {
width: 100%;
height: 300px;
filter: brightness(60%);
}
<nav>
<ul>
<li>
<a class="navbar-brand" href="#">Navbar Brand</a>
</li>
<li>
Home
</li>
<li>
About
</li>
<li>
services
</li>
</ul>
</nav>
<div class="title-image">
<img src="https://images.unsplash.com/photo-1599546824091-f49550ce8cbc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60">
</div>
JSfiddle Demo
Just add z-index to your nav element as follow
nav{
position: fixed;
background-color: #fff;
z-index:999;
}
nav ul{
margin: 0;
padding: 0;
}
.navbar-brand{
padding-right: 20px;
}
nav li{
display: inline-block;
padding: 10px;
}
nav a{
color: #000;
text-decoration: none;
}
nav a:hover{
color: #ff6600;
text-decoration: none;
}
.title-image img{
width: 100%;
height: 300px;
filter: brightness(60%);
}
<nav>
<ul>
<li>
<a class="navbar-brand" href="#">Navbar Brand</a>
</li>
<li>
Home
</li>
<li>
About
</li>
<li>
services
</li>
</ul>
</nav>
<div class="title-image">
<img src="https://images.unsplash.com/photo-1599546824091-f49550ce8cbc?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=500&q=60">
</div>
The navbar didn't disappear, it is just beneath the image. To have it in front, you should use z-index: 10; (or any value greater than 0).
See more at : https://developer.mozilla.org/en-US/docs/Web/CSS/z-index
In addition, keep in mind that your image - or any element after your navbar - will be positioned on top of your page. May be you'll want to let the equivalent of the navbar height as space before any content.

Gaps at the top and side of navigation bar buttons

I'm creating a website and have run into a minor but incredibly infuriating problem.
I'm creating a Navigation bar and the buttons of the navigation bar have a gap between the far left, and top of the screen. In other words, all buttons have a gap from the top and the leftmost button has a gap from the left of the screen.
The relevant parts of my Stylesheet are shown below.
body {
margin: 0px;
padding: 0px;
font-family: Arial;
}
.navbar_base {
width: 100%;
overflow: hidden;
background-color: rgb(0, 21, 144);
top: 0px;
color: white;
display: block;
}
.navbar_button {
float: left;
padding: 10px;
margin: 0px;
border-radius: 3px;
list-style-type: none;
}
.navbar_button:hover {
background-color: rgb(50, 64, 147);
}
I suspect it's because I have assigned the class to the list rather then the links, but I'm using MVC5 and adding classes to the Html.Actionlink() is cumbersome. I'd like to avoid this solution if possible.
Furthermore I've tried assigning margin: 0px and padding: 0px on ul like so:
ul .navbar_button {
margin: 0px;
padding: 0px;
}
to no avail
Also I am not interested in using Bootstrap at the moment because this is a sort of side project for a specific person and I would like to create something unique for him.
Here is the HTML portion of my code:
<div class= "navbar_base">
<ul>
<li class="navbar_button"> #Html.ActionLink("MainPage","MainPage") </li>
<li class="navbar_button"> #Html.ActionLink("About","AboutPage") </li>
</ul>
</div>
I've looked at the following questions as well, but the answers didn't help me.
Gap at top of page despite margin:0 and padding: 0
Why does this div have gaps at top and bottom
How do I get rid of the two gaps?
Note: I do realize I am still a beginner and MVC may be out of my scope but I have job to do soon and I reckoned the fastest way to learn about how to do MVC is to actually do MVC
<div class= "navbar_base">
<ul>
<li class="navbar_button"> #Html.ActionLink("MainPage","MainPage") </li>
<li class="navbar_button"> #Html.ActionLink("About","AboutPage") </li>
</ul>
By default ul has some margin left properties , you can remove it by
.navbar_base > ul{ margin-left:0px; }
You need to remove the margin and padding from the ul too (your attempt to do so removed it from the li)
body, ul {
margin: 0px;
padding: 0px;
font-family: Arial;
}
.navbar_base {
width: 100%;
overflow: hidden;
background-color: rgb(0, 21, 144);
top: 0px;
color: white;
display: block;
}
.navbar_button {
float: left;
padding: 10px;
margin: 0px;
border-radius: 3px;
list-style-type: none;
}
.navbar_button:hover {
background-color: rgb(50, 64, 147);
}
<div class="navbar_base">
<ul>
<li class="navbar_button"> #Html.ActionLink("MainPage","MainPage") </li>
<li class="navbar_button"> #Html.ActionLink("About","AboutPage") </li>
</ul>
</div>

Dropdown menu in HTML/CSS doesn't actually "drop down"

I'm trying to add a drop-down menu for one of the options in my nav menu for a simple html page. However, when I hover over the nav menu option, the menu doesn't actually drop down. It just replaces the nav menu option with the first option in the drop-down whenever I hover over it. I'm not exactly sure why it isn't "dropping down".
Any help would be really appreciated... Here's the HTML for the nav and attempted drop-down.
<nav>
<ul>
<li>Eiffel Tower</li>
<li>Fashion</li>
<li>Food</li>
<li>Museums</li>
<div class="dropDiv">
<li class="dropdown">History</li>
<div class="dropdownContent">
<a href=leaders.shtml>Leaders of Paris</a>
<a href=future.shtml>Future of Paris</a>
</div>
</div>
<li>Language</li>
<li>Works Cited</li>
</ul>
</nav>
and here is the CSS snippet for the Dropdown menu:
.dropdown {
float: left;
background-color: #FFF0F5;
width: 100%;
}
.dropDiv {
position: relative;
display: inline-block;
width: 100%;
}
.dropdownContent {
display: none;
position: absolute;
background-color: #FFF0F5;
height: 200px;
width: 100%;
z-index: 1;
}
.dropdownContent a {
display: block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdownContent a:hover {background-color: #fff8dc;}
.dropDiv:hover .dropdownContent {
display: block;
z-index: 1;
height: 200px;
}
.dropDiv:hover .dropdown {
background-color: #fff8dc;
}
I'm not really sure why the drop-down part isn't displaying, i'm sure it's some stupid mistake but it's eluded me for an hour and a half...
I see you have mentioned position: absolute in dropdownContent class. This is causing to overlap. Just remove it and try. By default it sets to static, which mean Elements render in order, as they appear in the document flow. Where as absolute means element is positioned relative to its first positioned ancestor element.
The problem is in your HTML.
For the dropdown within an item of the 1st level you'll need a code block that looks like your 1st level. That is, another <ul> with a group of <li>s one for each 2nd level option.
You have a lot of unwanted css and markup. Just fix it. I have created a basic one for you. May be you can try,
.dropdownContent {
display: none;
background-color: #FFF0F5;
}
.dropdownContent a:hover {
background-color: #fff8dc;
}
.dropdownContent a{
display: block;
}
.dropdown:hover .dropdownContent {
display: block;
z-index: 1;
}
<nav>
<ul>
<li>Eiffel Tower</li>
<li>Fashion</li>
<li>Food</li>
<li>Museums</li>
<li class="dropdown">
History
<div class="dropdownContent">
<a href=leaders.shtml>Leaders of Paris</a>
<a href=future.shtml>Future of Paris</a>
</div>
</li>
<li>Language</li>
<li>Works Cited</li>
</ul>
</nav>

navbar change color on click html

I'm running into issues on my first ever website. I have successfully created a navigation bar at the top which looks and acts somewhat how I want it to (other than the color scheme but that can come later). The issue is that whenever I click a different link on the bar I want that box to change color, but it is currently stuck highlighted on the homepage. I think this is something super simple but i cannot find it. Thank you for the help.
body
{
font-family:sans-serif;
width: 100%;
margin: 0px;
}
/* upper strip holding the tabs*/
ul
{
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
position:fixed;
top: 0px;
width: 100%;
background-color: #328CC1
}
li
{
float:left;
border-right:3px solid #30FFE3;
}
li a
{
display: block;
color: whitesmoke;
text-decoration: none;
padding: 14px 16px;
text-align: center;
}
li a:hover:not(.active)
{
background-color: #111;
}
a.active
{
background-color: #EAB126
}
<ul>
<li><a class="active" href="#home">Home</a></li>
<li>Academics</li>
<li>Athletics</li>
<li>Contact</li>
</ul>
Change this:
a.active
background-color: #EAB126
}
to this:
li a:focus {
background-color: #EAB126
}
Here is the JSFiddle demo
This does what you ask BUT if this is a navigation bar then bear in mind that the control will lose focus as soon as you change page. It would be better if you use Javascript/JQuery to handle that much more easily.
A typical way of handling this is to have each page have a class that includes the page name: <div class="academics"> for example.
Now modify your header (within the page div) as follows:
<ul>
<li><a class="for_home" href="#home">Home</a></li>
<li><a class="for_academics" href="#academics">Academics</a></li>
<li><a class="for_athletics" href="#athletics">Athletics</a></li>
<li><a class="forcontact" href="#contact">Contact</a></li>
</ul>
this would be followed by the css as follows:
.home .for_home, .academics .for_academics, .athletics .for_athletics, .contact .for_contact {
background-color: #EAB126;
}
Then the menu item for the current page will be highlighted.
Congratulations on your first website!

CSS - How to overlap elements over everything but each other? (No margin/padding etc)

I am making a navigation bar and I would like it to have pure CSS dropdown menus, the problem with this is that in order for them to not overlap the rest of the page you have to set them to position: absolute, however this causes all the links to occupy the same space. Giving them individual margins will space them out but as they occupy the same virtual space only the last link is actually clickable.
HTML:
<div id="navbar">
<ul>
Home
About Me
<div id="programs">
Programs
<li><a class="navbutton" style="margin-top:0px;">Test</a></li>
<li><a class="navbutton" style="margin-top:62px;">Test 2</a></li>
<li><a class="navbutton" style="margin-top:124px;">Test 3</a></li>
<li><a class="navbutton" style="margin-top:186px;">Test 4</a></li>
</div>
</ul>
</div>
CSS:
#navbar
{
border-bottom: 1px solid #00A2E8;
}
.navbutton
{
color: #BBBBBB;
text-align: center;
font-size: 18px;
display: inline-block;
width: 120px;
height: 45px;
padding-top: 17px;
transition: all 0.3s;
position: relative;
}
#programs
{
display: inline-block;
}
#programs > li
{
display: none;
list-style-type: none;
}
#programs:hover > li
{
display: block;
position: absolute;
}
Is there any way to make them overlap everything but each other, so that the links are clickable but not moving the whole page down? Preferably a solution without JQuery/Javascript please. Thanks!
ok i think i may have figured this problem out. all i did was add a z-index of 1 to the .navbutton element and it worked. all the other css i did not modify at all.
.navbutton
{
color:#BBBBBB;
text-align:center;
font-size:18px;
display:inline-block;
width:120px;
height:45px;
padding-top:17px;
transition:all 0.3s;
position:relative;
z-index: 1; /* this is the rule too add */
}
I hope that helps :-)
You are looking for z-index.
<div style="z-index: 1;">Top</div>
<div style="z-index: 0;">Middle</div>
<div style="z-index: -1;">Bottom</div>