How to change header background image on hover menu - html

I have been searching for a way to change the background image of my banner when I hover my menu bar.
here you have my html
<div id="container">
<header>
<nav>
<ul>
<li>
home
</li>
<li>
bedrijf
</li>
<li>
diensten
</li>
<li>
foto's
</li>
<li>contacteer ons</li>
</ul>
</nav>
</header>
So what i wanna do is when I hover over one of my buttons the background of header should change to a picture I pick for this page.
I hope this is possible it would be great.

You could do that in jQuery:
$("li > a").hover(function(){
$("header").css("background-image", "img.jpg");
});
This code says if you hover over an element with a-Tag which is in a li-Tag then it changes the background-image of the header to img.jpg.
Now if you want that the background-image changes back if you don't hover then add this code too:
$("li > a").mouseleave(function(){
$("header").css("background-image", "none");
});

You could use pseudo elements (:before) on the LI's when you hover over them.
It's a bit hacky, but does work and without javascript too.
working demo here:
http://jsfiddle.net/ezjjyptk/

Here is a solution I've come up with. Basically, add an empty, absolutely positioned div inside each <li> element, and move it up from behind the header on hover.
JSFiddle
<div id="container">
<header>
<nav>
<ul>
<li>
home
<div style="background:green">
</div>
</li>
<li>
bedrijf
<div style="background:yellow">
</div>
</li>
<li>
diensten
</li>
<li>
foto's
</li>
<li>contacteer ons</li>
</ul>
</nav>
</header>
header {
position: relative;
width: 100%;
background: red;
}
ul li a {
position: relative;
z-index: 3;
}
ul li:hover div {
z-index: 1;
}
ul li div {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: -1;
}

I wouldn't say it's impossible without javascript, but I certainly wouldn't call this code pretty
.fixed {
position: absolute;
top: 50px;
left: 0;
opacity: 0;
transition: opacity .5s ease-in-out;
}
li {
display: inline-block;
}
.show1:hover + #header1 {
opacity: 1;
}
.show2:hover + #header2 {
opacity: 1;
}
.show3:hover + #header3 {
opacity: 1;
}
.show4:hover + #header4 {
opacity: 1;
}
<li>
<a class="show1" href="index.html">Show 1</a>
<img class="fixed" id="header1" src="https://upload.wikimedia.org/wikipedia/commons/thumb/6/6f/Color_blocks.svg/800px-Color_blocks.svg.png" />
</li>
<li>
<a class="show2" href="bedrijf.html">Show 2</a>
<img class="fixed" id="header2" src="http://static1.squarespace.com/static/534d1b21e4b077040469bae0/t/5362730de4b04c365eb65a37/1398960956409/The+two+colour+shirt" />
</li>
<li>
<a class="show3" href="diensten.html">Show 3</a>
<img class="fixed" id="header3" src="http://i1.wp.com/makeapowerfulpoint.com/wp-content/uploads/2015/03/Drunk-Tank-Pink.png" />
</li>
<li>
<a class="show4" href="pictures.html">Show 4</a>
<img class="fixed" id="header4" src="http://3.bp.blogspot.com/-YFX5nFtmjiA/T7co_rB3keI/AAAAAAAAI0c/EkNV9tX3xt8/s920/color%2Bwheel%2Bstar%2Bblock%2Blogo%2Bv1.jpg" />
</li>

Related

How to make different img appear when hovering over a different li

