hover changing id specific division - html

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>

Related

Nav bar slightly to the right [duplicate]

This question already has answers here:
Does UL have default margin or padding [duplicate]
(2 answers)
Closed 7 months ago.
For some reason, my nav bar is slightly set to the right. I've tried configuring everything to do with position but it doesn't seem to be working. I'm not sure if it's a CSS property or I just messed up with the configuration but it's a few pixels off-center no matter what. It might not be visible instantly (in the image) but I checked it with a virtual ruler and it is off.
header {
margin: 0 auto;
width: 100%;
height: 70px;
display: flex;
align-items: center;
justify-content: space-around;
background: transparent;
position: fixed;
top: 0;
transition: 0.3s;
z-index: 5;
}
.nav-show {
opacity: 0;
}
header:hover {
opacity: 1;
background: var(--main-header-background);
}
.nav-bar {
list-style: none;
position: relative;
display: inline-flex;
}
a.nav-link {
margin: 2px;
padding: 5px 10px;
text-decoration: none;
color: var(--main-fonts-color);
font-family: var(--main-font-family);
cursor: pointer;
text-transform: uppercase;
}
.active {
background: var(--main-decor-color);
}
.nav-link:hover {
color: #000000;
background: var(--main-decor-color);
}
<header>
<nav>
<ul class="nav-bar">
<div class="bg"></div>
<li>
<a class="nav-link" href=""></a>
</li>
<li>
<a class="nav-link" href=""></a>
</li>
<li><a class="nav-link" href="">Test</a></li>
<li><a class="nav-link" href="">Test</a></li>
</ul>
</nav>
</header>
Just as a note it's about 50px off based on what I see in photoshop.
Add these properties to your CSS
* {
margin: 0;
padding: 0;
}
Sometimes the browser will apply its default margin and padding to the elements, which happened in your case, where the header has an unusual left margin. Thus, we set margin and padding of every element to 0.
* {
margin: 0;
padding: 0;
}
header {
margin: 0 auto;
width: 100%;
height: 70px;
display: flex;
align-items: center;
justify-content: space-around;
background: transparent;
position: fixed;
top: 0;
transition: 0.3s;
z-index: 5;
}
.nav-show {
opacity: 0;
}
header:hover {
opacity: 1;
background: var(--main-header-background);
}
.nav-bar {
list-style: none;
position: relative;
display: inline-flex;
}
a.nav-link {
margin: 2px;
padding: 5px 10px;
text-decoration: none;
color: var(--main-fonts-color);
font-family: var(--main-font-family);
cursor: pointer;
text-transform: uppercase;
}
.active {
background: var(--main-decor-color);
}
.nav-link:hover {
color: #000000;
background: var(--main-decor-color);
}
<header>
<nav>
<ul class="nav-bar">
<div class="bg"></div>
<li>
<a class="nav-link" href="">Test</a>
</li>
<li>
<a class="nav-link" href="">Test</a>
</li>
<li><a class="nav-link" href="">Test</a></li>
<li><a class="nav-link" href="">Test</a></li>
</ul>
</nav>
</header>
nav {
display:block;
padding:10px;
margin:5px 55px 5px 55px;
}

What is the best way of sizing a header logo in css

I can't seem to find a great way of sizing my logo to fit within the header, I am trying to get a result with the logo on the left side of the header and the naviagtion on the right side. I've tried this and it seems to work but I don't know if there's a better way to approach this. If anyone could give some tips it would be greatly appreciated.
My CSS and HTML
a {
text-align: center;
color: #232323;
text-decoration: none;
transition: .1s;
}
button {
border: none;
outline: none;
color: #f3f3f3;
cursor: pointer;
transition: .3s;
}
header {
display: flex;
align-items: center;
justify-content: space-between;
position: fixed;
top: 0;
padding: 1em 2em;
max-height: 20%;
width: 100%;
z-index: 2;
background-color: #ffffff;
}
.logo-wrapper {
display: inline-block;
overflow: hidden;
}
.logo-wrapper img {
height: auto;
width: auto;
max-width: 150px;
}
nav {
display: inline-block;
}
.nav-item {
display: inline-block;
margin: 0 1.5em;
}
.get-started {
background-color: #f27649;
padding: 1em 2em;
border-radius: 3em;
}
.get-started:hover {
filter: brightness(85%);
}
<header>
<div class="logo-wrapper">
<a href="/">
<img src="images/devchat.png" alt="DevTalk Logo">
</a>
</div>
<nav>
<ul>
<li class="nav-item">Learning</li>
<li class="nav-item">About</li>
<li class="nav-item">Blog</li>
<li class="nav-item">Explore</li>
<li class="nav-item">Contact</li>
<li class="nav-item">Login</li>
<li class="nav-item"><button class="get-started" href="#">Get Started <i class="fas fa-angle-right"></i></button></li>
</ul>
</nav>
</header>

