Image rollovers without JavaScript - html

I'm trying to discover a way that doesn't use javascript that allows you to hover over the smaller divs (or images) to change the background of the larger div. Is this possible purely with HTML & CSS?
The example has 2 problems:
1. Only rolling over one of the divs works (because it's straight after)
2. When rolling over that div, the background of the main div reverts after moving the mouse off, so it's not a permanent change
I'm very curious and appreciate any advice here, thanks!
UPDATE:
I've just created this: https://jsfiddle.net/ehzsmusr/
The backgrounds seem to change, but don't stay when you hover over something else. Can this be fixed?
#main {
width: 300px;
height: 200px;
background: red;
float: left;
}
.hover1 {
float: left;
background: blue;
width: 100px;
height: 75px;
}
.hover2 {
float: left;
background: green;
width: 100px;
height: 75px;
}
.hover1:hover + #main {
background: #ccc
}
.hover2:hover + #main {
background: #ccc
}
<div class='container'>
<div class='hover1'></div>
<div class='hover2'></div>
<div id='main'></div>
</div>

if you don't mind clicking as you mentioned in the comment, here's one implementation of the checkbox hack mentioned by #kabanus ( using radio buttons instead )
body {
margin: 0;
padding: 0;
}
#container {
width: 100vw;
height: 100vh;
background: #eee;
}
input {
display: none;
}
label {
display: block;
width: 50px;
height: 50px;
float: left;
margin: 20px;
}
#redL {
background: red;
}
#greenL {
background: green;
}
#blueL {
background: blue;
}
#red:checked ~ #big {
background: red;
}
#green:checked ~ #big {
background: green;
}
#blue:checked ~ #big {
background: blue;
}
#big {
width: 50vw;
height: 50vh;
background: #fff;
clear: both;
margin: auto;
}
<div id="container">
<input type="radio" id="red" name="color" />
<label for="red" id="redL"></label>
<input type="radio" id="green" name="color" />
<label for="green" id="greenL"></label>
<input type="radio" id="blue" name="color" />
<label for="blue" id="blueL"></label>
<div id="big">
</div>
</div>
Another hack would be setting the transition-delay to 604800s ( or more ) so the color changes and goes back after that numbers of seconds ( one week later ).
body {
margin: 0;
padding: 0;
}
#container {
width: 100vw;
height: 100vh;
background: #eee;
}
#redL {
background: red;
}
#greenL {
background: green;
}
#blueL {
background: blue;
}
label {
display: block;
width: 50px;
height: 50px;
float: left;
margin: 20px;
}
#redL:hover ~ #big {
background: red;
transition-delay: 0s;
}
#greenL:hover ~ #big {
background: green;
transition-delay: 0s;
}
#blueL:hover ~ #big {
background: blue;
transition-delay: 0s;
}
#big {
width: 50vw;
height: 50vh;
background: #fff;
clear: both;
margin: auto;
transition: all .1s 604800s;
}
<div id="container">
<label id="redL"></label>
<label id="greenL"></label>
<label id="blueL"></label>
<div id="big">
</div>
</div>

Related

When float two elements right they switch positions

I need to position two of my elements on the right hand side of the parent element, however, when using the float: right property, it makes the elements to switch positions.
I had a look at this thread: Prevent Right Floated Elements from Swapping however, adding the display: inline-block and text-align: right didn't solve the problem.
Here is a
.container {
width: 300px;
height: 20px;
background-color: red;
margin: 0 auto;
}
.element1 {
float: right;
height: 20px;
width: 10px;
background-color: blue;
color: white;
padding: 10px;
}
.element2 {
float: right;
height: 20px;
width: 10px;
background-color: yellow;
padding: 10px;
}
<div class="container">
<div class="element1">1</div>
<div class="element2">2</div>
</div>
My desired result would be blue element followed by yellow element.
UPDATE:
I do understand that this is expected behaviour and the second element is send all the way to the right after the first element, and I do know that changing the elements around would fix the problem, however, just wondering if there is a CSS solution for it.
.container {
display: flex;
}
.element4 {
margin-right: auto;
}
.element5 {
margin-left: auto;
}
.container {
width: 300px;
background-color: red;
}
.element {
height: 20px;
width: 10px;
padding: 10px;
}
.element1 {
background-color: blue;
color: white;
}
.element2 {
background-color: yellow;
color: black;
}
.element3 {
background-color: green;
color: white;
}
.element4 {
background-color: gold;
color: black;
}
.element5 {
background-color: magenta;
color: black;
}
.element6 {
background-color: goldenrod;
color: white;
}
<div class="container">
<div class="element element1">1</div>
<div class="element element2">2</div>
<div class="element element3">3</div>
<div class="element element4">4</div>
<div class="element element5">5</div>
<div class="element element6">6</div>
</div>
This is expected behaviour, either switch your elements around in your HTML or use another method of positioning besides float.
It floats the first element first, then it sees the next one and this then needs to be floated over again so it moves past the original one.
use this.
.container{
width: 300px;
height: 20px;
background-color: red;
margin: 0 auto;
position:relative;
}
.element1 {
position:absolute;
right:0;
height: 20px;
width: 10px;
background-color: blue;
}
.element2 {
position:absolute;
right:10px;
height: 20px;
width: 10px;
background-color: yellow;
}
<div class="container">
<div class="element1">
</div>
<div class="element2">
</div>
</div>