im trying to make the background of a parent div change when hovering over a li in its child.. i did that but now i want to display a different img when hovering over a different li inside the same child div... i cant seem to do that and cant find anyone else having the same problem.. and if it needs JavaScript also i dont mind
i tried to specify the li but it doesnt work
heres the css code
#mainpage{
pointer-events: none;
}
#listman {
pointer-events: auto;
}
#mainpage:hover {
background: url("bg1.jpg");
background-repeat: no-repeat;
height: 500x;
background-position: top;
background-size: 100%;
}
and heres the html part
div id="mainpage">
<div id="listman">
<ul class="bigger">
<li id="listman1" href="#"> <a> 1 </a> </li>
<li id="listman2" href="#"> <a> 2 </a> </li>
<li id="listman3" href="#"> <a> 3 </a> </li>
<li id="listman4" href="#"> <a> 4 </a> </li>
<li id="listman5" href="#"> <a> 5 </a> </li>
<li id="listman6" href="#"> <a> 6 </a> </li>
<li id="listman7" href="#"> <a> 7 </a> </li>
<li id="listman8" href="#"> <a> 8 </a> </li>
</ul>
</div>
</div>
</div>
i want to make a different img to appear when hovering over (listman2) can anyone help me?
Try using this CSS syntax:
div#listman > ul.bigger > div#listman2 > li {
background-image: url("bg1.png");
}
div#listman > ul.bigger > div#listman2 > li:hover + div#listman > ul.bigger > div#listman2 > li {
background-image: url("bg2.png");
}
I'm not sure it will work though
If the li elements do not have their own position and you can make mainpage be the closest positioned element to them then you can put the image on an absolutely positioned pseudo element on each li when it is hovered.
This snippet uses a CSS variable to set the background image for each li element.
#mainpage {
pointer-events: none;
}
#listman {
pointer-events: auto;
}
ul.bigger {
position: relative;
}
li:hover::after {
content: '';
position: absolute;
background: var(--bg);
background-repeat: no-repeat;
background-position: top;
background-size: 100%;
width: 100%;
height: 500px;
z-index: -1;
top: 0;
left: 0;
}
#listman1 {
--bg: url(https://picsum.photos/id/1015/1024/768);
}
#listman2 {
--bg: url(https://picsum.photos/id/1016/1024/768);
}
#listman3 {
--bg: url(https://picsum.photos/id/1018/1024/768);
}
#listman4 {
--bg: url(https://picsum.photos/id/1019/1024/768);
}
#listman5 {
--bg: url(https://picsum.photos/id/1020/1024/768);
}
#listman6 {
--bg: url(https://picsum.photos/id/1022/1024/768);
}
#listman7 {
--bg: url(https://picsum.photos/id/1023/1024/768);
}
#listman8 {
--bg: url(https://picsum.photos/id/1024/1024/768);
}
<div id="mainpage">
<div id="listman">
<ul class="bigger">
<li id="listman1" href="#"> <a> 1 </a> </li>
<li id="listman2" href="#"> <a> 2 </a> </li>
<li id="listman3" href="#"> <a> 3 </a> </li>
<li id="listman4" href="#"> <a> 4 </a> </li>
<li id="listman5" href="#"> <a> 5 </a> </li>
<li id="listman6" href="#"> <a> 6 </a> </li>
<li id="listman7" href="#"> <a> 7 </a> </li>
<li id="listman8" href="#"> <a> 8 </a> </li>
</ul>
</div>
</div>

Sticky rows in tree-like nested list structure

I have a long list of items that are nested.
section#container {
height: 250px;
width: 350px;
overflow: auto;
}
li {
position: relative;
background: white;
}
div {
position: sticky;
top: 0;
}
<section id="container">
<ul>
<li>
<div>Aaaaaaaaaaaa</div>
<ul>
<li>
<div>Bbbbbbbbbbbb</div>
</li>
<li>
<div>Cccccccccc</div>
</li>
<li>
<div>Dddddddddddd</div>
<ul>
<li>
<div>Eeeeeeeeee</div>
</li>
<li>
<div>Fffffffff</div>
<ul>
<li>
<div>Ggggg</div>
</li>
<li>
<div>Hhhh</div>
</li>
<li>
<div>Iiii</div>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>
<pre>More<br>content<br>so<br>we<br>can<br>scroll<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.</pre>
</li>
</ul>
</section>
When scrolling, I want to always see the "path that led to the current top row". For example, when scrolling further down, you should see
A # "Path"
B # leading up to
D # G (breadcrumbs)
F #
G
H
...
Or maybe better visualized, see THIS DEMO of marchaos/react-virtualized-sticky-tree. It does exactly what I am looking for. The library however uses JavaScript and just sets the top property according to the nest level. I am looking for a simple CSS-only solution (independent on the amount of recursion), which I think should be fairly basic but I cannot seem to find a solution.
In the snippet above I attempted so with position:sticky on the divs, and it somewhat works, but they are unfortunately all on the same line. Thanks for any hints.
You can simply set z-index to an individual content in order to differentiate it from the other contents which doesn't have sticky position and set the top of an them.
Remember to set top which is different from the one another in order to align them one under another because if you set the same top will cause an overlapping.
the following is an example
section#container {
height: 240px;
width: 350px;
overflow: auto;
}
li {
position: relative;
background: white;
}
div {
position: sticky;
}
.first {
z-index: 99999;
background: red;
top: 0;
}
.second {
z-index: 99999;
background: blue;
top: 15px;
}
.third {
z-index: 99999;
background: green;
top: 30px;
}
<section id="container">
<ul>
<li>
<div class="first">Aaaaaaaaaaaa</div>
<ul>
<li>
<div>Bbbbbbbbbbbb</div>
</li>
<li>
<div>Cccccccccc</div>
</li>
<li>
<div class="second">Dddddddddddd</div>
<ul>
<li>
<div>Eeeeeeeeee</div>
</li>
<li>
<div class="third">Fffffffff</div>
<ul>
<li>
<div>Ggggg</div>
</li>
<li>
<div>Hhhh</div>
</li>
<li>
<div>Iiii</div>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>
<pre>More<br>content<br>so<br>we<br>can<br>scroll<br>.<br>.<br>.<br>.<br>.<br>.<br>.<br>.</pre>
</li>
</ul>
</section>

