Card with vertical aligned content, and animated states - css

What is the best way to vertically align content within cards, whilst still allowing them to animate on hover, to show different screens?
For example if I have cards, each with a front which is shown and a back which is faded in on hover:
<div class="items">
<div class="item">
<div class="front">
<i class="material-icons">build</i>
<span>Item 1</span>
</div>
<div class="back">hello there</div>
</div>
<div class="item">
<div class="front">
<i class="material-icons">build</i>
<span>Item 1</span>
</div>
<div class="back">hello there</div>
</div>
<div class="item">
<div class="front">
<i class="material-icons">build</i>
<span>Item 1</span>
</div>
<div class="back">hello there</div>
</div>
</div>
Then using this css:
.item {
align-items: center;
cursor: pointer;
display: flex;
flex-direction: column;
justify-content: center;
height: 200px;
background-color: tomato;
margin: 0 auto 1rem auto;
width: 200px;
position: relative;
}
.item .material-icons {
display: block;
}
.back,
.front {
opacity: 0;
position: absolute;
text-align: center;
width: 100%;
-webkit-transition: opacity .25s ease-in-out;
-moz-transition: opacity .25s ease-in-out;
-ms-transition: opacity .25s ease-in-out;
-o-transition: opacity .25s ease-in-out;
transition: opacity .25s ease-in-out;
}
.front {
opacity: 1;
}
.item:hover .front {
opacity: 0;
}
.item:hover .back {
opacity: 1;
}
.material-icons {
font-size: 5em !important;
}
However after using position absolute, my vertical centering doesn't work, and i'm pretty sure this might not be the best approach!
You can see a half working version here:
http://jsfiddle.net/kmturley/8o29y7pd/26/
Thanks!

You can use transform to do the centering. It works great with absolute position elements. You won't need flexbox at all.
position: absolute;
left: 50%; top: 50%;
transform: translate(-50%, -50%);
text-align: center;
http://jsfiddle.net/1354hzqb/

Just add a top position of 50% and transform: translate(0, -50%) to your .back, .front classes.
#import url('https://fonts.googleapis.com/css?family=Material+Icons');
.item {
cursor: pointer;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 200px;
background-color: tomato;
margin: 0 auto 1rem auto;
width: 200px;
position: relative;
}
.item .material-icons {
display: block;
}
.back,
.front {
opacity: 0;
position: absolute;
text-align: center;
width: 100%;
top: 50%;
transform: translate(0, -50%);
-webkit-transition: opacity .25s ease-in-out;
-moz-transition: opacity .25s ease-in-out;
-ms-transition: opacity .25s ease-in-out;
-o-transition: opacity .25s ease-in-out;
transition: opacity .25s ease-in-out;
}
.front {
opacity: 1;
}
.item:hover .front {
opacity: 0;
}
.item:hover .back {
opacity: 1;
}
.material-icons {
font-size: 5em !important;
}
<div class="items">
<div class="item">
<div class="front">
<i class="material-icons">build</i>
<span>Item 1</span>
</div>
<div class="back">hello there</div>
</div>
<div class="item">
<div class="front">
<i class="material-icons">build</i>
<span>Item 1</span>
</div>
<div class="back">hello there</div>
</div>
<div class="item">
<div class="front">
<i class="material-icons">build</i>
<span>Item 1</span>
</div>
<div class="back">hello there</div>
</div>
</div>

Related

scale and transform img on hover

So what I'm trying to make is a banner that has scale and transform like in this webpage: https://www.johannesfog.dk/
So the two landscape images overlap each other on hover.
This is how far I got:
<div class="container">
<div class="image1">
<a href="#">
<img src="">
</a>
</div>
<div class="image2">
<a href="#">
<img src="">
</a>
</div>
</div>
<script src=""></script>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
display: flex;
flex-wrap: wrap;
overflow: hidden;
}
.container .image1 {
width: 50%;
overflow: hidden;
}
.container .image2 {
width: 50%;
overflow: hidden;
}
.container img {
width: calc(100% - (0px * 2));
}
.container .image1:hover {
transform: scale(1.5);
transform: translate(50%, 0%);
overflow: hidden;
transition: ease-in-out, transform .3s ease-in-out;
}
.container .image2:hover {
transform: scale(1.5);
transform: translate(-50%, 0%);
overflow: hidden;
transition: ease-in-out, transform .3s ease-in-out;
}
Right now the img only transform.

