Fixed navigation bar extends too far on large monitors - html

I've created a website with a fixed horizontal navigation bar that is to the right of the screen, with the logo on the left.
Everything with it works fine until the monitor extends over 1240px wide. At that point the navbar extends too far to the right in comparison to the rest of the content (that has a max-width of 1024px).
Now, I know exactly why it does this, I've coded it to do that by using right: 0, however I still don't want it to extend past the 1024px that I've set the rest of the content to and I have no idea how to resolve the issue.
I've tried setting a max-width of the div-element and it does nothing, I've tried creating other elements with max-width: 1024px and position: relative (as I was suggested this option), but with no luck.
The only long way around the issue I can find is to have a media query for each resolution and using margins instead of right: 0 although this seems like an unnecessarily long and complicated solution.
I'm also not a developer by any means, this is my first time coding ever, so my knowledge is very limited.
main,
header {
max-width: 1024px;
margin: auto;
}
#media screen and (min-width: 780px) {
.navbar {
background-color: #ffffffde;
position: fixed;
padding: 3px;
max-width: 1024px;
z-index: 50;
right: 0;
margin-right: 2rem;
}
.navbar a {
float: left;
display: block;
color: #000000;
text-align: center;
padding: 8px;
text-decoration: none;
font-size: 17px;
top: 15px;
}
.navbar .icon {
display: none;
}
}
.dropdown {
float: left;
}
.dropdown .dropbtn {
font-size: 17px;
border: none;
outline: none;
color: black;
padding: 8px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9d3;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 50;
}
.dropdown-content a {
float: none;
color: black;
padding: 8px;
text-decoration: none;
display: block;
text-align: left;
}
.navbar a:hover,
.dropdown:hover .dropbtn {
background-color: rgba(224, 222, 222, 0.705);
}
.dropdown-content a:hover {
background-color: rgba(224, 222, 222, 0.705);
color: black;
}
.dropdown:hover .dropdown-content {
display: block;
}
<header>
<a href="/en/index.html">
<div class="logo">Logo</div>
</a>
<div class="navbar" id="myNavbar">
About
<div class="dropdown">
<button class="dropbtn">Services
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Services and Solutions
Training and Workshops
</div>
</div>
Partners
Contact
Svenska
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
</header>
(I've excluded the CSS for tablet and mobile since those work, as well as the JS since that isn't relevant for the issue and didn't want it to take up unnecessary space in the post)
If anyone has any possible solutions to my issue I'm down to try most things since I've already searched high and low for an answer to my question.

Fixed position elements are removed from the normal DOM flow. Therefore fixed position elements are always relative to the viewport and ignore everything else on the page.
However, since you know the <main> content width is 1024px, you can use calc() to adjust the right position of the fixed navbar...
The width of the whitespace to the left/right of the main content is: 100% (the page width) minus 1024px (the main content width) split in half (left & right sides). Therefore, this will give you the right edge of the main content.
right: calc((100% - 1024px)/2);
.navbar {
background-color: #ffffff;
position: fixed;
padding: 3px;
max-width: 1024px;
z-index: 50;
right: calc((100% - 1024px)/2);
margin-right: 2rem;
}
Codeply

Always try to use :
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
So that most of the issues of spanning area more than defined or overflowing the container can be removed .
See this for box-sizing
Here padding/margin = 0 remove default values of element which sometimes are reason of problem too .
Now still it is rendering outside than give the values according to screen size and try to remove some extra margin
* {
box-sizing: border-box;
padding: 0;
margin: 0;
}
main,
header {
max-width: 1024px;
margin: auto;
}
#media screen and (min-width: 780px) {
.navbar {
background-color: #ffffffde;
position: fixed;
padding: 3px;
max-width: 1024px;
z-index: 50;
right: 0;
margin-right: 2rem;
}
.navbar a {
float: left;
display: block;
color: #000000;
text-align: center;
padding: 8px;
text-decoration: none;
font-size: 17px;
top: 15px;
}
.navbar .icon {
display: none;
}
}
.dropdown {
float: left;
}
.dropdown .dropbtn {
font-size: 17px;
border: none;
outline: none;
color: black;
padding: 8px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9d3;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 50;
}
.dropdown-content a {
float: none;
color: black;
padding: 8px;
text-decoration: none;
display: block;
text-align: left;
}
.navbar a:hover,
.dropdown:hover .dropbtn {
background-color: rgba(224, 222, 222, 0.705);
}
.dropdown-content a:hover {
background-color: rgba(224, 222, 222, 0.705);
color: black;
}
.dropdown:hover .dropdown-content {
display: block;
}
<header>
<a href="/en/index.html">
<div class="logo">Logo</div>
</a>
<div class="navbar" id="myNavbar">
About
<div class="dropdown">
<button class="dropbtn">Services
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Services and Solutions
Training and Workshops
</div>
</div>
Partners
Contact
Svenska
<a href="javascript:void(0);" class="icon" onclick="myFunction()">
<i class="fa fa-bars"></i>
</a>
</div>
</header>

