Make an element move by hovering over another element - html

I would like to hide the nav element totally from the user. When the user hovers over an icon (in this case, an image), the nav element should slide in from the top of the screen. I thought about a countdown of 0.5 seconds before the animation starts, then 1 second when the nav element moves from its initial position (above the screen) to its final position (at the top of the screen). The nav bar should stay there until the user stops hovering over the icon or the nav bar.
The problem is that my nav element appears in its final position when the icon is hovered - it does not move as it should do. From tests I've done so far, it seems that I cannot move an element when hovering over another, but I hope I'm wrong and that it's possible - I explored an option but it ran the animation in a loop and I could only get it working once, nor moving from location A to location B as it was moving from A to B to A. I tried using margins, paddings, changing height, I browsed many topics from here and other websites, but I could not find a way, probably because my code is a mess. I am learning JS so I am open to all solutions.
Here is my code, as cleaned as possible:
<header>
<div class="thing">
<img src="img/mainicon.png" class="mainicon">
<nav>
<ul>
<li><a class="menu" href="#">Half-Life</a></li>
<li><a class="menu" href="#">Half-Life 2</a></li>
<li><a class="menu" href="#">Half-Life Alyx</a></li>
</ul>
</nav>
</div>
<div class="clean"></div>
</header>
.mainicon {
width: 8%;
margin-left: 2%;
margin-top: 2%;
float: left;
}
.mainicon:hover + nav {
display: flex;
margin-top: 0px;
}
nav:hover {
display: flex;
margin-top: 0px;
}
.clean{
clear: both;
content: ".";
display: block;
height: 0;
line-height: 0;
visibility: hidden;
}
nav {
height: 100px;
display: none;
justify-content: center;
align-items: center;
border: 4px solid #2CBCD6;
background-color: grey;
text-align: center;
}
ul {
padding: 0;
width: 100%;
}
li {
display: inline;
font-size: 25px;
}
a {
outline: none;
text-decoration: none;
display: inline-block;
width: 20%;
margin-right: 0.625%;
text-align: center;
line-height: 3;
color: black;
}

It's simpler than you think. Don't complicate it for yourself.
In short, all you need to do is set the margin-top to something negative to hide it and then margin-top to 0 when hovering over the image or the menu.
Here is a demonstration:
body {
margin: 0;
background-color: aqua;
}
.thing {
display: flex;
align-items: center;
}
.mainicon {
position: fixed;
margin-top: 0px;
top: 10;
left: 60px;
height: 40px;
width: auto;
object-fit: contain;
background-color: bisque;
}
.mainicon:hover + nav,
nav:hover {
margin-top: 0px;
}
nav {
background-color: grey;
display: flex;
height: 60px;
width: 100%;
justify-content: center;
align-items: center;
margin-top: -60px;
transition: all 0.3s ease-in-out;
}
ul {
padding: 0;
margin: 0;
display: flex;
}
li {
list-style-type: none;
width: 100px;
}
a {
outline: none;
text-decoration: none;
}
<header>
<div class="thing">
<img
src="https://cdn1.iconfinder.com/data/icons/heroicons-ui/24/menu-512.png"
class="mainicon"
/>
<nav>
<ul>
<li><a class="menu" href="#">Half-Life</a></li>
<li><a class="menu" href="#">Half-Life 2</a></li>
<li><a class="menu" href="#">Half-Life Alyx</a></li>
</ul>
</nav>
</div>
</header

Related

Aligning all text to the right besides the first child