why can't I change the height of the box?

i'm trying to create a responsive photo gallery but I couldn't change neither the height or the width of my box.
I was able to fix its width adding style property to the html but it won't work for the height as well.
I've read many thread about the height problem but nothing works for me, some suggested the absolute position instead of relative one but it ruins my layout.
Can't seem to get what i am doing wrong, could anyone help me please?!
Here's my code:
.container {
position: relative;
width: 100%;
margin-top: 5%;
background-color: #203B5B;
padding: 1% 1% 0% 1%;
height: 100%;
}
.container:after {
content: '';
display: inline-block;
width: 100%;
}
* {
box-sizing: border-box;
-webkit-box-sizing: border-box;
/* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box;
/* Firefox, other Gecko */
}
.box {
margin-top: 0%;
height: 20%;
padding-bottom: 90%;
position: relative;
float: left;
cursor: default;
overflow: hidden;
}
.box:after {
content: '';
position: absolute;
cursor: default;
background-color: #000;
margin-left: 0%;
margin-top: 6%;
opacity: 0;
z-index: 55;
width: 50%;
height: 72.5%;
-webkit-transition: all .15s ease-out;
-moz-transition: all .15s ease-out;
-ms-transition: all .15s ease-out;
-o-transition: all .15s ease-out;
transition: all .15s ease-out;
}
.box:hover::after {
background-color: #000;
opacity: .7;
}
.container .box .caption {
position: absolute;
z-index: 60;
width: 45%;
margin-left: 2.5%;
top: 40%;
-webkit-transform: scale(3);
-moz-transform: scale(3);
-ms-transform: scale(3);
-o-transform: scale(3);
transform: scale(3);
text-align: center;
-webkit-transition: all .15s ease-out;
-moz-transition: all .15s ease-out;
-ms-transition: all .15s ease-out;
-o-transition: all .15s ease-out;
transition: all .15s ease-out;
font: 90% montserrat, sans-serif;
color: #fff;
opacity: 0;
}
.box:hover .caption {
-webkit-transform: scale(1) translate(0%, -50%);
-moz-transform: scale(1) translate(0%, -50%);
-ms-transform: scale(1) translate(0%, -50%);
-o-transform: scale(1) translate(0%, -50%);
transform: scale(1) translate(0%, -50%);
opacity: 1;
}
.box img {
position: absolute;
left: 50%;
border-style: solid;
border-color: rgba(255, 255, 255, 0.86);
border-width: 3.5px;
outline-style: solid;
outline-width: 5px;
outline-color: #182D46;
cursor: pointer;
top: 37%;
height: 70%;
width: 95%;
-webkit-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
z-index: 50;
-webkit-transition: all .15s ease-out;
-moz-transition: all .15s ease-out;
-ms-transition: all .15s ease-out;
-o-transition: all .15s ease-out;
transition: all .15s ease-out;
}
.box:hover img {
height: 70%;
}
<div class="container">
<div class="box" style="width:14.6%;">
<div class="caption">1</div>
<img src="1.jpg" width="495" height="533" alt="" />
</div>
<div class="box" style="width:14.6%;">
<div class="caption">2</div>
<img src="2.png" width="495" height="533" alt="" />
</div>
<div class="box" style="width:14.6%; ">
<div class="caption">3</div>
<img src="3.jpg" width="495" height="533" alt="" />
</div>
<div class="box" style="width:14.6%">
<div class="caption">4</div>
<img src="4.png" width="495" height="533" alt="" />
</div>
<div class="box" style="width:14.6%">
<div class="caption">5</div>
<img src="5.png" width="495" height="533" alt="" />
</div>
<div class="box" style="width:14.6%">
<div class="caption">6</div>
<img src="6.png" width="495" height="533" alt="" />
</div>
<div class="box" style="width:14.6%">
<div class="caption">7</div>
<img src="7.png" width="495" height="533" alt="" />
</div>
</div>
thank you!
If you're setting the width and height for your box div in percentages, it is taking x% of the parent. In this case the container div. Which in this case also got its width and height set in percentages. But you haven't set a width and height for its parent, the <body>. And the <body> has <html> as its parent.
So either define a fixed height and width for the container div, or define a width and height for your <body> and <html> like so:
html,
body {
width: 100%;
height: 100%;
}
Code snippet
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
}
.container {
position: relative;
width: 100%;
margin-top: 5%;
background-color: #203B5B;
padding: 1% 1% 0% 1%;
height: 100%;
}
.box {
width: 14.25%;
height: 50%;
position: relative;
float: left;
cursor: default;
overflow: hidden;
}
.box img {
border: 3.5px solid rgba(255, 255, 255, 0.86);
cursor: pointer;
width: 100%;
}
<div class="container">
<div class="box">
<div class="caption">1</div>
<img src="https://www.fillmurray.com/640/360" />
</div>
<div class="box">
<div class="caption">2</div>
<img src="https://www.fillmurray.com/640/360" />
</div>
<div class="box">
<div class="caption">3</div>
<img src="https://www.fillmurray.com/640/360" />
</div>
<div class="box">
<div class="caption">4</div>
<img src="https://www.fillmurray.com/640/360" />
</div>
<div class="box">
<div class="caption">5</div>
<img src="https://www.fillmurray.com/640/360" />
</div>
<div class="box">
<div class="caption">6</div>
<img src="https://www.fillmurray.com/640/360" />
</div>
<div class="box">
<div class="caption">7</div>
<img src="https://www.fillmurray.com/640/360" />
</div>
</div>

