FLEX PROPERTIESCSS flex propertyThe flex property in CSS is shorthand for flex-grow, flex-shrink, and flex-basis. It only works on the flex-items, so if the container's item is not a flex-item, the flex property will not affect the corresponding item.This property is used to set the length of flexible items. The positioning of child elements and the main container is easy with this CSS property. It is used to set how a flex-item will shrink or grow to fit in the space. The flex property can be specified by one, two, or three values. Syntax : flex: flex-grow flex-shrink flex-basis | auto | none | initial | inherit;Property Values : Eg: < !DOCTYPE html> < html> < head> < title>CSS flex Property< /title> < style> .container { width: 200px; height: 100px; border: 1px solid black; display: flex; margin: 15px; background-color: blue; } .flex-item{ flex: 1; // unitless number: flex-grow } .flex-item1{ flex: 0 50px; // flex-grow, flex-basis } .flex-item2{ flex: 2 2; // flex-grow, flex-shrink } .flex-item, .flex-item1, .flex-item2 { background-color: lightblue; margin: 4px; } < /style> < /head> < body> < h1> CSS flex Property < /h1> < h3> flex: 1; < /h3> < div class = "container"> < div class = "flex-item"> < /div> < div class = "flex-item"> < /div> < div class = "flex-item"> < /div> < /div> < h3> flex: 0 50px; < /h3> < div class = "container"> < div class = "flex-item1"> < /div> < div class = "flex-item1"> < /div> < div class = "flex-item1"> < /div> < /div> < h3> flex: 2 2; < /h3> < div class = "container"> < div class = "flex-item2"> < /div> < div class = "flex-item2"> < /div> < div class = "flex-item2"> < /div> < /div> < /body> < /html>Output : CSS flex Propertyflex: 1;flex: 0 50px;flex: 2 2;CSS justify-contentThis CSS property is used to align the items of the flexible box container when the items do not use all available space on the main-axis (horizontally). It defines how the browser distributes the space around and between the content items. This CSS property can't be used to describe containers or items along the vertical axis. To align the items vertically, we have to use the align-items property. justify-content: center | flex-start | flex-end | space-around | space-evenly | space-between | initial | inherit;The default value of this property is flex-start. Let's understand its property values in detail. Property values : < !DOCTYPE html> < html> < head> < title>CSS filter property< /title> < style> #flexstart{ display:flex; justify-content: flex-start; } #flexend{ display:flex; justify-content: flex-end; } #cent{ display:flex; justify-content: center; } #evenly{ display:flex; justify-content: space-evenly; } #around{ display:flex; justify-content: space-around; } #between{ display:flex; justify-content: space-between; } .flex-item{ width:30px; height:30px; margin:3px; padding:3px; color:white; font-size:1em; font-weight:bold; text-align:center; border: 1px solid black; background-color:blue; } < /style> < /head> < body> < div id="flexstart"> < h1>flex-start< /h1> < div class="flex-item">1< /div> < div class="flex-item">2< /div> < div class="flex-item">3< /div> < div class="flex-item">4< /div> < div class="flex-item">5< /div> < /div> < div id="flexend"> < h1>flex-end< /h1> < div class="flex-item">1< /div> < div class="flex-item">2< /div> < div class="flex-item">3< /div> < div class="flex-item">4< /div> < div class="flex-item">5< /div> < /div> < div id="cent"> < h1>center< /h1> < div class="flex-item">1< /div> < div class="flex-item">2< /div> < div class="flex-item">3< /div> < div class="flex-item">4< /div> < div class="flex-item">5< /div> < /div> < h1>space-evenly< /h1> < div id="evenly"> < div class="flex-item">1< /div> < div class="flex-item">2< /div> < div class="flex-item">3< /div> < div class="flex-item">4< /div> < div class="flex-item">5< /div> < /div> < h1>space-around< /h1> < div id="around"> < div class="flex-item">1< /div> < div class="flex-item">2< /div> < div class="flex-item">3< /div> < div class="flex-item">4< /div> < div class="flex-item">5< /div> < /div> < h1>space-between< /h1> < div id="between"> < div class="flex-item">1< /div> < div class="flex-item">2< /div> < div class="flex-item">3< /div> < div class="flex-item">4< /div> < div class="flex-item">5< /div> < /div> < /body> < /html>Output : flex-start1
2
3
4
5 flex-end1
2
3
4
5
center1
2
3
4
5
space-evenly1
2
3
4
5
space-around1
2
3
4
5
space-between1
2
3
4
5
CSS flex-basis property : This CSS property specifies the initial size of the flex item. It only works on the flex-items, so if the container's item is not a flex-item, the flex-basis property will not affect the corresponding item. Generally, this CSS property is used with the other flex properties that are flex-shrink and flex-grow and usually defined by the flex shorthand to ensure that all values are set. Syntax : flex-basis: auto | width | initial | inherit;Values : < !DOCTYPE html> < html> < head> < style> .container { display: flex; background-color: lightblue; } .flex-item { background-color: white; text-align: center; line-height: 40px; font-size: 25px; margin: 5px; } < /style> < /head> < body> < h1> Example of the flex-basis property < /h1> < div class = "container"> < div class = "flex-item" style = "flex-basis: auto;"> auto < /div> < div class = "flex-item" style = "flex-basis: initial;"> initial < /div> < div class = "flex-item" style = "flex-basis: inherit;"> inherit < /div> < div class = "flex-item" style = "flex-basis: 150px;"> 150px < /div> < div class = "flex-item" style = "flex-basis: auto"> auto < /div> < /div> < /body> < /html>Output : Example of the flex-basis property auto
initial
inherit
100px
auto
Example for flex-basis property : < !DOCTYPE html> < html> < head> < style> .container { height: 50px; border: 2px solid red; display: flex; background-color: blue; } .container div{ padding-top: 25px; flex-basis: 100px; } .flex-item{ text-align: center; font-size: 25px; } .container div:nth-of-type(1) { flex-basis: 50%; } .container div:nth-of-type(3) { flex-basis: 200px; } .container div:nth-of-type(5) { flex-basis: 7em; } < /style> < /head> < body> < h1>The flex-basis Property< /h1> < div class = "container"> < div class = "flex-item" style= "background-color: lightblue;">50%< /div> < div class = "flex-item" style= "background-color: yellow;">100px< /div> < div class = "flex-item" style= "background-color: pink;">200px< /div> < div class = "flex-item" style= "background-color: orange;">100px< /div> < div class = "flex-item" style= "background-color: lightgreen;">7em< /div> < /div> < /body> < /html>Output : < h1>The flex-basis Property< /h1> 50%
100px
200px
100px
7em
Example for flex-shrink property : < !DOCTYPE html> < html> < head> < style> .container { height: 50px; border: 2px solid red; display: flex; } .container div{ padding-top: 10px; flex: 0 0 100px; } .flex-item{ text-align: center; font-size: 25px; } < /style> < /head> < body> < h1>The flex-basis Property< /h1> < div class = "container"> < div class = "flex-item" style= "background-color: lightblue;">1< /div> < div class = "flex-item" style= "background-color: yellow;">2< /div> < div class = "flex-item" style= "background-color: pink;">3< /div> < div class = "flex-item" style= "background-color: orange;">4< /div> < div class = "flex-item" style= "background-color: lightgreen;">5< /div> < /div> < /body> < /html>Output : The flex-basis Property1
2
3
4
5 Example for flex-grow property : < !DOCTYPE html> < html> < head> < style> .container { display: -webkit-flex; display: flex; background-color: lightblue; } .flex-item { background-color: lightgreen; text-align: center; font-size: 25px; width: 100px; height: 100px; padding-top: 20px; margin: 5px; } < /style> < /head> < body> < h1> flex-grow: 0; < /h1> < div class="container"> < div class="flex-item" style = "flex-grow: 0;"> flex-item 1 < /div> < div class="flex-item" style = "flex-grow: 0;"> flex-item 2 < /div> < div class="flex-item" style = "flex-grow: 0;"> flex-item 3 < /div> < /div> < h1> flex-grow: 1; < /h1> < div class="container"> < div class="flex-item" style = "flex-grow: 1;"> flex-item 1 < /div> < div class="flex-item" style = "flex-grow: 1;"> flex-item 2 < /div> < div class="flex-item" style = "flex-grow: 1;"> flex-item 3 < /div> < /div> < /body> < /html>Output : flex-grow: 0; flex-item 1
flex-item 2
flex-item 3
flex-grow: 1; flex-item 1
flex-item 2
flex-item 3
Example for flex-shrink property : < !DOCTYPE html> < html> < head> < style> .container { width: 400px; height: 100px; border: 5px solid red; display: flex; background-color: blue; margin: 30px; } .flex-item{ background-color: lightblue; font-size: 25px; margin: 5px; flex-grow: 1; flex-shrink: 1; flex-basis: 100px; } < /style> < /head> < body> < h1> Example of the flex-shrink property < /h1> < div class="container"> < div class = "flex-item">< /div> < div class = "flex-item">< /div> < div class = "flex-item" style = "flex-shrink: 5;"> 5 < /div> < div class = "flex-item" style = "flex-shrink: initial;"> initial < /div> < div class = "flex-item" style = "flex-shrink: inherit;"> inherit < /div> < /div> < div class="container"> < div class = "flex-item">< /div> < div class = "flex-item" style = "flex-shrink: 8;"> 8 < /div> < div class = "flex-item" style = "flex-shrink: 10;"> 10 < /div> < div class = "flex-item" style = "flex-shrink: 6;"> 6 < /div> < div class = "flex-item">< /div> < /div> < /body> < /html>Output : Example of the flex-shrink property 5
initial
inherit
8
10
6
« Previous Next Topic » (SQL - Constraints) |