I have a div container inside a flex container with set maximum width. When a child is removed the width will decrease. Is it possible to animate the width change with just CSS?
function remove(el) {
var element = el;
element.remove();
}
#flex {
display: flex;
}
#parent {
max-width: 200px;
background: blue;
border: 2px solid green;
height: 100px;
width: auto;
}
#child {
width: 100px;
height: 100px;
background: red;
}
#other {
flex: 1;
height: 100px;
background: yellow;
}
<div id="flex">
<div id="parent">
<div id="child" onclick="remove(this)">
</div>
</div>
<div id="other">
</div>
</div>
You cannot do with pure CSS. The animation is based on the width changes, so you need to set the width of #child to 0 by Javascript. To remove #child completely, you can delay it with setTimeout.
function remove(el) {
var element = el;
el.style.width = 0; //trigger the animation with width changes
setTimeout(() => {
element.remove();
}, 500); //0.5 seconds
}
#flex {
display: flex;
}
#parent {
max-width: 200px;
background: blue;
border: 2px solid green;
height: 100px;
width: auto;
}
#child {
width: 100px;
height: 100px;
background: red;
transition: width 0.5s; /* Width animation in 0.5 seconds */
}
#other {
flex: 1;
height: 100px;
background: yellow;
}
<div id="flex">
<div id="parent">
<div id="child" onclick="remove(this)">
</div>
</div>
<div id="other">
</div>
</div>
Related
Is it possible to make a div with a height of 100%, and have the inner item that overflows scroll.
It sounds like a simple problem (and it might be), but I have been thinking about it for days. Some things that don't work in my specific case are:
height: calc(100% - xx px), because the header is of a variable height.
Putting all high children as direct children in the wrapper component (I use a component that has some layers
<div class="wrapper">
<div class="header">
A header
</div>
<div class="container">
A container
<div class="with-many">
Don't scroll
<div class="divs">
Scroll
</div>
</div>
</div>
</div>
.wrapper {
width: 80vw;
height: 80vh;
background-color: lightgrey;
* {
width: 100%;
}
.header {
height: 30px;
background-color: orange;
}
.container {
height: 100%;
background-color: lightgreen;
.with-many {
height: 100%;
overflow-y: auto;
background-color: green;
.divs {
height: 400vh;
background-color: blue;
}
}
}
}
https://jsfiddle.net/zdb8pmuL/1/
Do you mean like this?:
.wrapper {
width: 80vw;
height: 80vh;
background-color: lightgrey;
}
.wrapper * {
width: 100%;
}
.header {
height: 30px;
background-color: orange;
}
.container {
height: 100%;
background-color: lightgreen;
display: flex;
flex-direction: column;
}
.with {
height: 100%;
background-color: green;
}
.another-wrapper {
overflow-y: scroll;
height: 100%;
}
.divs {
height: 400vh;
background-color: blue;
}
<div class="wrapper">
<div class="header">
A header
</div>
<div class="container">
A container
<div class="with">
With many
<div class="another-wrapper">
<div class="divs">
nested divs
</div>
</div>
</div>
</div>
</div>
Let's say I have a parent <div> with a fixed height and with flex item children.
Is there a way to have its children height not fixed but extending depending on their content ?
.main-container {
height: 250px;
background-color: green;
display: flex;
flex: 0 1 auto;
overflow: auto;
}
.div1 {
width: 100%;
height: 100%;
background-color: lightcoral;
}
.div2 {
width: 100%;
background-color: lightgrey;
}
.div3 {
width: 100%;
background-color: lightpink;
}
div p {
font-size: 10em;
}
<div class="main-container">
<div class="div1">
<p>a</p>
</div>
<div class="div2">q</div>
<div class="div3">s</div>
</div>
Wanted result :
You don't need to use fit-content (which is not supported in Firefox: https://caniuse.com/#feat=mdn-css_properties_height_fit-content), you simply need to disable the stertch effect by adding align-self:flex-start; to the needed element
.main-container {
height: 250px;
background-color: green;
display: flex;
flex: 0 1 auto;
overflow: auto;
}
.div1 {
width: 100%;
align-self:flex-start;
background-color: lightcoral;
}
.div2 {
width: 100%;
background-color: lightgrey;
}
.div3 {
width: 100%;
background-color: lightpink;
}
div p {
font-size: 10em;
}
<div class="main-container">
<div class="div1">
<p>a</p>
</div>
<div class="div2">q</div>
<div class="div3">s</div>
</div>
Is there a way to have its children height not fixed but extending depending on their content ?
If you want a div to be as high as it content needs, you can apply height: fit-content; on that div.
Example in the snippet below (for the left column):
.main-container {
height: 250px;
background-color: green;
display: flex;
flex: 0 1 auto;
overflow: auto;
}
.div1 {
width: 100%;
height: fit-content;
background-color: lightcoral;
}
.div2 {
width: 100%;
background-color: lightgrey;
}
.div3 {
width: 100%;
background-color: lightpink;
}
div p {
font-size: 10em;
}
<div class="main-container">
<div class="div1">
<p>a</p>
</div>
<div class="div2">q</div>
<div class="div3">s</div>
</div>
I have following situation. Container c has fixed property. It's always 100% height. Not moving.
Container b has dynamic height.
Container a (body) can't have scrollbar visible at all.
Question: How to force the scrollbar from body to disappear, but the scrollbar in container b to appear? Thanks!
.a {
display: flex;
height: 300px;
}
.b,
.c {
width: 50%;
}
.b {
background: blue;
height: 600px;
overflow-y: scroll;
}
.c {
background: green;
height: 300px;
position: fixed;
right: 0;
}
<div class='a'>
<div class='b'>
</div>
<div class='c'>
</div>
</div>
Credit to SirExotic
Your .b container needs content that exceeds the height of b to activate the scroll bar.
body {
overflow-y: hidden; /* added */
}
.a {
display: flex;
height: 300px;
}
.b,
.c {
width: 50%;
}
.b {
background: blue;
height: 600px;
overflow-y: scroll;
}
.b-content {
min-height: 800px;
}
.c {
background: green;
height: 300px;
position: fixed;
right: 0;
}
<div class='a'>
<div class='b'>
<div class="b-content">
here I am
</div>
</div>
<div class='c'>
</div>
</div>
I have the blue square that will contain things adding up to 800px.
I want the red square to always be fully visible. That is when you narrow the viewport, the red square should overlap the blue square, and not disappear on the right like it does actually.
How can I achieve that?
.container {
display: flex;
}
div {
height: 80px;
}
.should-be-overlapped {
width: 100%;
min-width: 800px;
background: blue;
}
.always-full-width {
width: 400px;
background: red;
}
<div class="container">
<div class="should-be-overlapped"></div>
<div class="always-full-width"></div>
</div>
To accomplish that, there is mainly 2 ways.
Either add a wrapper around the blue (which I recommend).
Updated codepen
Stack snippet
.container {
position: relative;
display: flex;
}
div {
height: 80px;
}
.wrapper {
flex: 1;
overflow: hidden;
}
.should-be-overlapped {
width: 100%;
min-width: 800px;
background: blue;
}
.always-full-width {
flex: 0 0 400px;
background: red;
}
<div class="container">
<div class="wrapper">
<div class="should-be-overlapped">
</div>
</div>
<div class="always-full-width">
</div>
</div>
Or use position: absolute.
Updated codepen
.container {
display: flex;
position: relative;
overflow: hidden;
}
div {
height: 80px;
}
.should-be-overlapped {
width: 100%;
min-width: 800px;
background: blue;
}
.always-full-width {
position: absolute;
right: 0;
width: 400px;
background: red;
}
.<div class="container">
<div class="should-be-overlapped">
</div>
<div class="always-full-width">
</div>
</div>
I have the blue square that will contain things adding up to 800px.
Then you should do it with the flex: 0 1 800px, which will enable it to shrink:
.container {
display: flex;
}
div {
height: 80px;
}
.should-be-overlapped {
/*min-width: 800px;*/
flex: 0 1 800px; /* doesn't grow but shrinks, initial width set to 800px (this is also its "max-width") */
background: blue;
word-break: break-all; /* for longer unbreakable strings, just for demo */
}
.always-full-width {
/*width: 400px;*/
flex: 0 0 400px; /* since you're using flexbox, doesn't grow nor shrink, initial width set to 400px (fixed) */
background: red;
}
<div class="container">
<div class="should-be-overlapped">aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa</div>
<div class="always-full-width"></div>
</div>
I've got such an issue:
I want to increase hovered div width with simultaneously width decrease of his siblings. Everything works fine without setting transition property.
If I set transition and move mouse quickly. My divs don't fully fill their parent.
Here's my code:
#parent {
display: block;
position: relative;
}
#parent .child {
float: left;
height: 300px;
width: 20%;
background-color: red;
transition: width .5s;
}
#parent .child:nth-child(2) {
background-color: grey;
}
#parent .child:nth-child(3) {
background-color: green;
}
#parent .child:nth-child(4) {
background-color: yellow;
}
#parent .child:nth-child(5) {
background-color: brown;
}
#parent:hover .child {
width: 17.25%;
}
#parent:hover .child:hover {
width: 31%;
}
<div id="parent">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
<div class="child">5</div>
</div>
Do you have any ideas how to solve this problem? Can I fix it with pure CSS or I should use javascript for it?
Perhaps using Flexbox would give you a smoother effect.
You can adjust the flex property as you need to get the dimensions right.
body,
html {
/* for demo purposes */
padding: 0;
margin: 0;
}
#parent {
display: flex;
}
#parent .child {
height: 300px;
flex: 1 1 auto;
background-color: red;
transition: all .5s;
}
#parent .child:nth-child(2) {
background-color: grey;
}
#parent .child:nth-child(3) {
background-color: green;
}
#parent .child:nth-child(4) {
background-color: yellow;
}
#parent .child:nth-child(5) {
background-color: brown;
}
#parent .child:hover {
flex: 2 1 auto;
}
<div id="parent">
<div class="child">1</div>
<div class="child">2</div>
<div class="child">3</div>
<div class="child">4</div>
<div class="child">5</div>
</div>