CSS Image Swap One at a Time

JSFiddle Here: https://jsfiddle.net/br84z1Lj/
On hover, both images swap, instead of just one. If I end the div for "swapMe" and restart it for the next image, it puts the next image on the next line, instead of staying in line. I'm completely stuck on getting only one image to change on hover instead of both!
CSS
#charset "utf-8";
/* CSS Document */
#thumbs {
width: 460px;
margin-top: 90px;
margin-left: auto;
margin-right: auto;
text-align: justify;
-ms-text-justify: distribute-all-lines;
text-justify: distribute-all-lines;
}
#thumbs a {
vertical-align: top;
display: inline-block;
*display: inline;
zoom: 1;
}
.stretch {
width: 100%;
display: inline-block;
font-size: 0;
line-height: 0
}
.swapMe img {
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-ms-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}
.swap1,
.swapMe:hover .swap2 {
-webkit-opacity: 1;
-moz-opacity: 1;
opacity: 1;
}
.swapMe:hover .swap1,
.swap2 {
-webkit-opacity: 0;
-moz-opacity: 0;
opacity: 0;
}
HTML
<body>
<div id="thumbs">
<div class="swapMe">
<a id="single_image1" href="#"><img src="http://dummyimage.com/200/000000/fff" class="swap1" style="position: absolute" height="150" width="188" alt="" />
<img src="http://dummyimage.com/200/d100d1/fff" height="150" width="188" class="swap2">
</a>
<a id="single_image2" href="#"><img src="http://dummyimage.com/200/000000/fff" class="swap1" style="position: absolute" height="150" width="188" alt="" />
<img src="http://dummyimage.com/200/d100d1/fff" height="150" width="188" class="swap2">
</a>
<span class="stretch"></span>
</div>
</div>
</body>
</html>
Revising your bottom hover statement to this should do the trick, if I'm understanding what you're looking for! Side note, prefixed opacity declarations aren't really necessary at this point, every modern browser within any version I can think of supports plain old opacity.
.swap1:hover,
.swap2:hover {
opacity: 0;
}
https://jsfiddle.net/will0220/awpv02p7/
Please do try this css code:
.swap1,
.swapMe a:hover .swap2 {
-webkit-opacity: 1;
-moz-opacity: 1;
opacity: 1;
}
.swapMe a:hover .swap1,
.swap2 {
-webkit-opacity: 0;
-moz-opacity: 0;
opacity: 0;
}
you need to wrap the 2 images in separate div's. You can use display: inline-block
I updated your fiddle. https://jsfiddle.net/br84z1Lj/1/
CSS
#charset "utf-8";
/* CSS Document */
#thumbs {
width: 460px;
margin-top: 90px;
margin-left: auto;
margin-right: auto;
text-align: justify;
-ms-text-justify: distribute-all-lines;
text-justify: distribute-all-lines;
}
#thumbs a {
vertical-align: top;
display: inline-block;
*display: inline;
zoom: 1;
}
.stretch {
width: 100%;
display: inline-block;
font-size: 0;
line-height: 0
}
.swapMe {
display: inline-block;
}
.swapMe img {
-webkit-transition: all 1s ease-in-out;
-moz-transition: all 1s ease-in-out;
-ms-transition: all 1s ease-in-out;
-o-transition: all 1s ease-in-out;
transition: all 1s ease-in-out;
}
.swap1,
.swapMe:hover .swap2 {
-webkit-opacity: 1;
-moz-opacity: 1;
opacity: 1;
}
.swapMe:hover .swap1,
.swap2 {
-webkit-opacity: 0;
-moz-opacity: 0;
opacity: 0;
}
HTML
<body>
<div id="thumbs">
<div class="swapMe">
<a id="single_image1" href="#"><img src="http://dummyimage.com/200/000000/fff" class="swap1" style="position: absolute" height="150" width="188" alt="" />
<img src="http://dummyimage.com/200/d100d1/fff" height="150" width="188" class="swap2">
</a>
</div>
<div class="swapMe">
<a id="single_image2" href="#"><img src="http://dummyimage.com/200/000000/fff" class="swap1" style="position: absolute" height="150" width="188" alt="" />
<img src="http://dummyimage.com/200/d100d1/fff" height="150" width="188" class="swap2">
</a>
</div>
<span class="stretch"></span>
</div>
</body>
</html>

