CSS3 - Display div h3 elements horizontally on hover with absolute position - html

I am trying to display h3 elements of parent div on hover horizontally,
as shown in screenshot below:
If I try position: absolute, all the h3 elements are collapsing, which results in another issue: I am not able to hover and get the list of h3, as they are collapsed.
codepen url :https://codepen.io/divyar34/pen/PpjrYO
HTML:
<ul>
<li>
aaa
<div>
<div>
<h3>111</h3>
<ul>
<li>zz1</li>
<li>zz2</li>
</ul>
</div>
<div>
<h3>1112</h3>
<ul>
<li>zz11</li>
<li>zz21</li>
</ul>
</div>
<div>
<h3>1113</h3>
<ul>
<li>zz1</li>
<li>zz2</li>
</ul>
</div>
</div>
</li>
<li>
bb
<div>
<div>
<h3>2220</h3>
<ul>
<li>yy</li>
<li>yyyy</li>
</ul>
</div>
<div>
<h3>2221</h3>
<ul>
<li>yy1</li>
<li>yyyy1</li>
</ul>
</div>
</div>
</li>
<li>
cc
<div>
<div>
<h3>3330</h3>
<ul>
<li>ccccc</li>
<li>dddd</li>
</ul>
</div>
<div>
<h3>3331</h3>
<ul>
<li>ccccc</li>
<li>dddd</li>
</ul>
</div>
<div>
<h3>3332</h3>
<ul>
<li>ccccc</li>
<li>dddd</li>
</ul>
</div>
<div>
<h3>3333</h3>
<ul>
<li>ccccc</li>
<li>dddd</li>
</ul>
</div>
</div>
</li>
</ul>
CSS:
a{
text-decoration:none;
padding: 5px;
border-radius: 10px;
color:white;
background:rgb(0,176,240);
border:none;
}
ul li{
list-style-type:none;
display:inline;
cursor:pointer;
float: left;
width: auto;
padding-left: 6px;
}
ul li:hover a{
background:orange;
}
ul li:hover ul{
width: 100%;
visibility: visible;
}
ul li ul{
display:block;
padding-left:5px;
padding-top:5px;
visibility: hidden;
position: absolute;
left: 0;
line-height: 40px;
}
div ul{
width:100%
}
ul li div{
visibility: hidden;
position:absolute;
}
ul li div div ul{
display:none;
}
ul li:hover div{
visibility: visible;
}
ul li div div{
display:block;
}
ul li ul li div div h3{
display:inline;
text-decoration:none;
padding: 5px;
border-radius: 10px;
color:white;
background:rgb(0,176,240);
border:none;
}

MAJOR EDITS: to accommodate the OP's added info on the issue
First of all, you are using a lot of nested div and ul, so you should consider using classes to avoid unnecessary confusion. Alternatively, If using classes affects some functionalities, then make use of CSS Selectors, instead.
Run the working snippet below, or
Use this JSFiddle if you want to play around, further.
/* New plus Modified CSS */
/*General CSS*/
li{
list-style-type:none;
display:inline;
cursor:pointer;
float: left;
width: auto;
padding-left: 6px;
}
li a{
text-decoration:none;
padding: 5px;
border-radius: 10px;
color:white;
background:rgb(0,176,240);
border:none;
}
li a + div div,
h3 + ul li {
border: solid 1px grey;
display: inline-block;
margin: 10px 2px;
padding: 0 10px;
}
li a + div,
h3 + ul {
padding-left:5px;
padding-top:5px;
position: absolute;
left: 0;
line-height: 40px;
visibility: hidden;
}
/*END General CSS*/
/*Hover CSS*/
li:hover a{
background:orange;
}
li:hover a + div,
li a + div div:hover h3 + ul {
visibility: visible;
}
/*END Hover CSS*/
/* END New plus Modified CSS */
<ul>
<li>
aaa
<div>
<div>
<h3>111</h3>
<ul>
<li>zz1</li>
<li>zz2</li>
</ul>
</div>
<div>
<h3>1112</h3>
<ul>
<li>zz11</li>
<li>zz21</li>
</ul>
</div>
<div>
<h3>1113</h3>
<ul>
<li>zz1</li>
<li>zz2</li>
</ul>
</div>
</div>
</li>
<li>
bb
<div>
<div>
<h3>2220</h3>
<ul>
<li>yy</li>
<li>yyyy</li>
</ul>
</div>
<div>
<h3>2221</h3>
<ul>
<li>yy1</li>
<li>yyyy1</li>
</ul>
</div>
</div>
</li>
<li>
cc
<div>
<div>
<h3>3330</h3>
<ul>
<li>ccccc</li>
<li>dddd</li>
</ul>
</div>
<div>
<h3>3331</h3>
<ul>
<li>ccccc</li>
<li>dddd</li>
</ul>
</div>
<div>
<h3>3332</h3>
<ul>
<li>ccccc</li>
<li>dddd</li>
</ul>
</div>
<div>
<h3>3333</h3>
<ul>
<li>ccccc</li>
<li>dddd</li>
</ul>
</div>
</div>
</li>
</ul>