How can I keep horizontal element alignment inside an expanding list item?

I have been tasked with styling a website, where I have encountered a hurdle regarding the horizontal alignment of elements inside list items.
I have replicated the structure of the menu in question with this JSFiddle.
I want to know how I can keep the position of the green divs (as shown from the start) when I expand the menu, using the button in the fiddle. I need them to keep horizontal alignment with the first <a> element in their respective <li> element, when the submenus are displayed.
you can do it like this for example:
<html>
<script>
function clickFunction(){
var elements = document.getElementsByClassName("submenu");
for(var i = 0; i < elements.length; i++){
elements[i].classList.toggle("display-sublist");
}
}
</script>
<style>
ul{
list-style-type: none;
margin: 0px;
padding: 0px;
}
ul li{
width: 100%;
background-color: blue;
}
.submenu{
display: none;
}
.display-sublist{
display: block;
}
ul li a{
width: 95%;
background-color: red;
}
.main-test {
display: inline-block;
float: left;
width: 90%;
}
.cancel-test{
display: inline-block;
background-color: green;
float: right;
width: 10%;
}
.expand-button{
clear: both;
display: block;
}
</style>
<body>
<ul>
<li>
<a class="main-test" href="#">Something</a>
<a class="cancel-test">x</div>
<ul class="submenu">
<li>
Sub-Something
</li>
<li>
Sub-Something
</li>
<li>
Sub-Something
</li>
</ul>
</li>
<li>
<a class="main-test"href="#">Something</a>
<a class="cancel-test">x</a>
<ul class="submenu">
<li>
Sub-Something
</li>
<li>
Sub-Something
</li>
</ul>
</li>
<li>
Something
</li>
<li>
Something
</li>
</ul>
<button onclick="clickFunction()" class="expand-button">Expand</button>
</body>
</html>

CSS Drop Down Menu - List elements are static

I may seem really silly or outright wrong in the way I code. However, when I create a drop down menu in CSS the new li elements get pushed to the other side of the page and not in the container box. How would I fix this?
Here is the code:
<nav>
<div class="wrapper">
<div class="brand">
<img class="UKLogo" src="images/logo.png" alt="">
</div> <!-- brand -->
<div class="navigation">
<ul class="nav-ul">
<li> HOME </li>
<li> ABOUT </li>
<a href="#">
<li class="course-li">
COURSES
<ul class="drop-down">
<li class="list-item"> Driver CPC </li>
<li> First Aid </li>
<li> Other </li>
</ul>
</li>
<li> CONTACT </li>
<!-- <li> TESTOMONIALS </li> -->
<!-- <li> FAQs </li> -->
</ul>
</div> <!-- Navigation -->
</div> <!-- Wrapper -->
</nav>
nav {
margin: 0px;
padding: 0px;
height: 75px;
background-color: #FFF;
}
.brand {
margin: auto;
width: 960px;
}
.company-name {
padding: 0px;
margin: 0px;
}
.UKLogo {
padding: 0px;
margin: 0px;
position: relative;
top: 11px;
}
.navigation ul li {
display: inline-block;
margin: 10px;
position: relative;
left: 380px;
top: -46px;
}
.navigation ul a {
color: black;
margin-left: 40px;
text-decoration: none;
font-family: Lato;
font-weight: 300;
}
.navigation ul a:hover {
color: #169ec5;
font-weight: 300;
}
.course-li:hover .drop-down {
left: 0px;
}
.drop-down {
position: absolute;
left: -5px;
z-index: 1;
background-color: white;
left: -9999px;
}
Thank you ever so much for looking and helping. Always open to criticism whether its the way I code or anything else.
Here is a JSFiddle https://jsfiddle.net/vj41qLts/
Many Thanks!
You need to declare a position in the parent, for the child to reside in. An element with position: absolute; will position itself to the first parent with position: relative;. If there is no parent with position: relative;, it will use the browser window instead.
See fix example here: https://jsfiddle.net/vj41qLts/1/
I think there are two thing you need to change:
ul li will select everything li in the navigation even the dropdown, ul>li will only select the immediate child, instead of running down the nested elements.
you need to add position:relative; in your dropdown's parent.
One of the first issues I see is the fact that your markup for your main links isn't setup correctly. Following a structure more link the below should give make it work the way you want it to:
<nav>
<ul>
<li><a href="#">Home<a></li>
<li><a href="#">About<a></li>
<li>
<a href="#">Courses<a>
<div>
<ul>
<li>A link</li>
<li>A link</li>
</ul>
</div>
</li>
</ul>
</nav>
Then use CSS or JS to control showing and hiding the dropdown of links.