css transition on multiple elements

I have multiple css transitions on different elements.
In my example, if you hover over the circle section the transitions occur on both the circle and the box color change underneath. However if you come out of the circle and into the box section, the circle transition does not occur
See fiddle for complete example: http://jsfiddle.net/Lsnbpt8r/
Heres my html:
div class="row">
<div class="col-sm-4 text-center transistion">
<div class="box">
<i class="circle-pos circle glyphicon glyphicon-home icon"></i>
<h3 class="heading">
Construction
</h3>
<p>This is how we do it</p>
</div>
</div>
<div class="col-sm-4 text-center transistion">
<div class="box">
<i class="circle-pos circle glyphicon glyphicon-wrench icon"></i>
<h3 class="heading">
Interior Design
</h3>
<p>This is how we do it</p>
</div>
</div>
<div class="col-sm-4 text-center transistion">
<div class="box">
<i class="circle-pos circle glyphicon glyphicon-thumbs-up icon"></i>
<h3 class="heading">
Service
</h3>
<p>This is how we do it</p>
</div>
</div>
</div>
Here's some of my css:
.circle {
width: 120px;
height: 120px;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
border-radius: 50%;
background: #f3f3f3;
-webkit-transition: all 300ms linear;
-moz-transition: all 300ms linear;
-o-transition: all 300ms linear;
-ms-transition: all 300ms linear;
transition: all 300ms linear;
}
.circle:hover{
width: 100px;
height: 100px;
background: #f7f7f7;
}
.box{
border: 0px 1px 2px 1px solid #f1f1f1;
border-top: 5px solid #003176;
height: 200px;
-webkit-transition: all 300ms linear;
-moz-transition: all 300ms linear;
-o-transition: all 300ms linear;
-ms-transition: all 300ms linear;
transition: all 300ms linear;
}
.box:hover{
background-color: #135379;
}
How can I make it so that whatever part of the section is hovered on all element transitions will take place ?
Cheers in advance.
It's because the effects are applied to each element's :hover:
.circle:hover{
width: 100px;
height: 100px;
background: #f7f7f7;
}
...
.circle-pos:hover{
margin-top: -50px;
}
So, if you hover the box, but not the circle, it won't have any effect. Instead, set the transition to the :hover of the common parent container, in this case, the .box div:
Updated Fiddle
.box:hover .circle{
width: 100px;
height: 100px;
background: #f7f7f7;
}
....
.box:hover .circle-pos{
margin-top: -50px;
}
EDIT
The same with the .icon:hover { if you want, it can be .box:hover .icon{: http://jsfiddle.net/Lsnbpt8r/3/

My elements in a menu seem to have no width?

see this codepen for a demo
EDIT of course, hard coding a width for an element is no solution. It should be just the size of the actual title.
HTML
<br/>
<nav class="navigation">
<div class="navfake"></div>
<div class="singleelement">
<div class="container">
<div class="title">Test title 2</div>
<div class="titlepicture">some picture</div>
</div>
</div>
<div class="singleelement">
<div class="container">
<div class="title">Test title newsfeed super long 1</div>
<div class="titlepicture">some picture</div>
</div>
</div>
</nav>
css
/** the newsfeed and footer */
nav {
width: 100%;
height: 20px;
position: relative;
}
.navfake {
width: 100%;
height: 100%;
background: green;
z-index: 10;
position: relative;
}
.singleelement {
display: inline-block;
height: 60px;
text-align: center;
}
.container {
position: absolute;
top: 0px;
-webkit-transition: all .8s ease;
-moz-transition: all .8s ease;
-ms-transition: all .8s ease;
-o-transition: all .8s ease;
transition: all .8s ease;
height: 200px;
}
.titlepicture {
position: absolute;
width: 100%;
background-color: red;
z-index: 10;
height: 100px;
overflow: hidden;
}
.container:hover {
top: -80px;
}
.container:hover .titlepicture {
height: 100px;
z-index: 10;
}
.title {
z-index: 555;
position: relative;
height: 20px;
width: 100%;
}
.footer {
position: relative;
z-index: 1000;
background-color: white;
}
The trick was to replace position absolute with position relative
HTML
<nav class="navigation">
<div class="navfake"></div>
<div class="singleelement">
<div class="container">
<div class="title">Test title 2</div>
<div class="titlepicture">some picture</div>
</div>
</div>
<div class="singleelement">
<div class="container">
<div class="title">Test title newsfeed super long 1</div>
<div class="titlepicture">some picture</div>
</div>
</div>
</nav>
<div class="footer">some text blablablablablablabla
<br/>some text blablablablablablabla
<br/>some text
<br/>some text
<br/>some text
<br/>some text
<br/>some text
<br/>xxx
</div>
CSS
/** the newsfeed and footer */
nav {
width: 100%;
height: 20px;
}
.navfake {
width: 100%;
height: 100%;
background: green;
z-index: 10;
position: relative;
}
.singleelement {
display: inline-block;
height: 60px;
text-align: center;
position: relative;
}
.container {
top: -20px;
position: relative; /**changed*/
-webkit-transition: all .8s ease;
-moz-transition: all .8s ease;
-ms-transition: all .8s ease;
-o-transition: all .8s ease;
transition: all .8s ease;
height: 200px;
}
.titlepicture {
position: absolute;
background-color: red;
z-index: 10;
height: 100px;
overflow: hidden;
}
.container:hover {
top: -80px;
}
.container:hover .titlepicture {
height: 100px;
z-index: 10;
}
.title {
z-index: 555;
position: relative;
height: 20px;
width: 100%;
}
.footer {
position: relative;
z-index: 1000;
background-color: white;
}
On .singleelement add some sort of width to it. For example 64px and it will work.
Fiddle: http://jsfiddle.net/7WZk5/
Perhaps this is what you're looking for: http://codepen.io/aytimothy/pen/mlkAh
Have you tried adding the "width:" CSS into your "container" Class?
.container {
position: absolute;
top: 0px;
-webkit-transition: all .8s ease;
-moz-transition: all .8s ease;
-ms-transition: all .8s ease;
-o-transition: all .8s ease;
transition: all .8s ease;
height: 200px;
width: 50%;
}
Well, 50% since you have two elements. If you want to have different widths for each of them, you could always put the width as a STYLE attribute for each element.

Resources