CSS - Flexbox ,shrink items - css

I have a flex box container, with a child element inside. When the screen size shrinks, I want the child to also shrink. Here is my existing code:
HTML:
<div class="container">
<div class="child"></div>
</div>
CSS:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.child {
width: 570px;
height: 500px;
background-color: red;
}
Does anyone know how to accomplish this? Thanks.
UPDATE:
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.child {
flex-basis: 200px;
width: 800px;
background-color: red;
}
UPDATE 2:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
max-width: 570px;
}
.child {
width: 100%;
height: 500px;
background-color: red;
}
Update number 3. Note this has not flex direction, therefore is column by default:
.container {
display: flex;
align-items: center;
justify-content: center;
padding: 0 20px;
}
.child {
background-color: red;
width: 800px;
height: 800px;
}

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.child {
background-color: red;
width: 80%;
max-width: 800px;
height: 800px;
}
<div class="container">
<div class="child"></div>
</div>

You can add a media query to accomplish what you aim:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
display: flex;
align-items: center;
flex-direction: column;
justify-content: center;
}
.child {
background-color: red;
width: 100%;
height: 800px;
}
#media screen and (min-width: 800px){
.container {
padding: 0 20px;
}
.child {
width: 800px;
}
}

Here is one just set the width of the .container to 100% of its container or body so when when the body shrinks also the width of .container reduced and give the .child 50% of its width
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
height: 50vh;
}
.child {
position: relative;
width: 50%;
height: 100%;
background-color: red;
}
<div class="container">
<div class="child"></div>
</div>

Related

How to let a Flex column on the bottom keeping the items order using CSS? [duplicate]

This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 4 months ago.
I have a horizontally centered column of Flex items ordered from 1 to 5 that are aligned from the top of the container like this:
body, html {
height: 100%;
position: relative;
margin: 0;
padding: 0;
}
.container {
display: inline-flex;
flex-wrap: wrap;
flex-direction: column;
align-items: flex-end;
align-content: center;
width: 100%;
height: 100%;
background: pink;
}
.item {
margin: 1px;
width: 30px;
height: 30px;
background: green;
}
<div class=container><div class=item>1</div><div class=item>2</div><div class=item>3</div><div class=item>4</div><div class=item>5</div></div>
I would like to let it aligned by the bottom of the container instead. I manage to do it with flex-direction: column-reverse; like in the next Snippet:
body, html {
height: 100%;
position: relative;
margin: 0;
padding: 0;
}
.container {
display: inline-flex;
flex-wrap: wrap;
flex-direction: column-reverse;
align-items: flex-end;
align-content: center;
width: 100%;
height: 100%;
background: pink;
}
.item {
margin: 1px;
width: 30px;
height: 30px;
background: green;
}
<div class=container><div class=item>1</div><div class=item>2</div><div class=item>3</div><div class=item>4</div><div class=item>5</div></div>
However, as you see, the items get out of order! Is there a way to let a flex column on the bottom without reversing the items order using CSS? I tried every Flex property that I know so far without success.
You can use justify-content: end;
.container {
width: 150px;
height: 150px;
border: 1px solid black;
display: flex;
flex-direction: column;
align-items: center;
justify-content: end;
}
.content {
width: 25px;
height: 25px;
border: 1px solid black;
}
<div class="container">
<div class="content">1</div>
<div class="content">2</div>
<div class="content">3</div>
<div class="content">4</div>
<div class="content">5</div>
</div>
You need to use the justify-content property to align content along the main axis (in your case vertically). You are using align-items which defines how the items should be aligned along the cross axis.
body, html {
height: 100%;
position: relative;
margin: 0;
padding: 0;
}
.container {
display: inline-flex;
flex-wrap: wrap;
flex-direction: column;
justify-content: flex-end;
align-content: center;
width: 100%;
height: 100%;
background: pink;
}
.item {
margin: 1px;
width: 30px;
height: 30px;
background: green;
}
<div class=container>
<div class=item>1</div>
<div class=item>2</div>
<div class=item>3</div>
<div class=item>4</div>
<div class=item>5</div>
</div>

css not working when trying to center self