CSS - Get image to show text overlayed on top of the image on hover?

I currently have a few list items. Each one has an image followed by some text.
ul {
width:300px;
list-style-type: none;
}
<ul>
<li>
<img src="http://placehold.it/350x150"/>
Cool image
</li>
<li>
<img src="http://placehold.it/350x150"/>
Cool image
</li>
<li>
<img src="http://placehold.it/350x150"/>
Cool image
</li>
</ul>
I'm having trouble seeing how/if I can do this with pure css? I'd like the image to have the text on top of it on hover (kinda like how imgur.com does it.) On imgur.com if you hover over a thumbnail, at the bottom it tells you how many upvotes something has. How would I go about this?
If I understand correctly - you simply need to add position text absolutely inside relatively positioned LI item. And don't forget to add your .thumbnail class to LIs.
You may also play around a bit with text positioning using top, left, bottom and right CSS properties.
Note: to make text visible only on hover - simply hide text by default with display:none and show it on :hover event.
Note2: also animation could be added via transitions and opacity.
ul {
width:300px;
list-style-type: none;
}
.thumbnail {
position:relative;
}
.thumbnail .text {
opacity: 0;
position: absolute;
bottom: 5px;
left: 0px;
transition: opacity 500ms;
}
.thumbnail:hover .text {
opacity: 1;
}
<ul>
<li class="thumbnail">
<img src="http://placehold.it/350x150"/>
Cool image
</li>
<li class="thumbnail">
<img src="http://placehold.it/350x150"/>
Cool image
</li>
<li class="thumbnail">
<img src="http://placehold.it/350x150"/>
Cool image
</li>
</ul>
<ul>
<li>
<div class='container'>
<img src="http://placehold.it/350x150"/>
<p>Cool image</p>
</div>
</li>
...
</ul>
.container {
position:relative;
}
.container p {
position:absolute;
width: ;
height: ;
top: 0;
left: 0;
visibility: hidden;
opacity: 0;
}
.container:hover .container p {
visibility: visible;
opacity: 1;
}
Is this what you're looking for?
Note that there is just one link per <li>, and that the overlay is inside it.
ul {
width:300px;
list-style-type: none;
}
li {
position:relative;
}
.thumbnailContainer:link .overlay {
position:absolute;
top:0px;
left:0px;
opacity:0.7;
display:none;
width:350px;
height:150px;
}
.thumbnailContainer:hover .overlay {
display:block;
}
<ul>
<li>
<a class="thumbnailContainer" href=""><img src="http://placehold.it/350x150"/>
<div class="overlay">Cool image</div></a>
</li>
<li>
<a class="thumbnailContainer" href=""><img src="http://placehold.it/350x150"/>
<div class="overlay">Cool image</div></a>
</li>
<li>
<a class="thumbnailContainer" href=""><img src="http://placehold.it/350x150"/>
<div class="overlay">Cool image</div></a>
</li>
</ul>
Follow these Steps:
Make the li position:relative
Make the image and text position: absolute and top: 0;left: 0
Give opacity: 0 to text
make opacity: 1 on hover
[img containing a]:hover ~ [text element] {
opacity: 1;
}
Here is a fiddle of what I suggested: http://jsfiddle.net/zdz8x6wq/