I am creating a slider in CSS and I'm trying to make the child element slide the length of the parent container, no matter what the width of the parent container is. I currently have the width of the parent container set to 22%. This is my first attempt at trying to make the child slide the length of the parent:
div.parent {
position: absolute;
display: block;
width: 22%;
height: 60px;
background: #333;
color: white;
cursor: pointer;
margin: 10px;
overflow: hidden;
min-width: 60px;
max-width: 30%;
}
div.child {
position: absolute;
height: 60px;
width: 60px;
background-color: #888;
z-index: 0;
color: #333;
text-align: center;
line-height: 60px;
font-size: 40px;
transition: .5s;
}
div.parent:hover div.child {
transform: translateX(100%);
background: orange;
color: #fff;
}
<div class="slider-container">
<div class="parent">
<div class="child">
</div>
</div>
</div>
Obviously, that's not the solution. I found this JSFiddle that had a pretty good solution. However, when I move mouse within the parent container, the child moves back and forth (see example below). I would like the child element to stay still regardless if the mouse is moving within the slider or not (just like how it behaves in the snippet above).
div.parent {
position: absolute;
display: block;
width: 22%;
height: 60px;
background: #333;
color: white;
cursor: pointer;
margin: 10px;
overflow: hidden;
min-width: 60px;
max-width: 30%;
}
div.child-wrapper{
position: absolute;
width: 100%;
height: 100px;
transition: all .5s;
z-index: 1;
background: #333;
}
div.child {
position: absolute;
height: 60px;
width: 60px;
background-color: #888;
transition: inherit;
z-index: 0;
color: #333;
text-align: center;
line-height: 60px;
font-size: 40px;
}
div.child-wrapper:hover {
transform: translateX(100%);
background: orange;
}
div.child-wrapper:hover div.child {
transform: translateX(-100%);
background: orange;
color: #fff;
}
<div class="slider-container">
<div class="parent">
<div class="child-wrapper">
<div class="child">
</div>
</div>
</div>
</div>
Is there another way that I can make the child element slide the length of the parent element, regardless of the width of the parent?
Since you're using an absolutely positioned child you can use the left (and right, top, etc) properties to position the child. See below.
Remember transform% is the percentage of the element's width, not the parent's. If you see the child cube, translateX just moves it the width of the child in the X direction
div.parent {
position: absolute;
display: block;
width:52%;
height: 60px;
background: #333;
color: white;
cursor: pointer;
margin: 10px;
overflow: hidden;
min-width: 60px;
max-width: 30%;
}
div.child {
position: absolute;
height: 60px;
width: 60px;
left:0;
background-color: #888;
transition: inherit;
z-index: 0;
color: #333;
text-align: center;
line-height: 60px;
font-size: 40px;
transition: .5s;
}
div.parent:hover div.child {
left:100%;
transform: translateX(-100%);
background: orange;
color: #fff;
}
<div class="slider-container">
<div class="parent">
<div class="child">
</div>
</div>
</div>
Even simpler version here using margin-left instead:
.parent {
width: 52%;
height: 60px;
background: #333;
color: white;
cursor: pointer;
margin: 10px;
}
.child {
height: 100%;
width: 60px;
margin-left: 0;
background-color: #888;
color: #333;
text-align: center;
line-height: 60px;
font-size: 40px;
transition: 0.5s;
}
.parent:hover .child {
margin-left: 100%;
translate: -100%;
background: orange;
}
<div class="parent">
<div class="child">
</div>
</div>
Related
I need to scale an image when I hover button/block that contains it but It doesn't work.
If I hover only image it scales perfectly but when I try to hover block it doesn't affect image.
So it will be like: hover button - image animation plays.
I expect that hovering block/button will affect image
.cont {}
.btn-1 {
font-family: 'Exo 2', sans-serif;
font-size: 18px;
width: 350px;
height: 100px;
background-color: #f790b0;
margin-bottom: 5px;
border: none;
}
.square {
width: 100px;
height: 100px;
background-color: #f58;
position: absolute;
}
img {
z-index: 4;
position: absolute;
width: 60px;
height: 60px;
padding: 20px;
transition: 0.7s;
}
.btn-1:hover~img {
transform: scale(1.2, 0.5);
}
span {
margin-left: 20px;
}
<link href="https://fonts.googleapis.com/css?family=Exo+2:400,700&display=swap&subset=cyrillic" rel="stylesheet">
<div class="cont">
<div class="block">
<div class="square"><img src="https://sandtonchronicle.co.za/wp-content/uploads/sites/33/2015/01/Facebook-logo-thum_621307002-423x317.jpg"></div>
<button class="btn-1">
Избранное
</button>
</div>
</div>
First you have to move the button and the square. Because with css, the previous element cannot be selected. Then the css should be like this. .btn-1:hover~.square img Then I took the picture before using row-reverse.
.btn-1 {
font-family: 'Exo 2', sans-serif;
font-size: 18px;
width: 350px;
height: 100px;
background-color: #f790b0;
margin-bottom: 5px;
border: none;
}
.block {
display: inline-flex;
flex-direction: row-reverse;
}
.square {
width: 100px;
height: 100px;
background-color: #f58;
}
img {
z-index: 4;
position: absolute;
width: 60px;
height: 60px;
padding: 20px;
transition: 0.7s;
}
.btn-1:hover~.square img {
transform: scale(1.2, 0.5);
}
span {
margin-left: 20px;
}
<div class="cont">
<div class="block">
<button class="btn-1">
Button
</button>
<div class="square"><img src="https://picsum.photos/200"></div>
</div>
</div>
To use sibling selector, the element has to be after the hovered element, not before. After tweaking your code a little bit,
HTML
<div class="cont">
<div class="block">
<button class="btn-1">
Избранное
</button>
<div class="square">
<img src="https://picsum.photos/100">
</div>
</div>
</div>
CSS
.btn-1 {
font-family: 'Exo 2', sans-serif;
font-size: 18px;
width: 350px;
height: 100px;
background-color: #f790b0;
margin-bottom: 5px;
border: none;
}
.square {
width: 100px;
height: 100px;
background-color: #f58;
position: absolute;
top: 0;
}
div.block{
position: relative;
}
img {
z-index: 4;
position: absolute;
width: 60px;
height: 60px;
padding: 20px;
transition: 0.7s;
}
.btn-1:hover ~ div img{
transform: scale(1.2, 0.5);
}
span {
margin-left: 20px;
}
Codepen: https://codepen.io/ashfaq_haq/pen/YzzQJBd
I'm having some CSS issues I hope someone here can help with. I basically am trying to set a group of inline divs that slide out to the right, expanding to the width of the parent div containing them. As you can see from the jsfiddle I have (https://jsfiddle.net/0o9bw101/), the divs expand to the width of the parent div, instead of expanding only to the rightmost border of the parent div. If anyone can help I'd be quite thankful. Thank you in advance!
Here is the CSS I'm using in case you want to see it here:
.container {
width: 70%;
height: 100px;
background-color: black;
}
.greyDiv {
height: 60px;
width: 60px;
background-color: white;
border-radius: 5px;
color: white;
display: inline-block;
margin: 20px;
vertical-align: top;
color: black;
}
.greyDiv:hover {
transition: 2s;
width: 70%;
position: absolute
}
Try this
.container {
width: 70%;
height: 100px;
background-color: black;
position:relative;
overflow:hidden;
}
.greyDiv {
height: 60px;
width: 60px;
background-color: white;
border-radius: 5px;
color: white;
display: inline-block;
margin: 20px;
vertical-align: top;
}
.greyDiv:hover {
transition: 2s;
width: 100%;
position: absolute;
}
<div class='container'>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
</div>
EDIT:
The trick is to add another box inside main container.
DEMO: https://jsfiddle.net/0o9bw101/3/
<div class='container'>
<div class='invisible_container'>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
<div class='greyDiv'></div>
</div>
</div>
previous answer:
It's hard to do when you mix parent's with in % with children margins in px.
Also having parent's position set to something other than default helps a bit.
Here is working example for you:
.container {
width: 70%;
height: 100px;
background-color: black;
position: absolute;
}
.greyDiv {
height: 60px;
width: 60px;
background-color: white;
border-radius: 5px;
color: white;
display: inline-block;
margin: 20px;
margin-left: 2%;
vertical-align: top;
}
.greyDiv:hover {
transition: 2s;
width: 96%;
position: absolute
}
DEMO: https://jsfiddle.net/0o9bw101/2/
This might not be quite what you were going for, but here elements will grow to the full width of the container (left to right) regardless of it's starting position using 2 extra elements per object.
The .inner is used to grow the background to the full width
The .placeholder is used to keep the other elements from collapsing left
#keyframes grow {
from {width: 0%;}
to {width: 92%;}
}
.container {
width: 70%;
height: 100px;
position: relative;
background-color: black;
}
.greyDiv {
height: 60px;
width: 60px;
background-color: white;
border-radius: 5px;
color: black;
display: inline-block;
margin: 20px 4%;
vertical-align: top;
position: relative;
z-index: 2;
}
.inner {
width: 0%;
height: 100%;
display: none;
background-color: transparent;
position: absolute;
left: 0;
top: 0;
z-index: 1;
}
.placeholder {
display: none;
background-color: transparent;
height: 60px;
width: 60px;
margin: 20px 4%;
}
.greyDiv:hover {
width: 100%;
position: absolute;
left: 0;
margin: 20px 0;
background-color: transparent;
z-index: 4;
}
.greyDiv:hover + .placeholder {
display: inline-block;
}
.greyDiv:hover .inner {
display: inline-block;
left: 4%;
width: 100%;
background-color: white;
animation-name: grow;
animation-duration: 2s;
animation-fill-mode: forwards;
z-index: 5;
}
<div class='container'>
<div class='greyDiv'>1a<div class="inner">1x</div></div><div class="placeholder"></div>
<div class='greyDiv'>2a<div class="inner">2x</div></div><div class="placeholder"></div>
<div class='greyDiv'>3a<div class="inner">3x</div></div><div class="placeholder"></div>
<div class='greyDiv'>4a<div class="inner">4x</div></div><div class="placeholder"></div>
</div>
I have to display on the mobile view for a webpage a list of divs, where each of them has a specific background-image and central h1 where I display the title. Stacked on each of these divs with the background-image, there is a black div with an opacity: 0.5 to make the image darker.
This is the my code:
.square-container {
min-height: auto;
background-color: white;
}
.square {
width: 100vmin;
height: 100vmin;
color: white;
}
.hover-square {
background: black;
width: 100vmin;
height: 100vmin;
margin-top: 0;
margin-bottom: 4px;
position: absolute;
opacity: 0.5;
}
.square-logo {
width: 12.5%;
height: auto;
margin-left: 50%;
transform: translateX(-50%);
}
h1 {
height: 87.5vmin;
width: 100%;
font-size: 36px;
text-align: center;
vertical-align: middle;
line-height: 100vmin;
margin: 4px auto;
z-index: 10 !important;
}
.square h1.first {
margin-top: 50px;
margin-bottom: 4px;
}
<div class="square-container">
<div class="square" style="background-color: #e74c3c">
<div class="hover-square"></div>
<h1 class="first">Case 1</h1>
<img class="square-logo" src="//pmcdeadline2.files.wordpress.com/2016/07/logo-tv-logo.png">
</div>
</div>
It is correctly working, but the title is kept below the black div. I have tried to modify the z-index of the h1 tag, but I had no luck so far. Do you have an idea on how to solve this issue?
This is a JSFiddle with the complete code. Thanks in advance for your replies!
When one mix elements (siblings) where some have a position other than static, they end up in a higher layer, hence, in your case, the h1 sits behind.
As mentioned, for z-index to work it need a position (other than static), though one rarely need to use z-index, instead make sure all, or none, has a position, so in your case, simply drop z-index and add position: relative
.square-container {
min-height: auto;
background-color: white;
}
.square {
width: 100vmin;
height: 100vmin;
color: white;
}
.hover-square {
background: black;
width: 100vmin;
height: 100vmin;
margin-top: 0;
margin-bottom: 4px;
position: absolute;
opacity: 0.5;
}
.square-logo {
width: 12.5%;
height: auto;
margin-left: 50%;
transform: translateX(-50%);
}
h1 {
position: relative;
height: 87.5vmin;
width: 100%;
font-size: 36px;
text-align: center;
vertical-align: middle;
line-height: 100vmin;
margin: 4px auto;
}
.square h1.first {
margin-top: 50px;
margin-bottom: 4px;
}
<div class="square-container">
<div class="square" style="background-color: #e74c3c">
<div class="hover-square"></div>
<h1 class="first">Case 1</h1>
<img class="square-logo" src="//pmcdeadline2.files.wordpress.com/2016/07/logo-tv-logo.png">
</div>
</div>
If the sole purpose of the hover-square is to darken the square, you could use a pseudo element instead, and save some markup and gain some flexibility
.square-container {
min-height: auto;
background-color: white;
}
.square {
position: relative;
width: 100vmin;
height: 100vmin;
color: white;
}
.square::before { /* added/changed to pseudo */
content: '';
background: black;
width: 100vmin;
height: 100vmin;
margin-top: 0;
margin-bottom: 4px;
position: absolute;
opacity: 0.5;
}
.square-logo {
width: 12.5%;
height: auto;
margin-left: 50%;
transform: translateX(-50%);
}
h1 {
position: relative;
height: 87.5vmin;
width: 100%;
font-size: 36px;
text-align: center;
vertical-align: middle;
line-height: 100vmin;
margin: 4px auto;
}
.square h1.first {
margin-top: 50px;
margin-bottom: 4px;
}
<div class="square-container">
<div class="square" style="background-color: #e74c3c">
<h1 class="first">Case 1</h1>
<img class="square-logo" src="//pmcdeadline2.files.wordpress.com/2016/07/logo-tv-logo.png">
</div>
</div>
For z-index to work you need to create stacking context and the easiest way to do this in this case is to just set position: relative on h1 element.
DEMO
But if you want h1 under navbar then you also need to set higher z-index on navbar so if h1 is 10 then navbar must be 11.
Just use position: relative
DEMO HERE
CSS
h1 {
position: relative;
height: 87.5vmin;
width: 100%;
font-size: 36px;
text-align: center;
vertical-align: middle;
line-height: 100vmin;
margin: 4px auto;
z-index: 10 !important;
}
I have a situation where in there are 2 children in a parent container. The first child occupies the entire content of the parent container.
The other child should be below the parent container. Currently it shows up on top of the parent. I am struggling to stack the 2nd child element behind the parent container.
Is it possible to do so. If so how do I approach the solution.
Note: I cannot get rid of the z-index of the parent container as it is the modal in this case
HTML
<div class="parent">
<h1>Parent</h1>
<code>position: absolute;<br/>
z-index: 1;</code>
<div class="outer-child">
<br/><br/><br/><br/><br/>
<h1>Outer Child</h1>
<code>position: relative;<br/>
z-index: 1;</code>
</div>
<div class="child">
<h1>Child</h1>
<code>position: absolute;<br/>
z-index: -1;</code>
</div>
</div>
CSS
html {
padding: 20px;
font: 12px/20px Arial, sans-serif;
}
div {
opacity: 0.7;
position: relative;
}
.parent {
z-index: 1;
opacity: 1;
position: absolute;
top: 40px;
left: 100px;
width: 330px;
border: 1px dashed #900;
background-color: #fdd;
padding: 40px 20px 20px;
height: 200px;
}
.child {
z-index: -1;
position: absolute;
top: 10px;
left: 260px;
width: 150px;
height: 110px;
border: 1px dashed #009;
padding-top: 125px;
background-color: #ddf;
text-align: center;
}
.outer-child {
z-index: 1;
opacity: 0.8;
position: absolute;
top: 10px;
left: 10px;
width: 330px;
border: 1px dashed #900;
background-color: #ffc;
padding: 20px 10px 10px;
height: 200px;
}
JSFiddle
Set the parent element z-index to initial
html {
padding: 20px;
font: 12px/20px Arial, sans-serif;
}
div {
opacity: 0.7;
position: relative;
}
.parent {
z-index: initial;
opacity: 1;
position: absolute;
top: 40px;
left: 100px;
width: 330px;
border: 1px dashed #900;
background-color: #fdd;
padding: 40px 20px 20px;
height: 200px;
}
.child {
z-index: -1;
position: absolute;
top: 10px;
left: 260px;
width: 150px;
height: 110px;
border: 1px dashed #009;
padding-top: 125px;
background-color: #ddf;
text-align: center;
}
https://jsfiddle.net/3269rjqh/1/
Once an element has a z-index, it creates new plane, so any child elements with a z-index are always going to be "above" it (in the z-plane). You can either remove the z-index from the parent, or restructure your HTML accordingly.
html {
padding: 20px;
font: 12px/20px Arial, sans-serif;
}
div {
opacity: 0.7;
position: relative;
}
.parent {
z-index: 1;
opacity: 1;
position: absolute;
top: 40px;
left: 100px;
width: 330px;
border: 1px dashed #900;
background-color: #fdd;
padding: 40px 20px 20px;
height: 200px;
}
.child {
z-index: -1;
position: absolute;
top: 10px;
left: 260px;
width: 150px;
height: 110px;
border: 1px dashed #009;
padding-top: 125px;
background-color: #ddf;
text-align: center;
}
<div class="parent">
<h1>Parent</h1>
<code>position: absolute;<br/>
z-index: 1;</code>
</div>
<div class="child">
<h1>Child</h1>
<code>position: absolute;<br/>
z-index: -1;</code>
</div>
I have this div and I want to show the title when I hover over title div. The problem is that I get the hover effect even if I hover on the edges of the div. So the div is treated as a square and not as a circle when I hover on it. This works pretty well on Firefox but not on Chrome and Safari.
Fiddle: http://jsfiddle.net/roeg629c/2/
Note: I do not want to change the aspect ratio of the image. The image should be 100% of the parent height.
HTML
<div class="video_wrap update" video_name="rikthejmna">
<div class="related img_wrap"><img src="http://img.youtube.com/vi/XyzYVpJGRG8/hqdefault.jpg"></div>
<div class="title">rikthejm na</div>
</div>
CSS
.video_wrap {
width: 232px;
height: 232px;
display: inline-block;
border-radius: 116px;
overflow: hidden;
margin: 10px;
position: relative;
z-index: 2;
}
.img_wrap img {height: 100%}
.related {height: 100%;}
.title {
position: relative;
top: -50px;
left: 0px;
background: #fff;
height: 50px;
opacity: .5;
color: #f8008c;
font-size: 12px;
text-align: center;
line-height: 50px;
overflow: hidden;
cursor: default;
transition: all .5s ease-in;
}
.title:hover {opacity: 1}
Avoid positioning of the .title, and opacity.
.video_wrap{
width: 232px;
height: 232px;
border-radius: 50%;
overflow: hidden;
margin: 10px;
}
.related {
width: 232px;
height: 232px;
position: absolute;
border-radius: 50%;
overflow: hidden;
z-index: -1;
}
.img_wrap img {
height: 100%;
}
.title{
margin: 185px 0 0;
background: rgba(255,255,255,.5);
line-height: 50px;
text-align: center;
transition: all .5s ease-in;
}
.title:hover{
background: #fff;
}
<div class="video_wrap update">
<div class="related img_wrap"><img src="http://img.youtube.com/vi/XyzYVpJGRG8/hqdefault.jpg"></div>
<div class="title">
rikthejm na
</div>
</div>