CSS: make drop-down menu as wide as top bar - html

I've made a top bar that contains drop-down MENU button on the right side. But this drop-down content has exactly the same size (width) as my MENU button. Finally - my goal is to make this drop-down content as wide as the top bar is.
My HTML code looks like this:
<div id="top-bar">
<div class="dropdown">
<button class="dropbtn">MENU</button>
<div class="dropdown-content">
Link
Link
Link
</div>
</div>
</div>
And more important part - CSS:
#top-bar{
left: 0;
top: 0;
float: left;
width:100%;
height:40px;
background-color: black;
}
.dropbtn {
background-color: blue;
color: white;
height: 40px;
font-size: 12px;
border: none;
cursor: pointer;
}
.dropdown {
float: right;
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
float: left;
position: absolute;
background-color: #f9f9f9;
width: 100%;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 10px 14px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display: block;
width: 100%;
float: left;
left: 0;
}
.dropdown:hover .dropbtn {
background-color: blue;
}
If you want to see how does it look like HERE is jsFiddle link.
Don't you have any idea how to solve my problem?

Just move position: relative from .dropdown to #top-bar.
By doing this, .dropdown-content will calculate width according to the nearest element with position: relative i.e #top-bar.
#top-bar{
position: relative;
height:40px;
float: left;
width: 100%;
background-color: black;
}
.dropbtn {
background-color: blue;
color: white;
height: 40px;
font-size: 12px;
border: none;
cursor: pointer;
}
.dropdown {
float: right;
}
.dropdown-content {
display: none;
left: 0;
top: 100%;
position: absolute;
background-color: #f9f9f9;
width: 100%;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 10px 14px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropbtn {
background-color: blue;
}
<div id="top-bar">
<div class="dropdown">
<button class="dropbtn">MENU</button>
<div class="dropdown-content">
Link
Link
Link
</div>
</div>
</div>

I think that's what you want
#top-bar{
left: 0;
top: 0;
float: left;
width:100%;
height:40px;
background-color: black;
}
.dropbtn {
display: block;
float: right;
background-color: blue;
color: white;
height: 40px;
font-size: 12px;
border: none;
cursor: pointer;
}
.dropdown {
width: 100%;
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
float: left;
background-color: #f9f9f9;
width: 100%;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}
.dropdown-content a {
color: black;
padding: 10px 14px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {background-color: #f1f1f1}
.dropdown:hover .dropdown-content {
display: block;
width: 100%;
float: left;
left: 0;
}
.dropdown:hover .dropbtn {
background-color: blue;
}
<div id="top-bar">
<div class="dropdown">
<button class="dropbtn">MENU</button>
<div class="dropdown-content">
Link
Link
Link
</div>
</div>
</div>
https://jsfiddle.net/69uts0dr/3/

Related

Dropdown menu to the right

When you move the cursor over the "Catalog" button, sub-items drop down. However, when you hover over the "Clothes" sub-item, the pop-up menu to the right does not appear and in general something goes wrong. Why?
.dropdown {
position: absolute;
/* margin: -60px 0 0 200px; */
display: inline-block;
}
.dropbtn {
background-color: #4CAF50;
color: greenyellow;
padding: 15px;
border-radius: 5px;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 30px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropright-content {
position: absolute;
top: 0;
left: 0px;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropright-content a {
background-color: #f1f1f1;
}
.dropright-content a:hover {
background-color: #ddd;
}
.dropright:hover .dropright-content {
display: block;
}
<div class="dropdown">
<div class="dropbtn">≡ Catalog</div>
<div class="dropdown-content">Clothes
<div class="dropright-content">
Men's clothing
Women's clothing
</div>
Electronics
Books
</div>
</div>
Firstly, your drop-right content can't have left: 0. This places it directly over the first menu level.
Then, your hover selector was off. It needs to be something like .dropdown-content a:hover+.dropright-content, because there's a sibling relationship.
That's a problem, though, since once you leave the hovered anchor the sibling hides. So we need to restructure to create a child relationship. Something like this:
.dropdown {
position: absolute;
/* margin: -60px 0 0 200px; */
display: inline-block;
}
.dropbtn {
background-color: #4CAF50;
color: greenyellow;
padding: 15px;
border-radius: 5px;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1f1f1;
min-width: 30px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropright-content {
position: absolute;
top: 0;
left: 100%;
color: black;
padding: 12px 16px;
text-decoration: none;
display: none;
}
.dropright-content a {
background-color: #f1f1f1;
}
.dropright-content a:hover {
background-color: #ddd;
}
.inner-dropdown:hover .dropright-content {
display: block;
}
<div class="dropdown">
<div class="dropbtn">≡ Каталог</div>
<div class="dropdown-content">
<div class="inner-dropdown">
Одежда
<div class="dropright-content">
Мужская одежда
Женская одежда
</div>
</div>
Электроника
Книги
</div>
</div>
CSS dropdowns have been used and discussed for ages. You might look into one of the many good examples to see how you can simplify your structure and CSS. Ideally you don't have separate style rules for the outer and inner drop panels.

Centering Drop Down Button

I am a newbie and just learn about coding. I got stuck on a pretty simple thing I guess. How do I center a drop down button in a page? This is the HTML code.
.dropbtn {
background-color: #04AA6D;
color: white;
padding: 16px;
font-size: 16px;
border: none;
min-width: 110px;
font-weight: bold;
vertical-align: middle;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: center;
background-color: #f1f1f1;
min-width: 110px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
text-align: center;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {background-color: #ddd;}
.dropdown:hover .dropdown-content {display: block;}
.dropdown:hover .dropbtn {background-color: #3e8e41;}
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
I did not use any CSS or JavaScript for this drop down button.
Should I also use CSS and JavaScript for this code to run well?
Please kindly advise.
Thank you.
You can change the position of the drop down to absolute and set the left and top.
.dropdown {
position: absolute;
top: 50%;
left: 50%;
display: inline-block;
transform: translate(-50%,-50%)
}
You can use:
body {
display: flex;
justify-content: center;
}
(
In a real world scenario you'll be applying this to the container element and not to entire body)
body {
display: flex;
justify-content: center;
}
.dropbtn {
background-color: #04AA6D;
color: white;
padding: 16px;
font-size: 16px;
border: none;
min-width: 110px;
font-weight: bold;
vertical-align: middle;
margin: 0 auto;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: center;
background-color: #f1f1f1;
min-width: 110px;
box-shadow: 0px 8px 16px 0px rgba(0, 0, 0, 0.2);
z-index: 1;
text-align: center;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropbtn {
background-color: #3e8e41;
}
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
Also you can use this simple method too:
.dropdown {
position: relative;
display: inline-block;
text-align: center;
width: 100%;
}
.dropbtn {
background-color: #04AA6D;
color: white;
padding: 16px;
font-size: 16px;
border: none;
min-width: 110px;
font-weight: bold;
vertical-align: middle;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: center;
background-color: #f1f1f1;
min-width: 110px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
text-align: center;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {background-color: #ddd;}
.dropdown:hover .dropdown-content {display: block;}
.dropdown:hover .dropbtn {background-color: #3e8e41;}
.dropdown{
display:flex;
flex-direction:column;
justify-content:center;
align-items:center;
border:solid 2px green;
height:100vh;
}
<!DOCTYPE html>
<html>
<div class="dropdown">
<button class="dropbtn">Dropdown</button>
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</div>
</html>

I can move text 50px from left

I have navigation bar. I want to move the text 50px from left. I can do this?
.navigation {
width: 100%;
height:35px;
background-color: #f1b500; /*f2c255*/
margin-top: 4.4em;
}
.navigation a {
float: left;
display: block;
color: white;
text-align: center;
padding: 8px 25px;
text-decoration: none;
font-size: 100%;
}
.navigation .icon {
display: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 100%;
border: none;
outline: none;
color: white;
padding: 8px 25px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1b500;
min-width: 120px;
box-shadow: 0px 8px 16px 9px rbga(0,0,0,0.2)
z-index: 1;
}
.dropdown-content a {
float: none;
color: white;
padding: 8px 15px;
text-decoration: none;
display: block;
text-align: inherit;
}
.navigation a:hover, .dropdown:hover .dropbtn {
background-color: #34b0ff;
color: black;
}
.dropdown-content a:hover {
background-color: #34b0ff;
color: black;
}
.dropdown:hover .dropdown-content {
display: block;
}
The menu text is now all the way to the left. I would like to move it 50 pixels further to the right. Is the code wrong or do you just need to add something? I tried adding margin-left: 50px; but it does not work. I state that I am a beginner on programming.
<div class="navigation" id="Nav">
Home
<div class="dropdown">
<button class="dropbtn">Dropbutton
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link1
Link2
Link3
</div>
</div>
Test
Test
</div>
This is my html code. I forgot to post it
add padding-left:50px to .navigation. and everything will move to the right 50px
.navigation {
width: 100%;
height:35px;
background-color: #f1b500; /*f2c255*/
margin-top: 4.4em;
padding-left:50px;
}
.navigation a {
float: left;
display: block;
color: white;
text-align: center;
padding: 8px 25px;
text-decoration: none;
font-size: 100%;
}
.navigation .icon {
display: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropdown .dropbtn {
font-size: 100%;
border: none;
outline: none;
color: white;
padding: 8px 25px;
background-color: inherit;
font-family: inherit;
margin: 0;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f1b500;
min-width: 120px;
box-shadow: 0px 8px 16px 9px rbga(0,0,0,0.2)
z-index: 1;
}
.dropdown-content a {
float: none;
color: white;
padding: 8px 15px;
text-decoration: none;
display: block;
text-align: inherit;
}
.navigation a:hover, .dropdown:hover .dropbtn {
background-color: #34b0ff;
color: black;
}
.dropdown-content a:hover {
background-color: #34b0ff;
color: black;
}
.dropdown:hover .dropdown-content {
display: block;
}
<div class="navigation" id="Nav">
Home
<div class="dropdown">
<button class="dropbtn">Dropbutton
<i class="fa fa-caret-down"></i>
</button>
<div class="dropdown-content">
Link1
Link2
Link3
</div>
</div>
Test
Test
</div>
yeah change your padding to 8px 25px 0px 50px
Either adding padding-left: 50px; or transform: translateY(50px);

Dropdown behind iframe?

I'm trying to get my dropdown menu to stay above iframe elements on a page. I know this question is asked often, but my issue is that my iframe goes out of whack when I set the position to relative rather than absolute, so (as far as I can tell) I can't use z-index. I know my code is pretty messy, I'm essentially just trying to keep the video in the center of the page at the same proportions while also maintaining responsiveness, and then keep the dropdown above the video.
Here's my CSS
.dropbtn {
background-color: inherit;
font-family: 'Inconsolata', monospace;
color: red;
font-size: 18px;
border: none;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: relative;
min-width: 50px;
background-color: white;
padding: 10px;
}
.dropdown-content a {
text-decoration: none;
display: block;
margin-top: 10px;
}
.dropdown-content a:hover {
color: black;
text-shadow: 1px 1px rgba(256, 0, 0, .4);
}
.dropdown:hover .dropdown-content {display: block;}
.dropdown:hover .dropbtn {background-color: white;}
Here's the HTML
<nav>
<a class="logo" href="index.html"></a>
<div id="navbar">
<span class="dropdown">
<button class="dropbtn">work</a></button>
<div class="dropdown-content">
video
design
photo
</div>
</span>
about
submit
store<br>
</div>
</nav>
<style>
#vidframe {
position: relative;
overflow: hidden;
padding-top: 56.25%;
}
#vid {
position: absolute;
top: 0;
left: 0;
width: 60%;
height: 60%;
margin-left: 20%;
border: 0;
}
</style>
<body>
<div id="vidframe">
<iframe id="vid" src="https://www.youtube.com/embed/zb_m_ZomwHA" frameborder="0"></iframe>
</div>
</body>
Here is a simple solution based on your code:
.dropbtn {
background-color: inherit;
font-family: 'Inconsolata', monospace;
color: red;
font-size: 18px;
border: none;
}
.dropdown {
position: relative;
display: inline-block;
}
.dropdown-content {
display: none;
position: absolute;
min-width: 50px;
background-color: white;
padding: 10px;
z-index: 2;
}
.dropdown-content a {
text-decoration: none;
display: block;
margin-top: 10px;
}
.dropdown-content a:hover {
color: black;
text-shadow: 1px 1px rgba(256, 0, 0, .4);
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropbtn {
background-color: white;
}
#vidframe {
overflow: hidden;
text-align: center;
position: relative;
padding-bottom: 56.25%;
padding-top: 25px;
height: 0;
z-index: 1;
}
#vid {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
}
<nav>
<a class="logo" href="index.html"></a>
<div id="navbar">
<span class="dropdown">
<button class="dropbtn">work</button>
<div class="dropdown-content">
video
design
photo
</div>
</span>
about
submit
store
<br>
</div>
</nav>
<div id="vidframe">
<iframe id="vid" src="https://www.youtube.com/embed/zb_m_ZomwHA" frameborder="0"></iframe>
</div>

How do I make my CSS dropdown menu color scheme differentiate from the body color scheme?

So, my newer index is here, but the DropDown link list(when you hover over the link stuff you will see it) on the blog portion of my website which is here does not change. Any help would be nice. This is the menu bar for my website with the link list that is set up as a widget, since it's a blogger blog attached to the website itself:
<style type="text/css">
ul{
list-style-type: none;
margin: 0px;
padding: 150px;
overflow: hidden;
background-color: transparent;
position: absolute;
top: -79px;
left: -703.8px;
width: 940px;
font-size:19px;
font-family: Geo;
}
li {
float: left;
text-align: center;
color: #FF733B;
background-color: transparent;
}
li a {
float: left;
display: block;
padding: 20px;
color: #F2F2F2;
transition: .5s;
text-decoration: none;
background-color: transparent;
}
.dropbtn {
background-color: transparent;
color: #efefef;
padding: auto;
font-size: 12px;
border: none;
cursor: pointer;
}
.dropdown {
float: left;
display: relative;
font-color: #000;
}
.dropdown-content {
display: none;
position: fixed;
background-color: #f9f9f9;
min-width: 260px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
top: 65px;
z-index: 4;
}
.dropdown-content a {
color: #000;
padding: 8px 10px;
text-decoration: none;
display: block;
}
.dropdown-content a:hover {
transition: .4s;
background-color: #f1f1f1;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown:hover .dropbtn {
background-color: transparent;
}
</style>
<p align="center">
<div class="table">
<ul>
<li>Home</li><li>DATA</li><li>About</li><div class="dropdown">
<li class="dropbtn"><li>Stuff</li>
<div class="dropdown-content">
NIGHTCORE Creator
Artwork
BLOG
</div></li></div></ul></div></p>
<img style="float: right; margin: -69.9px -8.5px 0px 75px; height: 125.5%; width: 125.5%;" src="http://www.substructures.us/substructures2.png" />