Smooth simultaneous transition of height and margin - html

How can I make smooth transition of block with margin? See my code below:
.counter {
margin: 5rem auto;
box-shadow: 0 1px 8px rgba(0, 0, 0, 0.2);
width: 40rem;
border-radius: 8px;
padding: 1rem;
text-align: center;
background-color: #f4f0fa;
}
.value {
font-size: 2rem;
color: #3c0080;
margin: 2rem 0;
font-weight: bold;
transition: all 1s;
max-height: 500px;
overflow: hidden;
}
.value.hidden {
margin: 0;
max-height: 0px;
}
<main class="counter">
<h1>Counter</h1>
<div id="val" class="value">10</div>
<button onClick="document.getElementById('val').classList.add('hidden')">Hide Counter</button>
<button onClick="document.getElementById('val').classList.remove('hidden')">Show Counter</button>
</main>
If you click on "Hide Counter", it is not smooth, but it looks like there are two different transitions (look on codepen). Why and how can I make smooth transition?

I just decided to change max-height to height and setting height to a fixed number
body {
display: flex;
height: calc(100vh - 16px);
align-items: center;
}
.counter {
margin: 0px auto;
box-shadow: 0 1px 8px rgba(0, 0, 0, 0.2);
width: calc(100vw - 48px);
border-radius: 8px;
padding: 1rem;
text-align: center;
background-color: #f4f0fa;
}
.value {
font-size: 2rem;
color: #3c0080;
margin: 2rem 0;
font-weight: bold;
transition: all 1s;
height: 50px;
overflow: hidden;
}
.value.hidden {
margin: 0;
height: 0px;
}
<main class="counter">
<h1>Counter</h1>
<div id="val" class="value">10</div>
<button onclick="document.getElementById('val').classList.add('hidden')">Hide Counter</button>
<button onclick="document.getElementById('val').classList.remove('hidden')">Show Counter</button>
</main>

I have found the bug in your code... you just need to go on line no. 24 in css in your codepen and change max-height to height and you are all set to go!!

Terry is right in his comment: transitioning e.g. something with actual height 50px from max-height: 100px; to max-height: 0; produces visible change only during the second half of animation, when it reaches the actual height.
CSS can transition or animate only explicitly given values.
Satyam is also right suggesting you can transition real effective height to get all transitions run simultaneously. So you have to either give element explicit (max-)height in CSS, or you can obtain it from actual computedStyle before triggering the transition. There are few important caveats, see comments POC:
.counter {
margin: 0 auto;
box-shadow: 0 1px 8px rgba(0, 0, 0, 0.2);
border-radius: 8px;
padding: 1rem;
text-align: center;
background-color: #f4f0fa;
}
.value {
font-size: 2rem;
color: #3c0080;
font-weight: bold;
transition: all 1s linear;
overflow: hidden;
background-color: grey;
margin: 2rem 0;
height: auto; /* you cannot transition this;
will be set in inline style
with JS */
}
.value.hidden {
height: 0px !important; /* to fight inline style, !important is necessary */
margin: 0 0;
font-size: 0;
}
<main class="counter">
<div id="val" class="value">10</div>
<button onClick="
if(!val.style.height){
val.style.height = getComputedStyle(val).height
}
/* caveat: 'seal' that value to give transition chance to start at it
(enforce style recalc and reflow)
*/
val.getBoundingClientRect();
val.classList.toggle('hidden');">Toggle Counter</button>
</main>

Related

Add a semi-transparent layer on top of a div

