So I already researched this briefly. I put an img in a div and made the img width 100%, which should have an impact on the height. I'm not understanding what's going on here. I just need my images to remain to their proper aspect ratio in the following flexbox.
.container {
display: flex;
position: relative;
flex-flow: row wrap;
justify-content: center;
align-items: stretch;
height: 100vh;
width: 80%;
/*padding-top: 2%;
padding-bottom: 18%;
margin: 5% auto 8% auto; */
background-color: white;
border: 2px solid red;
}
.top {
display: flex;
flex-flow: row wrap;
justify-content: center;
align-items: center;
border: 2px solid blue;
}
.top * {
1 1 50%;
display: flex;
flex-flow: row wrap;
justify-content: center;
align-content: center;
height: 100%;
}
.bottom {
display: flex;
flex-flow: row wrap;
justify-content: space-around;
align-items: flex-start;
border: 2px solid orange;
}
.top,
.bottom {
flex: 0 0 100%;
height: 50%;
}
.topa {
display: flex;
flex-flow: row wrap;
justify-content: center;
align-items: center;
align-content: center;
width: 50%;
height: 100%;
background-color: orange;
}
.topa div img {
width: 100%;
}
<div id="bigwrap">
<div class="container">
<div class="top">
<div class="topa">
<div>
<img src="http://placehold.it/209x205" width="209" height="205" alt="Picture of kid" />
</div>
<div>
<img src="http://placehold.it/209x205" width="209" height="205" alt="Picture of kid" />
</div>
</div>
<div class="topb">
</div>
</div>
<div class="bottom">
</div>
</div>
</div>
You are not using flexbox in the right way.
To do what you want margin:auto on .topa is enough...
Here is a css-tricks article to explain very weel how flexbox works
.container {
position: relative;
height: 100vh;
width: 80%;
background-color: white;
border: 2px solid red;
}
.top {
border: 2px solid blue;
}
.top * {
height: 100%;
}
.bottom {
border: 2px solid orange;
}
.topa {
width: 50%;
height: 100%;
background-color: orange;
margin: auto;
}
.topa div img {
width: 100%;
}
<div id="bigwrap">
<div class="container">
<div class="top">
<div class="topa">
<div>
<img src="http://placehold.it/209x205" width="209" height="205" alt="Picture of kid" />
</div>
<div>
<img src="http://placehold.it/209x205" width="209" height="205" alt="Picture of kid" />
</div>
</div>
<div class="topb">
</div>
</div>
<div class="bottom">
</div>
</div>
</div>
Use max-width for your image in CSS. And remove width?and height atribute from image tag. Or use height property auto in your img tag and css is-
.topa div img {
Max-width: 100%;
Height:auto;
}
Related
I want to make a image (inside a div) to the most left of the bottom div and I don't how to do this.
For example I have this image
.container {
display: flex;
flex-direction: column;
align-items: center;
}
.applicationimage {
width: 60px;
height: 60px;
border-radius: 100%;
}
.settings {
display: flex;
width: 80%;
height: 40rem;
background-color: white;
align-self: center;
}
<main>
<div class="container">
<div class="applicationinfo">
<img src="https://wallup.net/wp-content/uploads/2019/09/1667-beautiful-gray-cat-748x468.jpg" class="applicationimage">
</div>
<div class="settings">
<span>hi</span>
</div>
</div>
</main>
I'm new to html & css so I will appreciate your help making this image to the most left of his bottom div.
If you know the width of the div, it's easy. You can give the .applicationinfo element align-self: flex-start; and margin-left: 10%; (10% is calculated by this formula: (100% - widthOfDiv) / 2)
body {
background: black;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
}
.applicationinfo {
align-self: flex-start;
margin-left: 10%;
}
.applicationimage {
width: 60px;
height: 60px;
border-radius: 100%;
}
.settings {
display: flex;
width: 80%;
height: 40rem;
background-color: white;
align-self: center;
}
<main>
<div class="container">
<div class="applicationinfo">
<img src="https://wallup.net/wp-content/uploads/2019/09/1667-beautiful-gray-cat-748x468.jpg" class="applicationimage">
</div>
<div class="settings">
<span>hi</span>
</div>
</div>
</main>
remove the flex direction, here's it
display: flex;
justify-items: flex-end;
align-items: flex-end;}
You can do it by positioning applicationinfo since you haven't styled it.
you can add parent position: relative; then child to absolute; then give child top: 0; left: 0;
.container {
display: flex;
height: 80vh;
flex-direction: column;
align-items: center;
justify-content: center;
align-items: center;
position: relative;
background: #222;
border: 1px solid yellow;
}
.applicationinfo {
width: 80%;
top: 0;
left: 0;
border: 1px solid green;
}
.applicationimage {
width: 60px;
height: 60px;
border-radius: 100%;
border: 1px solid blue;
}
.settings {
display: flex;
width: 80%;
height: 80%;
background-color: white;
align-self: center;
}
<div class="container">
<div class="applicationinfo">
<img src="https://wallup.net/wp-content/uploads/2019/09/1667-beautiful-gray-cat-748x468.jpg" class="applicationimage">
</div>
<div class="settings">
<span>hi</span>
</div>
</div>
I'm facing this problem where I want to have a header, sidebar and content with flexbox. I can't get to a solution to divide these 3 childs. I've been trying to use flex-grow and flex-direction:row but I'm having a problem.
Image of the website
How I want it to be
<style>
.parent {
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
height: 100vh;
border: 20px solid black;
display: flex;
flex-direction: column;
}
.header {
width: 200px;
height: 200px;
background: cornflowerblue;
}
.side {
width: 200px;
height: 200px;
background: rgb(219, 133, 133);
}
.content {
width: 200px;
height: 200px;
background: rgb(115, 202, 180);
}
.text {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
height: 190px;
color: #fff;
}
</style>
<div class="parent">
<div class="header">
<h2 class="text">Header</h2>
</div>
<div class="side">
<h2 class="text">Side</h2>
</div>
<div class="content">
<h2 class="text">Content</h2>
</div>
</div>
You need to create two containers, one for all your elements and one for your header and content.
<div class="parent"> <!-- Container 1 -->
<div class="side">
<h2 class="text">Side</h2>
</div>
<div class="container"> <!-- Container 2 -->
<div class="header">
<h2 class="text">Header</h2>
</div>
<div class="content">
<h2 class="text">Content</h2>
</div>
</div>
</div>
Then, you can treat each container as a separate flex-box. the parent will have a flex-direction: row; and the container will have flex-direction: column;.
You also want to set values in percentages, not absolute values as you have right now (200px, 20rem..).
.parent {
box-sizing: border-box;
margin: 0;
padding: 0;
height: 100vh;
border: 20px solid black;
display: flex;
flex-direction: row;
}
.header {
height: 30%;
background: cornflowerblue;
}
.side {
width: 30%;
height: 100%;
background: rgb(219, 133, 133);
}
.content {
height: 70%;
background: rgb(115, 202, 180);
}
.text {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
height: 190px;
color: #fff;
}
.container {
display: flex;
flex-direction: column;
width: 70%;
height: 100%;
}
JSFiddle
Images to illustrate the separation:
You have to wrap your header & content section inside another div. Something like this below example. However, The best way to achieve this layout is using a CSS grid. Here is the complete CSS grid guide
.parent {
display: flex;
height: 100vh;
background: #000;
padding: 5px;
margin: 0;
}
.side {
border: 1px solid #000;
width: 30vw;
display: flex;
justify-content: center;
align-items: center;
background: #fff;
margin-right: 5px;
}
.main-body {
border: 1px solid #000;
width: 70vw;
}
.header,
.content {
display: flex;
justify-content: center;
background: #fff;
}
.header {
height: 25vh;
margin-bottom: 5px;
}
.content {
align-items:center;
height: 70vh;
}
<div class="parent">
<div class="side">
<h2 class="text">Side</h2>
</div>
<div class="main-body">
<div class="header">
<h2 class="text">Header</h2>
</div>
<div class="content">
<h2 class="text">Content</h2>
</div>
</div>
</div>
I don't think that you deeply understand how flexbox work. You should read more about it. I advice you to read a book called CSS-in Depth. You can download it online from a website called Z-lib. Try to understand the code that I posted for you.
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.parent {
height: 100vh;
border: 20px solid black;
display: flex;
background: pink;
}
.main {
display: flex;
backgound-color: green;
flex-direction: column;
flex: 2
}
.header {
background: cornflowerblue;
}
.side {
flex: 1;
background: rgb(219, 133, 133);
}
.content {
background: rgb(115, 202, 180);
flex: 1
}
.text {
display: flex;
flex-direction: row;
justify-content: center;
align-items: center;
height: 190px;
color: #fff;
}
</style>
<div class="parent">
<div class="side">
<h2 class="text">Side</h2>
</div>
<div class="main">
<div class="header">
<h2 class="text">Header</h2>
</div>
<div class="content">
<h2 class="text">Content</h2>
</div>
</div>
</div>
I have a list of messages (which is a flex child) in a container with unknown height and want to make them scrollable. But I cannot find a proper combination of flex-grow: 1, min-height: 0 and other flex tricks to make it working - message list is still bigger than its parent.
When I add overflow-y: auto to its parent - it works but this parent besides messages list includes some content which should not scroll.
Here's my example for this case: https://jsfiddle.net/ecbtrn58/2/
<div class="page">
<div class="messages-section">
<div class="header">Your messages</div>
<div class="content">
<img src="https://http.cat/100" width="70" height="50"/>
<div class="messages-list">
<div class="message">Hi.</div>
<div class="message">Hello.</div>
<div class="message">Good morning.</div>
<div class="message">Yo!</div>
</div>
</div>
</div>
</div>
.page {
background-color: #ddd;
width: 300px;
height: 300px;
.messages-section {
display: flex;
flex-direction: column;
width: 50%;
height: 100%;
background-color: #ccc;
.header {
background: #bbb;
padding: 5px;
}
.content {
display: flex;
flex-direction: column;
align-items: center;
padding: 5px;
.messages-list {
display: flex;
flex-direction: column;
overflow-y: auto;
/* What to add here to make it scrollable? */
.message {
height: 50px;
margin: 10px;
padding: 10px;
background: #1dc497;
}
}
}
}
}
How can I make messages list to scroll?
You have to set the height of .content to 100% and make it scrollable:
.page {
background-color: #ddd;
width: 300px;
height: 300px;
}
.page .messages-section {
display: flex;
flex-direction: column;
width: 50%;
height: 100%;
background-color: #ccc;
}
.page .messages-section .header {
background: #bbb;
padding: 5px;
}
.page .messages-section .content {
display: flex;
flex-direction: column;
align-items: center;
padding: 5px;
height: 100%;
overflow-x: scroll;
}
.page .messages-section .content .messages-list {
display: flex;
flex-direction: column;
overflow-y: auto;
/* What to add here to make it scrollable? */
}
.page .messages-section .content .messages-list .message {
height: 50px;
margin: 10px;
padding: 10px;
background: #1dc497;
}
<div class="page">
<div class="messages-section">
<div class="header">Your messages</div>
<div class="content">
<img src="https://http.cat/100" width="70" height="50" />
<div class="messages-list">
<div class="message">Hi.</div>
<div class="message">Hello.</div>
<div class="message">Good morning.</div>
<div class="message">Yo!</div>
</div>
</div>
</div>
</div>
I have a flex parent container set to flex-wrap: wrap and justify-content: flex-end. I have one flex-child, button-group which i want to align to flex-start, but align-self is not working.
.container{
width: 500px;
height: 500px;
background: pink;
display: flex;
justify-content: flex-end;
flex-wrap: wrap;
}
.button-group {
background: lightblue;
align-self: flex-start; /* Not working!*/
padding: 5px 10px;
}
.main-box {
width: 450px;
height: 300px;
background: lime;
}
.myfooter {
width: 50%;
height: 10%;
background-color: black;
}
<div class="container">
<div class="button-group">
<button></button>
<button></button>
<button></button>
</div>
<div class="main-box"></div>
<div class="myfooter"> </div>
</div>
How can I get the button group to align itself to the left while preserving the flex wrap?
Flexbox aligns items in 1D direction. Thats why the flex-start didnt work,
In oreder to make it work wrap it with a div and add appropriate styles as shown in the snippet
.container {
width: 500px;
height: 500px;
background: pink;
display: flex;
justify-content: flex-end;
flex-wrap: wrap;
}
.button-group {
background: lightblue;
padding: 5px 10px;
display: inline-block;
}
.main-box {
width: 450px;
height: 300px;
background: lime;
}
.myfooter {
width: 50%;
height: 10%;
background-color: black;
}
.holder{width: 100%;}
<div class="container">
<div class="holder">
<div class="button-group">
<button>1</button>
<button>2</button>
<button>3</button>
</div>
</div>
<div class="main-box"></div>
<div class="myfooter"> </div>
</div>
If you don't need margin to the right side of button-group, you can simply use .margin-right:auto; on .button-group to make it align itself to the left.
.container{
width: 500px;
height: 500px;
background: pink;
display: flex;
justify-content: flex-end;
flex-wrap: wrap;
}
.button-wrap {
display: flex;
flex-basis: 100%;
}
.button-group {
background: lightblue;
align-self: flex-start;
padding: 5px 10px;
margin-right: auto;
}
.main-box {
width: 450px;
height: 300px;
background: lime;
}
.myfooter {
width: 50%;
height: 10%;
background-color: black;
}
<div class="container">
<div class="button-wrap">
<div class="button-group">
<button></button>
<button></button>
<button></button>
</div>
</div>
<div class="main-box"></div>
<div class="myfooter"> </div>
</div>
Try this. You need to use a additional wrapper for button group and apply display: flex; flex-basis: 100%; for that wrapper.
https://jsfiddle.net/Sampath_Madhuranga/7ad2u804/
.container{
width: 500px;
height: 500px;
background: pink;
display: flex;
justify-content: flex-end;
flex-wrap: wrap;
}
.button-wrap {
display: flex;
flex-basis: 100%;
}
.button-group {
background: lightblue;
align-self: flex-start; /* Not working!*/
padding: 5px 10px;
}
.main-box {
width: 450px;
height: 300px;
background: lime;
}
.myfooter {
width: 50%;
height: 10%;
background-color: black;
}
<div class="container">
<div class="button-wrap">
<div class="button-group">
<button></button>
<button></button>
<button></button>
</div>
</div>
<div class="main-box"></div>
<div class="myfooter"> </div>
</div>
Here what happened, the flex-direction default is row, so your child elements is placed horizontally, but because the container width is static and it cant expand to wrap all of your child elements, it push them down, that cause your layout may look vertically but it is still placed horizontally. And in that case (flex-direction: row) the align-self property only set the flex items in the vertical direction.
How to fix it, first, you need to apply flex-direction: column. Then you can set the align-self for your flex-items and now and it will align self horizontally
.container{
width: 500px;
height: 500px;
background: pink;
flex-direction: column;
display: flex;
flex-wrap: wrap;
}
.button-group {
background: lightblue;
align-self: flex-start; /*working now!*/
padding: 5px 10px;
}
.main-box {
width: 450px;
height: 300px;
background: lime;
align-self: flex-end;
}
.myfooter {
width: 50%;
height: 10%;
background-color: black;
align-self: flex-end;
}
<div class="container">
<div class="button-group">
<button></button>
<button></button>
<button></button>
</div>
<div class="main-box"></div>
<div class="myfooter"> </div>
</div>
I need to place the orange button align at the bottom of the blue one.
With my current flex code I cannot get the result wanted. Any ideas how to fix it? Thanks
.content {
width: 350px;
height: 150px;
background-color: yellow;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.row1 {
background-color: red;
}
.row2 {
background-color: blue;
height: 100px
}
.icon {
width: 50px;
height: 50px;
background-color: orange;
align-items: center;
justify-content: center;
}
<div class="content">
<div class="row1">
</div>
<div class="row2">
<div class="icon">
</div>
</div>
</div>
You also need to set display: flex on row2 and then you can use align-self: flex-end on orange element.
.content {
width: 350px;
height: 150px;
background-color: yellow;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.row1 {
background-color: red;
}
.row2 {
background-color: blue;
display: flex;
height: 100px
}
.icon {
width: 50px;
height: 50px;
background-color: orange;
align-self: flex-end;
}
<div class="content">
<div class="row1"></div>
<div class="row2">
<div class="icon"></div>
</div>
</div>
Here we add this css in "row 2",
display: flex;
flex-direction: column;
its means if any object put at bottom then just parent block set above css and element we need to set as bottom add css "margin-top:auto;"
.content {
width: 350px;
height: 150px;
background-color: yellow;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.row1 {
background-color: red;
}
.row2 {
background-color: blue;
height: 100px;
display: flex;
flex-direction: column;
}
.icon {
width: 50px;
height: 50px;
background-color: orange;
align-items: center;
justify-content: center;
margin-top: auto;
}
<div class="content">
<div class="row1">
</div>
<div class="row2">
<div class="icon">
</div>
</div>
</div>
There is lot of stylings that can be removed from your code if it is not required, please find it here, I have changed .content and .row2 stylings
.content {
width: 350px;
height: 150px;
background-color: yellow;
display: flex;
}
.row1 {
background-color: red;
}
.row2 {
background-color: blue;
height: 100px;
flex: 1;
align-self: flex-end;
display: flex;
align-items: flex-end;
}
.icon {
width: 50px;
height: 50px;
background-color: orange;
align-items: center;
justify-content: center;
}
<div class="content">
<div class="row1">
</div>
<div class="row2">
<div class="icon">
</div>
</div>
</div>