I am new to CSS and I was trying to create a very basic navigation bar.
My HTML and CSS are given below.
The problem is when I hover over my FEATURES tab nothing happens even though I have changed the feature-menu displays to flex from none upon hovering.
I am not able to find any mistake in my code. Could anyone please tell where I have gone wrong?
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.header {
display: flex;
justify-content: space-between;
background: #D6E9FE;
position: fixed;
width: 100%;
padding: 50px;
}
.menu {
display: flex;
justify-content: space-between;
}
.menu li {
padding: 20px;
margin-right: 15px;
}
body {
height: 1200px;
font-size: 18px;
font-family: sans-serif;
color: #5D6063;
}
.dropdown span:hover .features-menu {
display: flex;
flex-direction: column;
background: #B2D6FF;
border-radius: 5px;
padding-top: 60px;
position: absolute;
top: 50px;
left: 990px;
}
.features-menu li {
list-style: none;
border-bottom: 1px solid #FFF;
padding: 0 40px 10px 20px;
margin: 10px;
}
.dropdown>span {
z-index: 2;
position: relative;
cursor: pointer;
}
.features-menu {
z-index: 1;
display: none;
}
<div class='header'>
<div class='logo'><img src='...' /></div>
<ul type="none" class='menu'>
<li class='dropdown'><span>Features ▾</span></li>
<ul class='features-menu'>
<li><a href='#'>Harder</a></li>
<li><a href='#'>Better</a></li>
<li><a href='#'>Faster</a></li>
<li><a href='#'>Stronger</a></li>
</ul>
<li><a href='#'>Blog</a></li>
<li><a href='#'>Subscribe</a></li>
<li><a href='#'>About</a></li>
</ul>
</div>
Let's review your HTML structure.
Here's the relevant part:
<ul type="none" class='menu'>
<li class='dropdown'><span>Features ▾</span></li>
<ul class='features-menu'>
<li><a href='#'>Harder</a></li>
<li><a href='#'>Better</a></li>
<li><a href='#'>Faster</a></li>
<li><a href='#'>Stronger</a></li>
</ul>
<li><a href='#'>Blog</a></li>
.
.
.
Here's the CSS selector you're applying:
.dropdown span:hover .features-menu
So the problem is clear.
You're going from one level (.dropdown), down a level (to the span), then back up a level (to the .features-menu).
You're trying to target the .features-menu from an element positioned lower in the HTML structure. CSS doesn't work that way.
CSS can only target downward or forward. It cannot target upward or backward.
These concepts are explained in detail in these posts:
Is there a CSS parent selector?
Is there a "previous sibling" CSS selector?
You can make your hovering work by targeting "forward" (using your original – malformed – HTML), using sibling combinators (+ or ~).
.dropdown:hover + .features-menu
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.header {
display: flex;
justify-content: space-between;
background: #D6E9FE;
position: fixed;
width: 100%;
padding: 50px;
}
.menu {
display: flex;
justify-content: space-between;
}
.menu li {
padding: 20px;
margin-right: 15px;
}
body {
height: 1200px;
font-size: 18px;
font-family: sans-serif;
color: #5D6063;
}
.dropdown:hover + .features-menu {
display: flex;
flex-direction: column;
background: #B2D6FF;
border-radius: 5px;
padding-top: 60px;
position: absolute;
top: 50px;
/* left: 990px; */
}
.features-menu li {
list-style: none;
border-bottom: 1px solid #FFF;
padding: 0 40px 10px 20px;
margin: 10px;
}
.dropdown>span {
z-index: 2;
position: relative;
cursor: pointer;
}
.features-menu {
z-index: 1;
display: none;
}
<div class='header'>
<div class='logo'><img src='...' /></div>
<ul type="none" class='menu'>
<li class='dropdown'><span>Features ▾</span></li><!-- closing tag doesn't go here -->
<ul class='features-menu'>
<li><a href='#'>Harder</a></li>
<li><a href='#'>Better</a></li>
<li><a href='#'>Faster</a></li>
<li><a href='#'>Stronger</a></li>
</ul>
<li><a href='#'>Blog</a></li>
<li><a href='#'>Subscribe</a></li>
<li><a href='#'>About</a></li>
</ul>
</div>
Or, you can make your hovering behavior work by targeting "downward" (using correctly formed HTML), using descendant selectors (> or [space]).
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.header {
display: flex;
justify-content: space-between;
background: #D6E9FE;
position: fixed;
width: 100%;
padding: 50px;
}
.menu {
display: flex;
justify-content: space-between;
}
.menu li {
padding: 20px;
margin-right: 15px;
}
body {
height: 1200px;
font-size: 18px;
font-family: sans-serif;
color: #5D6063;
}
.dropdown:hover > .features-menu {
display: flex;
flex-direction: column;
background: #B2D6FF;
border-radius: 5px;
padding-top: 60px;
position: absolute;
top: 50px;
/* left: 990px; */
}
.features-menu li {
list-style: none;
border-bottom: 1px solid #FFF;
padding: 0 40px 10px 20px;
margin: 10px;
}
.dropdown>span {
z-index: 2;
position: relative;
cursor: pointer;
}
.features-menu {
z-index: 1;
display: none;
}
<div class='header'>
<div class='logo'><img src='...' /></div>
<ul type="none" class='menu'>
<li class='dropdown'><span>Features ▾</span>
<ul class='features-menu'>
<li><a href='#'>Harder</a></li>
<li><a href='#'>Better</a></li>
<li><a href='#'>Faster</a></li>
<li><a href='#'>Stronger</a></li>
</ul>
</li><!-- closing tag goes here -->
<li><a href='#'>Blog</a></li>
<li><a href='#'>Subscribe</a></li>
<li><a href='#'>About</a></li>
</ul>
</div>
Related
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;
}
I'm having trouble figuring out why the dropdown menu goes horizontal. I made a horizontal menu to start with and tried adding a dropdown to it. However, it goes horizontal and I can't figure out why.
I've been racking my brain over this for hours but I don't know what to do.
Please help me.
nav li {
float: left;
}
li a {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
li:last-child {
border-right: none;
}
.navbar {
display: flex;
align-items: center;
width: 100%;
background-color: #ffffff;
}
#menu-container {
display: flex;
width: 100%;
justify-self: center;
justify-content: center;
}
.nav-menu {
display: flex;
justify-content: space-between;
align-items: center;
}
.dropdown {
position: relative;
z-index: 100;
}
.dropdown {
display: inline-block;
margin-left: 10px;
}
.dropdown:hover .dropdown-menu {
height: auto;
opacity: 1;
}
.dropdown-menu {
position: absolute;
z-index: 90;
align-items: stretch;
background-color: rgb(68, 68, 68);
list-style: none;
overflow: hidden;
height: auto;
opacity: 0;
transition: opacity 0.5s;
}
nav ul {
display: flex;
justify-content: space-between;
list-style: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #333;
position: relative;
top: 0;
}
<nav class="navbar">
<div id="menu-container">
<ul class="nav-menu">
<li class="nav-item">
<a class="nav-link" href="#">Men</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link-dropdown" href="#">Women</a>
<ul class="dropdown-menu">
<li><a class="nav-link" href="#">New</a></li>
<li><a class="nav-link" href="#">Tops</a></li>
<li><a class="nav-link" href="#">Bottoms</a></li>
<li><a class="nav-link" href="#">Accessories</a></li>
</ul>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Kids</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
</ul>
</div>
</nav>
if you write flex instead of block your navbar is shown as vertical because you can style the element on n the vertical side with flex
in CSS in the .new-menu write flex instead of block in display property
.nav-menu {
display: flex;
justify-content: space-between;
align-items: center;
flex-direction:column
;
}
In nav-menu class you need to add the property flex-direction and give it the value column.
This will make the cross axis go horizontally and main axis go vertically.
.nav-menu {
display: flex;
justify-content: space-between;
align-items: center;
flex-direction:column;
}
More about it in detail here Flex-direction
Dear you have a lot of errors in CSS Try this
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="wrapper">
<ul>
<li>home</li>
<li>About</li>
<li>Services
<ul>
<li>Marketing</li>
<li>Design
<ul>
<li>Web</li>
<li>Graphics</li>
<li>Interior</li>
</ul>
</li>
<li>Branding</li>
</ul>
</li>
<li>Portfolio</li>
<li>Contact</li>
</ul>
</div>
</body>
</html>
CSS Code
*{
margin: 0;
padding: 0;
}
body{
background-image: url(1.jpg);
-webkit-background-size:cover;
background-size:cover;
background-position: center center;
height: 100vh;
}
.wrapper{
width: 860px;
margin: 0 auto;
}
.wrapper ul{
list-style: none;
margin-top: 2%;
}
.wrapper ul li {
background: #262626;
width: 170px;
border: 1px solid #fff;
height: 50px;
line-height: 50px;
text-align: center;
float: left;
color: #fff;
font-size: 16px;
position: relative;
font-family: poppins;
text-transform: uppercase;
font-weight: bold;
}
.wrapper ul li:hover{
background: crimson;
}
.wrapper ul ul{
display: none;
}
.wrapper ul li:hover > ul{
display: block;
}
.wrapper ul ul ul{
margin-left: 170px;
top: 0;
position: absolute;
}
In your css you've provided the selector nav ul. Now what this does, it applies the style to every ul descendent of parent nav. This means it is getting applied even to the dropdown-menu. Hence to specify that it is applicable only to the child we use the child combinator selector nav > div > ul
nav > div > ul {
display: flex;
justify-content: space-between;
list-style: none;
margin: 0;
padding: 0;
background-color: #333;
position: relative;
top: 0;
}
Hey guys I'm not sure what I'm doing wrong, but I want the 3rd li item (input) to go to the end of the container, when I use the justify-content: space-between; - nothing happens, I've tried aligning them, but still nothing.
nav {
width: 100%;
background-color: black;
padding-left: 30px;
padding-right: 10px;
}
.navContact {
margin-left: auto;
}
.navbar {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.mainNav {
display: flex;
}
.navitem a {
color: white;
text-decoration: none;
border-right: 2px solid rgb(255, 123, 0);
padding: 10px 15px;
}
<hr class="hrNav">
<nav class="navbar">
<ul class="mainNav">
<li class="navitem">About me</li>
<li class="navitem">Contact</li>
<li class="navitem"><input type="text" placeholder="Search..."></li>
</ul>
</nav>
You can set flex: 1 in the ul and the last item of the ul, then set margin-left: auto in the last li inside ul as well
*,
*::after,
*::before {
box-sizing: border-box
}
nav {
width: 100%;
background-color: black;
padding-left: 30px;
padding-right: 10px;
display: flex;
justify-content: space-between;
}
.mainNav {
display: flex;
flex: 1;
}
.navitem a {
color: white;
text-decoration: none;
border-right: 2px solid rgb(255, 123, 0);
padding: 10px 15px;
}
.navitem:last-of-type {
margin-left: auto
}
<hr class="hrNav">
<nav class="navbar">
<ul class="mainNav">
<li class="navitem">About me</li>
<li class="navitem">Contact</li>
<li class="navitem"><input type="text" placeholder="Search..."></li>
</ul>
</nav>
I have elements in the li list and I want to connect them using a simple line so that it looks like a pipeline.
I want to implement pipeline like structure to these elements
The HTML I have prepared
<ul>
<li class="pipeline-box">New</li> ---
<li class="pipeline-box">Order Placed</li> ---
<li class="pipeline-box">Order confirmed</li>---
<li class="pipeline-box">In Process</li>---
<li class="pipeline-box">Ready For Dispatch</li>---
<li class="pipeline-box">On the Way</li>---
<li class="pipeline-box">Delivered</li>
</ul>
The CSS for pipeline-box
.pipeline-box {
border: 1px solid #696666;
width: fit-content;
padding: 6px;
background: #cccec9;
border-radius: 50px;
font-family: "avenir Medium";
font-size: 0.8rem !important;
}
I want it like this
You're not really using the ul/li feature so I'd suggest
.pipeline-box {
border: 1px solid #696666;
width: fit-content;
padding: 6px;
background: #cccec9;
border-radius: 50px;
font-family: "avenir Medium";
font-size: 0.8rem !important;
}
.list {
flex-direction: row;
box-sizing: border-box;
display: flex;
place-content: center space-around;
align-items: center;
}
<div class="list">
<div class="pipeline-box">New</div> ---
<div class="pipeline-box">Order Placed</div> ---
<div class="pipeline-box">Order confirmed</div>---
<div class="pipeline-box">In Process</div>---
<div class="pipeline-box">Ready For Dispatch</div>---
<div class="pipeline-box">On the Way</div>---
<div class="pipeline-box">Delivered</div>
</div>
Sidenote: using !important is usually viewed as bad practice, avoid it if you can
Just use the CSS pseudo-element :after. The --- connectors are just presentation elements, not part of your content and as such would be better separated from the code structure.
In the example below I used :last-child to remove the extra connector in the last element.
.pipeline-box {
display: inline-block;
}
.pipeline-box::after {
content: "---";
}
.pipeline-box:last-child::after {
display: none;
}
<ul class="list">
<li class="pipeline-box">New</li>
<li class="pipeline-box">Order Placed</li>
<li class="pipeline-box">Order confirmed</li>
<li class="pipeline-box">In Process</li>
<li class="pipeline-box">Ready For Dispatch</li>
<li class="pipeline-box">On the Way</li>
<li class="pipeline-box">Delivered</li>
</ul>
And by the way you don't necessarily need the "pipeline-box" class on every element (specially if all elements have the same class, there's no point), you could use the class already on the ul and target your objects using .list li, assuming all li elements in your list will be connected. Or add the pipeline-box class to the ul instead.
Added a span inside li which will contain the text and will be shown as bubble and then I made that link using the ::before of li.
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
.pipeline-box {
display: inline-block;
margin-bottom: 10px;
margin-right: -5px;
position: relative;
width: auto;
}
.pipeline-box::before {
background: #c5c5c5;
content: "";
height: 1px;
left: 32px;
position: absolute;
right: 0;
top: 50%;
z-index: 0;
}
.pipeline-box>span {
background: #f8f8f8;
border: 1px solid #c5c5c5;
border-radius: 50px;
display: inline-block;
font-family: "avenir Medium";
font-size: 0.8em !important;
line-height: 1em;
margin: 0 30px 0 0;
padding: 7px 12px;
position: relative;
z-index: 1;
}
.pipeline-box:last-child>span {
margin: 0;
}
<ul>
<li class="pipeline-box"><span>New</span></li>
<li class="pipeline-box"><span>Order Placed</span></li>
<li class="pipeline-box"><span>Order confirmed</span></li>
<li class="pipeline-box"><span>In Process</span></li>
<li class="pipeline-box"><span>Ready For Dispatch</span></li>
<li class="pipeline-box"><span>On the Way</span></li>
<li class="pipeline-box"><span>Delivered</span></li>
</ul>
I figured out the solution.
the HTML page is now
<ul class="pipeline-box">
<li><span>New</span></li>
<li><span>Order Placed</span></li>
<li><span>Order confirmed</span></li>
<li><span>In Process</span></li>
<li><span>Ready For Dispatch</span></li>
<li><span>On the Way</span></li>
<li><span>Delivered</span></li>
</ul>
and the SCSS is
.pipeline-box {
li{
align-content: center;
border: 1px solid #696666;
text-align: center;
width: 125px;
padding: 6px;
background: #f8f8f8;
border-radius: 50px;
font-family: "avenir Medium";
font-size: 0.8rem !important;
position: relative;
}
li:after{
content: '';
width: 17px;
height: 2px;
background-color: #afa4a4;
position: absolute;
z-index: 0;
top: calc(50% - 2px);
right: -18px;
}
li:before{
content: '';
width: 17px;
height: 2px;
background-color: #afa4a4;
position: absolute;
z-index: 0;
top: calc(50% - 2px);
left: -18px;
}
li:last-child:after {
display: none;
}
li:first-child::before {
display: none;
}
}
It looks like this now.
result
I am trying to have a centred list of numbers in a responsive page. The issue I am having is that as you adjust the screen width the list items inside the unordered list are no longer centred inside the containing div.
I have removed the default padding on the ul element but I think something is missing to bring it all together. Can someone tell me where I am going wrong?
#content {
background-color: yellow;
overflow: hidden;
padding: 0 15px;
width: 100%;
box-sizing: border-box;
}
#heading {
text-align: center;
}
#number_container {
overflow: hidden;
background-color: #07c;
width: auto;
}
ul {
padding-left: 0;
}
li {
list-style: none;
display: list-item;
}
ul li a {
float: left;
padding: 5px;
min-width: 35px;
margin-right: 2px;
min-height: 35px;
line-height: 28px;
color: #333;
text-decoration: none;
text-align: center;
background-color: #fff;
border: #fff solid 1px;
}
<div id="content">
<div id="heading">Centered Text</div>
<div id="number_container">
<ul>
<li><a id="1">1</a></li>
<li><a id="2">2</a></li>
<li><a id="3">3</a></li>
<li><a id="4">4</a></li>
<li><a id="5">5</a></li>
<li><a id="6">6</a></li>
<li><a id="7">7</a></li>
<li><a id="8">8</a></li>
<li><a id="9">9</a></li>
<li><a id="10">10</a></li>
<li><a id="11">11</a></li>
<li><a id="12">12</a></li>
<li><a id="13">13</a></li>
<li><a id="14">14</a></li>
<li><a id="15">15</a></li>
<li><a id="16">16</a></li>
<li><a id="17">17</a></li>
<li><a id="18">10</a></li>
<li><a id="19">11</a></li>
<li><a id="20">12</a></li>
<li><a id="21">13</a></li>
<li><a id="22">14</a></li>
<li><a id="23">15</a></li>
<li><a id="24">16</a></li>
<li><a id="25">17</a></li>
</ul>
</div>
</div>
Fiddle Link
Another edited version of my answer:
Use these settings :
#number_container {
overflow: hidden;
background-color: #07c;
text-align: justify;
}
ul {
padding: 0;
}
li {
display: inline-block;
list-style: none;
}
ul li a {
display: inline-block;
padding: 5px 5px 3px 5px;
margin: 5px;
min-width: 35px;
min-height: 35px;
line-height: 37px;
color: #333;
text-decoration: none;
text-align: center;
background-color: #fff;
}
This aligns the last row of <li> elements left, which you apparently were aiming at. Another possibility would be text-align: center; for #number_container (see also my fiddle https://jsfiddle.net/f77ujsqb/1/ ), which aligns the last row centered.
remove the display from li
display: list-item;