How to effect other div properties when a different div is hovered on?

this is my 1st post over here, ill try to be as clear as possible and hope I'm following the site's rules.
Not so long ago I started to play a little bit with some HTML/CSS, I got pretty good at it but not enough I guess and I hope to get some help over here.
I'm trying to build a navigation menu and one of it's features I would like it to have is when I hover over the "Account" button it should split the menu in the middle and show a new section where the user can log-in into his or hers account.
This is what I got so far and for some reason I cant make the effect when hovering over the "Account" div but it doe's work when I apply the hover effect on the entire top div (where the "Account div is a part of). tried all "linking" methods suggested in this post and still nothing... Also last thing: When i did overflow: hidden; on the .middle div it added a space between the top and bottom divs.
I would highly appreciate any help and if it's possible to leave it at the CSS level (without any jQuery or any other coding)
Thanks in advance,
Tony.
.container {
width: 560px;
height: auto;
}
.top {
height: 50px;
background-color: red;
}
.top-L {
float: right;
padding: 5px 20px;
}
.top-R {
float: right;
padding: 5px 20px;
height: 50px;
display: block;
}
.middle {
height: 0px;
width: 560px;
overflow: hidden;
background-color: blue;
transition: .4s ease;
}
.bottom {
height: 50px;
background-color: green;
}
.top:hover + .middle {
height: 50px;
transition: .4s ease;
}
.middle:hover {
height: 50px;
}
<html>
<body style="font-family: sans-serif; background-color: #ccc;">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet"/>
<div class="container">
<div class="top">
<div class="top-L">Other</div>
<div class="top-R">Account</div>
</div>
<div class="middle">
<div>
<input type="text" placeholder="Enter Username" name="uname" required>
<input type="password" placeholder="Enter Password" name="psw" required>
</div>
</div>
<div class="bottom"></div>
</div>
</body>
</html>
UPDATED ANSWER
What you are looking for is not possible with your current structure. However, we can do it via Click, Instead of Hover. With some changes :). It is not the perfect solution but should work in your situation.
*{
box-sizing: border-box;
}
.container {
width: 560px;
height: auto;
}
.top {
/* height: 50px; */
background-color: red;
display: flex;
justify-content: flex-end;
}
.top-L label, .top-R label{
padding: 20px;
}
.top-L {
order: 1;
padding: 20px;
}
.top-R {
padding: 20px;
}
.middle {
height: 0px;
width: 560px;
overflow: hidden;
background-color: blue;
transition: .4s ease;
}
.bottom {
height: 50px;
background-color: green;
}
.top:hover + .middle {
/* height: 50px; */
transition: .4s ease;
}
#account{
display: none;
}
#account:checked + .middle{
height: 50px;
padding: 10px;
}
.middle:hover {
/* height: 50px; */
}
<div class="container">
<div class="top">
<div class="top-L">Other</div>
<div class="top-R" ><label for="account">Account</label> </div>
</div>
<input type="checkbox" id="account"/>
<div class="middle">
<div>
<input type="text" placeholder="Enter Username" name="uname" required>
<input type="password" placeholder="Enter Password" name="psw" required>
</div>
</div>
<div class="bottom"></div>
</div>
If I am right this will help you,
nav > ul > li {
display: inline-block
}
nav > ul > li ul {
visibility: hidden;
position: absolute;
top: 105%;
left: 0;
transition: 0.2s 1s;
}
nav > ul > li:hover ul {
visibility: visible;
transition-delay: 0s;
}
nav ul {
list-style: none;
padding: 0;
margin: 0;
}
nav li {
width: 100px;
background: #eee;
margin: 2px;
position: relative;
padding: 10px;
}
nav a {
display: block;
}
body {
padding: 10px;
}
<nav>
<ul>
<li>Last</li>
<li>one</li>
<li>is</li>
<li>
Dropdown
<ul>
<li>One</li>
<li>Two</li>
<li>Three</a></li>
</ul>
</li>
</ul>
</nav>
#Tony Mor made some correction on your existing css. I believe it's what you want.
.top-R {
float: right;
padding: 5px 20px;
display: block;
}
and
.middle div {
padding-left: 10px;
padding-top: 15px;
}