I'm trying to align all navigation links, besides the logo, to the right side of the container/navigation. I want to keep 1rem margin on both sides so that the content has some space to breathe.
I've tried using the code below but nothing on the page changes:
.menu:not(:first-child){
text-align: right;
}
<body>
<div class="body-wrap">
<header class="header">
<nav role="navigation">
<ul class="menu">
<li class="home-link"><img src="https://www.nicolefenton.com/_/images/dec/circle-menu.svg" height="12" width="12" alt=""></li>
<li>About</li>
<li>Writing</li>
<li>Speaking</li>
<li>Projects</li>
</ul>
</nav>
</header>
</div>
</body>
* { box-sizing: inherit; margin: 0; padding: 0; }
body {
position: relative;
line-height: 1.5em;
min-width: 320px;
margin: 0 auto;
color: #222222;
border: 30px solid #ffffff;
background-color: #f8f7f3;
}
.body-wrap {
display: flex;
min-height: 100vh;
display: box;
}
.header {
width: 100%;
max-width: 960px;
margin-right: 1rem;
margin-left: 1rem;
}
.menu {
display: flex;
position: absolute;
top: -0.83rem;
width: 100%;
max-width: 960px;
}
.menu:not(:first-child){
text-align: right;
}
li {
flex-grow: 1;
position: relative;
margin-right: 1em;
display: inline-block;
}
I expect all the nav links to align to the right when using the :not(:first-child) selector.
This:
.menu:not(:first-child)
selects class menu items that aren't a first child.
What you want is:
.menu :not(:first-child)
which selects non-first-child elements within a .menu class.
Notice the space.
Or better yet, make it more obvious what you really mean:
.menu li:not(:first-child)
You might just have to change to this if all you are looking to do is align the text to the right.
.menu li:not(:first-child){
text-align: right;
}

How do I position an List element to the absoulute center of the Page?

basically i think its an easy task but i cannot get it right
I have a navigation bar with 5 elements. The third one, which is an round image should be in the center of the webbpage. but because the 4th list element is slightly longer (the word means opening hrs) it does shift to the right a bit. So altough my list (navigation bar) is in the middle of the webpage, the logo itself isnt. I tried a lot but couldnt figure it out.
heres the fiddle:
<header>
<nav>
<ul class="navi">
<li>Home</li>
<li>Gallerie</li>
<li><img class="logo" src="https://upload.wikimedia.org/wikipedia/commons/7/72/Turkey_emblem_round.png" href="#" alt="LS-Lounge"></li>
<li>Öffnungszeiten</li>
<li>Kontakt</li>
</ul>
</nav>
</header>
css here:
nav {
width: 100%;
padding-top: 10px;
padding-bottom: 5px;
font-family: 'Lobster', sans-serif;
letter-spacing: 2px;
text-decoration: none;
background-color: #171717;
text-align: center;
}
ul .navi {
margin: 0 auto;
text-align: center;
}
.logo {
display: block;
height: 10vh;
width: auto;
margin: 0 auto;
padding: 0 5px;
}
.navi {
}
.navi li {
display: inline-block;
vertical-align: middle;
text-align: center;
padding: 0 20;
}
.navi li a{
color: white;
text-decoration: none;
font-size: 4vh;
position: relative;
}
https://jsfiddle.net/emw3b854/
Flexbox would work (You may need to remove the space in you css "ul .navi" should be "ul.navi"
ul.navi {
margin: 0 auto;
text-align: center;
display: flex;
}
.navi li {
display: inline-block;
vertical-align: middle;
text-align: center;
padding: 0 20;
flex-grow: 1;
}
Added demo:
https://jsfiddle.net/u0v6x5e3/

Centering a div container which is next to a second div container?

I've a div container which is named headline. In this div there are two elements, a menu bar of type unordered list and a div container. I'll centering horizontally the menu bar, the other div container should dock on the right display side with a margin of 5%. How I can do this, has someone an idea?
Okay here is my litte example from jsfiddle: http://jsfiddle.net/nchm3gyj/
HTML
<div class="headline">
<ul class="navbar">
<li>Home</li>
<li>Team</li>
<li>Info</li>
<li>Downloads</li>
</ul>
<img class="facebook" src="" />
</div>
CSS
* {
margin: 0px;
padding: 0px;
}
.headline {
height: 60px;
width: 100%;
background-color: black;
margin-top: 10px;
}
.headline .navbar{
margin: 0px;
padding: 0px;
padding-left: 10px;
padding-right: 10px;
float: left;
height: 60px;
width: auto;
background-color: yellow;
list-style: none;
}
.headline .navbar li{
display: inline;
}
.headline .navbar li a {
text-decoration: none;
line-height: 60px;
padding-left: 10px;
padding-right: 10px;
}
.headline .facebook {
width: 60px;
height: 60px;
margin-right: 5%;
float: right;
}
#clear {
clear: both;
}
If you want your navigation bar centered in the parent block, here is one way of doing it.
Apply display: inline-block to the .navbar and text-align: center to .headline.
Assuming that you want the navigation bar centered with respect to the full
width of the parent block, you need to take the image out of the content flow.
You can do this by applying position: absolute to the .facebook element.
.headline {
height: 60px;
width: 100%;
background-color: black;
margin-top: 10px;
text-align: center;
position: relative;
}
.headline .navbar{
margin: 0px;
padding: 0px;
padding-left: 10px;
padding-right: 10px;
height: 60px;
width: auto;
display: inline-block;
background-color: yellow;
list-style: none;
}
.headline .navbar li{
display: inline;
}
.headline .navbar li a {
text-decoration: none;
line-height: 60px;
padding-left: 10px;
padding-right: 10px;
}
.headline .facebook {
position: absolute;
top: 0;
right: 5%;
width: 60px;
height: 60px;
}
<div class="headline">
<ul class="navbar">
<li>Home</li>
<li>Team</li>
<li>Info</li>
<li>Downloads</li>
</ul>
<img class="facebook" src="http://placehold.it/60x60" />
</div>
I think you might need to position: absolute the facebook image and display: inline-block your menu bar (being centered by the .headline):
http://jsfiddle.net/nchm3gyj/32/
I'm a bit unsure of what you're trying to do, is this it? Applied text-align: center to .headline and display: inline-block to .navbar then position: absolute to .facebook?
http://jsfiddle.net/nchm3gyj/42/