I'm trying to make an item center,but somehow it not work
Here is html part:
<div>
<div className={styles.unanswerQuestionContainer}>
<div className={styles.headerContainer}>
<div>Unanswered Questions</div>
<div>Answered Questions</div>
</div>
</div>
</div>
Here is css part:
.unanswerQuestionContainer {
height: 60vh;
width: 60vw;
border: 1px solid;
display: flex;
margin: 20px 0 0 0;
align-self: center;
}
.headerContainer {
display: flex;
flex-direction: row;
justify-content: space-between;
width: 100%;
}
But it show something like this:
It do not center the div, how can i center this ??
Edit: When i try to inspect then it show all of this is margin:
You need to add following style to outer div(parent) unanswerQuestionContainer
display: flex;
justify-content: center;
align-items: center;
height: 100%;
body{
height: 100vh;
margin: 0;
}
.unanswerQuestionContainer {
height: 60vh;
width: 60vw;
border: 1px solid;
display: flex;
margin: 20px 0 0 0;
align-self: center;
}
.headerContainer {
display: flex;
flex-direction: row;
justify-content: space-between;
width: 100%;
}
.outer{
display: flex;
justify-content: center;
align-items: center;
height: 100%;
}
<div class="outer">
<div class="unanswerQuestionContainer">
<div class="headerContainer">
<div>Unanswered Questions</div>
<div>Answered Questions</div>
</div>
</div>
</div>
You will have to adjust which css properties you will use in order to prevent the two h1 from overlapping.
<div className={styles.unanswerQuestionContainer}>
<div class="Questions">
<p id="question-type">Answered Question</p>
<p id="question-type">Unanswered Question</p>
</div>
</div>
.questions h1 {
width: 100px;
height: 100px;
position: absolute;
top:0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}

Restrict element height to content, and maintain collapsed overflow

I have a pop-up modal which works overall, however the one annoyance is it has a hardcoded max-height which I'd like to eliminate.
Option #1:
Initially I explored using height: auto on the modal, which does keep the modal height to the natural height of the contents. However this effects the collapsing of the modal when you scale the browser viewport to a short height. The modal overflows out of the viewport, instead of only the green image area overflowing.
Option #2: I'm aware of the possibility of max-content (for height... or even max-height ?) but I haven't been able to get it to work anywhere, and anyhow it has spotty browser support.
Option #3 (current): Setting the modal to height: 100% and max-height: 500px is good enough, however obviously the content needs to be shorter than that.
Overall, requirements are:
A - In small screens, the modal should collapse with the green image area overflowing, thereby maintaining modal title and buttons in view.
B - In large screens, the modal height should only be as big as the contents.
C - Whatever happens, the modal should never visibly go past the global padding (2em).
See #modal in CSS below:
Demo and code here (Codepen)
html, body {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
overflow: hidden;
}
#app {
background-color: gray;
width: 75%;
height: 75%;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
padding: 2em;
}
#container {
height: 100%;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
#modal {
/* OPTION #1 */
/* FAILS in small screen: overflow of green image not invoked */
/* height: auto; */
/* OPTION #2 */
/* Not working? */
/* height: max-content; */
/* OPTION #3 */
/* WORKS but specifying a max-height is not ideal */
height: 100%;
max-height: 500px;
width: auto;
position: relative;
background-color: pink;
overflow: hidden;
}
#modal_inner {
display: flex;
flex-direction: column;
align-items: center;
height: 100%;
padding: 2em;
box-sizing: border-box;
}
#image {
overflow-y: auto;
overflow-x: hidden;
flex: 1;
}
#image .inner {
background-color: lime;
padding: 1em;
width: 400px;
height: 200px;
}
#controls {
background-color: yellow;
display: flex;
justify-content: center;
max-width: 20em;
width: 100%;
padding: 1em;
margin-top: 1em;
}
#cta {
background-color: white;
display: flex;
justify-content: center;
max-width: 10em;
padding: 1em;
margin-top: 1em;
}
<div id="app">
<div id="container">
<div id="modal">
<div id="modal_inner">
<div id="title">TITLE</div>
<div id="image">
<div class="inner">image</div>
</div>
<div id="controls">controls</div>
<div id="cta">submit</div>
</div>
</div>
</div>
</div>
You are almost good, use max-height:100% and also add display:flex that will give the height:100% effect you are trying to achieve on the modal_inner
html, body {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
overflow: hidden;
}
#app {
background-color: gray;
width: 75%;
height: 75%;
display: flex;
justify-content: center;
align-items: center;
overflow: hidden;
padding: 2em;
}
#container {
height: 100%;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
}
#modal {
max-height: 100%;
display:flex;
position: relative;
background-color: pink;
overflow: hidden;
}
#modal_inner {
display: flex;
flex-direction: column;
align-items: center;
/*height: 100%; remove this*/
padding: 2em;
box-sizing: border-box;
}
#image {
overflow-y: auto;
overflow-x: hidden;
flex: 1;
}
#image .inner {
background-color: lime;
padding: 1em;
width: 400px;
height: 200px;
}
#controls {
background-color: yellow;
display: flex;
justify-content: center;
max-width: 20em;
width: 100%;
padding: 1em;
margin-top: 1em;
}
#cta {
background-color: white;
display: flex;
justify-content: center;
max-width: 10em;
padding: 1em;
margin-top: 1em;
}
<div id="app">
<div id="container">
<div id="modal">
<div id="modal_inner">
<div id="title">TITLE</div>
<div id="image">
<div class="inner">image</div>
</div>
<div id="controls">controls</div>
<div id="cta">submit</div>
</div>
</div>
</div>
</div>