How to style object when the user clicks it

In css for the links I can style the as before user clicks and after user clicks. But how can I do this for simple text or an object that when a user clicks that object then change its color.
For Example
<style>
.object:onmouseclick {
background-color:green;
padding:5px;
}
</style>
What we have to write in place of onmouseclick.
If you were styling a link you would use :active or :focus but as you're using this on a dom element, you would have to use jQuery to add a class to the clicked item and apply the style through that class...
$('.object').click(function() {
$(this).toggleClass('clicked');
});
.object {
background-color: green;
padding: 5px;
cursor: pointer;
width: 300px;
color: orange;
}
.object.clicked {
background-color: blue;
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="object">
<h1>The Mutants Are Revolting</h1>
<p>Bender?! You stole the atom. That's the ONLY thing about being a slave. I am Singing Wind, Chief of the Martians. Anyhoo, your net-suits will allow you to experience Fry's worm infested bowels as if you were actually wriggling through them.</p>
</div>
button:active or button:focus should do the trick.
a {
background-color: red;
}
a:active {
background-color: #efefef;
}
myButton
and optionally
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.object{
background:green;
padding:5px;
width: 300px;
height: 300px;
position: relative;
}
input{
display: none;
}
label{
display: block;
width: 300px;
height: 300px;
}
input:checked ~ label{
position: absolute; top: 0; left: 0;
width: 100%;
height: 100%;
background: #00f url(http://www.pubzi.com/f/sm-chess-horse.png) no-repeat center center;
border: 1px solid #000;
}
<div class="object">
<input type="checkbox" id="c1" name="checkbox" />
<label for="c1"></label>
</div>

Stacking div elements

I want to stack some div elements like the picture below without having to manually enter the position of every new div I add. Is there some way I can write a style tha will stack my elements like this? I would like to avoid javascript.
Doing something like:
div{
left:-30px;
}
will not work because its gonna move all of them by the same amount.
What I know I can probably do is have smaller divs as big as the gap next to each other and have them contain the bigger ones. The problem with this tho is I want to be able to change the stack order by manipulating the big element's z-index which wont work if they are children of different divs.
Here is a stack snippet:
div {
position: relative;
float: left;
width: 200px;
height: 300px;
}
#div_1 {
background-color: red;
}
#div_2 {
background-color: blue;
}
#div_3 {
background-color: yellow;
}
#div_4 {
background-color: green;
}
<body>
<div id="div_1">div1</div>
<div id="div_2">div2</div>
<div id="div_3">div3</div>
<div id="div_4">div4</div>
</body>
Is this what you are asking for ?
div {
position: relative;
float: left;
width: 200px;
height: 300px;
margin-right: -50px;
z-index: 0;
box-shadow: 0 0 2px 2px rgba(0,0,0,0.8);
}
div:hover {
z-index: 100
}
#div_1 {
background-color: red;
}
#div_2 {
background-color: blue;
}
#div_3 {
background-color: yellow;
}
#div_4 {
background-color: green;
}
<body>
<div id="div_1">div1</div>
<div id="div_2">div2</div>
<div id="div_3">div3</div>
<div id="div_4">div4</div>
</body>
Use dispaly:inline:block float:left;
body {
background: #d300ff;
margin: 0;
padding: 0;
}
.strip {
width: 100px;
height: 700px;
display: inline-block;
margin: 0;
padding: 0;
float: left;
}
.strip1 {
background: #fe0000;
}
.strip2 {
background: #ffa901;
}
.strip3 {
background: #41ff01;
}
.strip4 {
background: #01b7ff;
}
.strip5 {
background: #011eff;
}
<div class="strip strip1"></div>
<div class="strip strip2"></div>
<div class="strip strip3"></div>
<div class="strip strip4"></div>
<div class="strip strip5"></div>