Fill empty div with background and expand dimensions of parent a tag

I have a navigation which holds either text or images for links. I want the image to change on hover, so am using CSS backgrounds inside an empty div. However, I am looking for a way of doing this without using "position: absolute;" as the containing a tag will not expand to fill its dimensions. I would also like to do this without using a transparent placeholder image as I want to find a more elegant solution.
Here's the jsfiddle and the code:
http://jsfiddle.net/urhLs736/1/
<nav id="navigation">
<ul>
<li><a onclick="example1.html">PAGE 1</a></li>
<li><a onclick="example2.html">PAGE 2</a></li>
<li><div id="nav-image"></div></li>
</ul>
</nav>
and for the CSS:
#navigation {
z-index: 1;
background-color: #3A5E90;
color: #FFFFFF;
text-align: center;
width: 100%;
padding-top: 5px;
padding-bottom: 5px;
}
#navigation.fixed {
position: fixed;
top: 0px;
}
#navigation li {
display: inline;
}
#navigation a {
cursor: pointer;
padding-left: 3.5%;
padding-right: 3.5%;
color: #FFFFFF;
text-decoration: none;
}
#navigation a:hover {
background: #FFFFFF;
color: #3A5E90;
padding-top: 2%;
padding-bottom: 2%;
}
#nav-image {
display: inline;
background: url('https://avatars1.githubusercontent.com/u/3081095?v=2&s=72') no-repeat;
background-size: 100%;
margin-bottom: -6px;
height: 24px;
width: 100px;
}
#nav-image:hover {
height: 24px;
width: 100px;
background: url('https://avatars3.githubusercontent.com/u/5278945?v=2&s=96') no-repeat;
background-size: 100%;
}
I think that your layout will work as is with a minor adjustment to the CSS:
#nav-image {
display: inline-block;
background: url('https://avatars1.githubusercontent.com/u/3081095?v=2&s=72') no-repeat;
background-size: 100%;
margin-bottom: -6px;
height: 24px;
width: 100px;
border: 1px dotted yellow;
}
If you use display: inline-block, the div will take up the specified width and height and the background image will be visible, and the hover effect will work as you expect.
See demo: http://jsfiddle.net/audetwebdesign/9fd1dxn4/
In order to achieve this, you have to change both your HTML and your CSS.
First, your HTML should go like this:
<nav id="navigation">
<ul>
<li><a onclick="example1.html">PAGE 1</a>
</li>
<li><a onclick="example2.html">PAGE 2</a>
</li>
<li id="nav-image">PAGE 3
</li>
</ul>
</nav>
Note that I have added some content in your empty div. If you have an empty <li>, you'll have no background at all (just like your example) since you have a 0x0 pixels li element. I have added some content so the li displays as a general rule, which anyways won't be necessary after you see the CSS, which is the following:
#navigation {
z-index: 1;
background-color: #3A5E90;
color: #FFFFFF;
text-align: center;
width: 100%;
padding-top: 5px;
padding-bottom: 5px;
}
#navigation.fixed {
position: fixed;
top: 0px;
}
#navigation li {
display: inline-block;
height: 24px;
width: 100px;
padding-left: 3.5%;
padding-right: 3.5%;
}
#navigation ul li a {
cursor: pointer;
padding:2% 3.5%;
color: #FFFFFF;
text-decoration: none;
display:block;
height: 24px;
width: 100px;
}
#navigation a:hover {
background: #FFFFFF;
color: #3A5E90;
}
#nav-image {
background: url('https://avatars1.githubusercontent.com/u/3081095?v=2&s=72') no-repeat;
background-size: 100%;
}
#nav-image:hover {
background: url('https://avatars3.githubusercontent.com/u/5278945?v=2&s=96') no-repeat;
background-size: 100%;
}
#nav-image:hover a {
background:transparent
}
OK, now you see I have made some changes and added width and height to the li (the same you had in your sample, but you can change it to anything you want). Now, if you delete the content inside the empty DIV, you'll see how the rendering changes. While it's very easy to solve, I'll leave it to you so you can practice and understand how the whole positioning and display thing works. Also, you can add paddings, margins, et
Here you have a fiddle so you can see it in action and play around

