Trigger CSS :hover only when hovering over border box - html

I am creating a rectangular outline with a 5px thin border box around an empty <div>. When the user hovers over the border I want the border to change colour. That's all working fine, but I don't want the border to remain changed when the user's mouse is inside the <div> because it's no longer actually on the border.
See an example here:
https://jsfiddle.net/qbcc1trt/
.outer {
position: relative;
overflow: hidden;
display: inline-block;
}
.myborder {
content: '';
position: absolute;
bottom: 5%;
left: 20%;
width: 40%;
height: 50%;
box-shadow: inset 0px 0px 0px 5px rgba(0, 0, 0, 0.6);
}
.myborder:hover {
content: '';
position: absolute;
bottom: 5%;
left: 20%;
width: 40%;
height: 50%;
box-shadow: inset 0px 0px 0px 5px rgba(100, 200, 100, 0.6);
}
<div class="outer">
<img src="https://s-media-cache-ak0.pinimg.com/736x/ff/00/5e/ff005e0fa600c51c5e36f6059bbe6053.jpg">
<div class="myborder"></div>
</div>
Any way to accomplish this?

:hover events only work on the top most element (and the elements inside). So you can achieve this effect by making another div the same size as your myborder but subtracting the size of the border. Then place it directly above myborder.
This way, the hover event will trigger while over the border (box shadow in your case) but no the inside. See the demo below
.outer {
position: relative;
overflow: hidden;
display: inline-block;
}
.myborder {
position: absolute;
bottom: 5%;
left: 20%;
width: 40%;
height: 50%;
box-shadow: inset 0px 0px 0px 5px rgba(0, 0, 0, 0.6);
}
.hover-cover {
position: absolute;
bottom: calc(5% + 5px);
left: calc(20% + 5px);
box-shadow: none;
z-index: 1;
width: calc( 40% - 10px);
height: calc( 50% - 10px);
}
.myborder:hover {
box-shadow: inset 0px 0px 0px 5px rgba(100, 200, 100, 0.6);
}
<div class="outer">
<img src="https://s-media-cache-ak0.pinimg.com/736x/ff/00/5e/ff005e0fa600c51c5e36f6059bbe6053.jpg">
<div class="hover-cover"></div>
<div class="myborder"></div>
</div>