How to make full height sidebar with dynamic main content?

I have an angular2 app with left a sidebar and dynamic main content.
HTML:
<body>
<div class="full_height">
<div class="sidebar">Some content</div>
<div class="main">Some dynamic content</div>
</div>
</body>
CSS:
body{
width: 100%;
height: 100%;
margin: 0;
}
.full_height{
display: flex;
flex-direction: row;
align-items: stretch;
}
.sidebar{
background: black;
color: white;
width: 150px;
flex: 0 0 auto;
}
.main{
flex: 1 1 auto;
}
I need the sidebar grow to 100% height of the browser body if no information in the main block. And I need the sidebar and the main block have equal height when a big amount of information have been loaded to the main block via AJAX.
How to make the first part work?
JSFiddle
I think you just forgot to assign a 100% height to the HTML element. Is this what you tried to do ?
html, body{
width: 100%;
height: 100%;
margin: 0;
}
.full_height{
display: flex;
flex-direction: row;
align-items: stretch;
height: 100%;
}
.sidebar{
background: black;
color: white;
width: 150px;
flex: 0 0 auto;
}
.main{
flex: 1 1 auto;
}
<body>
<div class="full_height">
<div class="sidebar">Some content</div>
<div class="main">Some dynamic content</div>
</div>
</body>
If you want to make full height sidebar then sidebar will be fixed position and rest of the content will be relative position. Please check the below Snippet.
html{
box-sizing: border-box;
height: 100%;
width: 100%;
}
*,
*:after,
*:before{
box-sizing: inherit;
}
body{
margin: 0;
padding: 0;
}
.full_height{
background-color: white;
display: flex;
flex-direction: column;
}
#media (min-width: 992px){
.full_height{
padding-left: 330px;
}
}
.sidebar{
background-color: #5c5c5c;
color: white;
display: flex;
flex-direction: column;
margin-bottom: 30px;
padding: 20px;
width: 100%;
}
#media (min-width: 992px){
.sidebar{
height: 100%;
left: 0;
overflow-x: hidden;
overflow-y: auto;
position: fixed;
top: 0;
width: 310px;
z-index: 1030;
}
}
.main{
background-color: #009688;
color: white;
font-size: 20px;
margin-bottom: 30px;
padding: 20px;
width: 100%;
}
<div class="full_height">
<div class="sidebar">Some content</div>
<div class="main">Some dynamic content</div>
</div>
Snippet two This relative content
html{
box-sizing: border-box;
height: 100%;
width: 100%;
}
*,
*:after,
*:before{
box-sizing: inherit;
}
body{
height: 100%;
margin: 0;
padding: 0;
width: 100%;
}
.full_height{
background-color: black;
display: flex;
flex-direction: column;
padding: 20px;
}
#media (min-width: 992px){
.full_height{
align-items: stretch;
flex-direction: row;
flex-wrap: nowrap;
height: 100%;
width: 100%;
}
}
.sidebar{
background-color: #f5f5f5;
color: #009688;
flex-basis: 100%;
flex-grow: 0;
flex-shrink: 0;
max-width: 100%;
padding: 20px;
}
#media (min-width: 992px){
.sidebar{
flex-basis: 310px;
max-width: 310px;
}
}
.main{
background-color: white;
color: black;
padding: 20px;
width: 100%;
}
#media (min-width: 992px){
.main{
padding-left: 40px;
}
}
<div class="full_height">
<div class="sidebar">Sidebar content</div>
<div class="main">Main Content</div>
</div>
Check the Snippet in full width view. All snippet is responsive.
If you want both the .sidebar and .main to fill the entire screen, just add the 100vh height.
.sidebar,
.main {
height: 100vh;
}

