CSS: Hovering over a divs with a different z-indexes - css

Let's say I have to divs named "A" and "B", respectively.
<div id="A">
<div id="B">
</div>
</div>
div A has a z-index of 1 and has a width and height of 100%.
div B has a z-index of 2 and has a width and height of 50% and a top and left of 25%;
#A{
position:absolute;
width:100%
height:100%;
top:0%;
left:0%;
z-index:1;
background-color:black;
}
#B{
position:absolute;
width:50%;
height:50%;
left:25%;
top:25%;
z-index:2;
background-color:gray
}
If you were to hover over Div A and Div B at the same time, Div A will still register as being hovered over. In this hypothetical scenario, whenever I hover over Div A, I want Div A to turn Red and Div B to turn Gray, and whenever I hover over Div B, I want Div B to be Blue and Div A to be Black. How can I do this? I would prefer a CSS answer if there is one available.
I would think using #A:not(hover){} would work, but when I tried it, it failed.

Your hypothetical situation already exists in the way that you you want:
Hovering over A turns A red and B gray
Hovering over B turns B blue and A red
#A {
position: absolute;
width: 100%;
height: 100%;
top: 0%;
left: 0%;
z-index: 1;
background-color: black;
}
#A:hover {
background: red;
}
#B {
position: absolute;
width: 50%;
height: 50%;
left: 25%;
top: 25%;
z-index: 2;
background-color: grey;
}
#B:hover {
background: blue;
}
<div id="A">
<div id="B"></div>
</div>
However, if you really do want to stop <div> #A from being hovered over when <div> #B is hovered over, you can make use of a sibling element, as is described in this answer.
Hope this helps! :)

You cannot achieve what you're trying to do with CSS. The hover feature only allows you to change the attributes of that one element. I would recommend using JQuery.
$(document).ready(function(){
$("#A").hover(function(){
$("#A").css("background-color", "red");
$("#B").css("background-color", "gray");
});
/* Continue writing other hover functions */
});
JQuery allows you to style multiple elements given an action

You can't with pure css, you must help of jquery :
Part of Css:
#A:hover {
background-color: red;
}
#B:hover {
background-color: blue;
}
Part of jQuery:
$('#B').hover(function(){
$('#A').css('background-color','black');
},function(){
$('#A').css('background-color','red');
})
$(document).ready(function(){
$('#B').hover(function(){
$('#A').css('background-color','black');
},function(){
$('#A').css('background-color','red');
})
})
#A {
position:absolute;
width:100%;
height:100%;
top:0%;
left:0%;
z-index:1;
background-color:black;
border: 1px solid;
}
#B {
position:absolute;
width:50%;
height:50%;
left:25%;
top:25%;
z-index:2;
border: 1px solid;
background-color: gray;
}
#A:hover {
background-color: red;
}
#B:hover {
background-color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div id="A">
<div id="B">
</div>
</div>

I found a pure css way using the link from Obsidian Age's answer:
<div id="ContainerDiv">
<div id="A"></div>
<div id="B"></div>
</div>
http://jsfiddle.net/k3Zdt/248/
All I had to do was take Div B out of Div A and remove the z-indexes.

Related

How to have overflow scroll only in one axis but visibility and no scroll in the other

I have researched this but could only find very old answers and no real solution. This is the problem:
div {position: absolute;width:300px;height:250px;background:black;overflow-y:scroll;overflow-x:visible;}
span {display:block;position:relative;background:red;width:300px;height:530px;}
button {position: absolute;width:100px;height:50px;background:blue;left:250px;}
<div>
<span>
<button>
</button>
</span>
</div>
the blue rectangle should stick out of the main div without the need of a scrollbar, whereas the red rectangle should be scrollable in the y axis. But it seems like I cant have a scrollbar only in one axis. Is there a workaround for this? CSS only please.
It works if you put an extra div outside and move the button out of the scrollable area:
#container{position: relative;}
#scrollable {position: absolute;width: 300px;height: 250px;background: black;overflow-y: scroll;overflow-x: hidden;}
#red {display: block;position: relative;background: red;width: 300px;height: 530px;}
button {position: absolute;width: 100px;height: 50px;background: blue;left: 250px;}
<div id="container">
<div id="scrollable">
<span id="red"></span>
</div>
<button></button>
</div>
You need at least one more element as a holder. With an element that has scrolling enabled, no element can stick out to the side.
also, you should use a div instead of making a span a block element, since span tags should be used to mark up individual words as inline-block element inside div tags.
div {
position: absolute;
width:300px;
height:250px;
background:black;
}
div.scroll {
position: relative;
width:100%;
height:100%;
background:black;
overflow-y:scroll;
overflow-x:hidden;
}
div.content {
position:relative;
background:red;
width:100%;
height:530px;
}
button {
position: absolute;
width:100px;
height:50px;
background:blue;
top: 0;
right:-50px;
}
<div>
<div class="scroll">
<div class="content">
</div>
</div>
<button>
</button>
</div>
Okay, I found a way around with only CSS, and without changing the markup.
You can set the div's width bigger to include the blue button.
Then you can make the scrollbar closer to the div, as if the div is smaller.
You can't use margin and padding on scrollbar. So you have to make it transparent and cast a shadow on it to make it look like it's next to the div.
You can use this component with other components by using position: absolute and z-index css-tricks
But honestly, it's a lot of work. It could be easier for you to go on another way.
div {
position: absolute;
width:400px;
height:250px;
overflow-y:scroll;
overflow-x:visible;
}
div::-webkit-scrollbar {
width: 10px;
}
div::-webkit-scrollbar-track {
box-shadow: -90px 0px 0px 0px lightgrey;
border: solid 3px transparent;
}
div::-webkit-scrollbar-thumb {
box-shadow: -90px 0px 0px 0px grey;
border: solid 3px transparent;
}
span {
display:block;
position:relative;
background:red;
width:300px;
height:530px;
}
button {
position: absolute;
width:100px;
height:50px;
background:blue;
left:250px;
}
<div>
<span>
<button>
</button>
</span>
</div>