One thing you could do is place the elements with coordinates on an absolute level:
Of course you will put the values ​​based on the position
position: absolute;
top: 0;
left: 20px
div.absolute {
position: absolute;
top: 80px;
right: 0;
width: 200px;
height: 100px;
border: 3px solid #73AD21;
}

Related

How could I show my navigation bar over my body / section text?

My problem is when I scroll up, the text on the page goes through my navbar and it looks really unprofessional and I need to fix it. I want to make it so that my navigation bar is layered on top of my body / section text (body text includes navigation bar but section only includes text not on my nav bar),
I tried adding a background-color and that worked except when I used my dark / light mode switcher, I have css properties for dark mode and light mode but I can't use a background color otherwise when you switch, it will still show the background color for light mode (or whatever mode you were deigning for).
I also tried Z-Indexes since that is supposed to work but no, literally did nothing. Which was really weird. I wish I could elaborate more but that's all I can say, it just didn't work. Here is an example of what I did but I can't actually show you the exact code since I already deleted those Z-Indexes since they didn't work.
EXAMPLE NAV BAR CSS {
z-index: 100;
}
EXAMPLE SECTION / TEXT CSS {
z-index: -100;
}
Here's my navigation bar code:
<ul>
<div class="without-dark-ul">
<img class="logo" width="100px" src="\imgs\logo.png">
<h1 class="logo-side">[PRIVATE] Accounting</h1>
<h2 class="logo-side-slogan">The best, afforadable accounting.</h2>
<br>
<hr>
<h3 class="navbar-text">Navigation</h3>
<div class="navbar">
<li><a class="main-nav" href="#">Home</a></li>
<li>Pricing</li>
<li>Contact</li>
</div>
<div class="vl"></div>
<h3 class="dark-mode-text">Dark / Light Mode</h3>
</div>
<div class="ul-dark">
<li class="li-dark">
<span>Dark</span>
<span>Light</span>
</li>
</div>
</ul>
And here's my CSS for my navbar (and I've got some in navbar CSS and some in dark mode CSS, I honestly don't know why I did it like that though.)
/* START NAVBAR */
.logo-side {
margin-left: 140px;
margin-top: -95px;
font-size: 40px
font-
}
.logo-side-slogan {
margin-left: 140px;
margin-top: -30px;
}
.logo {
margin-top: 20px;
margin-left: 20px;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
position: fixed;
/* position: -webkit-sticky; Safari */
/* position: sticky; */
top: 0;
width: 100%;
z-index: 100;
}
ul > hr {
margin-left: 10px;
margin-right: 10px;
margin-top: -10px;
}
li {
float: left;
}
li a {
display: block;
text-align: center;
padding: 14px 16px;
text-decoration: none;
color: inherit;
}
li a:hover {
background-color: rgb(105, 103, 103);
}
.main-nav {
background-color: #383838;
color: white;
}
.navbar {
margin-left: 20px;
margin-top: 30px;
padding-bottom: 70px;
}
.navbar-text {
margin-left: 20px;
margin-bottom: -20px;
}
.navigation-bar {
background-color: #262626;
position: absolute;
}
/* END NAVBAR */
/* START DARK MODE */ (This includes some nav bar CSS properties.)
.li-dark {
list-style: none;
width: 100%;
height: 60px;
text-align: center;
text-transform: uppercase;
transition: 0.5s;
}
.ul-dark.active li {
transform: translateY(-30px);
}
ul li span {
display: block;
height: 30px;
line-height: 30px;
width: 100%;
}
ul li span:nth-child(1) {
background: #262626;
color: #fff;
}
.ul-dark {
position: static;
top: 20px;
right: 20px;
margin-top: 25px;
margin-left: 320px;
padding: 0;
width: 100px;
height: 30px;
border: 1px solid #000;
cursor: pointer;
overflow: hidden;
}
.dark-mode-text {
margin-left: 320px;
margin-top: -113px;
}
/* END DARK MODE */
I don't know if that is all you need but please ask me to send more code if you need more!
Thanks for your help.
Solninja A
give a position relative to body and your text parent
and z-index:10000;

