I'm trying to make a website. This is the gallery, and images should move to the center when you put the mouse over them, but id doesn't work, because the image magically disappears.
main {
width: 942px;
}
div {
background-image: url("https://upload.wikimedia.org/wikipedia/commons/thumb/7/7a/Supermillion_Visuals_-_Aston_Fresh_Park_1-02601.jpg/1200px-Supermillion_Visuals_-_Aston_Fresh_Park_1-02601.jpg");
background-size: 217px 111px;
width: 217px;
height: 111px;
z-index: 1;
background-repeat: no-repeat;
position: absolute;
}
div:hover {
width: 100%;
height: 1000px;
transition: ease-in-out 1s;
z-index: 5;
}
.a {
top: 205px;
}
.a:hover {
background-position: 400px 170px;
background-size: 615px 333px;
}
.b {
top: 390px;
}
.b:hover {
background-position: 400px 0px;
background-size: 615px 333px;
}
.c {
top: 575px;
}
.c:hover {
background-position: 400px -170px;
background-size: 615px 333px;
}
.d {
top: 760px;
}
.d:hover {
background-position: 400px -340px;
background-size: 615px 333px;
z-index: 6;
}
<main>
<div class="a"></div>
<div class="b"></div>
<div class="c"></div>
<div class="d"></div>
</main>
It's because your changing the backhground-position making the image outside the bound of the div.
Do it diffenretly using scale and adjust the transform-origin:
div {
width: 217px;
position:relative;
margin:5px;
background-size:0 0;
}
/* keep the ratio of the image*/
div:before {
content:"";
display:block;
padding-top:50%; /* 1/2 */
}
/**/
/* the image */
div:after {
content:"";
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
transition: ease-in-out 0.3s;
background-image:inherit;
background-size: 100% 100%;
}
/**/
div:hover::after {
transform:scale(2); /* make the image bigger*/
z-index:9;
transition: ease-in-out 1s;
}
/* same X different Y*/
.a::after {transform-origin:-120% -100%;}
.b::after {transform-origin:-120% -40%;}
.c::after {transform-origin:-120% 140%;}
.d::after {transform-origin:-120% 200%;}
<main>
<div class="a" style="background-image:url(https://picsum.photos/id/1000/400/200)"></div>
<div class="b" style="background-image:url(https://picsum.photos/id/100/400/200)"></div>
<div class="c" style="background-image:url(https://picsum.photos/id/1047/400/200)"></div>
<div class="d" style="background-image:url(https://picsum.photos/id/17/400/200)"></div>
</main>
Related
Is it possible to do free transforms of an element using CSS - similar to a mesh transform?
The closest I can get to this is using something like transform: perspective(400px) rotateY(45deg); with three elements, but I would like it to be one continuous img element.
You can consider 3 elements and background-image. The trick is to adjust the background-size/background-position to create the illusion of one continuous image.
Hover to see the result:
.box {
margin: 50px auto;
width: 200px;
height: 200px;;
background-size: 300% auto;
background-position: center;
position: relative;
}
.box:before,
.box:after {
content: "";
position: absolute;
width: 100%;
top: 0;
bottom: 0;
background-image: inherit;
background-size: 300% auto;
transform: perspective(800px);
transition: 0.5s all;
}
.box:before {
right: 100%;
background-position: left;
transform-origin: right;
}
.box:after {
left: 100%;
background-position: right;
transform-origin: left;
}
.box:hover::before {
transform: perspective(800px) rotateY(50deg);
filter: brightness(0.8);
}
.box:hover::after {
transform: perspective(800px) rotateY(-50deg);
filter: brightness(0.8);
}
<div class="box" style="background-image: url(https://picsum.photos/id/1/1000/800)">
</div>
This question already has answers here:
How can I horizontally center an element?
(133 answers)
Closed 4 years ago.
I've already tried multiple answers on previous questions, but I can't seem to find out how to move my slideshow to the center of the page (horizontally). If I try to do so with text-align: center; it doesn't work. I am not that good in HTML and CSS, so any help is appreciated.
The size of the slideshow is good, but the position needs to be center, instead of in the top left of my page. Padding also didn't seem to work.
Slider{
display: block;
width:90%;
height:92%;
background-color: #0AF8B3;
overflow: hidden;
position: absolute;
border: 2px solid red;
}
Slider > * {
position: absolute;
display: block;
width:100%;
height:100%;
background: #FF8000;
animation: slide 12s infinite;
overflow: hidden;
}
Slide:nth-child(1) {
left: 0%;
animation-delay: -1s;
background-image: url(Here's a link);
background-size: cover;
background-position: center;
}
Slide:nth-child(2) {
animation-delay: 2s;
background-image: url(Here's a link);
background-size: cover;
background-position: center;
}
Slide:nth-child(3) {
animation-delay: 5s;
background-image: url(Here's a link);
background-size: cover;
background-position: center;
}
Slide:nth-child(4) {
left: 0%;
animation-delay: 8s;
background-image: url(Here's a link);
background-size: cover;
background-position: center;
}
slide p {
font-family: Arial, Helvetica, sans-serif;
font-size: 40px;
text-align: center;
display: inline-block;
width: 100%;
margin-top: 560px;
color: black;
}
#keyframes slide {
0% { left: 100%; width: 100%;}
5% { left: 0%;}
25% {left: 0%;}
30% {left: -100%; width: 100%;}
30.0001% {left: -100%; width: 0%;}
100% { left: 100%; width: 0%;}
}
<section id="PageC">
<h1>Photos</h1>
<slider>
<slide><p>A 2015</p></slide>
<slide><p>V 2017</p></slide>
<slide><p>A 2018</p></slide>
<slide><p>F 2018</p></slide>
</slider>
<article>Slideshow lorem ipsum.</article>
</section>
Thank you!
Please see below. Changes are documented in the CSS code. Hope it helps.
Slider {
display: block;
width: 90%;
height: 200px; /* Adjusted */
background-color: #0AF8B3;
overflow: hidden;
/* position: absolute; REMOVED */
position: relative; /* Added */
border: 2px solid red;
margin: 0 auto; /* Added */
}
Slider>* {
position: absolute;
top: 0;
display: block;
width: 100%;
height: 100%;
background: #FF8000;
animation: slide 12s infinite;
overflow: hidden;
}
Slide:nth-child(1) {
left: 0%;
animation-delay: -1s;
background-image: url(https://via.placeholder.com/200x100/ff0000);
background-size: cover;
background-position: center;
}
Slide:nth-child(2) {
animation-delay: 2s;
background-image: url(https://via.placeholder.com/200x100/00ff00);
background-size: cover;
background-position: center;
}
Slide:nth-child(3) {
animation-delay: 5s;
background-image: url(https://via.placeholder.com/200x100/0000ff);
background-size: cover;
background-position: center;
}
Slide:nth-child(4) {
left: 0%;
animation-delay: 8s;
background-image: url(https://via.placeholder.com/200x100/00ffff);
background-size: cover;
background-position: center;
}
slide p {
font-family: Arial, Helvetica, sans-serif;
font-size: 40px;
text-align: center;
display: inline-block;
width: 100%;
margin-top: 560px;
color: black;
}
#keyframes slide {
0% {
left: 100%;
width: 100%;
}
5% {
left: 0%;
}
25% {
left: 0%;
}
30% {
left: -100%;
width: 100%;
}
30.0001% {
left: -100%;
width: 0%;
}
100% {
left: 100%;
width: 0%;
}
}
<section id="PageC">
<h1>Photos</h1>
<slider>
<slide>
<p>A 2015</p>
</slide>
<slide>
<p>V 2017</p>
</slide>
<slide>
<p>A 2018</p>
</slide>
<slide>
<p>F 2018</p>
</slide>
</slider>
<article>Slideshow lorem ipsum.</article>
</section>
Try with margin: auto;
Slider{
display: block;
width:90%;
height:92%;
background-color: #0AF8B3;
overflow: hidden;
margin: auto;
border: 2px solid red;
}
https://jsfiddle.net/vaf6nv36/1/
Can the balloons image slowly transition over the apple image?
I think that I need more transition parameters, or I should use opacity?
Can someone help me?
HTML:
<div class="img1">
</div>
<div class="img2">
</div>
CSS:
.img1, .img2{
border: 1px solid black;
transition: 1s;
position: absolute;
}
.img1{
left: 25%;
height: 500px;
width: 500px;
z-index: 1;
background-image: url(http://cdn.pcwallart.com/images/balloons-photography-vintage-wallpaper-1.jpg);
}
.img2{
right: 25%;
width: 500px;
height: 500px;
bottom: 0;
z-index: 2;
background-image: url(https://i.pinimg.com/736x/c1/7b/15/c17b150e93c4e9c50d963b076484bee7--apple-wallpaper-iphone-wallpaper.jpg);
}
.img1:hover{
z-index: 999;
}
Although theoretically you can transition z-index, it wouldn't make much sense, i.e. would not result in the crossfade effect which you obviously are looking for: z-index values are integers, which - when you change them in the smallest possible steps (integers, no commas) - results in states either before the other one OR behind the other one - no transitional "half states" in between. If you want to do a kind of continuous crossfade between two elements, you should use a transition on opacity.
In your particular case, since your DIVs are not directly above each other, but only overlap each other, you can solve that by having a second DIV identical to img2 (I called its class .img3), but with z-index: 0 and this CSS rule:
.img1:hover + .img2 {
opacity: 0;
}
This will fade out img2, but still show img3, which however is behind img1, creating the impression of a transition between img1 and img2.
https://jsfiddle.net/2a2epLfv/1/
.img1,
.img2,
.img3 {
border: 1px solid black;
transition: 1s;
position: absolute;
}
.img1 {
left: 20%;
height: 300px;
width: 300px;
z-index: 1;
background-image: url(http://cdn.pcwallart.com/images/balloons-photography-vintage-wallpaper-1.jpg);
}
.img2,
.img3 {
right: 20%;
width: 300px;
height: 300px;
top: 100px;
background-image: url(https://i.pinimg.com/736x/c1/7b/15/c17b150e93c4e9c50d963b076484bee7--apple-wallpaper-iphone-wallpaper.jpg);
}
.img2 {
z-index: 2;
}
.img3 {
z-index: 0;
}
.img1:hover+.img2 {
opacity: 0;
}
<div class="img1"></div>
<div class="img2"></div>
<div class="img3"></div>
I fear z-index transition only makes the element pass step by step through every layer. To make a nice effect you need to combine it with opacity transition and scale / position transition. The fiddle to show you the idea:
.img1, .img2{
border: 1px solid black;
transition: 1s;
position: absolute;
}
.img1{
left: 25%;
height: 500px;
width: 500px;
z-index: 1;
transform: scale(0.9);
opacity: 0.5;
background-image: url(http://cdn.pcwallart.com/images/balloons-photography-vintage-wallpaper-1.jpg);
}
.img2{
right: 25%;
width: 500px;
height: 500px;
bottom: 0;
z-index: 2;
background-image: url(https://i.pinimg.com/736x/c1/7b/15/c17b150e93c4e9c50d963b076484bee7--apple-wallpaper-iphone-wallpaper.jpg);
}
.img1:hover{
animation: fronte 1s linear forwards;
}
#keyframes fronte {
from { z-index: 0; transform: scale(0.9); opacity: 0.5; }
to { z-index: 4; transform: scale(1.1); opacity: 1; }
}
<div class="img1">
</div>
<div class="img2">
</div>
This is the trick I used.
.minus{animation:move 2s;animation-fill-mode:forwards;animation-delay:2s;}
#-webkit-keyframes move {
0 {z-index:1;opacity:1}
50% {opacity:0}
100% { z-index:-1;opacity:1}
}
#keyframes move {
0 {z-index:1;opacity:1}
50% {opacity:0}
100% {z-index:-1;opacity:1}
}
#main{background:red;width:100vw;height:100vh;position:fixed;top:0;left:0;opacity:.9}
.minus{position:fixed;top:10px;left:10px;z-index:1;color:#000}
<div id="main"></div>
<div class="minus">FADE</div>
I'm trying to make the child image slide to the left and dissapear just like the parent. If your run the snipped now the image is scaling instead of sliding to the left.
How can I prevent the image to scale? And make the image slide to the left and dissapear just like the parent?
The transition will trigger by the media query.
This is my code.
.parent {
height: 130px;
width: 180px;
background-color: #fff;
border-bottom-right-radius: 10px;
border: 1px solid #000000;
text-align: center;
float: left;
position: fixed;
z-index: 1;
transition: ease all 0.3s;
}
.child {
width: 100%;
height: 100%;
background-size: contain;
text-indent: -9999px;
display: block;
background-image: url(https://static.pexels.com/photos/34490/keyboard-computer-keys-white.jpg);
background-repeat: no-repeat;
background-position: center center;
}
#media screen and (max-width: 655px){
.parent {
visibility: hidden;
width: 1px;
}
}
<div class="parent">
<span class="child">
</span>
</div>
Easiest solution that I could suggest you is set visibility:visible; for .child element and this works, but it is not supported by some browser. And this is only element which makes child element visible on hiding parent element.
The visibility property can be used to hide an element while leaving
the space where it would have been. It can also hide rows or columns
of a table.
Solution - 1
Check this jsfiddle
.parent {
height: 130px;
width: 180px;
background-color: #fff;
border-bottom-right-radius: 10px;
border: 1px solid #000000;
text-align: center;
float: left;
position: fixed;
z-index: 1;
transition: ease all 0.3s;
}
.child {
width: 100%;
height: 100%;
background-size: contain;
text-indent: -9999px;
display: block;
background-image: url(https://static.pexels.com/photos/34490/keyboard-computer-keys-white.jpg);
background-repeat: no-repeat;
background-position: center center;
}
#media screen and (max-width: 655px){
.parent {
visibility: hidden;
width: 1px;
}
.child{
width:180px;
height:130px;
visibility:visible;
background-size:100% 100%;
}
}
<div class="parent">
<span class="child">
</span>
</div>
Solution - 2
Check this jsFiddle
Create two different element and hide below div using opacity.
#bx{
width:200px;
height:130px;
background:#111;
transition: ease all 0.3s;
}
#b{
width:200px;
height:120px;
top:13px;
left:8px;
position:absolute;
background-image: url(https://static.pexels.com/photos/34490/keyboard-computer-keys-white.jpg);
background-repeat: no-repeat;
background-position: center center;
background-size:100%;
margin-left:0;
transition: ease all 0.3s;
}
#media screen and (max-width: 655px){
#bx{
width:1px;
opacity:0;
}
#b{
margin-left:-220px;
}
}
<div id="bx"></div>
<div id="b"></div>
The reason why the .child disappears with parent is because:
...the .child's height and width are 100% of .parent. Guess what .child's height and width are when.parent's` width and height are 1px and 130px (hint: what's a 100% of 1px and 100% of 130px?)
Another reason is because all children of an element that has visibility:hidden will be hidden as well, unless a child element has visibility: visible explicitly set (must have it as a declared CSS rule)
So knowing this, we should counter those properties when that MQ (media query) kicks in. Details are commented in the Snippet below:
.child {
width: 100%;
height: 100%;
background-size: contain;
text-indent: -9999px;
display: block;
background-image: url(https://static.pexels.com/photos/34490/keyboard-computer-keys-white.jpg);
background-repeat: no-repeat;
background-position: center center;
/* These properties were added so that `.child` is in the normal
|| "flow" to which `.parent` is not due to it having `position: fixed`
*/
position: relative;
right: 0;
bottom: 0;
}
#media screen and (max-width: 640px) {
.parent {
visibility: hidden;
width: 1px;
}
/* This ruleset will counter the properties
|| that were previously discussed
*/
.child {
visibility: visible;
min-width: 180px;
min-height: 130px;
}
}
SNIPPET
.parent {
height: 130px;
width: 180px;
background-color: #fff;
border-bottom-right-radius: 10px;
border: 1px solid #000000;
text-align: center;
float: left;
position: fixed;
z-index: -1;
background: red;
}
.child {
width: 100%;
height: 100%;
background-size: contain;
text-indent: -9999px;
display: block;
background-image: url(https://static.pexels.com/photos/34490/keyboard-computer-keys-white.jpg);
background-repeat: no-repeat;
background-position: center center;
position: relative;
right: 0;
bottom: 0;
}
#media screen and (max-width: 640px) {
.parent {
width: 1px;
overflow-x: hidden;
transition: width 0.3s ease;
}
.child {
visibility: visible;
max-width: 0px;
min-height: 130px;
transition: transform 1s ease-out;
transform: translateX(-180px);
}
}
<div class="parent">
<span class="child">
</span>
</div>
Check out example that you need to understand.
.sibling {
width:300px;
height:56px;
background-color:hsla(40, 50%, 60%, .6);
position:absolute;
left:240px;
}
.parent {
visibility:visible;
width:170px;
height:170px;
border-radius:50%;
background-image: linear-gradient(90deg, hsla(10, 90%, 50%, 1) 50%, hsla(100, 90%, 50%, .0) 50%);
position:absolute;
-webkit-transform: rotate(90deg);
transform: rotate(90deg);
-webkit-transition:0s;
transition:0s;
}
.sibling:hover ~ .parent {
visibility:visible;
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
-webkit-transition: .6s ease-in .2s;
transition: .6s ease-in .2s;
}
.child {
width:150px;
height:150px;
border-radius:50%;
background-image: linear-gradient(90deg, hsla(5, 35%, 50%, 1) 50%, hsla(100, 90%, 50%, .0) 50%);
position: relative;
top: 10px;
left: 10px;
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
-webkit-transition:0s;
transition:0s;
}
.sibling:hover ~ .parent > .child {
-webkit-transform: rotate(90deg);
transform: rotate(90deg);
-webkit-transition: .1s ease-in .9s;
transition: .1s ease-in .9s;
}
<div class="sibling"></div>
<div class="parent">
<div class="child"></div>
</div>
i am trying to get the background color to change on hover. Something like this.
I have tried various approaches but cannot get it to work, presumably it is the way my CSS and HTML is set up. I cannot figure out why it is not working, as it should be easy to implement
Please see code below.
CSS
.image-container {
position: relative;
}
.image-container .after {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
width: 100%;
height: 100%;
display: none;
color: #FFF;
background-size: 100% 200%;
background-image: linear-gradient(to bottom, red 50%, black 50%);
-webkit-transition: background-position 1s;
-moz-transition: background-position 1s;
transition: background-position 1s;
}
.image-container .after p {
position: relative;
text-align: center;
font-size: 26px;
text-transform: uppercase;
font-weight: 300;
line-height: 300px;
height: 300px;
}
.image-container:hover .after {
display: block;
background: rgba(0,0,0,0.3);
background-position: 0 -100%;
}
[class*='col-'] {
float: left;
}
.col-1-3 {
width: 33.33%;
}
HTML
<div class="col-1-3 image-container">
<img class="portrait-image geysir" src="images/geysir.jpg">
<div class="after">GEYSIR</div>
</div>
Remove the background declaration on the hover. It's overriding all the other backgrounds you declared previously.
.image-container:hover .after {
display: block;
background-position: 0 -100%;
}
It should then work.
Based on the given fiddle, I would use a transparent .png image as a second overlapping element like that. Not sure if that's your intention...
.container{
position:relative;
}
.box {
width: 400px; height: 200px;
background-size: 100% 200%;
background-image: linear-gradient(to bottom, red 50%, black 50%);
-webkit-transition: background-position 1s;
-moz-transition: background-position 1s;
transition: background-position 1s;
}
.box:hover {
background-position: 0 -100%;
}
.geysir{
position:absolute;
top:0px;
}
<div class="container">
<div class="box"></div>
<img class="portrait-image geysir" src="http://cdn.mysitemyway.com/etc-mysitemyway/icons/legacy-previews/icons-256/high-resolution-dark-blue-denim-jeans-icons-arrows/008776-high-resolution-dark-blue-denim-jeans-icon-arrows-hand-pointer1-right.png">
</div>
Do you want to have the background, including the text slide in from the top on hover? In which case you would be better transitioning a bottom move like this:
.image-container {
position: relative;
overflow: hidden;
}
.image-container .after {
position: absolute;
bottom: 100%;
margin: auto;
width: 100%;
height: 100%;
color: #fff;
background-color: black;
-webkit-transition: bottom 1s;
-moz-transition: bottom 1s;
transition: bottom 1s;
}
.image-container:hover .after {
bottom: 0;
}
Fiddle
If you're looking to have your text appear on a red background that shifts to black, try using a combination of the above with what you were using. Avoid using display: none/block as this stops the transistion from functioning.
.image-container {
position: relative;
overflow: hidden;
}
.image-container .after {
position: absolute;
bottom: 100%;
margin: auto;
width: 100%;
height: 100%;
color: #fff;
background-size: 100% 200%;
background-image: linear-gradient(to bottom, red 50%, black 50%);
-webkit-transition: background-position 2s;
-moz-transition: background-position 2s;
transition: background-position: 2s;
}
.image-container:hover .after {
bottom: 0;
background-position: 0 -100%;
}
Fiddle