CSS: Button Visibility Problem

I am working on a project with a video player. I want to add play/pause and skip buttons together but one of the buttons is always invisible, however working. The codes I am using:
in .css file:
.buttons { position:absolute; top: 326px; left:150px; }
.buttons DIV { width: 26px; height: 20px; cursor: pointer; }
.buttons .pause { background-image: url("button_pause.png"); }
.buttons .play { background-image: url("button_play.png"); }
.buttons .skip { background-image: url("button_skip.png"); }
in html file:
<div class="buttons">
<div id="skip" onclick="skipCurrentSong();"></div>
<div id="playpause" onclick="setPlayPause();"></div>
</div>
the functions in js file work properly but the skip button is invisible. I have tried to create a different class in css file for the skip button and updated the html file accordingly but this gave the same output also. Can anyone say what mistake I am making and how to correct it?
Thanks in advance.
Some extra codes:
.css file:
#CHARSET "UTF-8";
BODY { height: 530px; overflow: hidden; }
#tv { width: 532px; height: 443px; background: url("tv.png") no-repeat; margin: 0 auto; margin-top: 20px; z-index: 3; position: relative; }
#title { color: #dddddd; text-align: right; float: right; margin-top: 320px; margin-right: 120px; }
.buttons { position:absolute; top: 326px; left:150px; }
.buttons DIV { width: 26px; height: 20px; cursor: pointer; background-color: white;}
.buttons .pause { background-image: url("button_pause.png"); }
.buttons .play { background-image: url("button_play.png"); }
.buttons .skip { background-image: url("button_skip.png"); }
FORM { display: block; margin: 0 auto; background: url("player.png"); height: 295px; width: 482px; clear: both; position: relative; top: -421px; margin-bottom: -295px; z-index: 4; }
FORM LABEL { color: #00aad4; margin-bottom: 10px; padding-top: 40px; }
FORM INPUT { border: none; border-bottom: 3px solid #00aad4; font-size: 24px; width: 200px; }
FORM * { display: block; margin: 0 auto; text-align: center; }
FORM .loader { margin-top: 10px; }
.loader { background: url("load.gif"); width: 16px; height: 16px; margin: 0 auto; visibility: hidden; }
.load .loader { visibility: visible; }
in html file:
<div id="tv">
<div id="title"></div>
<div class="buttons">
<div id="playpause" onclick="setPlayPause();"></div>
<div id="skip" onclick="skipCurrentSong();"></div>
</div>
</div>
Updated: This will give you three buttons. Do you want pause/play combined?
CSS:
.buttons { position:absolute; top: 326px; left:150px; }
.buttons div { width: 26px; height: 20px; cursor: pointer; background-color: white;}
.buttons #pause { background-image: url("button_pause.png"); }
.buttons #play { background-image: url("button_play.png"); }
.buttons #skip { background-image: url("button_skip.png"); }
HTML:
<div id="tv">
<div id="title"></div>
<div class="buttons">
<div id="play" onclick="setPlayPause();"></div>
<div id="pause" onclick="setPlayPause();"></div>
<div id="skip" onclick="skipCurrentSong();"></div>
</div>
</div>
I changed the .css code as:
.skipbutton { position:absolute; top:326px; left:120px; }
.skipbutton DIV { width: 26px; height: 20px; cursor: pointer; background-color: gray;}
.skipbutton .skip { background-image: url("button_skip.png"); }
.buttons { position:absolute; top: 326px; left:90px; }
.buttons DIV { width: 26px; height: 20px; cursor: pointer; }
.buttons .pause { background-image: url("button_pause.png"); }
.buttons .play { background-image: url("button_play.png"); }
and changed .html as:
<div id="tv">
<div id="title"></div>
<div class="skipbutton">
<div class="skip" onclick="skipCurrentSong();"></div>
</div>
<div class="buttons">
<div id="playpause" onclick="setPlayPause();"></div>
</div>
</div>
surpsiringly for skip button div class worked while for playpause button, div id works and div class just kills the button. It is a little awkward as the two buttons have the same structe in css file.
I tried to seperate the classes for two buttons earlier but this time it finally worked.
Thanks to lasseespeholt.