I know the answer has been marked as answered but I found a solution that doesn't use calc but nth-child instead which has better compatibility table than calc.
.outer {
position: relative;
overflow: hidden;
display: inline-block;
}
.myborder {
content: '';
position: absolute;
bottom: 5%;
left: 20%;
width: 40%;
height: 50%;
}
.myborder div:nth-child(1) {
box-shadow: inset 0px 0px 0px 5px rgba(0, 0, 0, 0.6);
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.myborder div:nth-child(1):hover {
box-shadow: inset 0px 0px 0px 5px rgba(100, 200, 100, 0.6);
}
.myborder div:nth-child(2) {
position: absolute;
top: 5px;
right: 5px;
bottom: 5px;
left: 5px;
}
<div class="outer"><img src="https://s-media-cache-ak0.pinimg.com/736x/ff/00/5e/ff005e0fa600c51c5e36f6059bbe6053.jpg">
<div class="myborder">
<div></div>
<div></div>
</div>
</div>

It's almost the same solution as the one provided by #Kevin:
https://jsfiddle.net/qbcc1trt/1/
The idea is to put two elements, one (B) above the other one (A), so when the user will :hover element B he will actually not :hover element A.
You need to make sure the element B is not inside element A
.outer {
position: relative;
overflow: hidden;
display: inline-block;
}
.borderContainer {
position: absolute;
bottom: 5%;
left: 20%;
width: 40%;
height: 50%;
}
.myborder {
content: '';
box-shadow: inset 0px 0px 0px 5px rgba(0, 0, 0, 0.6);
width: 100%;
height: 100%;
}
.inner {
position: absolute;
width: calc(100% - 5px * 2);
height: calc(100% - 5px * 2);
top: 5px;
left: 5px;
z-index: 100;
}
.myborder:hover {
content: '';
box-shadow: inset 0px 0px 0px 5px rgba(100, 200, 100, 0.6);
width: 100%;
height: 100%;
}
<div class="outer"><img src="https://s-media-cache-ak0.pinimg.com/736x/ff/00/5e/ff005e0fa600c51c5e36f6059bbe6053.jpg">
<div class="borderContainer">
<div class="myborder">
</div>
<div class="inner">
</div>
</div>
</div>
Note the I used here a parent container (which might be easier, depending on your solution).

Related

Is is possible to add grain to box-shadow?

A client wants the grain effect on the box-shadow of this modal.
You can see the grain the box-shadow in the screenshot attached.
How can I achive this using CSS? I've looked but couldn't find anything.
Tried adding an after with a grain effect and applying the box-shadow to it
.grain {
content: "";
display: block;
position: absolute;
width: 100%;
height: 100%;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
background: transparent url(grain.jpg) 0 0;
background-size: 320px 320px;
opacity: .1!important;
z-index: 30;
background-color: #000;
pointer-events: none;
box-shadow: 10px 10px 0px 0px rgba(0,0,0,0.75);
-webkit-box-shadow: 10px 10px 0px 0px rgba(0,0,0,0.75);
-moz-box-shadow: 10px 10px 0px 0px rgba(0,0,0,0.75);
}
You need to use a .png image with transparency; I found a bunch here.
Set position: relative on your element, and position: absolute on the ::after pseudo-element.
.grainy-shadow {
position: relative;
width: 200px;
height: 200px;
background: blue;
margin: 2rem;
}
.grainy-shadow::after {
content: '';
position: absolute;
top: 1rem;
left: 1rem;
bottom: -1rem;
right: -1rem;
z-index: -1;
background-image: url("https://www.transparenttextures.com/patterns/black-orchid.png");
}
body {
background: url(https://img.freepik.com/premium-vector/colorful-random-shapes-abstract-background-geometrical-circle-background-with-copy-space-yellow_655111-46.jpg?w=2000);
}
<div class="grainy-shadow">
<h1>content</h1>
</div>

How do I create a responsive, scaling "pile" effect on divs in CSS?

How can I create a "pile" effect like in this picture?
I'd like the formation/spacing of the pile to stay as it is, and for the pile to shift left or right as the window is resized.
I've been fiddling around with absolute/relative positioning, but I'm a CSS newbie and I'm not sure if this is the way to go.
This is what I have so far:
.boxes {
position: absolute;
top: 100px;
}
.box1 {
position: relative;
left: 10vw;
width:fit-content;
padding: 0px 10px 4px 10px;
box-shadow: 0px 0px 0px 2px rgba(0, 0, 0);
}
.box2 {
position: relative;
width:fit-content;
left: 16vw;
bottom: 13vh;
padding: 0px 10px 4px 10px;
box-shadow: 0px 0px 0px 2px rgba(0, 0, 0);
transform: rotate(10.84deg);
}
.box3 {
position: relative;
width:fit-content;
left: 25vw;
bottom: 20vh;
padding: 0px 10px 4px 10px;
box-shadow: 0px 0px 0px 2px rgba(0, 0, 0);
transform: rotate(21deg);
}
<div class="boxes">
<div class="box1">box1</div>
<div class="box2">box2</div>
<div class="box3">box3</div>
</div>
You should set boxes to relative and it's child absolute like:
.boxes {
top: 100px;
position: relative;
}
.box1 {
position: absolute;
...
}
.box2{
position: relative;
width: 70px;
left: 19vw;
bottom: 7vh;
width: 70px;
transform: rotate(16deg);
padding: 0px 10px 4px 10px;
box-shadow: 0px 0px 0px 2px rgb(0 0 0);
}
.box3 {
left: 27vw;
width: 40px;
bottom: 32vh;
position: relative;
padding: 0px 10px 4px 10px;
transform: rotate(-4deg);
box-shadow: 0px 0px 0px 2px rgb(0 0 0);
}
//etc..
Try to play with rotate transform - MDN
I have created a #wrapper with a width and a height. Then I gave the wrapper position: relative; because we will position the single elements with position: absolute;.
Here is the code I used:
* {
margin: 0;
padding: 0;
}
#wrapper {
position: relative;
height: 150px;
width: 450px;
border: 1px solid;
}
#blue {
width: 100px;
height: 30px;
background: blue;
position: absolute;
bottom: 0;
left: 70px;
}
#purple {
width: 100px;
height: 30px;
background: purple;
position: absolute;
bottom: 20px;
left: 145px;
transform: rotate(25deg);
}
#green {
width: 50px;
height: 30px;
background: green;
position: absolute;
bottom: 63px;
left: 177px;
transform: rotate(-10deg);
}
#red {
width: 100px;
height: 30px;
background: red;
position: absolute;
bottom: 21px;
left: 220px;
transform: rotate(28deg);
}
<div id="wrapper">
<div id="blue"></div>
<div id="purple"></div>
<div id="green"></div>
<div id="red"></div>
</div>

