I have a block called case-card which consists of a child div called case-card__wrapper that houses text.
On case-card hover, I want the case-card__wrapper to move up slightly. Not in one action, but as transition.
I feel like I have the general idea, but unsure on how to get the transition to work? At the moment, it just phases from one spot to another:
.case-card {
height: 560px;
width:600px;
color: #ededed;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
position: relative;
transform: translateY(20px);
transition: all .3s ease-in-out;
background-color: #333;
overflow: hidden;
}
.case-card__wrapper{
position: absolute;
transition: 0.5s;
/*top: 0; Don't want to add this because I want the text to be centered in the div and rather not define a number since the div heights may vary */
}
.case-card__title{
font-size: 16px;
}
.case-card:hover .case-card__wrapper {
top: 150px;
}
<div class="case-card">
<div class="case-card__wrapper">
<h4 class="case-card__title">Title</h4>
<p class="case-card__subtitle">Subtitle</p>
</div>
</div>
I'm trying to do this just via CSS. Any ideas?
If you cannot set initial value simply use transfrom. You can also remove position:absolute
.case-card {
height: 560px;
width:600px;
color: #ededed;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
position: relative;
transform: translateY(20px);
transition: all .3s ease-in-out;
background-color: #333;
overflow: hidden;
}
.case-card__wrapper{
transition: 0.5s;
}
.case-card__title{
font-size: 16px;
}
.case-card:hover .case-card__wrapper {
transform:translateY(-100%);
}
<div class="case-card">
<div class="case-card__wrapper">
<h4 class="case-card__title">Title</h4>
<p class="case-card__subtitle">Subtitle</p>
</div>
</div>
Related
Related to this previous question of mine, I come to you today with a HTML structure problem.
HTML CODE
<label>
<article class="m__item m__item-2">
<input type="checkbox" class="m__switch" value="1">
<div class="m__info ">
<div class="m__texte">
<h3 class="m__titre">Velouté de légumes d'antan</h3>
<p class="m__details">Carotte, panais, topinambour</p>
</div>
<p class="m__prix"> 35€</p>
</div>
<div class="m__selection">
<span class="material-icons check__icon">check_circle</span>
</div>
</article>
</label>
Problem to solve
The W3C html validator tells me this :
Error: Element article not allowed as child of element label in this context. (Suppressing further errors from this subtree.)
What did I try so far to solve this issue ?
Not much, I wasted a lot of time turning the problem upside down and eventually I came to exchange label and article tags but then further error messages show up in the validator such as the one quoted above but with "div" not being allowed as child element of "label", etc.
Important things regarding this code
If you checked the link provided at the beginning of this post, you'll see that the container has an animation triggered by clicking on a hidden check-box with the help of the label that help the whole container to be clickable.
And that's what matters here the most, that the whole capsule remains clickable whatever the changes that must be done in order to pass the validator without error.
Thank you for reading me so far and for any help you may provide to me,
I'm of course at your disposal for any further information that may be needed to solve this small issue.
Regards,
Elrad
A <label> Element is normally used alongside <input> elements, in order to explain what the input is for. But not for wrapping entire parts to just make them clickable.
My guess is, you only need the label in order to trigger the state of the input field? Then you can try it like this:
remove the label entirely (or replace it with a <div>
make your field position absolute and full height/width, so it's the same size as your article element. Also opacity to zero so you don't see it
article element needs to be position relative
This way, the input field is as big as the whole article element and doesn't matter where you click, you always trigger the select field.
Stylings for each element:
input
display: block;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
opacity: 0;
.m__item (your article)
position: relative;
.m__plats {
display: flex;
flex-direction: column;
gap: 20px;
}
.m__plats .m__item {
background: white;
border-radius: 15px;
box-shadow: rgba(149, 157, 165, 0.2) 0px 8px 24px;
display: flex;
position: relative;
}
.m__plats .m__info {
padding: 10px;
max-width: 100%;
overflow: hidden;
flex: 1;
display: flex;
justify-content: space-between;
gap: 16px;
}
.m__plats .m__info .m__texte {
display: flex;
flex-direction: column;
justify-content: space-between;
overflow: hidden;
white-space: nowrap;
}
.m__plats .m__info .m__texte .m__titre {
font-size: 16px;
font-weight: bold;
margin: 0;
text-overflow: ellipsis;
overflow: hidden;
-webkit-line-clamp: 1;
-webkit-box-orient: vertical;
}
.m__plats .m__info .m__texte .m__details {
font-weight: 200;
font-size: 14px;
margin: 3px 0;
text-overflow: ellipsis;
line-clamp: 1;
text-overflow: ellipsis;
overflow: hidden;
-webkit-line-clamp: 1;
}
.m__plats .m__info .m__prix {
display: flex;
align-items: flex-end;
margin-bottom: 0;
font-weight: bold;
}
label {
cursor: pointer;
}
.m__selection {
cursor: pointer;
background: #99e2d0;
height: auto;
width: 0;
max-width: 15%;
opacity: 0;
border-radius: 0 15px 15px 0;
display: flex;
align-items: center;
justify-content: center;
transition: transform 0.4s ease-in-out, width 0.4s ease-in-out, opacity 0.4s ease-in-out;
}
.m__selection .material-icons {
font-family: 'Material Icons';
font-weight: normal;
font-style: normal;
font-size: 30px;
line-height: 1;
letter-spacing: normal;
text-transform: none;
display: inline-block;
white-space: nowrap;
word-wrap: normal;
direction: ltr;
}
.m__selection .check__icon {
font-size: 30px;
color: white;
transform: rotate(0deg);
transition: 0.4s ease-in-out;
}
#m__switch {
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
opacity: 0;
}
#m__switch:checked ~ .m__selection {
opacity: 100%;
width: 7rem;
transition: transform 0.4s ease-in-out, width 0.4s ease-in-out, opacity 0.4s ease-in-out;
}
.check__icon {
animation: m__rotate 2s linear 1;
}
#keyframes m__rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
<body>
<div class="m__main">
<div class="m__plats" id="entrées">
<div>
<article class="m__item">
<input type="checkbox" id="m__switch" value="1">
<div class="m__info ">
<div class="m__texte">
<h3 class="m__titre">Ravioles de foie gras</h3>
<p class="m__details">Accompagnés de leur crème à la truffe</p>
</div>
<p class="m__prix"> 25€</p>
</div>
<div class="m__selection">
<span class="material-icons check__icon">V</span>
</div>
</article>
</div>
</div>
</div>
</body>
Note: There's a drawback!
The problem with this solution is, that the user can't select and copy any text inside your <article> element, because the input field is overlapping all the text.
If that's not a problem, go with it.
If it's a problem, you may should consider adding a small javascript click event which is adding a class to trigger your animation.
so I have a div which is a card and content in it. I achieved to make it moves up a bit but the transition property seems to not work.
Here is the HTML code
<div class="card">
<p> Title </p>
</div>
and here is the CSS code
.card:hover {
position: relative;
top: -10px;
transition: 1s;
}
So basically there is multiple cards and it works well every card with the .card class moves up when the mouse is over it, but it moves instantaneously, the transition does not work. Does anyone knows how to fix it? have a great day
This is because you have specified the position and transition only in the :hover block of code, meaning the transition timing is not specified until after the hover has already occurred. In other words, only the item that changes on hover (the top value) should be in the :hover.
Specify the position and transition outside the :hover block, like this for example:
.card {
position: relative;
transition: 1s
}
.card:hover {
top: -10px;
}
You can use transform: translateY
try this
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.box {
border: 1px solid black;
width: 150px;
height: 150px;
cursor: pointer;
transition: all .5s ease-in-out;
}
.box:hover {
transform: translateY(-10px);
}
<div class="box"></div>
Instead of playing with top which requires a positon attribute to move it out of flow, just add a margin to displace the element:
.card:hover {
margin-top: -20px;
margin-bottom: 20px;
transition: 1s;
}
/* for visualization purpose only */
body {
padding-top: 50px;
}
div {
border: 2px solid black;
display: flex;
justify-content: center;
align-items: center;
height: 40vh;
width: 10vw;
}
<div class="card">Card</div
Here is a snippet of my Project Code:
body {
height: 100%;
margin: 0;
padding: 0;
font-family: 'Montserrat', sans-serif;
font-size: 16px;
}
input, button {
font-family: 'Montserrat', sans-serif;
}
.img_main_container {
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
.centered {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.btn_sign_up, .btn_login:hover {
background: #5d8ffc;
color: #fff;
border: 1px solid #5d8ffc;
border-radius: 5px;
display: block;
width: 300px;
height: 40px;
transition: 0.3s;
-webkit-transition: 0.3s;
-moz-transition: 0.3s;
}
.btn_login, .btn_sign_up:hover {
background-color: Transparent;
background-repeat:no-repeat;
color: #ffffff;
border-radius: 5px;
display: block;
width: 300px;
height: 40px;
cursor:pointer;
overflow: hidden;
outline:none;
transition: 0.3s;
-webkit-transition: 0.3s;
-moz-transition: 0.3s;
}
<!DOCTYPE html>
<html>
<body>
<div class="img_main_container">
<img src="https://natgeo.imgix.net/subjects/headers/ND%20header%20(1).jpg?auto=compress,format&w=1920&h=960&fit=crop" alt="Storm" style="width:100%;">
<div class="centered">
<button class="btn_sign_up">Sign up</button>
<button class="btn_login">Login</button>
</div>
</div>
</body>
</html>
Everything works as you can see. But I want that the two buttons are side by side like this in that image here:
I tried so many examples but nothing worked. And I still can't figure out what's wrong here. It would be great if anyone could help. Thanks in advance. :)
Since you are using display:block on your buttons (.btn_sign_up, .btn_login), you can't make two buttons side by side, because block covering whole horizontal section.
Instead of this use display:inline-block and you will have buttons side by side.
More information you can get on the W3Schools
Flexbox would be a good solution for this. Add display: flex to the .centered class. This will place direct children of .centered side by side.
.centered {
display: flex;
}
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Use Flexbox for this issue.This is the better solution. Add display: flex to the .centered class.
.centered { display: flex; align-items: center}
I have a message space with the response field nice and small at the bottom. When the user clicks in, it expands. However, when focused, if I then go to click my button, it looses focus - as expected, but the button isn't triggered, I have to click it again. How can I best resolve this?
.wrap {
align-items: center;
display: flex;
justify-content: flex-end;
}
textarea {
height: 40px;
width: 100%;
transition: height .2s ease;
}
textarea:focus {
height: 150px;
}
.fancy-button {
background-color: red;
cursor: pointer;
margin-left: 20px;
height: 30px;
width: 30px;
}
<div class="wrap">
<textarea></textarea>
<div class="fancy-button" onclick="alert('click');">
</div>
I think the issue is that when you click, the button is not technically in this place but only visually. If you add a wrapper that cover all the space you may be able to catch the click from the first time.
While you are having the fast transition the browser is calculating and updating the position of the button; thus your click is outside the area of the button.
You may also notice in this solution that if you click below the button the alert may not be triggered because of the same reasons. The wrapper is having its height decreasing rapidly and the click may be outside.
.wrap {
display: flex;
}
textarea {
height: 40px;
width: 100%;
transition: height .2s ease;
}
textarea:focus {
height: 150px;
}
.wrap > div {
display: flex;
align-items:center;
}
.fancy-button {
background-color: red;
cursor: pointer;
margin-left: 20px;
height: 30px;
width: 30px;
}
<div class="wrap">
<textarea></textarea>
<div onclick="alert('click');">
<div class="fancy-button" >
</div>
</div>
</div>
And if you only decrease the transition time you may also be able to catch the click the first time:
.wrap {
display: flex;
align-items: center;
}
textarea {
height: 40px;
width: 100%;
transition: height 2s ease;
}
textarea:focus {
height: 150px;
}
.fancy-button {
background-color: red;
cursor: pointer;
margin-left: 20px;
height: 30px;
width: 30px;
}
<div class="wrap">
<textarea></textarea>
<div onclick="alert('click');" class="fancy-button">
</div>
</div>
You can also keep the duration and add a delay:
.wrap {
display: flex;
align-items: center;
}
textarea {
height: 40px;
width: 100%;
transition: height .2s ease;
transition-delay:0.1s;
}
textarea:focus {
height: 150px;
transition-delay:0s;
}
.fancy-button {
background-color: red;
cursor: pointer;
margin-left: 20px;
height: 30px;
width: 30px;
}
<div class="wrap">
<textarea></textarea>
<div onclick="alert('click');" class="fancy-button">
</div>
</div>
I don't know if this for you can works, because I don't know your needs, but if you use hover instead of focus the button is free to be clicked
.wrap {
align-items: center;
display: flex;
justify-content: flex-end;
}
textarea {
height: 40px;
width: 100%;
transition: height .2s ease;
}
textarea:hover {
height: 150px;
}
.fancy-button {
background-color: red;
cursor: pointer;
margin-left: 20px;
height: 30px;
width: 30px;
}
<div class="wrap">
<textarea></textarea>
<a class="fancy-button" onclick="alert('click');"><a/>
</div>
I have a list of div elements with a big letter (A, B, C or D) and span (description) inside. I would like to center these span elements horizontally respect to each parent div, but keeping a fixed distance among all big letters. I'm looking for something like this:
The centering part is done with:
span{
position: absolute;
left: 50%;
transform: translateX(-50%);
}
But when I apply a simple transition (opacity for example) to div elements, the first letter in span elements is moved from it's place. If I remove the transition or transform property the issue goes away, but I need both (transform for centering).
Question: How to fix the issue with transition and transform, or how to center span elements without transform, but keeping the fixed distance among big letters?
Here is the code, and a DEMO:
HTML:
<div class="elements">
<div>A<span>Description</span></div>
<div>B<span>Really Long Description</span></div>
<div>C<span>Description</span></div>
<div>D<span>Description</span></div>
</div>
SCSS:
.elements{
margin-left: 50px;
div{
position: relative;
display: inline-block;
font-size: 40px;
margin: 0 10px;
opacity: 0.7;
transition: 0.3s;
&:hover{
opacity: 1;
span{
opacity: 1;
}
}
span{
position: absolute;
top: 100%;
left: 50%;
transform: translateX(-50%);
transition: 0.3s;
font-size: 15px;
white-space: nowrap;
pointer-events: none;
opacity: 0;
}
}
}
Depending on compatibility requirements, you could always use the flex box model, and it's handy justify-content property, like so:
.elements {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex; /*Only one out of the above that's really needed...*/
justify-content: space-around;/*Only one out of the below that's really needed...*/
-ms-flex-pack: distribute;
justify-content: space-around;
}
.elements >div {
position: relative;
display: inline-block;
opacity: 0.7;
font-size: 30pt;
text-align: center;
transition: 0.3s opacity;
}
.elements >div:hover {
opacity: 1;
}
.elements >div >div {
font-size: 12pt;
white-space: nowrap;
}
<div class="elements">
<div>A
<div>Description</div>
</div>
<div>B
<div>Long Description</div>
</div>
<div>C
<div>Description</div>
</div>
</div>
Your question has a very nice solution, tables! While they should only be used for tabular data semantically, they do have some nice display properties, and they'be been used since forever so compatibility is not an issue.
body{
display:table;
table-layout:fixed;
}
.elements{display:table-row;}
.elements >div {
width:20%; /*100% / Number of cells*/
display:table-cell;
padding:0 10px; /*There needs to be a little spacing otherwise it looks bad :) */
opacity: 0.7;
font-size: 30pt;
text-align: center;
transition: 0.3s opacity;
}
.elements >div:hover {
opacity: 1;
}
.elements >div >div {
font-size: 12pt;
white-space: nowrap;
}
<div class="elements">
<div>A
<div>Description</div>
</div>
<div>B
<div>Long Description</div>
</div>
<div>C
<div>Description</div>
</div>
<div>D<div>Description</div></div>
</div>
I have not found a way to make work properly transition and transform properties (Firefox at least). But I found a way to center each span elements inside it parent div, keeping distance among big letters (A, B, C & D).
See the SOLUTION DEMO, and the code added (SCSS):
$div-width: 40px;
$span-width: 200px;
.elements{
div{
width: $div-width; // fixed width
text-align: center; // center text
span{
width: $span-width; // big fixed width
left: -($span-width/2 - $div-width/2); // -(200px / 2 - 40px / 2)
}
}
}