Expand background on hover - html

I have a div with a background image, what I would like to do, is when I hover over it, the hidden part of background-image to display like in the example below:
My jsfiddle example:
div.test {
background: url(http://media2.intoday.in/indiatoday/images/Photo_gallery/emraan_012315020812.jpg);
background-size: cover;
width: 70px;
height: 70px;
background-position: center;
border-radius: 100%;
display: inline-block;
transition: all 1s;
position: absolute;
top: 100px;
}
.test:hover{
transform: scale(1.2);
}
body {
text-align: center;
}
<div class="test">
</div>
As you can see, in my example the image is just getting larger, instead I want to display another 20px of the image (without compromising border-radius).

Example with one html element:
div.test {
background: url(http://media2.intoday.in/indiatoday/images/Photo_gallery/emraan_012315020812.jpg) no-repeat 50% 50%;
background-size: 140px;
width: 70px;
height: 70px;
background-position: center;
border-radius: 100%;
display: inline-block;
transition: all 1s;
position: absolute;
top: 100px;
transform-origin: center center;
}
.test:hover{
width: 90px;
height: 90px;
margin-left: -10px;
margin-top: -10px;
}
body {
text-align: center;
}
<div class="test">
</div>
Example with clip-path and shape-inside:
div.test {
background: url(http://media2.intoday.in/indiatoday/images/Photo_gallery/emraan_012315020812.jpg) no-repeat 50% 50%;
background-size: cover;
shape-inside: circle(30% at 50% 50%);
clip-path: circle(30% at 50% 50%);
-webkit-clip-path: circle(30% at 50% 50%);
width: 70px;
height: 70px;
background-position: center;
display: inline-block;
transition: all 1s;
position: absolute;
top: 100px;
transform-origin: center center;
}
.test:hover{
shape-inside: circle(50% at 50% 50%);
clip-path: circle(50% at 50% 50%);
-webkit-clip-path: circle(50% at 50% 50%);
}
body {
text-align: center;
}
<div class="test">
</div>

you might oversize a bit the background image
(background-size:auto 90px;) and add some padding and reset position on hover (.test:hover{padding:10px; margin:-10px;})
those rules are suppose to be understood by most of actual browsers if not all.
div.test {
background: url(http://media2.intoday.in/indiatoday/images/Photo_gallery/emraan_012315020812.jpg);
background-size:auto 90px;
width: 70px;
height: 70px;
background-position: center;
border-radius: 100%;
display: inline-block;
transition: all 1s;
position: absolute;
top: 100px;
}
.test:hover{
padding:10px;
margin:-10px;;
}
body {
text-align: center;
}
<div class="test">
</div>
another possibility is to use an inset shadow
div.test {
background: url(http://media2.intoday.in/indiatoday/images/Photo_gallery/emraan_012315020812.jpg);
background-size: auto 90px;
width: 90px;
height: 90px;
/* hide buggy ff render */
background-clip: content-box;
padding: 1px;
/* end fix ff */
background-position: center;
border-radius: 100%;
display: inline-block;
transition: all 1s;
position: absolute;
top: 90px;
box-shadow: inset 0 0 0 10px white;
}
.test:hover {
box-shadow: inset 0 0 0 0px white;
}
body {
text-align: center;
}
<div class="test">
</div>
There is also : padding, box-sizing and background-clip
div.test {
background: url(http://media2.intoday.in/indiatoday/images/Photo_gallery/emraan_012315020812.jpg) ;
background-size: auto 90px;
width: 90px;
height: 90px;
padding: 10px;
background-clip:content-box;
box-sizing:border-box;
background-position: center;
border-radius: 100%;
display: inline-block;
transition: all 1s;
position: absolute;
top: 90px;
}
.test:hover {
padding:0px;
}
/* show transparency */
html {
min-height:100%;
text-align: center;
background:linear-gradient(45deg, gray, yellow,purple,lime,pink,turquoise, gray, yellow,purple,lime,pink,turquoise);
}
<div class="test"></div>

You are missing to remove border-radius property on the hover event:
div.test {
background: url(http://media2.intoday.in/indiatoday/images/Photo_gallery/emraan_012315020812.jpg);
background-size: cover;
width: 70px;
height: 70px;
background-position: center;
border-radius: 100%;
display: inline-block;
transition: all 1s;
position: absolute;
top: 100px;
}
.test:hover{
transform: scale(1.2);
border-radius: 0px;
}
body {
text-align: center;
}
<div class="test">
</div>

Related

I want my image to be shown in a circle and when hovered on it gets full

I have tried achieving this effect using border-radius (code below) but I want the circle to be smaller, as shown in this example https://imgur.com/a/gKHtVXr and I don't think I can acheive this with border-radius.
<img class = "zyra-thumb" src = "thumbnail champion/thumb2.png" alt="zyra">
CSS:
.zyra-thumb{
width: 200px;
height: 200px;
object-fit: cover;
border-radius: 150px;
transition: border-radius 0.3s;
}
.zyra-thumb:hover{
border-radius: 10px;
}
you can use clip-path to display only center of the thumb image and
transition: clip-path 1s;to give needed animation on hover
.thumb {
background: purple;
width: 200px;
height: 200px;
display: flex;
justify-content: center;
align-items: center;
border-radius: 10px;
}
.zyra-thumb {
width: 100%;
object-fit: cover;
clip-path: circle(25% at 50% 50%);
transition: clip-path 1s;
border-radius: 10px;
}
.zyra-thumb:hover {
clip-path: circle(100% at 50% 50%);;
}
<div class="thumb">
<img class="zyra-thumb" src="https://www.gravatar.com/avatar/0cf65651ae9a3ba2858ef0d0a7dbf900?s=256&d=identicon&r=PG&f=1" alt="zyra">
</div>
Based on your attached image I assume this is the effect you are trying to achieve.
.zyra-thumb {
background: url(https://i.stack.imgur.com/OR7ho.jpg) no-repeat center;
background-size: cover;
position: relative;
width: 400px;
height: 400px;
overflow: hidden;
display: inline-block;
border-radius: 20px;
}
.zyra-thumb::before {
content: "";
display: block;
width: 50%;
padding-bottom: 50%;
position: absolute;
top: 50%;
left: 50%;
z-index: 2;
transform: translate(-50%, -50%);
border: solid 400px #41373f;
border-radius: 50%;
transition: all ease-in-out 0.3s;
}
.zyra-thumb:hover::before {
width: 150%;
padding-bottom: 150%;
}
<div class="zyra-thumb">
</div>
Simply use border-radius:0px; in hover.

max-width and max-height on background image

I want to show an background-image while hovering over the normal image. They both should have the same height and width. I thought to make it work by giving the div with the background image display: inline-block so it takes the same size as the image, but that makes it so the width: 100% and height: 100% of the image stop working cause they try to take 100% of the width from the inline-block element.
How can I make the background-image the same size as the image while keeping the values of height and width as ..% of the .box div.
.box{
width: 500px;
height: 400px;
border: 1px solid black;
text-align: center;
}
.around-image{
background: url(http://via.placeholder.com/600x400) no-repeat center;
background-size: cover;
display: inline-block;
}
img{
max-height: 100%;
max-width: 100%;
height: 100%;
width: 100%;
opacity: 1;
vertical-align: middle;
}
.helper{
display: inline-block;
height: 100%;
vertical-align: middle;
}
img:hover{
opacity: 0;
}
<div class="box">
<div class="around-image">
<span class="helper"></span><img src="http://via.placeholder.com/300x200">
</div>
</div>
Edit
Thanks to the answer from #dbigpot I probably got a better solution which is changing the background-image on hover. Only problem is that I can't use the max-height and max-width on the image and I need that part of the code so my images always look good inside of the div.
Is there anyway to set max-height or max-width on a background-image?
.box{
margin: 30px;
width: 400px;
height: 200px;
border: 1px solid black;
text-align: center;
background: url(http://via.placeholder.com/300x200) no-repeat center;
background-size: cover;
position: relative;
}
.box:hover{
background: url(http://via.placeholder.com/200x300) no-repeat center;
}
.height p{
position: absolute;
left: -60px;
top: 45%;
-webkit-transform: rotate(-90deg);
}
.width p{
position: absolute;
left: 40%;
top: -40px;
-webkit-transform: rotate(0);
}
<div class="box">
<div class="height">
<p>height: 200px</p>
</span>
<div class="width">
<p>width: 400px</p>
</span>
</div>
As you see, the 300x200 image is stretched over an area of 400x200. I don't want the image to stretch.
To prevent stretching of the image just change the value of the background-size property to contain:
.box {
background-size: contain;
}
.box {
margin: 30px;
width: 400px;
height: 200px;
border: 1px solid;
text-align: center;
background: url(http://via.placeholder.com/300x200) no-repeat center;
background-size: contain;
position: relative;
}
.box:hover {
background: url(http://via.placeholder.com/200x300) no-repeat center;
}
.height p {
position: absolute;
left: -60px;
top: 45%;
-webkit-transform: rotate(-90deg);
}
.width p {
position: absolute;
left: 40%;
top: -40px;
-webkit-transform: rotate(0);
}
<div class="box">
<div class="height">
<p>height: 200px</p>
</div>
<div class="width">
<p>width: 400px</p>
</div>
</div>
You could try using background-size:cover
.box{
width: 500px;
height: 400px;
border: 1px solid black;
text-align: center;
}
.around-image{
background: url(http://via.placeholder.com/300x200/ff0000) no-repeat center;
background-size: cover;
display: inline-block;
}
img{
max-height: 100%;
max-width: 100%;
height: 100%;
width: 100%;
opacity: 1;
vertical-align: middle;
}
.helper{
display: inline-block;
height: 100%;
vertical-align: middle;
}
img:hover{
opacity: 0;
}
<div class="box">
<div class="around-image">
<span class="helper"></span><img src="http://via.placeholder.com/300x200">
</div>
</div>
You could use something like this -
#target {
width: 200px;
height: 200px;
background: url('https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRN_bhuF3l7rhMNNBk4lgEoOp2qnB2TAJd5h_rVGtsWzZ0K0uzs');
}
#target:hover {
background: url('http://3.bp.blogspot.com/-KdSQjJhV4jI/TpMRe96ndUI/AAAAAAAACfQ/BC6ZMvbp_DA/s1600/gerbera-elegant-flowers-9.jpg');
}
<div id="target"></div>
Adjust the background css properties to suit your need.

Mask position incorrect when I stop using a background image

Using http://jsfiddle.net/4UNuB/5/ as an example, the image has been set as background
.box1 {
border: #999 2px solid;
width: 180px;
height: 250px;
background-image: url(http://smilesoftware.com/assets/images/uploads/products/icon_pdfpenipad_140x140.png);
background-position: center;
background-repeat: no-repeat;
}
But, if I can't use a background image, and instead have an img src within the div itself like this
<div class="box1">
<img src="http://placehold.it/180x250">
</div>
The mask no longer covers the image, instead sitting below it.
See this fiddle http://jsfiddle.net/4UNuB/6/
But the mask is still being applied to the same place as before, so why does it move, and how to stop it moving?
Add Position Absolute and relative css for boxes.
Check The Fiddle here
.box1 {
border: #999 2px solid;
width: 180px;
height: 250px;
/*background-image: url(http://smilesoftware.com/assets/images/uploads/products/icon_pdfpenipad_140x140.png);*/
background-position: center;
background-repeat: no-repeat;
position: relative;
}
.black-box {
text-align: center;
font-size: 16px;
color: #fff;
background-color: rgba(00,00,00,0.8);
width: 100%;
height: 100%;
opacity: 1.0;
transition: all 0.5s ease-in-out;
position: absolute;
top: 0;
}
You can put both in the same anchor and use positioning + z-index:
.box1 {
border: #999 2px solid;
width: 180px;
height: 250px;
/*background-image: url(http://smilesoftware.com/assets/images/uploads/products/icon_pdfpenipad_140x140.png);
background-position: center;
background-repeat: no-repeat;*/
}
img {
position: absolute;
z-index: 0;
}
.black-box {
position: relative;
z-index: 1;
text-align: center;
font-size: 16px;
color: #fff;
background-color: rgba(00,00,00,0.8);
width: 100%;
height: 100%;
opacity: 1.0;
transition: all 0.5s ease-in-out;
}
.black-box:hover {
opacity: 0.0;
transition: all 0.5s ease-in-out;
}
h2 {
padding-top: 110px;
margin: 0px;
}
<div class="box1">
<a href="http://placehold.it">
<img src="http://placehold.it/180x250">
<div class="black-box">
<h2>View Details</h2>
</div>
</a>
</div>
Also, I had to remove the h2's margin.

CSS Folding animation with transform

I have this square: https://jsfiddle.net/34f93mL3/
As you can see, when you hover over it, the top folds down and when it reaches the bottom it becomes a polkadotted pink.
However, what I want to happen is for it to mimic an actual folding motion, meaning it should not have polkadots until it's "folded" a little more.
Here is the full code, which uses only HTML and CSS:
body {
background: white
}
#slow-container {
top: 100px;
left: 200px;
height: 50px;
position: absolute;
width: 100px;
background: lightblue;
}
#slow-container:before {
top: -50px;
height: 50px;
position: absolute;
width: 100px;
background: lightblue;
}
#slow-container2 {
top: -50px;
height: 50px;
position: absolute;
width: 100px;
background: lightblue;
}
.slow-parent1 {
height: 0;
overflow: hidden;
background: lightgreen;
}
.slow-parent2 {
background: white;
}
.slow-parent3 {
height: 300px;
background: red;
}
#slow-container2 {
transition: all 1s linear;
transform-origin: bottom center;
}
#slow-container:hover #slow-container2 {
transform: rotateX(180deg);
background-color: lightpink;
background-image: radial-gradient(#fff 10%, transparent 10%), radial-gradient(#fff 10%, transparent 10%);
background-size: 30px 30px;
background-position: 0 0, 15px 15px;
}
<div id="slow-container">
<div id="slow-container2">
</div>
<div class="slow-parent1">
<div class="slow-parent2">
<div class="slow-parent3">
stuff goes here later
</div>
</div>
</div>
</div>
</div>
Just remove in your hover style code #fff 10%, from radial gradient
Use CSS3 properties perspective to feel folding effect.
References: https://css-tricks.com/almanac/properties/p/perspective/
body {
background: white
}
#slow-container {
top: 100px;
left: 200px;
height: 50px;
position: absolute;
width: 100px;
background: lightblue;
}
#slow-container:before {
top: -50px;
height: 50px;
position: absolute;
width: 100px;
background: lightblue;
}
#slow-container2 {
top: -50px;
height: 50px;
position: absolute;
width: 100px;
background: lightblue;
}
.slow-parent1 {
height: 0;
overflow: hidden;
background: lightgreen;
}
.slow-parent2 {
background: white;
}
.slow-parent3 {
height: 300px;
background: red;
}
#slow-container2 {
transition: all 1s linear;
transform-origin: bottom center;
}
#slow-container:hover #slow-container2 {
transform: perspective(200px) rotateX(180deg);
background-color: lightpink;
background-image: radial-gradient(#fff 10%, transparent 10%), radial-gradient(#fff 10%, transparent 10%);
background-size: 30px 30px;
background-position: 0 0, 15px 15px;
}
<div id="slow-container">
<div id="slow-container2">
</div>
<div class="slow-parent1">
<div class="slow-parent2">
<div class="slow-parent3">
stuff goes here later
</div>
</div>
</div>
</div>

center button inside a fluid div

I am having a tricky(at least for me) problem with a div which is a flip card done purely in css. The front of the card shows an image and the back has a different image and a button. The problem is due to the fluid design I had to set the padding. Which places all of the text below the div
.card {
margin: 0 auto;
position: relative;
float: left;
padding-bottom: 25%;
width: 100%;
max-width: 600px;
}
.card__front,
.card__back {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.card__front,
.card__back {
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-transition: -webkit-transform 0.7s;
transition: transform 0.7s;
}
.card__front {
background-image: url("http://i58.tinypic.com/mmu649.jpg");
padding-top: 56.25%;
background-size: cover;
-moz-background-size: cover;
background-position: center;
}
.card__back {
background-image: url("http://i58.tinypic.com/mmu649.jpg");
-webkit-transform: rotateY(-180deg);
transform: rotateY(-180deg);
padding-top: 56.25%;
background-size: cover;
-moz-background-size: cover;
background-position: center;
}
.card.effect__hover:hover .card__front {
-webkit-transform: rotateY(-180deg);
transform: rotateY(-180deg);
}
.card.effect__hover:hover .card__back {
-webkit-transform: rotateY(0);
transform: rotateY(0);
}
.card__text {
width: auto;
text-align: center;
height: 100%;
margin-top: -32%;
}
.button {
background-color: #000000;
text-indent: 0;
border: 1px solid #000000;
display: inline-block;
color: #ffffff;
font-family: Arial;
font-size: 21px;
font-weight: normal;
font-style: normal;
height: 69px;
line-height: 69px;
width: 200px;
text-decoration: none;
text-align: center;
opacity: 0.8;
}
.button:hover {
background: -webkit-gradient(linear, left top, left bottom, color- stop(0.05, #000000), color-stop(1, #000000));
background: -moz-linear-gradient(center top, #000000 5%, #000000 100%);
filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#000000', endColorstr='#000000');
background-color: #000000;
}
.button:active {
position: relative;
top: 1px;
}
<div class="card effect__hover">
<div class="card__front">
<div class="card__text"></div>
</div>
<div class="card__back">
<div class="card__text"> see more
</div>
</div>
</div>
<!-- /card -->
Here is the preview of the code: http://jsfiddle.net/j29mvoLm/
All I am trying to get is to center the same button in every card like div.
Is it possible to do it without using negative margin?
This should do you justice:
.element {
position: relative;
top: 50%;
transform: translateY(-50%);
}
or
display: table-cell;
vertical-align: middle;
It is possible. See snippet. This answer uses nested relative and absolute positioned elements and also utilized box-sizing:border-box; and padding to maintain aspect ratio.
If you need to to set a max-width, add another wrapper element around your card and set the max-width on that element.
.card-wrapper {
margin: auto;
max-width: 600px;
}
.card {
box-sizing: border-box;
margin: auto;
padding: 28.125% 50%; /* 16:9 aspect ratio */
position: relative;
width: 100%;
}
.card__front, .card__back {
bottom: 0;
height: 100%;
left: 0;
margin: auto;
position: absolute;
right: 0;
top: 0;
width: 100%;
}
.card__front, .card__back {
-webkit-backface-visibility: hidden;
backface-visibility: hidden;
-webkit-transition: -webkit-transform 0.7s;
transition: transform 0.7s;
}
.card__front {
background-image: url("http://i58.tinypic.com/mmu649.jpg");
background-size: cover;
-moz-background-size: cover;
background-position: center;
}
.card__back {
background-image: url("http://i58.tinypic.com/mmu649.jpg");
-webkit-transform: rotateY(-180deg);
transform: rotateY(-180deg);
background-size: cover;
-moz-background-size: cover;
background-position: center;
}
.card.effect__hover:hover .card__front {
-webkit-transform: rotateY(-180deg);
transform: rotateY(-180deg);
}
.card.effect__hover:hover .card__back {
-webkit-transform: rotateY(0);
transform: rotateY(0);
}
.card__text {
height: 100%;
position: relative;
text-align: center;
width: 100%;
}
.button {
background-color: #000000;
border: 1px solid #000000;
bottom: 0;
color: #ffffff;
display: block;
font-family: Arial;
font-size: 21px;
font-style: normal;
font-weight: normal;
height: 69px;
left: 0;
line-height: 69px;
margin: auto;
opacity: 0.8;
position: absolute;
right: 0;
text-align: center;
text-decoration: none;
text-indent: 0;
top: 0;
width: 200px;
}
.button:hover {
background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #000000), color-stop(1, #000000) );
background:-moz-linear-gradient( center top, #000000 5%, #000000 100% );
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#000000', endColorstr='#000000');
background-color:#000000;
}
.button:active {
position:relative;
top:1px;
}
<div class="card-wrapper">
<div class="card effect__hover">
<div class="card__front">
<div class="card__text"></div>
</div>
<div class="card__back">
<div class="card__text"> see more </div>
</div>
</div><!-- /card -->
</div>