HTML / CSS columns overlap

I made a fiddle here.
Why does dice-canvas-container use the full width of the window and not stop at the start of attack-canvas-container?
Is it because both columns are positioned absolute?
<div id="attack-container">
<div id="dice-canvas-container">
<div id="plyra-dice-canvas"></div>
<div id="plyrb-dice-canvas"></div>
</div>
<div id="attack-canvas-container">
..................
</div>
</div>
If suitable with your requirement then you can go with flex css instead of position: absolute
#attack-container {
padding: 0;
text-align: center;
/* position: absolute; */
max-width: 1728px;
max-height: 1080px;
width: 100%;
height: 100%;
left: 0;
top: 0;
z-index: 1047;
/* float:left; */
display:flex;
}
#dice-canvas-container {
padding: 0;
text-align: center;
max-width: 1428px !important;
max-height: 1080px;
width: 100%;
height: 100%;
/* position: absolute; */
left: 0;
top: 0;
opacity: 0.8;
z-index: 1048;
/* display: block; */
/* float:left; */
background-color: red;
min-height:150px;
}
#attack-canvas-container {
#extend %background-gradient;
max-width: 300px;
max-height: 1080px;
font-size: 90%;
width: 100%;
height: 100%;
z-index: 1048;
/* position: absolute; */
right: 0;
top: 0;
box-sizing: border-box;
text-align: left;
-webkit-box-shadow: -4px 0px 5px 0px rgba(50, 50, 50, 0.3);
-moz-box-shadow: -4px 0px 5px 0px rgba(50, 50, 50, 0.3);
box-shadow: -4px 0px 5px 0px rgba(50, 50, 50, 0.3);
min-height: 150px;
}
#plyra-dice-canvas,
#plyrb-dice-canvas {
padding: 0;
text-align: left;
position: absolute;
max-width: 1428px;
max-height: 540px;
width: 100%;
height: 50%;
z-index: 1049;
}
#plyra-dice-canvas {
left: 0;
top: 0;
height: 50%;
}
#plyrb-dice-canvas {
left: 0;
top: 50%;
height: 50%;
border-top: 1px solid $brand-primary;
}
<div id="attack-container">
<div id="dice-canvas-container">
<div id="plyra-dice-canvas"></div>
<div id="plyrb-dice-canvas"></div>
</div>
<div id="attack-canvas-container">
</div>
</div>
Check the updated Fiddle.
Because you are using position: absolute (1) with width: 100% (2) for both containers in combination with z-index (3).
(1) : because of this, both container are absolutely positioned, not relatively.
(2) : since both have 100% width, they will overlap the other one.
(3) : the one with the higher z-index wins the upper hand.
You need to change the absolute positioning and give proper widths to the divs.

Diagonal overlay on background image with double border