How to halve size of sticky navbar so that is only covers half the screen, without resizing navbar img

I am trying to halve the size of my sticky navbar, and have it appear overlayed rather than above of my header image. When I attempt to rescale the container/wrapper divs it seems to adjust the size of the image which I have as the navbar. I also would like to have the dropdown menu appear wherever you are located on the page, rather than just at the top.
I have tried using 'position: absolute' in my css on all of the navbar bits. This seems to work in overlaying the navbar, however it does not appear at the top of the page and the dropdown menu is not accessable.
#wrapper {
width: 100%;
}
#demoncontrast {
filter:contrast(100%) brightness(180%);
}
.navbar {
overflow: hidden;
background-color: #333;
z-index: 9999;
}
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 0px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover, .dropdown:hover .dropbtn {
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 657px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
.content {
padding: 16px;
}
.sticky {
position: fixed;
top: 0;
width: 100%
}
.sticky + .content {
padding-top: 102px;
}
<div class="container">
<div id="wrapper">
<div class="navbar" id="myHeader">
<div class="dropdown">
<button class="dropbtn">
<img src="NONENTITYHEADERNB.png" id="opacity" align="left" style="max-width: 50%;height:auto;">
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Bio and album stream
Where to buy
Upcoming shows and links
</div>
</div>
</div>
</div>
</div>
I also would like to have the dropdown menu appear wherever you are located on the page, rather than just at the top.
You have to use the position property with fixed value: position: fixed. This will make your element (in this case is navbar) sticks on window and always moves along with viewers.
And to ensure your element are not overlaid, you may want to add z-index property to a certain level. For example: z-index: 999.
When I attempt to rescale the container/wrapper divs it seems to adjust the size of the image which I have as the navbar
The reason lies in here:
<img src="NONENTITYHEADERNB.png" id="opacity" style="max-width: 50%;height:auto;">
Your max-width:50% means this image only allow to have 50% size compare to the parent width, both container and wrapper are its parent.
If I understand you correctly, you want to make the image auto-rescaled when the container change size right? If so, then I afraid you have to put more effort with JavaScript rather then CSS.

Pure CSS Sticky Horizontal Subnav - dropdown not displaying

I am trying to create a horizontal subnavigation bar in CSS (without an unordered list), but I can't get the dropdown menu to appear.
Here's my code in HTML:
<div class="navbar sticky">
Home
<div class="subnav">
<button class="subnavbtn">Learn <i class="fa fa-caret-down"></i></button>
<div class="subnav-content">
Print
Review
Examples
More Info
</div>
</div>
<div class="subnav">
<button class="subnavbtn">Game <i class="fa fa-caret-down"></i></button>
<div class="subnav-content">
Play Now!
How to Play
Cards
</div>
</div>
Minigames
</div>
Here's my code in CSS:
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.navbar {
overflow: hidden;
background-color: green;
position: sticky;
top: 0;
}
.navbar a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.subnav {
float: left;
overflow: hidden;
}
.subnav .subnavbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover,
.subnav:hover .subnavbtn {
background-color: chartreuse;
color: black;
}
.subnav-content {
display: none;
position: absolute;
left: 0;
background-color: red;
width: 100%;
z-index: 1;
visibility: hidden;
}
.subnav-content a {
float: left;
color: white;
text-decoration: none;
display: inline;
}
.subnav-content a:hover {
background-color: #eee;
color: black;
}
.subnav:hover .subnav-content {
visibility: visible;
display: block;
}
I've tried changing the opacity or even using visibility, but it just won't work for me. Sometimes the drop down will appear, however the top nav bar will transform (the "Game" link will shift right, starting at the point where "More Info" ends even though they are on different bars).
Most solutions I've seen while searching this issue is that they are not using (display: block;), but I have been and I don't know what to do at this point.
Here's fiddle
Remove overflow:hidden; from your .navbar declaration and replace it with float:left; and width:100%;
Floated elements are removed from the calculated height of the parent element. However, overflow:hidden; invokes the height to be calculated via block formatting context but, was hiding your dropdowns cause overflow is hidden.
Also, floating the parent element means the children dictate the parent's height making it more dynamic.
Revised Fiddle Here
Just remove the position property from the div with class name navbar.
.navbar {
overflow: hidden;
background-color: green;
top: 0;
}
Dropdown menu appear out of the navbar.
So, you should replace overflow: hidden with height: 50px in .navbar:
.navbar {
height: 50px;
background-color: green;
position: sticky;
top: 0;
}

Dropdown menu is not aligned properly

I am having some trouble with my dropdown menu bar and the dropdown is not aligned properly as well.
Modified code on JSFiddle
HTML
<div class="container">
Home
<div class="dropdown">
<button class="dropbtn">Recipes</button>
<div class="dropdown-content">
Choc Chip Cookie
Choc Brownie
Choc Pretzels
</div>
</div>
Gallery
Contact
</div>
CSS
.container {
overflow: hidden;
background-color: #3399CC;
border-style: solid;
border-color: black;
border-width: medium;
width: 50%;
margin: auto;
text-align: center;
font-family: 'Lobster',cursive;
}
.container a {
float: left;
font-size: 22px;
color: black;
text-align: center;
padding: 10px 14px;
text-decoration: none;
align-content: center;
width: 21%; /* Modified by me to change the width of Home, Gallery and Contact element */
display: block;
}
.dropdown {
float: left;
overflow: hidden;
width: 24%; /* Modified by me to change the width of Recipe element */
}
.dropdown .dropbtn {
font-size: 22px;
border: none;
outline: none;
color: black;
padding: 10px 14px;
background-color: inherit;
font-family: 'Lobster',cursive;
}
.container a:hover, .dropdown:hover { /* Modified by me. Change the background color of dropdown element instead of the button */
background-color: #52C3EC;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #3399CC;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
width: 250px;
}
.dropdown-content a {
float: none;
color: black;
text-align: left;
padding: 10px 14px;
padding-left: 40px; /* Modified by me to align the list item in the dropdown menu */
text-decoration: none;
display: block;
width: 78%; /* Modified by me to change the width of the list item in the dropdown menu */
}
.dropdown-content a:hover {
background-color: #52C3EC;
}
.dropdown:hover .dropdown-content {
display: block;
}
I'm not changing your page's structure, only the CSS code used in them. I've marked which part of the code that I modified along with the explataion.
You can still see a little bit of space and the end of Contact element. That's because scaling the size in percentage is hard. You can fix that by changing the unit to pixel. I'll leave the decision to you.
Also the result is best viewed in your browser not in JSFiddle due to size constraint.

Needing help structuring a navbar on html and css

I'm new to HTML and CSS.
I'm trying to do a website, and I'm starting by the navbar, but this navbar is not "scalable" for every screen side, when it is on full screen fine but when I minimize it it does not load the part on the right side wich is "About". All of the menus are pointing to the same page and for now that's the objective.
Here's the Code:
body {}
.navbardiv {}
.navbar_ul {
list-style-type: none;
margin: 0;
padding: 0;
/*overflow:hidden;*/
background-color: #333;
border: 5px solid gray;
margin: -8px;
width: auto;
min-width: 600px;
height: 70px;
}
li {
float: left;
padding: 10px 150px;
}
li a {
display: block;
color: white;
text-align: center;
padding: 16px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
<!--NAVBAR-->
<div class="navbardiv">
<ul class="navbar_ul">
<li class="navbar_li_Contact">Contact</li>
<li class="navbar_li_WebHosting">Webhosting</li>
<li class="navbar_li_About">About</li>
</ul>
</div>
You are setting the padding for li to 150px which is very high, you need to reduce.
But if you want the links to take the whole width and to be evenlly spaced, then you can use flex box and justify-content: space-between;
see code snippet:
body {}
.navbardiv {}
.navbar_ul {
list-style-type: none;
margin: 0;
padding: 0;
/*overflow:hidden;*/
background-color: #333;
border: 5px solid gray;
margin: -8px;
width: auto;
min-width: 600px;
height: 70px;
display: flex;
justify-content: space-between;
}
li {
float: left;
padding: 10px 20px;
}
li a {
display: block;
color: white;
text-align: center;
padding: 16px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
<!--NAVBAR-->
<div class="navbardiv">
<ul class="navbar_ul">
<li class="navbar_li_Contact">Contact</li>
<li class="navbar_li_WebHosting">Webhosting</li>
<li class="navbar_li_About">About</li>
</ul>
</div>
Your navbar is not responsive because of min-width: 600px; part. What this one will do is that when screen resolution is below 600px in width it will keep your navbar at 600 px. Thus it will align it to leftmost part of the screen which will leave you a cropped right edge.min-width:100%; wont work either since it will start to crop when inner elements of the navbar will not fit.
It is easy to fix this. Just change it to width:100%;.