Related

Make dropdown child 100% of container

I have a dropdown inside a black container of 600px and i would like to have the red childrens show in a width of 100% of container instead of 100% of page width as it happens now. I need this to be in % because the website is responsive.
Please take a look at Jsfiddle Demo
<div id="navcontainer">
<ul>
<li style="display: inline;float: left;">
Filters
<ul style="text-align:left">
<li>Brand</li>
<li>Model</li>
<li>Color</li>
<li>Features</li>
</ul>
</li>
<li style="display: inline;float: right;">
Sort by
<ul style="text-align:right">
<li>Newest</li>
<li>Oldest</li>
<li>Cheapest</li>
</ul>
</li>
</ul>
</div>
add position:relative; for - #navcontainer ul
#navcontainer {
background:#222;
margin: 0 auto;
width: 600px;
}
#navcontainer ul {
text-align:center;
margin:0;
padding:0;
list-style:none;
width:100%;
position:relative;
}
#navcontainer ul li {
display:inline;
/*position:relative;*/
}
#navcontainer ul li a {
display:inline-block;
color:#fff;
font-size:20px;
padding:20px 30px;
}
#navcontainer ul li a:hover {
background:#999;
}
#navcontainer ul ul {
position:absolute;
left:0;
top:100%;
z-index:10;
width:100%;
display:none;
background:red;
}
#navcontainer ul ul li {
float:none;
}
#navcontainer ul ul li a {
text-align:left;
}
#navcontainer ul li:hover ul {
display:block;
}
<div id="navcontainer">
<ul>
<li>
Eggs
<ul>
<li>sub menu</li>
<li>sub menu</li>
<li>sub menu</li>
<li>sub menu</li>
</ul>
</li>
</ul>
</div>
you can use position attribute for many diffrent uses but there you need to hava a contained sub menu and you cant use position:absolute; for parent element
you can use it for child elements and here you do it for both of theme .
change this line to solve your problem .
#navcontainer ul ul {
position:relative;
}
sorry for my bad language skils .
The above description and view the demo you away into a problem solved
You should add the parent element the following characteristics :
#navcontainer {
background: rgb(34, 34, 34) none repeat scroll 0 0;
border: 1px solid;
height: 65px;
margin: 0 auto;
position: relative;
text-align: center;
width: 600px;
}

How to align logo next to navigation?

This is what it looks like:
How would I correctly vertically align the navigation with the logo?
HTML:
<div class="menu-nav">
<ul>
<li>Logo</li>
<li>Home</li>
<li>Chat Now</li>
<li>Mobile Chat</li>
<li>Report User</li>
</ul>
</div>
CSS:
.menu-nav {
}
.menu-nav ul li {
display: inline-block;
}
.menu-nav ul li a{
color: #fff;
margin: 10px;
}
.menu-nav ul a.mg {
}
.menu-nav ul li a.logo {
background: url("../img/logo.png") no-repeat;
text-indent: -9999px;
height:60px;
display:block;
width:215px;
padding: 0;
}
You can add a display method named table to the ul tag and then include a display method table-cell with vertical-align:middle to effectively, vertically align your links to the logo.
HTML:
<div class="menu-nav">
<ul class="a">
<li>Logo
</li>
<li class="navItem">Home
</li>
<li class="navItem">Chat Now
</li>
<li class="navItem">Mobile Chat
</li>
<li class="navItem">Report User
</li>
</ul>
</div>
CSS:
.a{
display:table;
}
.a li{
display: table-cell;
vertical-align:middle;
}
Which is better seen through this JSFiddle
EDIT:
You can update your margin by giving the li tags a class and calling that specific class. This can also be found in the JSFiddle above and you can play with it a bit as well.
Please try below code i have just added line-height:60px; for .menu-nav ul li a
HTML :
<div class="menu-nav">
<ul>
<li>Logo</li>
<li>Home</li>
<li>Chat Now</li>
<li>Mobile Chat</li>
<li>Report User</li>
</ul>
</div>
CSS:
.menu-nav {
}
.menu-nav ul li {
display: inline-block;
}
.menu-nav ul li a{
color: #fff;
margin: 10px;
line-height:60px;
}
.menu-nav ul a.mg {
}
.menu-nav ul li a.logo {
background: url("../img/logo.png") no-repeat;
text-indent: -9999px;
height:60px;
display:block;
width:215px;
padding: 0;
}