How to get rid of white-space at the bottom of div element when text is entered

I have a blank HTML page and I want to align 2 elements...Vertically and Horizontally. These elements are a <img> tag, a <p> tag for text, and 2 <div> tags for containing those elements...
When I resize my window I don't want these elements to be cut-off by my browser. After countless hours of trying to figure this out, and searching Stack and various other websites...I came close, but I could never get it 100% like I want it...
There's this white-space at the bottom and the ride side of the bordered second div near the text, and the culprit appears to be the <p>. When I get rid of the tag the white-space goes away. However, I want the text under the image so I need it...
The white-space is making me question whether the content is placed in the center or not. How can I get rid of it?
HTML
<body>
<div id="container">
<div id="content">
<p>
<img src="http://www.iconsdb.com/icons/preview/blue/square-xxl.png" alt="Under Construction">
<br> UNDER CONSTRUCTION!
</p>
</div>
</div>
</body>
CSS
body
{
margin:0;
background-color: seagreen;
}
#container
{
position:relative;
height:100%;
width:100%;
min-width:400px;
}
#content
{
position:absolute;
top:50%;
left:50%;
transform:translate(-50%,-50%);
outline:3px solid red;
}
#content p
{
margin:0;
text-align:center;
font-family:Courier;
font-size:48px;
white-space:nowrap;
color:springgreen;
}
I changed you HTML to enclose your text in a span tag and removed the br:
<body>
<div id="container">
<div id="content">
<p>
<img src="http://www.iconsdb.com/icons/preview/blue/square-xxl.png" alt="Under Construction">
<span>UNDER CONSTRUCTION!</span>
</p>
</div>
</div>
</body>
Then I added this to your CSS. It styles the enclosing span as a block, so you don't need to <br> tag in your HTML. It also uses line-height to adjust spacing above and below the line of text.
#content span {
display: block;
margin: 0;
line-height: .8;
}
And removed the position attribute from here:
#container
{
/*position:relative;*/ /* Removed */
height:100%;
width:100%;
min-width:400px;
}
Here is a sample fiddle
UPDATE
It appears the reason why you are seeing white-space still on Firefox is that you are using outline instead of border on your CSS for #content.
I don't know exactly why Firefox is rendering the outline differently. But if you change your CSS for #content to the following, you'll get the same result on Chrome, Firefox, Edge and IE (11).:
#content
{
position:absolute;
top:50%;
left:50%;
transform:translate(-50%,-50%);
/*outline:3px solid red;*/
border: 3px solid red;
}
Here is the updated fiddle
I have gone through your code . i have made some changes in above given code . I hope this gone be helpful to you.
CSS
body
{
margin:0;
background-color: seagreen;
}
img{
display: block;
margin: auto;
width: 50%;
}
/* add this css to remove the white space under text */
p
{
margin-bottom: -9px !important;
}
#container
{
position:relative;
height:100%;
width:100%;
min-width:400px;
}
#content
{
position:absolute;
top:50%;
left:50%;
transform:translate(-50%,-50%);
outline:3px solid red;
margin-top: 200px;
padding-top: 10px;
}
#content p
{
margin:0;
text-align:center;
font-family:Courier;
font-size:48px;
white-space:nowrap;
color:springgreen;
}
<div id="container">
<div id="content">
<img src="http://spectrumapartments.com.au/wp-content/themes/spectrumapartments/img/building/red-squares.png" alt="Under Construction">
<br>
<p>UNDER CONSTRUCTION!</p>
</div>
</div>
I GAVE IT ANOTHER TRY, HOPEFULLY THIS WILL SOLVE IT FOR YOU. YOU SOUND VERY DESPERATE.
*{
border: 0;
margin: 0;
margin: 0;
}
.container {
font-size: 0;
}
.container span {
font-size: 35px;
background: #ff8ea1;
padding: 0;
margin: 0;
}
.container span.no-space {
display: inline-block;
vertical-align: top;
height: .75em;
line-height: .75em;
}
<div class="container">
<span>Under Construction</span>
<div style="height: 20px;"></div>
<span class="no-space">Under Construction</span>
</div>
TRY THIS ONE!