Trying to center my text in my tab navigation. Whats wrong with my code?

Heres my html for my tabbed navigation bar. Im having trouble centering the text within the li tags and putting padding on the top is not somthing I want to do because I want to put padding around all the text to make the whole tab clickable. If theres any advice for that as well please feel free to give me advice. So how can I make my link text centered and if possible how can I make the whole tab clickable?
<div id="tab_container">
<nav id="tabs">
<ul id="nav">
<li class="active">About</li>
<li class="inactive">Services</li>
<li class="inactive">Our Staff</li>
<li class="inactive">book</li>
<li class="inactive">Gift Cards</li>
<li class="inactive">Reviews</li>
</ul>
</nav>
</div>
This is my CSS. Please tell where I went wrong?!
#tab_container
{
background-color: #222;
display: -webkit-box;
-webkit-box-flex: 1;
display: block;
position: relative;
max-width: 970px;
width: 100%;
text-align: center;
}
#tabs
{
float: left;
margin-top: 0px;
width: 100%;
max-width: 970px;
background-color: #222;
padding-top: 20px;
text-align: center;
}
#nav
{
width: 100%;
max-width: 970px;
text-align: center;
}
ul
{
float: left;
max-width: 970px;
display: -webkit-box;
-webkit-box-flex: 1;
width: 100%;
padding-left: 0px;
margin-bottom: 0px;
margin-top: 0px;
margin-right: 0px;
text-align: center;
}
ul li
{
display: inline-block;
text-align: center;
width: 158px;
height: 70px;
background-color: black;
font-size: 18px;
text-transform: uppercase;
text-align: center;
margin:0 auto;
padding: 0;
}
ul li a
{
color: #54544b;
text-decoration: none;
text-align: center;
margin: 0px auto;
}
a:hover
{
color: #CF7BA1;
}
.active a
{
text-decoration: underline;
color: #CF7BA1;
}
Option 1:
Really simple. Just add line-height: 70px; to your a tag.
So the css would be:
ul li a
{
color: #54544b;
text-decoration: none;
text-align: center;
margin: 0px auto;
line-height: 70px;
}
Where the 70px is the height of the list element.
Option 2:
Alternatively, you could set your A to display: block; and add padding to that. This would make it clickable, but personally, I prefer to use line height.
ul li a
{
display: block;
padding-top: 15px;
padding-bottom: 15px;
}