Why wont this menu get aligned in the middle of the page?

I have been trying to create a menu that would be fixed in the middle of the page.
The menu is of 1170px in width and 30px in height with a blue line at the bottom.
Now....I'd like to make it a menu that is positioned in the middle of the page with the rest of the items on the page.
Align: center doesnt seem to work. What am I doing wrong?
<div align="center;" id="menu">
<ul>
<li>Nuestros Cursos
<ul>
<li>Spanish</li>
<li>English</li>
<li>French</li>
<li>German</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI-->
</ul> <!-- end main UL-->
<ul>
<li>Preparación de exámenes
<ul>
<li>Spanish</li>
<li>English</li>
<li>French</li>
<li>German</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI-->
</ul> <!-- end main UL-->
<ul>
<li>Estudiar en el Extranjero
<ul>
<li>Spanish</li>
<li>English</li>
<li>French</li>
<li>German</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI-->
</ul> <!-- end main UL-->
<ul>
<li>Quienes somos
<ul>
<li>Spanish</li>
<li>English</li>
<li>French</li>
<li>German</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI-->
</ul> <!-- end main UL-->
<ul>
<li>Trabaja con nosotros
<ul>
<li>Spanish</li>
<li>English</li>
<li>French</li>
<li>German</li>
</ul> <!-- end inner UL-->
</li> <!-- end main LI-->
</ul> <!-- end main UL-->
</div>
CSS
/*-----------------------new menu I am working on------------------------*/
#menu {
width:1170px;
height:30px;
text-align:center;
margin-left: auto; /*margin left es right fontosak olyan szinten hogy amit meretnek valasztok a menure...azt szepent automatikusan kozepre helyezi*/
margin-right: auto;
border-bottom:solid 3px #289dcb; /*#68bc1d szep zold szint jelent*/
border-radius: 10px 10px 0px 0px;
-moz-border-radius: 10px 10px 0px 0px;
-webkit-border-radius: 10px 10px 0px 0px;
}
#menu ul {
margin:0;
padding:0;
line-height:30px;
}
#menu li {
margin:0;
padding:0;
list-style:none;
float:left;
position:relative;
background:#3b3b3b; /*colours the menu and also the submenu background*/
}
#menu ul li a {
display:block;
height:30px;
width:200px;
text-align:center;
font-family:Arial, Helvetica, sans-serif;
text-decoration:none;
color: #FFF; /*colour of the text in the menu*/
border:1px solid #000;
}
#menu ul ul {
position: absolute; /* this is what orders the nested links to appear in a block under the main ul*/
visibility:hidden;
top:32px;
}
#menu ul li:hover ul { /* this is what makes the dropdown menu appear on hovering over it*/
visibility:visible;
}
#menu li:hover {
background:#ff5454; /* main menu box changes to this color (also the submenus) on hovering over it, red in this case*/
}
here is what it looks like: Demo js Fiddle
You simply need to add the following line to the css for #menu ul and it will center your menu.
display: inline-block;
Here is a modified version of your jsfiddle.
This CSS will do the trick. DEMO
I didn't use display: inline-block because it will give you gap between each <li>.
Instead i have used display: table and display: table-cell to make it align properly without any gaps in between list.
#menu {
width: 1170px;
height: 30px;
margin-left: auto;
margin-right: auto;
border-bottom: solid 3px #289dcb;
-moz-border-radius: 10px 10px 0px 0px;
-webkit-border-radius: 10px 10px 0px 0px;
display: table;
}
#menu ul {
margin: auto;
padding: 0;
line-height: 30px;
display: table-cell;
}
#menu li {
margin: 0;
padding: 0;
list-style: none;
float: left;
position: relative;
background: #3b3b3b;
width: 100%;
}
#menu ul li a {
display: block;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
text-decoration: none;
color: #FFF;
border: 1px solid #000;
}
You actually require modification to both your HTML and CSS:
Crucial points:
You were adding successive lists to #menu where you only need one, with child li for each menu item
text-align:center needed to be added to #menu and display:inline-block to #menu ul for centering to happen
Demo Fiddle
HTML
<div id="menu">
<ul>
<li>Nuestros Cursos
<ul>
<li>Spanish
</li>
<li>English
</li>
<li>French
</li>
<li>German
</li>
</ul>
</li>
<li>Preparación de exámenes
<ul>
<li>Spanish
</li>
<li>English
</li>
<li>French
</li>
<li>German
</li>
</ul>
</li>
<li>Estudiar en el Extranjero
<ul>
<li>Spanish
</li>
<li>English
</li>
<li>French
</li>
<li>German
</li>
</ul>
</li>
<li>Quienes somos
<ul>
<li>Spanish
</li>
<li>English
</li>
<li>French
</li>
<li>German
</li>
</ul>
</li>
<li>Trabaja con nosotros
<ul>
<li>Spanish
</li>
<li>English
</li>
<li>French
</li>
<li>German
</li>
</ul>
</li>
</ul>
</div>
CSS
#menu {
text-align: center;
}
#menu ul {
height: 30px;
display:inline-block;
margin: 0;
position:relative;
padding: 0;
line-height: 30px;
border-bottom: solid 3px #289dcb;
/*#68bc1d szep zold szint jelent*/
;
}
#menu li {
margin: 0;
padding: 0;
list-style: none;
float: left;
position: relative;
background: #3b3b3b;
/*colours the menu and also the submenu background*/
;
}
#menu a {
display: block;
height: 30px;
width: 200px;
text-align: center;
font-family: Arial, Helvetica, sans-serif;
text-decoration: none;
color: #FFF;
/*colour of the text in the menu*/
border: 1px solid #000;
}
#menu ul ul {
position: absolute;
/* this is what orders the nested links to appear in a block under the main ul*/
visibility: hidden;
top: 32px;
left:0;
}
#menu ul li:hover ul {
/* this is what makes the dropdown menu appear on hovering over it*/
visibility: visible;
}
#menu li:hover {
background: #ff5454;
/* main menu box changes to this color (also the submenus) on hovering over it, red in this case*/
;
}
Replace the given css and try, hope it will work fine
#menu {
width:1170px;
height:30px;
text-align:center;
margin-left: auto; /*margin left es right fontosak olyan szinten hogy amit meretnek valasztok a menure...azt szepent automatikusan kozepre helyezi*/
margin-right: auto;
border-radius: 10px 10px 0px 0px;
-moz-border-radius: 10px 10px 0px 0px;
-webkit-border-radius: 10px 10px 0px 0px;
}
#menu ul {
margin:0;
padding:0;
line-height:30px;
display:inline-block;
border-bottom:solid 3px #289dcb; /*#68bc1d szep zold szint jelent*/
}