Can't get display:block element (instead a display:flex element) to contain it's child in IE11

How can I get my block element to contain it's child in IE11 EDGE?
In the fiddle, you can see that <footer> is sitting below <login-page> when it should be sitting below <login>. (Because <login-page> isn't encapsulating <login>).
Works fine in Chrome, not IE11.
https://jsfiddle.net/10ohr6wy/1/
Would love to get some advice on this thanks. Ideally a non-obtrusive fix would be preferred, as have legacy constraints.
I believe you are actually dealing with the flex + min-height bug.
One way, to go over it, is to set the parent as a flex column element:
here
body {
display:flex;
flex-flow:column;
}
works fine. https://jsfiddle.net/10ohr6wy/3/
body {
display: flex;
flex-flow: column;
}
app {
min-height: 100vh;
display: flex;
flex-direction: column;
}
.xui-page-body {
padding-bottom: 40px;
display: flex;
flex: 1;
flex-direction: column;
}
login-page {
flex: 1;
padding: 10px;
margin-bottom: -40px;
display: block;
background: green;
}
login {
display: block;
height: 200px;
background: red;
}
footer {
height: 50px;
display: block;
background: black;
}
<app>
<div class="xui-page-body">
<login-page>
<login></login>
</login-page>
</div>
<footer></footer>
</app>
if you do not set flex-direction to column, then app needs to be the only child.
it can be sized via :
flex:1; if you want to include margins
body {
display: flex;
}
app {
flex:1;
min-height: 100vh;
display: flex;
flex-direction: column;
}
.xui-page-body {
padding-bottom: 40px;
display: flex;
flex: 1;
flex-direction: column;
}
login-page {
flex: 1;
padding: 10px;
margin-bottom: -40px;
display: block;
background: green;
}
login {
display: block;
height: 200px;
background: red;
}
footer {
height: 50px;
display: block;
background: black;
}
<app>
<div class="xui-page-body">
<login-page>
<login></login>
</login-page>
</div>
<footer></footer>
</app>
or
width:100%; and box-sizing:border-box to include border and padding if any.
body {
display: flex;
}
app {
width:100%;
box-sizing:border-box;
padding: 3em;
min-height: 100vh;
display: flex;
flex-direction: column;
}
.xui-page-body {
padding-bottom: 40px;
display: flex;
flex: 1;
flex-direction: column;
}
login-page {
flex: 1;
padding: 10px;
margin-bottom: -40px;
display: block;
background: green;
}
login {
display: block;
height: 200px;
background: red;
}
footer {
height: 50px;
display: block;
background: black;
}
<app>
<div class="xui-page-body">
<login-page>
<login></login>
</login-page>
</div>
<footer></footer>
</app>
Make the body element a flex container.
Give app full width.
revised fiddle
body {
display: flex; /* new */
}
app {
min-height: 100vh;
display: flex;
flex-direction: column;
width: 100%; /* new */
}
.xui-page-body {
padding-bottom: 40px;
display: flex;
flex: 1;
flex-direction: column;
}
login-page {
flex: 1;
padding: 10px;
margin-bottom: -40px;
display: block;
background:green;
}
login {
display: block;
height: 200px;
background: red;
}
footer {
height: 50px;
display: block;
background:black;
}
<app>
<div class="xui-page-body">
<login-page>
<login></login>
</login-page>
</div>
<footer></footer>
</app>
Also see: flex container min-height ignored in IE

Resources