DROPDOWN MENU- changing where it opens

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.

Alternate for 'margin-top'? - CSS

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

CSS with Floated DIV Elements

I believe I have a very unique problem. I am trying to create a menu and basically I have some floated child div's inside the main menu holding div at the top of a web page. The problem is that I need to have the parent element have an automatic height because I want it to be dynamic just in-case I change the padding on the menu buttons (child DIV's). Also, the parent has a width of 100% and a child inside of it that has an automatic width with a max-width set so that I can basically have the left and right child menu buttons inside of it come together when the page is sized smaller. However all is working well until you resize the page to the point where the left right right menu portions come together, then all of the child menu buttons want to stack instead of automatically create a vertical scrollbar for the main page.
I don't know if this will pose a problem because I plan on using media queries later to automatically switch up the CSS for mobile compatibility. However, I would like to find a solution to this problem. If I need to post all of my code to get the right answer please let me know and I will do.
Thank you so much.
Oh and by the way, I have searched on a solution to this for about an hour and nothing is working. I may as well post the code below because I really want to find a solution.
The HTML:
<body>
<div id="header" class="clearfix">
<div id="wrapper">
<div id="main-nav" class="float-left">
<ul id="main-nav-menu" class="menu">
<li id="main-menu-button">
<a href="#" data-description="Since 1976">
Pardee Electric
</a>
</li>
</ul>
</div>
<div id="main-nav" class="float-right">
<ul ud="main-nav-menu" class="menu">
<li id="main-menu-button" class="float-right">
<a href="#">
Get in Touch
</a>
</li>
<li id="main-menu-button" class="float-right">
<a href="#">
Residential
</a>
</li>
<li id="main-menu-button" class="float-right">
<a href="#">
Commercial
</a>
</li>
<li id="main-menu-button" class="float-right">
<a href="#">
Industrial
</a>
</li>
</ul>
</div>
</div>
</div>
</body>
The CSS:
/* body data */
body {
margin-top: 0px;
-webkit-font-smoothing: subpixel-antialiased;
background-color: #F0F0F0;
}
ul {
list-style: disc;
}
/* header data */
#header {
width: 100%;
height: auto;
background-color: #456DC0;
border-bottom: 1px solid #FFFFFF;
}
#wrapper {
width: auto;
max-width: 1024px;
height: auto;
background: none;
margin: 0 auto;
}
.clearfix:after {
content: " ";
display: block;
height: 0;
clear: both;
margin-top: 0;
}
#main-nav-menu {
}
#main-nav {
width: auto;
height: auto;
position: relative;
}
#main-nav ul, #main-nav .menu {
margin: 0px;
}
#main-nav li {
width: auto;
list-style: none;
margin: 0px;
position: relative;
display: inline;
}
#main-nav a {
font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 16px;
padding: 15px 20px 15px 20px;
position: relative;
letter-spacing: 0px;
text-align: center;
text-decoration: none;
text-transform: none;
text-shadow: 1px 1px 1px rgba(0, 0, 0, .4);
display: block;
color: #F0F0F0;
z-index: 98;
-webkit-transition: background-color .2s ease, border .2s ease, color .4s ease, opacity .2s ease-in-out;
background-color: #4186D2;
}
#main-nav a:active {
background-color: #000000;
}
#main-nav a:hover {
background-color: #333333;
color: #FFFFFF;
}
.float-right {
float: right;
}
.float-left {
float: left;
}
Demo in Jsfiddle
Hopefully the css posted OK. I am new to Stack.
Thanks again!
you need to give class="clearfix" to id="wrapper".