I need four boxes in the corners of the div. When hovering on some div, I want to display:block a hidden div in the middle, sharing one border with the box being currently hovered.
Here is jsfiddle with my current solution
It almost works fine. However, there are some bugs regarding the corner area. I display there the block element with background using :after. It is to achieve the effect of one border for two elements.
The problem:
So in Chrome hovering that area gives some strange interlacing effect. Each mouse movement by 1px hides and shows content div. You can see it here in action
In newest Firefox it seems to be ok, but in created jsfiddle there's some other bug which you can test yourself.
I'm using grey background just for better visualization of the problem. It is also suppose to work for box 1 for now. Tried some jQuery with mouseover and hover with no success.
EDIT - Final solution:
The most important thing was to set pointer-events: none; to after block element. Since I got some votes up here's more advanced code on jsfiddle using SASS
, and here's using plain css:
CSS:
.outer {
width: 90%;
height: 300px;
position: relative;
}
.box-content {
display: none;
width: 80%;
height: 80%;
position: absolute;
left: 10%;
top: 13%;
background: white;
z-index: 1;
}
.box {
width: 150px;
height: 60px;
position: absolute;
border: 1px solid white;
background: white;
}
.box:hover:after {
content: '';
background-color: white;
z-index: 2;
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
}
.box:hover p {
z-index: 3;
}
.box p {
position: absolute;
top: 23px;
left: 13px;
color: black;
padding: 0;
margin: 0;
}
.box-one {
top: 0;
left: 0;
}
.box-one:hover {
border: 1px solid blue;
}
.box-one:hover ~ .content-one {
border: 1px solid blue;
display: inline-block;
pointer-events: none;
}
.box-two {
top: 0;
right: 0;
}
.box-two:hover {
border: 1px solid red;
}
.box-two:hover ~ .content-two {
border: 1px solid red;
display: inline-block;
pointer-events: none;
}
.box-three {
bottom: 0;
left: 0;
}
.box-three:hover {
border: 1px solid yellow;
}
.box-three:hover ~ .content-three {
border: 1px solid yellow;
display: inline-block;
pointer-events: none;
}
.box-four {
bottom: 0;
right: 0;
}
.box-four:hover {
border: 1px solid green;
}
.box-four:hover ~ .content-four {
border: 1px solid green;
display: inline-block;
pointer-events: none;
}
HTML
<div class="outer">
<div class="box box-one">
<p>BOX NAME 1</p>
</div>
<div class="box box-two">
<p>BOX NAME 2</p>
</div>
<div class="box box-three">
<p>BOX NAME 3</p>
</div>
<div class="box box-four">
<p>BOX NAME 4</p>
</div>
<div class="box-content content-one"></div>
<div class="box-content content-two"></div>
<div class="box-content content-three"></div>
<div class="box-content content-four"></div>
</div>
The reason it is "flickering" is unrelated to the pseudo-element; it's because you are partially overlaying the .box-content element over the .box elements so, when you move your mouse, it is no longer hovering over the .box element that triggered the .box-content element to display, rather it is hovering over the .box-content element itself so it disappears. Move your mouse again and it's hovering over the .box element once more, triggering .box-content to display again. To fix this, simply add a :hover pseudo-class to the .box-content element, like so:
.outer {
width: 100%;
height: 300px;
position: relative;
}
.box {
width: 150px;
height: 60px;
border: 3px solid black;
position: absolute;
z-index: 1;
}
.box:after {
content: '';
background-color: grey;
z-index: -1;
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
}
.box:hover ~ .box-content {
display: inline-block;
}
.box p {
color: white;
padding: 10px;
z-index: 1000;
}
.box-content {
display: none;
width: 80%;
height: 80%;
border: 3px solid black;
position: absolute;
left: 10%;
top: 11%;
}
.box-content:after {
content: "";
background-color: grey;
z-index: 2;
display: block;
position: absolute;
width: 20%;
height: 30%;
left: 0;
top: 0;
}
.box-content:hover {
display: inline-block;
}
.box-two {
top: 0;
right: 0;
}
.box-three {
bottom: 0;
left: 0;
}
.box-four {
bottom: 0;
right: 0;
}
<div class="outer">
<div class="box">
<p>BOX NAME 1</p>
</div>
<div class="box box-two">
<p>BOX NAME 2</p>
</div>
<div class="box box-three">
<p>BOX NAME 3</p>
</div>
<div class="box box-four">
<p>BOX NAME 4</p>
</div>
<div class="box-content"></div>
</div>
Alternatively, if you do not wish .box-content to remain visible while hovering over it but not hovering over one of the .box elements then add the pointer-events property to .box-content, like so:
.outer {
width: 100%;
height: 300px;
position: relative;
}
.box {
width: 150px;
height: 60px;
border: 3px solid black;
position: absolute;
z-index: 1;
}
.box:after {
content: '';
background-color: grey;
z-index: -1;
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
}
.box:hover ~ .box-content {
display: inline-block;
}
.box p {
color: white;
padding: 10px;
z-index: 1000;
}
.box-content {
display: none;
width: 80%;
height: 80%;
border: 3px solid black;
position: absolute;
left: 10%;
top: 11%;
}
.box-content:after {
content: "";
background-color: grey;
z-index: 2;
display: block;
position: absolute;
width: 20%;
height: 30%;
left: 0;
top: 0;
pointer-events: none;
}
.box-two {
top: 0;
right: 0;
}
.box-three {
bottom: 0;
left: 0;
}
.box-four {
bottom: 0;
right: 0;
}
<div class="outer">
<div class="box">
<p>BOX NAME 1</p>
</div>
<div class="box box-two">
<p>BOX NAME 2</p>
</div>
<div class="box box-three">
<p>BOX NAME 3</p>
</div>
<div class="box box-four">
<p>BOX NAME 4</p>
</div>
<div class="box-content"></div>
</div>
Note, though, that Opera Mini doesn't support pointer-events and IE only added support in v11.
Related
I've tried the method showed here, but it aint worked out:
StackOverflow: Display div border on top of another div [duplicate] (my own postage)
I have 2 nested divs, and the above method isn't working for both divs.
Here is the code:
html:
<div className="App">
<div class="box mainClass1" id="1">
<div class="helperClass" />
</div>
<div class="box mainClass2" id="2">
<div class="helperClass" />
</div>
</div>
css:
.box {
width: 100px;
height: 100px;
position: absolute;
}
.mainClass1 {
top: 100px;
left: 100px;
background: teal;
}
.mainClass2 {
top: 150px;
left: 150px;
background: red;
}
.helperClass2 .helperClass {
width: 100%;
height: 100%;
border: 3px solid #4286f4;
box-sizing: border-box;
}
note: helperClass2 should be added to the list of classes of parent (outer) div.
.container {
position: relative;
}
.box1 {
width: 100px;
height: 100px;
position: absolute;
top: 100px;
left: 100px;
box-sizing: border-box;
background: blue;
}
.box1:after {
content: '';
display: block;
width: 95px;
height: 95px;
position: absolute;
border: 3px solid black;
z-index: 3;
}
.box2 {
width: 100px;
height: 100px;
position: absolute;
top: 150px;
left: 150px;
box-sizing: border-box;
background: red;
border: 3px solid black;
}
<div class="container">
<div class="box1"></div>
<div class="box2"></div>
</div>
I was building a meme with top and bottom text.
I am in need of rotating an image so I did it with transform: rotate(90deg);, but it's overlapped parent's div like the following example.
h1 {
margin-top: 100px;
}
.parent {
border: 1px solid black;
display: inline-block;
position: relative;
background: #777;
}
.parent .rotate {
transform: rotate(90deg);
width: 100px;
}
.parent h4 {
position: absolute;
left: 0;
right: 0;
text-align: center;
color: white;
z-index: 1;
}
.parent .top {
top: 10px;
}
.parent .bottom {
bottom: 10px;
}
<div class="parent">
<h4 class="top">Top Text</h4>
<img class="rotate" src="http://2.bp.blogspot.com/-7uOyzdXhG2Y/UVpJUbwqGzI/AAAAAAAAAo0/35w5N8tPvHE/s640/iphone-5-hd-wallpapers-hd-41664-tpmw7.jpg" />
<h4 class="bottom">Bottom Text</h4>
</div>
How can I change the style of the parent div to match the position and size of the rotated image?
First, we need to change the height of the div to be same as width.
We can do it by
.parent{
background-color: red;
width: 100%;
padding-top: 100%; /* 1:1 Aspect Ratio */
position: relative; /* If you want text inside of it */
}
Second, we need an additional div inside it that has absolute position with full width and height of it.
We can use flex to center the image inside that absolute div.
Here is a working code.
h1 {
margin-top: 100px;
}
.container {
display: inline-block;
width: 300px;
}
.parent {
border: 1px solid black;
display: inline-block;
position: relative;
background: #777;
width: 100%;
padding-top: 100%;
position: relative;
}
.p-absolute {
bottom: 0;
position: absolute;
top: 0;
left: 0;
right: 0;
margin-top: auto;
margin-bottom: 0;
display: flex;
align-items: center;
justify-content: center;
}
.parent .rotate {
transform: rotate(90deg);
max-width: 100%;
max-height: 100%;
}
.parent h4 {
position: absolute;
left: 0;
right: 0;
text-align: center;
color: white;
z-index: 1;
}
.parent .top {
top: 10px;
}
.parent .bottom {
bottom: 10px;
}
<div class="container">
<div class="parent">
<h4 class="top">Top Text</h4>
<div class="p-absolute">
<img class="rotate" src="http://2.bp.blogspot.com/-7uOyzdXhG2Y/UVpJUbwqGzI/AAAAAAAAAo0/35w5N8tPvHE/s640/iphone-5-hd-wallpapers-hd-41664-tpmw7.jpg" />
</div>
<h4 class="bottom">Bottom Text</h4>
</div>
</div>
I use a zoom tools with javascript and I have an issue with the CSS part because of the lens.
I would like the red square (a video link) to always be on top by modifying only the CSS if possible. But I am not able to do it, the lens hide always the element no matter the z-index attribute I affect to each class...
Have you some idea?
You can see the code HTML and CSS on the following link :
http://jsfiddle.net/8scpq7vz/
.cloudzoom-blank {
z-index: 10;
width: 465px;
height: 465px;
}
.cloudzoom-lens {
border: 1px solid #000;
width: 200px;
height: 200px;
cursor: move;
z-index: 10;
}
.product-essential {
position: relative;
top: 0;
left: 0;
}
.product-essential-b1 {
float: left;
}
.product-img-box {
position: relative;
z-index: 3;
float: left;
width: 465px;
margin-left: 16px;
background: #FFF;
}
.product-img-box .product-image {
width: 465px;
height: 465px;
padding: 0px;
border: 2px solid #E8E8E8;
background: #FFF;
}
.product-img-box .product-image div {
position: relative;
}
.product-img-box .product-image img#image {
position: relative;
width: 465px;
height: 465px;
opacity: 0.85;
}
.product-img-box .video {
position: absolute;
left: 10px;
top: 10px;
z-index: 100;
cursor: pointer;
}
.v2 {
float: left;
width: 41px;
height: 41px;
text-indent: -9999px;
background: #F22;
}
<div class="product-essential">
<div class="product-essential-b1">
<div class="product-img-box">
<p class="video" id="video-btn">
<a target="_blank" class="v2">Video</a>
</p>
<div class="ldesktop">
<div id="product-media">
<div class="product-image">
<div>
<img src="https://i.pinimg.com/474x/9d/d3/e5/9dd3e53d4d13d5000ef67028c8b03998.jpg" id="image">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cloudzoom-blank" style="position: absolute; top: 28px; left: 29px;">
<div class="cloudzoom-lens" style="overflow: hidden; position: absolute; top: 27px; left: 15px;">
<img style="position: absolute; left: -18px; top: -46px; max-width: none; width: 465px; height: 465px;" src="https://i.pinimg.com/474x/9d/d3/e5/9dd3e53d4d13d5000ef67028c8b03998.jpg">
</div>
</div>
Thank you.
Simply remove z-index from product-img-box to avoid creating a stacking context and be able to place the red square on the top:
.cloudzoom-blank {
z-index: 10;
width: 465px;
height: 465px;
}
.cloudzoom-lens {
border: 1px solid #000;
width: 200px;
height: 200px;
cursor: move;
z-index: 10;
}
.product-essential {
position: relative;
top: 0;
left: 0;
}
.product-essential-b1 {
float: left;
}
.product-img-box {
position: relative;
float: left;
width: 465px;
margin-left: 16px;
background: #FFF;
}
.product-img-box .product-image {
width: 465px;
height: 465px;
padding: 0px;
border: 2px solid #E8E8E8;
background: #FFF;
}
.product-img-box .product-image div {
position: relative;
}
.product-img-box .product-image img#image {
position: relative;
width: 465px;
height: 465px;
opacity: 0.85;
}
.product-img-box .video {
position: absolute;
left: 10px;
top: 10px;
z-index: 100;
cursor: pointer;
}
.v2 {
float: left;
width: 41px;
height: 41px;
text-indent: -9999px;
background: #F22;
}
<div class="product-essential">
<div class="product-essential-b1">
<div class="product-img-box">
<p class="video" id="video-btn">
<a target="_blank" class="v2">Video</a>
</p>
<div class="ldesktop">
<div id="product-media">
<div class="product-image">
<div>
<img src="https://i.pinimg.com/474x/9d/d3/e5/9dd3e53d4d13d5000ef67028c8b03998.jpg" id="image">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cloudzoom-blank" style="position: absolute; top: 28px; left: 29px;">
<div class="cloudzoom-lens" style="overflow: hidden; position: absolute; top: 27px; left: 15px;">
<img style="position: absolute; left: -18px; top: -46px; max-width: none; width: 465px; height: 465px;" src="https://i.pinimg.com/474x/9d/d3/e5/9dd3e53d4d13d5000ef67028c8b03998.jpg">
</div>
</div>
Related:
Why can't an element with a z-index value cover its child?
I have position but z index is not working
Have you tried this adding position:fixed to you .v2 class? Like this:
.v2 {
float: left;
width: 41px;
height: 41px;
text-indent: -9999px;
background: #F22;
position: fixed;
}
Not sure if that's what you looking for, but seems to be.
The video element's parent element product-essential is having lower z-index (0 as default) to it's sibling cloudzoom-blank. So elements in cloudzoom-blank is displayed above any element inside product-essential. (also if both have equal z-index, then one specified last will be on top)
You can change the arrangement of the elements if it is possible
Simple question, but don't know how to google it.
When no quantity to draw line. How to draw in list box something like this?
Just need simple css answer. Thank you.
You can do that by adding a pseudo element to the quantity element.
.quantity {
display: inline-block;
padding: 8px 12px;
border: 1px solid #000;
position: relative;
overflow: hidden;
}
.quantity--strikethrough:before {
content: '';
width: 100%;
height: 2px;
background: blue;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%) rotate(-45deg);
transform-origin: 50% 50%;
}
<div class="quantity">
6 vnt.
</div>
<div class="quantity quantity--strikethrough">
12 vnt.
</div>
If you simply want to draw a line on the element, this could be of use to you. Hope it helps!
.element{
position:relative;
display: inline-block;
}
.element:before{
content: "";
height: 1px;
width: 100%;
position:absolute;
top:50%;
background-color: #000;
}
<p class="element">Empty</p>
.block {
border: 1px solid;
display: inline-block;
margin: 2px;
padding: 0 2px;
position: relative;
overflow: hidden;
}
.line:before {
position: absolute;
content: "";
left: -10%;
top: 50%;
right: -10%;
border-top: 2px solid blue;
transform: rotate(-35deg);
}
<div class="block">100</div>
<div class="block line">50</div>
<div class="block line">50</div>
<div class="block line">150</div>
here is my try
div {
position: relative;
display: inline-block;
border: 1px solid #333;
padding: 10px;
float: left;
margin: 10px;
}
.cutoff {
overflow: hidden;
}
.cutoff::after {
height: 1px;
content: '';
width: 100%;
position: absolute;
background-color: blue;
left: 0;
transform: rotate(145deg);
top: 20px;
}
<div class="">
some text
</div>
<div class="cutoff">
some text
</div>
<div class="cutoff">
some text
</div>
I have an element with class A, with rules:
.A {
left: 0px;
}
For some reason, I need to make the element at the right position inside the container and I cannot exclude A class.
Specificity is your friend here:
Here's a couple of options....
Override the class using additional specificity such as another class (or ID)
.container {
height: 50vh;
position: relative;
width: 50%;
bordeR: 1px solid grey;
margin: auto;
}
.A {
position: absolute;
height: 25px;
width: 25px;
left: 0;
background: #000;
margin-bottom: 1em;
}
.A.right {
left: auto;
right: 0;
background: red;
}
<div class="container">
<div class="A"></div>
<div class="A right"></div>
</div>
or a nth-child selector
.container {
height: 50vh;
position: relative;
width: 50%;
bordeR: 1px solid grey;
margin: auto;
}
.A {
position: absolute;
height: 25px;
width: 25px;
left: 0;
background: #000;
margin-bottom: 1em;
}
.A:nth-child(2) {
left: auto;
right: 0;
background: red;
}
<div class="container">
<div class="A"></div>
<div class="A"></div>
</div>