Make elements take up space left in the container element
Posted By : Arun Kumar | 28-Dec-2014
Assigning space amongst child elements was really difficult before flex-box layout.Now its as easy as pie.
Lets start by creating our html markup
<div class="parent"> <div class="child"> abc </div> <div class="child"> abcdefghijklmno </div> <div class="child"> abcdefghijklmnopqrstu </div> </div>
and the css :
.parent{ width:400px; height:100px; background-color:yellow; display:flex; flex-direction:row; padding:10px; justify-content:space-around; } .child { background-color:red; border:1px solid blue; }
The output:
This is being done by the flex-box layout.
First we set display:flex to the parent container and justify-content:space-around.The property flex-direction:row is not really required here because the "row" is the default value for flex-direction property.
Lets try changing justify-content to space-between.
With justify-content to space-between the first child is at the start of the parent container and last child is at the end of the container.
If we wanted to give equal width to the child elements then we would have set flex:1 to the child element.
Thanks
More From Oodles
Ready to innovate? Let's get in touch
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Arun Kumar
Arun is a creative UI Developer