I'm trying to create an image like this in CSS.
I have the following code.
#sample {
height: 200px;
width: 300px;
background: url(https://placeholdit.imgix.net/~text?txtsize=28&bg=0099ff&txtclr=ffffff&txt=300%C3%97300&w=300&h=300&fm=png);
position: relative;
}
#overlay {
height: 300px;
width: 100px;
background: #444;
border-left: 3px solid green;
position: absolute;
right: -20px;
}
<div id="sample">
<div id="overlay">
</div>
</div>
Can I make such an structure using CSS alone ?
You need not use an overlay div. You can use an :after element to achieve the same.
Use overflow:hidden on parent and rotate the after element.
Shadow can be used for double border.
Here is a working example.
#sample {
height: 200px;
width: 300px;
background: url(https://placeholdit.imgix.net/~text?txtsize=28&bg=0099ff&txtclr=ffffff&txt=300%C3%97300&w=300&h=300&fm=png);
position: relative;
overflow: hidden;
}
#sample:after {
height: 300px;
width: 100px;
content: " ";
background: #444;
border-left: 3px solid green;
position: absolute;
right: -20px;
transform: rotate(25deg);
-webkit-box-shadow: -6px 0px 0px 0px rgba(68, 68, 68, 1);
-moz-box-shadow: -6px 0px 0px 0px rgba(68, 68, 68, 1);
box-shadow: -4px 0px 0px 0px rgba(68, 68, 68, 1);
}
<div id="sample">
</div>
To achieve the result parent div need to be set relative and then set it overflow hidden. To let rest of the child div will be invisible.
After that your child div use transform to rotate the element.
HTML
<div id="sample">
<div id="overlay">
</div>
</div>
CSS
#sample {
height: 200px;
width: 300px;
background: url(https://placeholdit.imgix.net/~text?txtsize=28&bg=0099ff&txtclr=ffffff&txt=300%C3%97300&w=300&h=300&fm=png);
position: relative;
overflow: hidden;
}
#overlay {
height: 300px;
width: 100px;
background: #444;
border-left: 3px solid green;
position: absolute;
right: -20px;
transform: rotate(25deg);
}
DEMO

Create a css folded-corner with shadow

How can I do a folded-corner with external shadow which continues to the parent div shadow, like that :
Thanks.
CSS Backgrounds and Borders Module Level 4 introduces the corner-shape property:
By default, non-zero border-radii define a quarter-ellipse that rounds
the affected corners. However in some cases, other corner shapes are
desired. The corner-shape property specifies a reinterpretation
of the radii to define other corner shapes.
In your case, you should set it to bevel:
Border radii define a diagonal slice at the corner.
The code would be something like
corner-shape: bevel;
border-radius: 0 0 30px;
box-shadow: 0 0 5px #000;
However, this spec is a draft not ready for implementation. So browsers haven't implemented it. But you can use corner-shape preview to see how it would look like.
tried this one, a bit complex, but it works
.box {
width: 200px;
height: 100px;
position: relative;
padding: 25px;
background: none;
}
.box .content {
position: relative;
z-index: 2;
}
.box .the_background {
position: absolute;
left: 0;
top: 0;
z-index: 1;
width: 100%;
height: 100%;
}
.box .the_background .square-top-right {
width: 250px;
height: 125px;
position: absolute;
top: 0;
left: 0;
background: #fff;
display: block;
z-index: 3;
}
.box .the_background .square-bottom-left {
width: 225px;
height: 150px;
position: absolute;
bottom: 0;
left: 0;
background: #fff;
display: block;
z-index: 3;
}
.box .the_background:after {
content: '';
width: 35px;
height: 35px;
background: #ddd;
position: absolute;
z-index: 2;
right: 7px;
bottom: 7px;
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
transform: rotate(45deg);
-webkit-box-shadow: 0px 0px 10px #000;
-moz-box-shadow: 0px 0px 10px #000;
box-shadow: 0px 0px 10px #000;
}
.box .the_background .square-shadow {
position: absolute;
left: 0;
top: 0;
z-index: 1;
width: 100%;
height: 100%;
}
.box .the_background .square-shadow:before {
content: '';
position: absolute;
left: 0;
width: 250px;
height: 125px;
-webkit-shadow: 0px 0px 10px #000;
-moz-shadow: 0px 0px 10px #000;
box-shadow: 0px 0px 10px #000;
}
.box .the_background .square-shadow:after {
content: '';
position: absolute;
bottom: 0;
left: 0;
width: 225px;
height: 25px;
-webkit-shadow: 0px 0px 10px #000;
-moz-shadow: 0px 0px 10px #000;
box-shadow: 0px 0px 10px #000;
}
<div class="box">
<div class="content">
Lorem ipsum...
</div>
<div class="the_background">
<div class="square-top-right"></div>
<div class="square-bottom-left"></div>
<div class="square-shadow"></div>
</div>
</div>