Positioning stacked divs at the bottom of a container

I'm building a staked bar graph in PHP. I need it to look like this:
Currently I am able to stack-up the gray and red values, but they are at the top of the .graph container. How do I align them at the bottom? I tried vertical-align: bottom but it did not really work.
<div class="graph">
<div class="bar">
<div class="views" style="height:'.$showViews.'px"></div>
<div class="actions" style="height:'.$showActions.'px"></div>
</div>
</div>
and CSS
.graph {
width: 200px;
height: 32px;
border-top: 1px solid #f5f5f5;
border-bottom: 1px solid #ccc;
background-color: #f5f5f5;
}
.graph .bar {
width: 10px;
float: left;
margin: 0 1px;
height:30
}
.graph .bar .views {
background-color: #ccc
}
.graph .bar .actions {
background-color: red
}
Here's my code on JSFIDDLE.
Thanks.
position:absolute will make your life easier!
Here's a fiddle:
http://jsfiddle.net/RZ8ye/1/
Basically, we're using position:absolute to stack the elements on top of one another. By giving the parent positioning (in our case, "relative"), we position the stacked elements relative to that. We set bottom, left, and right and then define the height with an inline style (percentage based, based on the parent)
just added position:relative value in CSS.
here is the modified code.
.graph .bar .views
{
background-color: #ccc;
height:10px; /*height can be added dynamically*/
position:relative;
}
.graph .bar .actions
{
background-color: red;
position:relative;
height:20px;
}
HTML looks like same as posted .
<div class="graph">
<div class="bar">
<div class="views" style="height:'.$showViews.'px"></div>
<div class="actions" style="height:'.$showActions.'px"></div>
</div>
<div class="bar">
<div class="views" style="height:'.$showViews.'px"></div>
<div class="actions" style="height:'.$showActions.'px"></div>
</div>
</div>
Here is the Demo. http://jsfiddle.net/HHnRQ/1/

How to position absolutely inside float:left?

Why adding of left rule changes behavior so drastically? Is it possible to position relative to default position?
http://jsfiddle.net/suzancioc/drDn3/6/
HTML:
<div class='level0'>
<div class='level1'>
Hello
</div>
<div class='level1'>
Hello
<div id='inner2'>inner2</div>
</div>
<div class='level1'>
Hello
<div id='inner3'>inner3</div>
</div>
</div>
CSS:
.level0 {
height:40px;
width: 500px;
background:red;
}
.level1 {
float:left;
margin:2px;
border-style: solid;
background: cyan;
}
#inner1 {
position: absolute;
background: green;
}
#inner2 {
position: absolute;
background: green;
left:0px;
}
#inner3 {
position: absolute;
background: green;
}
In order to position absolute something you need to assign that div(in your case) to a relative positioned parent
.level1 {
float:left;
margin:2px;
border-style: solid;
background: cyan;
position:relative;
}
Adding position:relative makes .level1 a sort of coordinate system for all elements inside of it.
Take a look at this JSFIDDLE

Overlay image over image with variable height in CSS

I have an image (base.jpg) which has the following css:
.thumb-image {
padding: 0;
border: none;
margin: 0 auto 5px;
display: block;
width: 205px;
background: #EEE;
color: #8A8989;
border-image: initial;}
<img class="thumb-image" src="base.jpg" alt="" onerror="this.src='thumb.png'">
The image height is variable. Is there anyway I can overlay another image (overlay.png which is the red image) on top of base.jpg on the bottom right cornerusing css by adding another class declaration to the above css?
Many thanks
You need a wrapper div and then absolute position the corner image.
<div id="wrap">
<img src="img/big.jpg" class="big" alt=""/>
<img src="img/corner.jpg" class="corner" alt=""/>
</div>
#wrap { position: relative; }
.big, .corner { display: block; }
.corner { position: absolute; right: 0; bottom: 0; }
There's not much you can do with just .thumb-image. If you modify the HTML somewhat, you can accomplish this fairly easily. I've put up an example here: http://jsfiddle.net/imsky/AsUuh/
This works IE8+ (with doctype), and across all other modern browsers, by using :before and generated content. You can convert it to use no modern features, but that would mean including an extra DIV inside each container. As an aside, :before doesn't work on IMG tags, so this is as minimal of markup as possible.
HTML:
<div class="thumb-container">
<div class="thumb-image">
<img src="http://placekitten.com/205/300">
</div>
</div>
CSS:
.thumb-image {
margin:0 auto 5px;
width:205px;
background:#EEE;
color:#8A8989;
border-image:initial;
position:relative;
z-index:0
}
.thumb-image img {
border:0;
display:block;
width:100%
}
.thumb-container {
position:relative
}
.thumb-image:before {
content:"";
display:block;
position:absolute;
width:100px;
height:100px;
bottom:0px;
right:0px;
z-index:1;
background:url(http://placekitten.com/100)
}

Resources