submenus in css/html

I have got a submenu which expands from a nav menu type object when I hover over it. Right now, my main nav menu looks like so...
<div id= "navbar">
<ul>
<li><a href= "#" class= "navlink" id= "first"> First
<div class= "firstsubmenu">
<ul>
<li> <a href= "#" class="firstsubmenulink"> First sub menu option </li>
<li> <a href= "#" class="firstsubmenulink"> Second sub menu option </li>
etc...
</ul>
</div></a></li>
<li><a href= "#" class= "navlink" id="second"> Second
<div class= "secondsubmenu">
<ul>
..and so on
</ul>
</div>
Right now, my css is looking like
ul
{
list-style-type:none;
margin:0;
padding:0;
overflow:hidden;
}
li
{
float:left;
}
.navlink:link
{
display:block;
width:120px;
text-align:center;
padding:10px;
text-decoration:none;
color:#FFFFFF;
}
.navlink:hover
{
background-color:#ADD8E6;
color:#FFFFFF;
}
.navlink:visited
{
background-color:#ADD8E6;
color:#FFFFFF;
}
Before I tried making each item in the submenu a clickable link, everything showed up perfectly fine. IE: firstsubmenu showed up perfectly. It's css is
.firstsubmenu
{
display : none;
position : absolute;
left : 75px;
top : 32px ;
background-color : red;
width : 930px;
height : 25px;
z-index : 10;
}
But now that I added the links (made every list element within an block), firstsubmenu no longer appears.
The css for each link looked something like this
.firstsubmenulink
{
display:block;
width:120px;
text-align:center;
padding:10px;
text-decoration:none;
color:#FFFFFF;
}
But as I said, the submenu no longer even appears. I realize this is a bit of a long post, but any advice would be great.
You can use the below css and create pure css based menu.
Css:
body { padding: 3em; }
#navbar * { padding:0; margin: 0; font: 1em arial; }
#navbar { position: absolute; z-index: 99; margin: 0 auto; float: left; line-height: 20px; }
#navbar a { display: block; border: 1px solid #fff; background: #EFBE37; text-decoration: none; padding: 3px 10px; color:#666666; }
#navbar a:hover { background: #C6991D; }
#navbar ul li, #navbar ul li ul li { width: 120px; list-style-type:none; }
#navbar ul li { float: left; width: 120px; }
#navbar ul li ul, #navbar:hover ul li ul, #navbar:hover ul li:hover ul li ul{
display:none;
list-style-type:none;
width: 120px;
}
#navbar:hover ul, #navbar:hover ul li:hover ul, #navbar:hover ul li:hover ul li:hover ul {
display:block;
}
#navbar:hover ul li:hover ul li:hover ul {
position: absolute;
margin-left: 120px;
margin-top: -20px;
}
Structure:
<div id="navbar">
<ul>
<li>Home</li>
<li>Abous Us »
<ul>
<li>About us 1</li>
<li>About us 2 »
<ul>
<li>XXX
<li>XXX
<li>XXX
</ul>
</li>
</ul>
</li>
<li>Download</li>
<li>Menus »
<ul>
<li>Menus 1</li>
<li>Menus 2 »
<ul>
<li>Menus 2-1
<li>Menus 2-2
<li>Menus 2-3
</ul>
</li>
</ul>
</li>
<li>Contact</li>
<li>Feedback</li>
</ul>
jsBin live demo
I had to fix lot of errors in your HTML. Here is the css:
#navbar ul{
list-style:none;
margin:0; padding:0;
display:table;
}
#navbar li{
top:0px;
background:#bbf;
display:inline-block;
width:100px;
}
#navbar li ul li{
display:none;
}
#navbar li:hover li{
display:block;
}
And the fixed HTML:
<div id="navbar">
<ul>
<li>
First
<ul class="firstsubmenu">
<li>1. option</li>
<li>2. option</li>
</ul>
</li>
<li>
Second
<ul class="secondsubmenu">
<li>1. option</li>
<li>2. option</li>
</ul>
</li>
</ul>
</div>
Now, after it works, do with colors whatever you want.
Use also alt tags in your links and images, it improves your SEO and compilance.

