Dropdown menu is not aligned properly - html

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.

Related

How can I create a topnav beside a sidenav I've already created?

For context, I'm practically making a reskin of Twitter. At least as of right now, that's what it is. I've created a sidenav already, and I want to add a topnav beside it. Something like this: image
I want to put "you - following - discover | profile" as the topnav.
I tried to go off of w3schools code for a topnav, but it didn't work. It just had a gray bar at the top with no buttons.
<div class="topnav">
<a class="active" href="#home">Home</a>
News
Contact
About
</div>
For context, I didn't add any buttons or change them originally but I don't think that would change anything. The sidenav is ontop of the topnav, and I want the topnav to be beside it.
My sidenav html:
<div class="sidenav">
<img src="./img/Aero Textual Icon Green.png" class="icon-sidenav" alt="Aero Icon Top Sidenav" style="cursor:default">
<span class="material-symbols-rounded inline-icon">home</span>Timeline
<span class="material-symbols-rounded inline-icon">notifications</span>Notifications
<span class="material-symbols-rounded inline-icon">near_me</span>Explore
<span class="material-symbols-rounded inline-icon">bookmark</span>Saved
<span class="material-symbols-rounded inline-icon">sms</span>Messages
<span class="material-symbols-rounded inline-icon">person</span>Profile
<span class="material-symbols-rounded inline-icon">settings</span>Settings
<span class="material-symbols-rounded inline-icon">more_horiz</span>More
<button>Post</button>
</div>
The pages css (All of its a sidenav and topnav right now):
.material-symbols-rounded {
font-variation-settings:
'FILL' 0,
'wght' 500,
'GRAD' -25,
'opsz' 48
}
.material-symbols-rounded-high {
font-variation-settings:
'FILL' 0,
'wght' 700,
'GRAD' 0,
'opsz' 48
}
body {
font-family: 'Coda', cursive;
background-color: #212121;
color: #fff;
}
.main {
margin-left: 160px;
font-size: 28px;
padding: 0px 10px;
}
.sidenav button {
font-family: 'Rajdhani', sans-serif;
font-weight: 600;
font-size: 23px;
margin-top: 25px;
margin-left: 163px;
border-radius: 50px;
width: 230px;
height: 55px;
background-color: #2EFFA8;
text-align: center;
color: #212121;
border: none;
text-decoration: none;
}
.sidenav button:hover {
background-color: #27d88e;
}
.icon-sidenav {
padding-top: 30px;
padding-bottom: 35px;
width: 170px;
display: block;
margin-left: auto;
margin-right: auto;
}
.inline-icon {
vertical-align: bottom;
font-size: 32px !important;
padding-right: 15px;
}
.sidenav {
height: 100%;
width: 550px;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #181818;
overflow-x: hidden;
padding-top: 20px;
}
.sidenav a.sidebarbtn {
padding-top: 15px;
padding-bottom: 15px;
padding-left: 200px;
text-decoration: none;
font-size: 22px;
color: #ffffff;
display: block;
}
.sidenav a:hover {
color: #2EFFA8;
cursor: pointer;
}
.sidenav a:active {
color: #2EFFA8;
}
.sidenav a.split {
float: left;
color: white;
}
.topnav {
background-color: #333;
overflow: hidden;
}
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #04AA6D;
color: white;
}
#media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}
This question doesn't work because the result is for topnav above sidenav, I want the topnav to the right of the sidenav
I can't find a question asking to put a topnav beside a sidenav, I've looked on here and around 5 other places.
you can change your top nav to display:flex, and justify its content to center, as follwing:
or you can make your layout more strict by using display grid and modifying the follwing (recommended):
.sidenav {
height: 100%;
width: 550px;
/* position: fixed; */
background-color: #181818;
overflow-x: hidden;
padding-top: 20px;
/*The following line says that this element spans for two rows*/
grid-row: 1/-1;
}
body{
display: grid;
padding: 0 15% 0 15%;
grid-template-columns: min-content max-content;
grid-template-rows: min-content max-content;
justify-content: left;
}
the result would be something like:
and then to make nav bars fixed in place, you can add another div after the top nav and make it scroll on overflow-y and you can hide its scrollbar if u want to.
finally i think grid would be the best choice to achieve your overall goal for the layout.
plz upvote if this helps.
Thanks

Fixed navigation bar extends too far on large monitors

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;
}

HTML + CSS hoverable dropdown expands header