I would like to allow my user to click on the card to "cancel it". I can take care of the JS, but I am wondering what's the best way to add a "red opaque overlay" on top of the card after the user clicks it.
So ideally I can write a css class such as but also make it that it's not simply transparent at 40% but also slightly "red". And I don't want to create another dive that wraps the whole card if possible.
.card-deactivated{
opacity:40%
}
.card{
max-width:300px;
min-width:200px;
height: 350;
display: flex;
flex-direction: column;
padding: 0px 0px 20px;
gap: 10px;
background: #FFFFFF;
/* Gray/4px */
box-shadow: 0px 4px 8px rgba(41, 41, 41, 0.08);
border-radius: 10px;
overflow:hidden;
}
.card-img{
order: 0;
display:block;
margin-bottom:2rem;
}
.card-title{
align-self:center;
margin-bottom:1rem;
}
.card-description{
align-self:center;
margin:0px 20px 1rem;
text-align:center;
}
<div class='card'>
<img class='card-img' alt='Card Image' src='data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAoHCBYVFRgWFhUZGRgaHRwcHBwcHBwaHxoaGBgaGRoaGhocIS4lHB4rHxgYJjgmKy8xNTU1GiQ7QDs0Py40NTEBDAwMEA8QHxISHzQrJCs0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NDQ0NP/AABEIAOEA4QMBIgACEQEDEQH/xAAbAAABBQE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gvRGV2ygKSSuRCRiZZ79kkkGKE3PRyHt9W/wCq6kmXgr9IrxqUKNUkky+wovbhorC8/B5pJKr6KpfJFRafEnu0SSSfRpQrHUd/uEbePiPY/VJJRCseP/mP9ioLP4h3CSStgMGXnVnmuD4z2SSUYEQv+JXGFb/6FJJJH1CZPCrstfJav2f/APjb9j9F1JXY/mZ+R8DHpJJJyo//2Q=='>
<h3 class='card-title'>Name Surname</h3>
<p class='card-description'>This is a long descriptionThis is a long descriptionThis is a long descriptionThis is a long descriptionThis is a long description </p>
</div>
Edit
I am achieving some sort of result by leveraging this css but I don't think I am doing it "right"...or at least in the best possible way.
.card{
max-width:300px;
min-width:200px;
height: 350;
display: flex;
flex-direction: column;
padding: 0px 0px 20px;
gap: 10px;
background: #FFFFFF;
/* Gray/4px */
box-shadow: 0px 4px 8px rgba(41, 41, 41, 0.08);
border-radius: 10px;
overflow:hidden;
position:relative;
}
.overlay-selected::after {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: .3;
background-image: linear-gradient(90deg, #eaee44, #eaee44);
}
You can achieve this by using one of the pseudo-elements, either ::before or ::after, that will act as an overlay when the user clicks on the .card element.
Basically, we'll initially style that overlay, we'll use the ::after pseudo-element, and place it behind the .card element using z-index of -1 so that the overlay is hidden initially. When a click occurs, we add a class, let's call it is-canceled, to the .card element that simply sets the z-index of the overlay to a higher value (like 2 or 999) so that the overlay appears on top of the .card element and act as the intended overlay.
To make the overlay transition smoothly, we'll initially set its background-color to transparent and override that when the is-canceled class is attached to the .card element. When the is-canceled class is attached to the .card element, we'll set the background-color to a reddish background (something like rgba(255, 0, 0, .7). Using the transition property we can instruct the browser to have a smooth transition when the is-canceled is added/removed from the .card element.
To illustrate, here's alive demo that allows you to toggle the overlay when you click on the .card element:
const card = document.querySelector('.card');
card.addEventListener('click', () => card.classList.toggle('is-canceled'));
.card {
position: relative; /* required to correctly position the overlay */
max-width: 300px;
min-width: 200px;
display: flex;
flex-direction: column;
padding: 0px 0px 20px;
gap: 10px;
box-shadow: 0px 4px 8px rgba(41, 41, 41, 0.08);
border-radius: 10px;
overflow: hidden;
}
/* initial overlay styles */
.card::after {
content: "";
position: absolute;
width: 100%;
height: 100%;
inset: 0;
background-color: transparent;
transition: all .4s 0s ease;
z-index: -1;
}
/* styles to be applied to the overlay when the "is-canceled" class is added */
.card.is-canceled::after {
background-color: rgba(255, 0, 0, .7);
z-index: 999;
}
.card-img {
order: 0;
display: block;
margin-bottom: 2rem;
}
.card-title {
align-self: center;
margin-bottom: 1rem;
}
.card-description {
align-self: center;
margin: 0px 20px 1rem;
text-align: center;
}
<div class="card">
<img class="card-img" alt="Card Image" src="https://via.placeholder.com/250">
<h3 class="card-title">Name Surname</h3>
<p class="card-description">This is a long descriptionThis is a long descriptionThis is a long descriptionThis is a long descriptionThis is a long description </p>
</div>
The above demo is just for demonstration purposes and you're invited to build upon it and customize it the way you see fit.

setting overflow to anything other than visible makes the contents disappear CSS

i want to make a discord channels kinda scrollable list thing but if i ever change the overflow property of the div that it's contained in, it's width becomes 0px
example of what i want to do:
html
<div class="channels">
<div class="dummyChannel"></div>
<div class="channelButton"># bruh</div>
<div class="channelButton"># bruh</div>
<div class="channelButton"># bruh</div>
</div>
css
.channelButton{
background-color: var(--bg-color);
margin: 10px;
border-radius: var(--roundness);
width: 200px;
padding: 5px;
padding-left: 10px;
box-shadow: 0 2.5px 15px var(--shadow-color);
transition: 0.25s;
}
.channelButton:hover{
box-shadow: 0 5px 25px var(--shadow-color);
}
.channelButton:active{
transition: 0.1s;
box-shadow: 0 0px 0px var(--shadow-color);
}
.channels{
width: 235px;
display: flex;
flex-direction: column;
overflow-x: visible;
overflow-y: auto;
}
.dummyChannel{
height: 70px;
}

How to put background image directly to a div

I have a div like this:
Now I need to remove a part of the circle from this shape which is shown as below:
Therefore the final shape would be looked like this:
So I decided to put the image as the background of my div.
<div class="col-4 customBack">
<div class="mainInfo">
<div class="circle2">
<img src="https://sitename.com/images/image.png">
</div>
<div class="paraDivRight2">
<h6 style="margin-top:5px;"><strong>Lorem Ipsum Dolor Simit</strong></h6>
<hr style="margin-top:-5px;">
<p style="margin-top:-10px;">012-3456789</p>
<p style="padding-top:5px;">ifno#sitename.com</p>
</div>
</div>
</div>
And here are the styles:
.mainInfo{
background-color: #fff;
border: .01rem round #e0e1ed;
border-radius: 20px;
color: #585CA1;
width:100%;
height:5em;
box-shadow: 0px 0px 17px -5px rgba(0,0,0,0.75);
margin-top: 3em;
display: flex;
align-items: center;
justify-content: center;
}
.customBack{
background-image: url("/img/shadow3.png") !important;
}
.circle2 {
position: relative;
left:-60%;
width: 9em;
height: 9em;
background: #fff;
border-radius: 50%;
box-shadow: 0px 0px 17px -5px rgba(0,0,0,0.65);
}
.circle2 img {
position: absolute;
max-width: 85%;
border-radius: 50%;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 100;
}
.paraDivRight2 {
position: absolute;
display: inline-block;
padding: 10px;
color:black;
top:0px !important;
padding-top:50px !important;
right: 20px;
text-align: right;
padding-right:50px !important;
}
.paraDivRight2 p {
line-height: 1em;
font-size: 10pt;
margin: 0;
letter-spacing: 1px;
}
As you can see I have put the background in .customBack class But the result is now looks like this:
So the question is, how can I properly place this background image which is (shadow3.png) as background image of this mainInfo div so the side of circle shape that needs to be removed, does not appear...
I'm really stuck with this, so please help me out...
Use CSS filter: drop-shadow() MDN Docs on a wrapper element.
Fix your class naming to use a friendlier convention
Use CSS flex for a simpler alignment of your elements
Stop using inline HTML style attributes
/* Quick Reset */
* {
margin: 0;
box-sizing: border-box;
}
.custom {
filter: drop-shadow(0 2px 5px rgba(0, 0, 0, 0.4));
display: flex;
flex-wrap: nowrap;
align-items: center;
}
.custom-image {
width: 9em;
height: 9em;
background: #fff;
border-radius: 50%;
padding: 1em;
}
.custom-image img {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 50%;
}
.custom-content {
position: relative;
background: #fff;
padding: 1em;
text-align: right;
border-radius: 0 1em 1em 0;
padding-left: 2em;
left: -1em;
}
.custom-content h4 {
border-bottom: 1px solid #ddd;
}
<div class="custom">
<div class="custom-image">
<img src="https://i.stack.imgur.com/qCWYU.jpg?s=256&g=1">
</div>
<div class="custom-content">
<h4>Lorem Ipsum Dolor Simit</h4>
<p>012-3456789</p>
<p>ifno#sitename.com</p>
</div>
</div>
I'm not 100% sure about this but it has worked for me in the past,try making the position attribute of the div relative and make it absolute for the image,then size it properly.

Make element with position: absolute stretch the shadow of parent?

I have a usual search as most websites do. The results are shown below on the div that is visually connected to the search input.
It looks like this:
I need to have one solid shadow for the div parent but can't figure out or find online the way to do this.
I thought that I could either make 2 separate shadows, but that will look inconsistent and just terrible. Or I could make a div below with the same height and width that will act as a shadow but that's a non-necessary complication + the .search-results div's height will change dynamically.
This is an example:
body {
background-color: gray;
}
.search-wrapper {
position: relative;
margin: 100px 100px 0px 100px;
width: 200px;
overflow: initial;
box-shadow: 0px 0px 10px 10px rgba(0,0,0,0.3);
}
.search {
width: 200px;
height: 30px;
color: white;
border-radius: 4px;
} .search input {
padding: 0;
background-color: #022222;
border: none;
height: 100%;
width: 100%;
color: white;
}
.search-results {
position: absolute;
height: 150px;
width: 200px;
background-color: black;
}
<div class="search-wrapper">
<div class="search">
<input placeholder="air max . . .">
</div>
<div class="search-results">
</div>
</div>
I am sure there must be a clever and simple way to do this.
Please help,
Thank you
You don't need to use positions here and you can use FlexBox instead. It's the best way and a lot easier. Also, you can ignore all of them, they will place on top of each other because they are block-level tags/elements. (divs)
You don't need to put the input in another div parent, use it as I did.
Sorry, I couldn't understand your code, so I must write the whole code from the beginning.
EDIT
I removed display flex, cause it's not necessary.
* {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: Arial;
border-radius: 10px;
background-color: #fff
}
body {
height: 100vh;
background-color: gray;
padding: 30px
}
.search-wrapper {
/* EDITED HERE ADDED HEIGHT */
position: relative;
z-index: 999;
width: 200px;
height: 160px;
box-shadow: 0 0 2px 5px rgba(232, 232, 232, .2)
}
.search-input {
width: 100%;
position: absolute;
padding-block: 5px;
border: none;
outline: none;
padding: 15px
}
.search-result {
/* EDITED HERE */
position: absolute;
z-index: 999;
bottom: 0;
width: 100%;
padding: .5px
}
p {
padding: 10px 0 10px 10px;
}
p:hover {
background-color: #e8e8e8;
cursor: pointer
}
<div class='search-wrapper'>
<input class='search-input' placeholder='Search...'>
<div class='search-result'>
<p>Nike Airforce</p>
<p>Nike Airforce</p>
<p>Nike Airforce</p>
</div>
</div>

z-index conflict with static element

https://jsfiddle.net/0Lfzbzc5/2/
in here I am trying to make the notification box on top of the body class div but couldn't do it the logic says positioned elements should be on top of the not positioned elements but that isn't happenning
tried even making body class div relative and giving it z-index but failed too
structure of notification box is an absolute element in relative element in absolute element (for CSS animation issues)
HTML
<div class="notiIcon glyphicon glyphicon-globe">
</div>
<div class='notiAbs '>
<div class='notiContainer'>
<div class="notiBox">
<div class="notiHeader">
<span class="notiHeaderSpan">notifications</span>
</div>
<div class="notiBody">
<div class="notiElement">Collaboratively enable high-quality imperatives before ubiquitous paradigms.
</div>
<div class="notiElement">Credibly productize customized services whereas.</div>
<div class="notiElement">Efficiently embrace real-time markets without.</div>
<div class="notiElement">Synergistically simplify collaborative web services.</div>
<div class="notiElement">Intrinsicly evisculate magnetic e-services through.</div>
<div class="notiElement">Holisticly build customer directed technologies.</div>
<div class="notiElement">Phosfluorescently synthesize team driven strategic.</div>
</div>
<div class="notiFooter"><span class="notiHeaderSpan">See All</span></div>
</div>
</div>
</div>
<div class="body">aasdasdasdasdasdasdas</div>
CSS
.notiAbs{
position: absolute;
left: 0;
right: 0;
margin-left: auto;
margin-right: auto;
/* overflow-y: hidden; */
height: 500px;
width: 500px;
/* overflow-x: hidden; */
overflow-y: hidden;
margin: 0 auto;
padding-left: 50px;
}
.notiContainer{
position: relative;
}
.notiIcon{
z-index: 5;
position: relative;
width:100%;
text-align: center;
font-size: 25;
cursor: pointer;
padding-top: 10px;
}
.notiIconNumber{
position: relative;
font-size: 15px;
color: white;
background-color: red;
top: -10;
left: -9;
padding: 2px;
}
.notiBox{
z-index: 4;
position: absolute;
height: 400px;
width: 400px;
display: block;
padding-top: 10px;
box-shadow: rgba(0, 0, 0, 0.298039) 0px 4px 7px;
}
.notiElement{
overflow-wrap:break-word;
font-size: 17px;
padding: 10 0px;
border-bottom-style: solid;
border-bottom-width: thin;
border-bottom-color: lightgray;
}
.notiHeader,.notiFooter{
text-align: center;
font-size: 20px;
font-weight: bold;
height: 15%;
display: table;
width: 100%;
}
.notiHeaderSpan,.notiFooterSpan{
display: table-cell;
vertical-align: middle;
}
.notiFooter{
box-shadow: 0px -4px 7px rgba(0, 0, 0, 0.1);
cursor: pointer;
}
.notiHeader{
box-shadow: 0px 4px 7px rgba(0, 0, 0, 0.1);
}
.notiBody{
padding: 20px;
overflow: auto;
height:70%;
}
.body{
}
It is on top but the background is transparent so it makes the illusion that it's not. Just set a background color as follows :
.notiBox{
z-index: 4;
position: absolute;
height: 400px;
width: 400px;
padding-top: 10px;
border-style:solid;
background:#666;
}
Check the Fiddle.
Your notification box which I believe is the element with class "notiBox" is on top. The reason why it appears not to be is because it has an inherited background-color of transparent.
If you set the background-color property to say "yellow" (for examples sake) you will see that it is on top of the element with class "body".
Does that make sense? I can explain further if you need me to.
I've updated my answer as looking at your HTML again i've realised that the element with class "notiBox" is probably the only element (and it's contents) you want to appear on top