I am having trouble centering the following CSS dropdown menu:
.container {
overflow: hidden;
background-color: none;
font-family: 'Questrial', sans-serif;
}
.container a {
float: right;
font-size: 20px;
color: green;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown {
float: right;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 20px;
border: none;
outline: none;
color: green;
padding: 14px 16px;
background-color: inherit;
font-family: 'Questrial', sans-serif;
}
.container a:hover,
.dropdown:hover .dropbtn {
background-color: none;
color: green;
}
.dropdown-content {
display: none;
position: absolute;
background-color: none;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: green;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: none;
color: green;
}
.dropdown:hover .dropdown-content {
display: block;
}
I have tried using position: absolute, and entering values for "top" and left" however this only seems to move the text, and loses the dropdown functionality.
Any help would be appreciated.
Thanks.
Full code here:
gist.github.com/ruslan024/1a7d2ce4936a35369221a8f0e41c5dd7
Do you mean something like this? Without the full HTML formatted into a snippet (not just some 3rd party gist) I had to make some assumptions. Apart from that it was just a few CSS tweaks.
.container {
background-color: #eee;
font-family: 'Questrial', sans-serif;
text-align: center;
height: 300px;
}
.container > a {
display: inline-block;
font-size: 20px;
color: green;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropbtn {
display: inline-block;
font-size: 20px;
padding: 14px 16px;
background-color: inherit;
font-family: 'Questrial', sans-serif;
color: green;
}
.dropdown-content {
display: none;
position: absolute;
background-color: none;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
float: none;
color: green;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: none;
color: green;
}
.dropbtn:hover .dropdown-content {
display: block;
}
<div class="container">
Contact Us
<div href="" class="dropbtn">Resources
<span class="dropdown-content">
Market Data Bank
Stock Quotes
Finacial Tools
</span>
</div>
<div href="" class="dropbtn">Services
<span class="dropdown-content">
Planning and Guidance
Portfolio Advisory Services
Wealth Planning
</span>
</div>
About Us
Home
</div>
Related
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 would like to add a logo at the left or the center of my navbar, can anyone tell me how could I achieve this?
I have tried this but when I do add these lines to my code, my links are upper than the logo, looks like the logo is pushing them or something.
Anyone?
.logo {
max-width:100px;
display:inline-block;
}
Here is my code:
#import url("https://fonts.googleapis.com/css2?family=Nunito:wght#300;400;600;700;900&display=swap");
html,
body {
height: 100%;
width: 100%;
font-family: "Nunito", sans-serif;
}
.nav {
background-color: #fff;
overflow: hidden;
justify-content: space-between;
}
.nav a {
float: left;
display: block;
color: rgba(0, 0, 0, 0.8);
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 0.75rem;
}
.nav a:hover {
background-color: #fff;
color: #000;
}
.nav .active {
color: #000;
font-weight: 600;
}
.nav .icon {
display: none;
}
.nav .dropdown {
float: left;
overflow: hidden;
}
.nav .dropdown:hover .dropbtn {
background-color: #fff;
color: #000;
}
.nav .dropdown:hover .dropdown-content {
display: block;
border-radius: 4px;
border: 1px solid rgba(0, 0, 0, 0.10);
}
.nav .dropdown .dropbtn {
font-size: 0.75rem;
border: none;
outline: none;
color: rgba(0, 0, 0, 0.8);
padding: 14px 16px;
background-color: inherit;
margin: 0;
font-family: "Nunito", sans-serif;
cursor: pointer;
}
.nav .dropdown-content {
display: none;
position: absolute;
background: #fff;
min-width: 160px;
z-index: 1;
}
.nav .dropdown-content a {
float: none;
color: #9b9b9b;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
transition: 0.4s ease;
}
.nav .dropdown-content a:hover {
background-color: #fff;
color: #000;
}
<div class="nav" id="topnav">
Feed
Discover
Jobs
<div class="dropdown">
<button class="dropbtn">Profile</button>
<div class="dropdown-content">
My profile
My projects
Settings
Help
</div>
</div>
About
☰
</div>
Also it can be centered, I'd be happy too.
Thank you!
First of all, justify-content: start; only works on an element with display: flex;.
USing display: flex; on the .nav element, use align-items to vertically center the elements;
#import url("https://fonts.googleapis.com/css2?family=Nunito:wght#300;400;600;700;900&display=swap");
html,
body {
height: 100%;
width: 100%;
font-family: "Nunito", sans-serif;
}
.nav {
background-color: #fff;
overflow: hidden;
justify-content: start;
display: flex; /* <-- */
align-items: center; /* <-- */
}
.nav a {
float: left;
display: block;
color: rgba(0, 0, 0, 0.8);
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 0.75rem;
}
.nav a:hover {
background-color: #fff;
color: #000;
}
.nav .active {
color: #000;
font-weight: 600;
}
.nav .icon {
display: none;
}
.nav .dropdown {
float: left;
overflow: hidden;
}
.nav .dropdown:hover .dropbtn {
background-color: #fff;
color: #000;
}
.nav .dropdown:hover .dropdown-content {
display: block;
border-radius: 4px;
border: 1px solid rgba(0, 0, 0, 0.10);
}
.nav .dropdown .dropbtn {
font-size: 0.75rem;
border: none;
outline: none;
color: rgba(0, 0, 0, 0.8);
padding: 14px 16px;
background-color: inherit;
margin: 0;
font-family: "Nunito", sans-serif;
cursor: pointer;
}
.nav .dropdown-content {
display: none;
position: absolute;
background: #fff;
min-width: 160px;
z-index: 1;
}
.nav .dropdown-content a {
float: none;
color: #9b9b9b;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
transition: 0.4s ease;
}
.nav .dropdown-content a:hover {
background-color: #fff;
color: #000;
}
<div class="nav" id="topnav">
<a><img id='logo' src='https://placehold.it/150x40'/></a>
Feed
Discover
Jobs
<div class="dropdown">
<button class="dropbtn">Profile</button>
<div class="dropdown-content">
My profile
My projects
Settings
Help
</div>
</div>
About
☰
</div>
Try using this code:
<img src="/path/to/image.png">
Where I found the answer.
I'm added a css code into .nav like
.nav {
background-color: #fff;
overflow: hidden;
justify-content: space-between;
align-items:center;
display:flex
}
then added a .nav .logo-img into bottom
.nav .logo-img {
position:relative;
justify-content:start;
}
#import url("https://fonts.googleapis.com/css2?family=Nunito:wght#300;400;600;700;900&display=swap");
html,
body {
height: 100%;
width: 100%;
font-family: "Nunito", sans-serif;
}
.nav {
background-color: #fff;
overflow: hidden;
justify-content: space-between;
align-items:center;
display:flex
}
.nav a {
float: left;
display: block;
color: rgba(0, 0, 0, 0.8);
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 0.75rem;
}
.nav a:hover {
background-color: #fff;
color: #000;
}
.nav .active {
color: #000;
font-weight: 600;
}
.nav .icon {
display: none;
}
.nav .dropdown {
float: left;
overflow: hidden;
}
.nav .dropdown:hover .dropbtn {
background-color: #fff;
color: #000;
}
.nav .dropdown:hover .dropdown-content {
display: block;
border-radius: 4px;
border: 1px solid rgba(0, 0, 0, 0.10);
}
.nav .dropdown .dropbtn {
font-size: 0.75rem;
border: none;
outline: none;
color: rgba(0, 0, 0, 0.8);
padding: 14px 16px;
background-color: inherit;
margin: 0;
font-family: "Nunito", sans-serif;
cursor: pointer;
}
.nav .dropdown-content {
display: none;
position: absolute;
background: #fff;
min-width: 160px;
z-index: 1;
}
.nav .dropdown-content a {
float: none;
color: #9b9b9b;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
transition: 0.4s ease;
}
.nav .dropdown-content a:hover {
background-color: #fff;
color: #000;
}
.nav .logo-img {
position:relative;
justify-content:start;
}
<div class="nav" id="topnav">
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/0/02/Stack_Overflow_logo.svg/1280px-Stack_Overflow_logo.svg.png" width="150px">
Feed
Discover
Jobs
<div class="dropdown">
<button class="dropbtn">Profile</button>
<div class="dropdown-content">
My profile
My projects
Settings
Help
</div>
</div>
About
☰
</div>
I have got a navbar problem again, because I had to change the code ect due to the navbar code overriding other contents on the page, but anyway The Nvigation text is not inline and I am finding it difficult to get it inline even after over an hor of research i am unable to find the answer..
My HTML Code:
<div class="navbar">
Home
News
<div class="dropdown">
<button class="dropbtn">Dropdown
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</div>
My CSS Code:
.navbar {
overflow: hidden;
background-color: #333;
font-family: Arial, Helvetica, sans-serif;
text-align: center
}
.navbar a {
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown {
display: inline-block;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
display: inline-block;
}
.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: center;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
You need to make .navbar a either inline-block or block. Your padding will not be applied as the a is inline by default.
.navbar a {
display: inline-block;
font-size: 16px;
...
}
I recommend you make the following changes.
Add the flexbox property to your .navbar class
Add align-items property center to your .navbar class - will align all the item menus
Get rid of display: inline-block on all your
.navbar {
overflow: hidden;
background-color: #333;
font-family: Arial, Helvetica, sans-serif;
text-align: center;
display: flex;
align-items: center;
}
.navbar a {
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
// .dropdown {
// display: inline-block;
// overflow: hidden;
// }
.dropdown .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
//display: inline-block;
}
.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: center;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
Welcome to StackOverflow Kyle!
The problem resides in not having a vertical-align style declaration on your .dropdown .dropbtn CSS class declarations. Add it in as shown below:
.dropdown, .dropbtn {
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
font-family: inherit;
margin: 0;
display: inline-block;
vertical-align: middle; // Add this!
}
You'll also need to add a comma between the classes to make sure the styles are applied to both class declarations!
Here is a link to a codepen where you can view the result:
https://codepen.io/mikeabeln_nwea/pen/pxaMKV
I checked out your CSS in pastebin. I added
.navbar {
display:flex;
justify-content:center;
}
and you could also delete the text-align:center property
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.