I have created this menu with a dropdown feature when you hover over "treatments". But when I apply the "position: fixed;" in the container class on CSS the dropdown menu doesn't function anymore. The menu goes to a fixed position but the dropdown feature won't function with the fixed position. I would like to solve it with CSS, any suggestions on how?
.container {
overflow: hidden;
background-color: rgba(48, 48, 48, 0.9);
font-family: Helvetica, sans-serif;
width: 100%;
position: fixed;
margin-top: 0;
margin-left: 0;
}
.container a {
float: right;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown {
float: right;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
}
.container a:hover,
.dropdown:hover .dropbtn {
background-color: #ff008f;
}
.dropdown-content {
display: none;
position: absolute;
background-color: rgba(255, 255, 255, 0.98);
min-width: 160px;
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: #f9e9ff;
}
.dropdown:hover .dropdown-content {
display: block;
}
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="UCL.css">
<title>Home</title>
<div class="stripes">
<div class="container">
Contact Us
<a href="products-gifts.html" target="_top">Products &
Gifts</a>
<div class="dropdown">
<button class="dropbtn">Treatments</button>
<div class="dropdown-content">
<a href="body-contouring.html" target="_top">Body
Contouring</a>
Cellulite
Laser Hair Reduction
<a href="laser-peels.html" target="_top">Laser
Peels</a>
LED
<a href="photofacial.html" target="_top>" Photofacial & Photobody</a>
<a href="spider-veins.html" target="_top">Spider
Veins</a>
</div>
</div>
Home
</div>
</head>
It was the overflow: hidden on your .container element that prevented the dropdown menus from showing - not the position: fixed. By hiding overflow, you're preventing elements (such as your dropdown menu) that exceed the dimensions of the .container from displaying.
.container {
/* overflow: hidden; */
background-color: rgba(48, 48, 48, 0.9);
font-family: Helvetica, sans-serif;
width: 100%;
position: fixed;
margin-top: 0;
margin-left: 0;
}
.container a {
float: right;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown {
float: right;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
}
.container a:hover,
.dropdown:hover .dropbtn {
background-color: #ff008f;
}
.dropdown-content {
display: none;
position: absolute;
background-color: rgba(255, 255, 255, 0.98);
min-width: 160px;
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: #f9e9ff;
}
.dropdown:hover .dropdown-content {
display: block;
}
<div class="stripes">
<div class="container">
Contact Us
<a href="products-gifts.html" target="_top">Products &
Gifts</a>
<div class="dropdown">
<button class="dropbtn">Treatments</button>
<div class="dropdown-content">
<a href="body-contouring.html" target="_top">Body
Contouring</a>
Cellulite
Laser Hair Reduction
<a href="laser-peels.html" target="_top">Laser
Peels</a>
LED
<a href="photofacial.html" target="_top>" Photofacial & Photobody</a>
<a href="spider-veins.html" target="_top">Spider
Veins</a>
</div>
</div>
Home
</div>
</div>
Related
I made this CSS/HTML.
.navbar {
width: 100%;
background-color: rgb(151, 44, 44);
overflow: auto;
}
.navbar a {
border-right:1px solid #f5b7b7;
float: left;
padding: 8px;
color: white;
text-decoration: none;
font-size: 1.5vh;
width: 25%; /* Four links of equal widths */
text-align: center;
}
.navbar a:hover {
background-color: rgb(92, 25, 25);
}
.navbar a.active {
background-color: #04AA6D;
}
/* ************************************************************** */
/* DROPDOWN */
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 1.5vh;
border: none;
outline: none;
color: white;
padding: 8px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.dropdown:hover .dropbtn {
background-color: rgb(92, 25, 25);
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 200px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: rgb(0, 0, 0);
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
width: 100%;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div class="navbar" id="tree">
Home1
Home2
Home3
<div class="dropdown">
<button class="dropbtn" > TOPICS ▼ </button>
<div class="dropdown-content">
t1
t2
t3
</div>
</div>
</body>
</html>
But there are 2 issues.
issue 1 -
Not able to center the drop down. using below code it centers but creates scroll bar on different resolutions and devices etc.
.dropdown {
float: left;
overflow: hidden;
position: relative;
transform: translate(150%,0%)
}
issue 2 -
Drop down button is not expanded for entire of its 25% area (as are other buttons). ie.. Hovering is valid only over the text "TOPIC V"
Any ideas why its happening..not able to resolve since 2 days ;[
For the first issue, use flexbox to align the items.
.navbar {
...
display: flex;
}
.dropdown {
...
width: 25%;
text-align: center;
}
For the second issue, you need to set the width to 100%. Check the code snippet below for more details.
.dropdown .dropbtn {
...
width: 100%;
}
.navbar {
width: 100%;
background-color: rgb(151, 44, 44);
overflow: auto;
display: flex;
}
.navbar a {
border-right:1px solid #f5b7b7;
float: left;
padding: 8px;
color: white;
text-decoration: none;
font-size: 1.5vh;
width: 25%; /* Four links of equal widths */
text-align: center;
}
.navbar a:hover {
background-color: rgb(92, 25, 25);
}
.navbar a.active {
background-color: #04AA6D;
}
/* ************************************************************** */
/* DROPDOWN */
.dropdown {
float: left;
overflow: hidden;
width: 25%;
text-align: center;
}
.dropdown .dropbtn {
font-size: 1.5vh;
border: none;
outline: none;
color: white;
padding: 8px;
background-color: inherit;
font-family: inherit;
margin: 0;
width: 100%;
}
.dropdown:hover .dropbtn {
background-color: rgb(92, 25, 25);
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 200px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: rgb(0, 0, 0);
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
width: 100%;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div class="navbar" id="tree">
Home1
Home2
Home3
<div class="dropdown">
<button class="dropbtn" > TOPICS ▼ </button>
<div class="dropdown-content">
t1
t2
t3
</div>
</div>
</body>
</html>
This question already has answers here:
How do I center floated elements?
(12 answers)
Closed 5 months ago.
I've got this nav bar almost right where I want it, but I just need to center it on the page. How can I center the navigation without changing anything else? No matter what I try, it will always align to the left of the page. I know it's probably because of the "float: left" properties, but changing those ruins the whole navbar layout.
.navbar {
overflow: hidden;
background-color: none;
text-align: center;
}
.navbar a {
float: left;
font-size: 16px;
color: grey;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition-duration: 0.3s;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: grey;
padding: 0px 0px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover,
.dropdown:hover .dropbtn {
color: #FFF;
}
.dropdown-content {
display: none;
position: absolute;
background-color: none;
margin-top: 45px;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: grey;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: none;
padding: 12px 25px;
color: #FFF;
}
.dropdown:hover .dropdown-content {
display: block;
}
.navbar .focus {
color: #FFF;
}
<div class="navbar">
HOME
<div class="dropdown"><a href="#">PRACTITIONERS
<button class="dropbtn">
<i class="fa fa-caret-down"></i>
</button></a>
<div class="dropdown-content">
Biochemistry
Biophysics
Nutrition
</div>
</div>
CONSUMERS
NETWORK
</div>
you can add display flex and justify content center like this
.navbar {
display: flex;
justify-content: center;
overflow: hidden;
background-color: none;
text-align: center;
}
.navbar {
overflow: hidden;
background-color: none;
text-align: center;
margin-left: 25%;
}
Make navbar a Flexbox and with justify-content: center, align its children to the center of navbar.
body {
background: lightgray;
}
.navbar {
/* add the following two styles */
display: flex;
justify-content: center;
overflow: hidden;
background-color: none;
text-align: center;
}
.navbar a {
float: left;
font-size: 16px;
color: grey;
text-align: center;
padding: 14px 16px;
text-decoration: none;
transition-duration: 0.3s;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: grey;
padding: 0px 0px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover,
.dropdown:hover .dropbtn {
color: #FFF;
}
.dropdown-content {
display: none;
position: absolute;
background-color: none;
margin-top: 45px;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: grey;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: none;
padding: 12px 25px;
color: #FFF;
}
.dropdown:hover .dropdown-content {
display: block;
}
.navbar .focus {
color: #FFF;
}
<div class="navbar">
HOME
<div class="dropdown"><a href="#">PRACTITIONERS
<button class="dropbtn">
<i class="fa fa-caret-down"></i>
</button></a>
<div class="dropdown-content">
Biochemistry
Biophysiscs
Nutrition
</div>
</div>
CONSUMERS
NETWORK
</div>
I want to align my dropdown menu a tags to the center but can't figure out how to do that, only started with CSS recently.
It would display the dropdown dropdownbtn items in the dropdown-content with the links aligned to the center.
.navbar {
overflow: hidden;
background-color: #333;
}
.navbar a {
float: left;
font-size: 16px;
color: #f2f2f2;
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: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover,
.dropdown:hover .dropbtn {
background-color: #ddd;
color: black;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
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;
}
<div class="navbar">
Home
<div class="dropdown">
<button class="dropbtn">Dropdown Menu</button>
<div class="dropdown-content">
Link 1
Link 2
</div>
</div>
Use a transform on the dropdown.
left: 50%;
transform: translatex(-50%);
Also I switched from float to flexbox for simplicity and a more modern approach.
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
::before,
::after {
box-sizing: inherit;
}
.navbar {
background-color: #333;
display: flex;
align-items: center;
}
.navbar a {
font-size: 16px;
color: #f2f2f2;
padding: 14px;
text-decoration: none;
}
.dropdown {
position: relative;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover,
.dropdown:hover .dropbtn {
background-color: #ddd;
color: black;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
left: 50%;
transform: translatex(-50%);
}
.dropdown-content a {
color: black;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
<div class="navbar">
Home
<div class="dropdown">
<button class="dropbtn">Dropdown Menu</button>
<div class="dropdown-content">
Link 1
Link 2
</div>
</div>
I'm building a navigation bar and I want
all the items on the same row [not sure why a 2nd row is created]
the dropdown items from 'Samples' button to display vertically instead of horizontally
JSFiddle: https://jsfiddle.net/RadiantSilvergun/xsbnjo5g/5/
I've used <div> elements instead of <ul> and <li> elements, is that a problem?
* {
box-sizing: border-box;
}
body {
margin: 0;
}
.topnav {
overflow: hidden;
background-color: #333;
}
.topnav a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.dropbutton {
background-color: #333;
color: white;
padding: 14px 16px;
font-size: 16px;
font: 1em times;
border: none;
cursor: pointer;
}
.dropdown {
position: static;
display: block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content>a {
position: relative;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content>a:hover {
background-color: #f1f1f1;
position: relative;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropbutton {
background-color: darkgrey;
}
.button {
background-color: black;
color: white;
border-radius: 5px;
;
padding: 15px 15px;
text-align: center;
font-size: 16px;
margin: 4px 2px;
}
<div class="topnav">
<div>Contact Me</div>
<div class="dropdown">
<button class="dropbutton">Samples</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
<div>Recommendations</div>
<div>Video Editing</div>
<div>Photoshop</div>
<div><a style="float: right" href="#">About</a></div>
</div>
I am wondering how to align test to the middle of the navigation bar. My code is below:
body {
font-family: 'Arial', sans-serif;
background-color: #f3f3f3;
overflow-x: hidden;
}
.navbar {
overflow: hidden;
background-color: #333;
font-family: Arial, Helvetica, sans-serif;
}
.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: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.navbar a:hover,
.dropdown:hover .dropbtn {
background-color: red;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
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;
}
<div class="navbar">
Home
<div class="dropdown">
<button class="dropbtn">Games <i class="fa fa-caret-down"></i></button>
<div class="dropdown-content">
Badge
Undead Nightmare
All Games
</div>
</div>
Videos
Newswire
Social Club
Downloads
Warehouse
Support
</div>
</div>
Nav Bar
I have tried using display: inline-block, I have tried removing float: left. All in which i tried below .navbar a and a bunch of other ways, but I still can't center it.
Any help would be greatly appreciated and thank you in advance.