I have a horizontal scrolling menu with some of the main menu items having a dropdown menu. The problem is if I scroll the main menu the drop down menus do not follow (absolute positioning), or if I make them follow (relative) they push the main menu up.
Absolute:
Relative:
The CSS is:
.navbar {
width:100%;
overflow-x: auto;
overflow-y: hidden;
white-space: nowrap;
}
.dropdown-content {
display: none; //displayed on hover
position: absolute; //or relative
background-color: #f9f9f9;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
Any suggestions as to what I can do to fix this?
What I am looking for is the absolute version - notice the drop down menu drops over the scroll bar, not pushing it down - but with the sub menu properly aligned.
See jsfiddle for example of absolute postioning:
https://jsfiddle.net/9hjgo1qc/7/
Just add vertical-align:top; on .dropdown class style. Fixes your problem.
Test it here
Update 1
Used JQuery to fix the problem.
See here
Update 2
You can achieve same thing for multiple menus with minor change in jquery method.
See here
Like I already said in the comment, you don't actually need any kind of positioning to achieve this. It's just a matter of good HTML code. I have already provided a codepen example, but it was anonymus and someone had rewritten it.
You can achieve exactly what you want in just a few lines of code. I have started editing your jsfiddle, but figured I have already given you all the code needed in my codepen example :)
.navbar {
margin: 200px auto;
display: flex;
width: 800px;
overflow-x: scroll;
}
ul {
min-width: 250px;
list-style: none;
}
li {
display: inline-block;
text-align: center;
height: 40px;
line-height: 40px; /* quick hack to center text vertically (assuming it is just one line), but I wouldn't use it for production - it is just for this quick example */
background: beige;
margin: 0;
border: 1px solid white;
}
ul li:not(:first-child) {
height: 0;
transition: all .3s ease-in;
visibility: hidden;
/* alternatively you can use transform and scaleY
transform: scaleY(0);
transform-origin: 50% 0; */
}
ul:hover li:not(:first-child) {
height: 40px;
visibility: visible;
/* transform: scaleY(1); */
}
<div class="navbar">
<ul>
<li>M1</li>
<li>M1-1</li>
<li>M1-2</li>
</ul>
<ul>
<li>M2</li>
<li>M2-1</li>
<li>M2-2</li>
</ul>
<ul>
<li>M3</li>
<li>M3-1</li>
<li>M3-2</li>
</ul>
<ul>
<li>M4</li>
<li>M4-1</li>
<li>M4-2</li>
</ul>
<ul>
<li>M5</li>
<li>M5-1</li>
<li>M5-2</li>
</ul>
</div>
This is dummy code, but I think you can translate it to your example, easily. Let me know if you need me to explain this further.
Related
I want to create a navbar with the name of the store. For a project of a course, I managed to make the navbar work, I just need to place its elements. However, the name of the store is written as in its logo, so it is an image (shrunk by code). I am having difficulty getting the text to be aligned in the middle with the image. As a result, I am left with the image and the navigation words below it, which I cannot center in the middle of the navbar.
Thanks for your help
Btw, the course staff said that I cant use flex.
Im Argentinian, so thats why some comments are in spanish
html code
<header>
<div class="navbar"><!-- Menú de Nav -->
<img src="./Img/Solo letras.png" alt="Nombre Dernocua">
<nav>
<ul class="Links">
<li>Contacto</li>
<li>Nosotros</li>
<li>Inicio</li>
<li>Productos</li>
<li>Ubicación</li>
</ul>
</nav>
</div>
</header>
Css code
/* Controla tam img */
img {
max-width: 100%;
max-height: 100%;
object-fit: cover;
}
/* header (navbar) */
.Logo {
display: block;
align-items: center;
height: 100px;
width: 200px;
}
header {
position: fixed;
width: 100%;
height: 10%;
padding: 10px 30px;
background-color: #DDA15E;
transition: all .50s ease;
}
.Links a {
align-items: center;
color: #283618;
font-size: 1.5rem;
font-weight: 200;
padding: 5px 0;
margin: 0px 30px;
transition: all .50s ease;
}
.Links li {
display: inline;
padding-right: 5px;
}
.Links a:hover {
text-shadow: #283618 1px 1px;
color: #ff7b08;
I tried everything I know.
Vertical aling: center
top: #px
left: #px
I dont remember everything, bcs Im stuck in this part since yesterday.
I was hoping to have the img on the side, as it is positioned at the moment. And align the navbar options in the center. Their location would be secondary, but I was also hoping to put them in the middle of the navbar.
You can use flexbox for this, it is as simple as that.
.navbar {
display: flex;
align-items: center; /* flex property for vertical alignment */
}
I made a simple HTML page with a simple drop-down menu (made with CSS). I have a problem with the drop-down text transparency: it shows the bottom text (as explained in the image below). The text in the menu is a link, so it's included in a tag. I tried to change the text color and opacity property but doesn't solve the problem. Any idea?
.menu {
margin-right: 0px;
margin-left: -40px;
}
li {
display: block;
float: left;
width: 25%;
height: 50px;
line-height: 50px;
font-family: Helvetica, sans-serif;
font-size: 20px;
text-align: center;
color: white;
background-color: #000000;
}
.pat:hover {
color: #EC008C;
}
.l {
width: 100%;
height: 50px;
margin-left: -20px;
}
.sub-menu {
visibility: hidden;
}
.menu .pat:hover .sub-menu {
visibility: visible;
}
<ul class="menu">
<li>Home</li>
<li class="pat">Patenti
<ul class="sub-menu">
<li class="l">Patente A</li>
<li class="l">Patente B</li>
<li class="l">Patenti superiori</li>
</ul>
</li>
<li>News</li>
<li>Contatti</li>
</ul>
Instead of visibility: hidden; and visibility: visible; on hover for submenus, it's better to use display: none and display: block, and use position: absolute on the submenu and position: relative on its parent menu entry.
The reason display: none doesn't take any space (and position: absolute will prevent the visible submenu to change the design of the main menu entries), whereas visibility: hidden; reserves the space for the hidden element and just makes it invisible, which makes it impossible to properly position it independently of objects which it should cover when visible.
Just add position: relative to .menu. It will create a stacking context causing it to appear above body text.
Here is a modified pen. I have rewritten all rules but only the last one is important.
My menu is using a series of nested <ul> tags with sub <ul> and <li> within existing <li> calls. This is pretty standard for most menu's are far as I am aware. To make my website work well on mobile devices, I have decided to take a more responsive design approach and use #media query's to show or hide content as the browser is resized.
When my menu is shrunk down, the drop downs are hidden (for now). I will most likely introduce a hamburger type menu later on where you can click on a + icon beside the menu on the mobile version and see it's sub links.
As of right now, when the browser is resized the menu is then ordered as an absolute display and this disables the scrolling on the menu. If a user, say on an iPhone, turns the phone sideways, the menu is then cut off and they cannot scroll down to see the rest of the menu in this orientation. Here is an image to depict the issue:
And here is an example of the nested html code:
<div id="navmenu">
<ul>
<li><div class="home"></div></li>
<li class='storelink dropdown'><a class='dropbtn' href='/store'>Store</a>
<div class='dropdown-content'>
<a href='/store?group=1'>All Items</a>
</div>
</li>
<li class='cartmenu'><a href='/cart'>Cart</a></li>
<li class='accountmenu dropdown'><a class='dropbtn' href='/account'>Account</a>
<div class='dropdown-content'>
<a href='/account'>Customer login</a>
</div>
</li>
<li><a href='/services'>Services</a></li>
</ul>
</div>
and finally the relevant css
#navmenu .dropdown:hover .dropdown-content {
display: block;
position: absolute;
}
#navmenu {
overflow-y: scroll;
display: none;
clear: both;
background: #333;
left: 0;
position: absolute;
float: none;
margin: 0;
padding: 0;
width: 100%;
border-bottom: 2px solid #fff;
}
#navmenu ul {
width: 100%;
text-align: center;
}
#navmenu li {
float: none;
clear: both;
background: #333;
width: 100%;
text-align: center;
}
#navmenu li a {
display: block;
width: 100%;
clear: both;
margin: 0;
padding: 10px 0;
}
Why won't this scroll when resized? Thanks
Try adding this:
#navmenu {
height: 100%;
...
}
Hmm, you can try giving your #navmenu a fixed height.
Alternatively, you can try setting #navmenu to position:fixed (not absolute) and height:100%.
I'm trying to create a horizontal menu with dropdown content boxes. I'm using the same method I'd use for a vertical menu with children that expand on :hover of their parent. It works fine, except that I can't seem to find a method that forces the dropdown content to stick around once the cursor moves from the parent element itself. You can see what I mean at http://asubtleweb.com/clients/kingswood/ ... The dropdown content isn't clickable because it contracts as soon as the mouse moves from its parent element.
Here's my CSS:
#header_menu nav { display: table; width: 30%; float: left; text-align: center; }
#header_menu nav ul, nav#mainmenu ul { list-style-type: none; }
#header_menu nav li { display: inline; margin-right: 2.5%; }
#header_menu nav a, nav#mainmenu a { font: 400 1.25em 'Oswald', sans-serif; color: black; text-transform: uppercase; }
#header_menu li .navhover { display: block; width: 100%; position: absolute; top: 50px; left: 0px; background-color: black; background-color: rgba(0,0,0,0.6); color: white; text-align: left; max-height: 0px; overflow: hidden; transition: all 1s linear; -wekbkit-transition: all 1s linear; }
#header_menu li:hover .navhover { max-height: 300px; min-height: 300px; }
#header_menu li .navhover article { margin: 20px; }
#header_menu li .navhover.news article { width: 30%; margin: 2.5% 0% 2.5% 2.5%; float: left; }
...and my HTML:
<div id="header_menu">
<nav>
<ul>
<li id="mission">
Mission
<div class="navhover">
[[CONTENT]]
</div>
</li>
<li id="news">
News
<div class="navhover news">
[[CONTENT]]
</div>
</li>
<li id="reserve">Reserve</li>
</ul>
</nav>
</div>
I've also tried making each parent element its own absolutely positioned block that expands on :hover, with no luck. I didn't expect to have so much trouble with the concept, but it's stumping me.
Simply add z-index: 1; to #header_menu li .navhover
As Francesco Frapporti pointed out:
Simply add z-index: 1; to #header_menu li .navhover
Edit: There is an error on your website moveWindow is not defined see body element
Simply add this to your css....
#header_menu li .navhover:hover{min-height:0;max-height:0;}
then it should work just fine!
The problem is that the parent element you are using with :hover doesn't actually "touch" the child element. So there is space between the parent and child element and that causes the parent element to lose focus when moving the mouse toward the child.
Choose whichever method you like to eliminate the gap between parent and child. Make the parent larger, move the child closer to the parent, etc.
I know there have been many questions about ul auto indents, I've tried with no success many of those answers and I have the additional problem of when adding a container with padding, it seems to only pad one side.
Basically I'm trying to get a ul bar fixed to the bottom of the window. This bar is inside a container (main) to give it padding from either side of the window). It is designed to auto expand with the width of the window (liquidish) so there is no defined width beyond the initial width=device width.
When written by itself, I get a small auto indention on the left side of the ul. I've tried adding 0 padding and margins to the ul and li elements and that seems to have no effect as well as others. When I add in the container it seems to pad only the side with the indent problem. I'm sure its something simple and stupid, but I appreciate any help.
As an aside.. the width of the li elements being 33.3% is due to using a spacing box hack found on stackoverflow. The use of it doesn't change/help/hurt anything in this problem, so I omitted it to keep this a little more simple.
JSFiddle Link http://jsfiddle.net/XdHXf/1/
HTML
<div class="main">
<nav>
<ul>
<li>Main</li>
<li>Second</li>
<li>Third</li>
</ul>
</nav>
</div>
CSS
.main{
width: 100%;
padding: 0 1em;
}
nav{
position: fixed;
bottom: 0px;
background: #455868;
width: 100%;
z-index: 1000;
}
nav ul {
list-style: none;
width: 100%;
padding: 0;
margin: 0;
}
nav li{
position: relative;
float: left;
width: 33.3%;
text-align: center;
background: #455868;
padding: 0;
margin: 0;
}
nav li a{
text-decoration: none;
display: inline-block;
width: 100%;
padding: 15px 0;
}
This comes from the browser's builtin stylesheet. If you add
body {
margin: 0;
}
the small extra space goes away.
As an alternative, you get similar results, when you check Normalized CSS under Fiddle Options.