How do i solve the issue of position of <ul>?

i created a style called "topbar" using css.
The code are as follows:
.topbar:hover ul{ display: inline;}
.topbar {
float: left;
margin-left: 20px;
margin-right: 20px;
font-family:"Georgia";
}
.topbar ul {
display: none;
top:30px;
position: absolute; border-style:solid;
border-width:1px; background-color:white;}
}
.clear {
clear: both;
}
then i went to create a ul "grid" and allows mouse hover enlarge of images
ul.grid, ul.grid > li {
margin: 0;
padding: 0;
}
ul.grid {
}
ul.grid > li {
float: left;
list-style-type: none;
text-align: center;
font-family:"Georgia", serif;
padding-top:50px;
padding-bottom:25px;
padding-right:25px;
padding-left:0px;
}
ul img:hover { width: 200px; height: 250px; }
my html code:
<body>
<div class="topbar">
<p>Title</p>
<ul>
<li>A-Z</li>
<li>Z-A</li>
</ul>
</div>
<div class="topbar">
<p>Genre</p>
<ul>
<li>Action</li>
<li>Comedy</li>
<li>Animation</li>
<li>Horror</li>
<li>Drama</li>
</ul>
</div>
<div class="clear"></div>
<br />
<div id="videos">
<ul class="grid">
<li>
<p class="image"><img src="http://3.bp.blogspot.com/-AGAaic1-yrM/TcWmj77lHzI/AAAAAAAAAkQ/K6zzSk1WgUY/s1600/thor-movie-poster-1.jpg" alt="Thor" width="175" height="200" /></p>
<p class="name">Thor</p>
<p class="genre">Action</p>
<p class="format">DVD</p>
<p class="year">2011</p>
</li>
<li>
<p class="image"><img src="http://www.galacool.com/wp-content/uploads/2010/02/hangover2.jpg" alt="Hangover" width="175" height="200" /></p>
<p class="name">Hangover</p>
<p class="genre">Comedy</p>
<p class="format">DVD</p>
<p class="year">2009</p>
</li>
</ul>
</div>
</body>
Why do i have a left unwanted space beside my "thor" movie image?
The left space must be the indentation of the List where you are putting them. Ideally, to remove this left-indentation, you should set both padding and margins to "0" for the "UL".
This small piece of code works perfectly in this context:
CSS
#navcontainer ul
{
margin: 0;
padding: 0;
list-style-type: none;
}
HTML
<div id="navcontainer">
<ul>
<li>Milk
<ul>
<li>Goat</li>
<li>Cow</li>
</ul>
</li>
<li>Eggs
<ul>
<li>Free-range</li>
<li>Other</li>
</ul>
</li>
</ul>
</div>