There is an issue with dropdown, when hover it.
Instead of normal opening, the dropdown menu expands header.
Where is an error in the code?
I wrote this code from example on w3school()Code example, by which this code is written
There are also several attempts to do the same:
first attempt
second attempt
And in all of these attempts i do the same error, but cannot find where
exactly.
Can somebody show where are the errors?
* {
margin: 0;
padding: 0;
overflow-x: hidden;
}
header, nav {
background-color: aliceblue;
color: darkcyan;
font-family: Arial, Helvetica, sans-serif;
width: 100%;
height: auto;
text-align: center;
padding: 10px;
}
a {
text-decoration: none;
color: darkcyan;
padding: 10px;
display: inline-block;
}
.active {
padding-left: 0;
}
a, .dropdown {
display: inline-block;
width: 100px;
padding: 10px;
}
.dropdown .dropbutton {
border: none;
outline: none;
color: inherit;
background-color: inherit;
font-size: inherit;
}
.dropdown-content {
display: none;
position: absolute;
background-color: whitesmoke;
color: black;
width: auto;
}
.dropdown-content a {
float: none;
padding: 10px;
display: block;
text-align: center;
}
.dropdown:hover .dropdown-content {
display: block;
position: relative;
width: auto;
text-align: center;
}
<!DOCTYPE html>
<html>
<head>
<title>Dropdown Third Version</title>
</head>
<body>
<header>
<h1>The Homework</h1>
<nav>
Home
Hobbies
Third Page
<div class="dropdown">
<button class="dropbutton">Dropdown</button>
<div class="dropdown-content">
One
Two
Three
</div>
</div>
</nav>
</header>
</body>
</html>
Make a simple change to position in css:
.dropdown:hover .dropdown-content {
display: block;
position: absolute;----------------------This One
width: auto;
text-align: center;
}
Use this link to understand: Difference Between relative and absolute
This is because you have given position:relative in css instead give position:absolute for following selector .inline:hover .dropdown
.inline:hover .dropdown {
display: block;
position: absolute;
padding: 10px;
background-color: white;
color: dimgray;
}
And one more modification is that for all sub menu you have mentioned the class dropdown instead of that under one div which is having class dropdown
<div class="inline">The Page
<div class="dropdown">
<div>One</div>
<div>Two</div>
<div>Three</div>
</div>
</div>
here is working example : https://liveweave.com/NpUwOZ
.dropdown:hover .dropdown-content {
display: block;
position: absolute;
width: auto;
text-align: center;
}
just delete the position line or change position in to absolute
You forgot to link the CSS file in your HTML code. Make sure you do that first.
Othetwise it won't do anything.

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;
}

CSS - overflow: hidden to not cover dropdown

I have the following CSS and HTML:
body { background-color: #c0c0c0; }
.title-bar, { background-color: #999; color: white; float: left; overflow: hidden; }
.title-bar {
border-bottom: 1px solid white;
height: 128px;
width: 100%;
}
.logo, .user-info { box-sizing: content-box; height: 100%; width: 128px; }
.logo{
align-items: center;
background-color: #369;
border-right: 1px solid white;
display: flex;
float: left;
font-size: 2em;
font-kerning: none;
justify-content: center;
}
.user-info {
align-items: center;
border-left: 1px solid white;
display: flex;
float: right;
justify-content: space-between;
flex-flow: column nowrap;
}
.user-info .circle {
border: 2px solid #369;
border-radius: 50%;
display: inline-block;
flex: 0 0 auto;
height: 32px;
margin: 8px 8px;
overflow: hidden;
transition: border 0.15s ease-out;
width: 32px;
}
.user-info .circle:hover { border-width: 4px; }
.user-info .container {
border-top: 1px solid white;
display: flex;
justify-content: center;
margin-top: 6px;width: 100%;
}
.hor-nav { background-color: #404040; }
.option { display: inline-block; position: relative; }
.hor-nav .option:hover {background-color: #369; }
.option a {
color: white;
display: inline-block;
font-size: 1em;
padding: 14px;
text-align: center;
transition: background-color 0.15s ease-out;
}
.option .dropdown { display: none; position: absolute; }
.option:hover .dropdown{ display: block; }
.dropdown a {
display: block;
text-align: left;
width: 100%;
}
<div class="title-bar">
<a class="logo" href="#">
</a>
<div class="user-info">
<span>User name</span>
<div class="container">
</div>
</div>
<div class="hor-nav">
<div class="option">
OPTION 1
<div class="dropdown">
ITEM 1
</div>
</div>
</div>
</div>
as you can see, the hor-nav bar's color spills onto the user-info area.
I have researched this and found that if I set overflow-x: hidden; it will not do this (see this article).
I have tried that and it is true - the nav bar does not spill into the user-info but, when you hover over one of the nav bar options, the dropdown does not come down but instead the vert-nav gives you a scroll bar (see this jsfiddle).
Additionally, if you do overflow-y: hidden; there is no scroll bar at all.
I am trying to get it so that the background-color of the hor-nav does not spill into other div's, but also allows the dropdown to be activated and work
thank you.
The easiest way to to this with least code change is to just give the user-info area a background color. Since the hor-nav section is lower on the z-index this will give the visual affect you want although the bar will still be under the user-info section it won't appear to be and the drop down will funtion as it does now.
Per your inquiry, you could do this another way by using percentage based widths for all 3 elements so they don't overlap eachother. Please see this fiddle for code change (note I change the markup order slightly, widths, and added box sizing css property)
The way I see it, you have 3 options
You can try adding margin-left/right to the hor-nav.
.hor-nav {
margin: auto 128px;
}
Another option is to set a certain width to the .hor-nav. Or practically cut the width of it.
.hor-nav {
width: calc(100% - 128px);
}
And third, is to add a background color to the .user-info