CSS 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.
SyntaxProperty Valuesflex-grow: It is a positive unitless number that determines the flex-grow factor. It specifies how much the item will grow compared to the other flexible-items. Negative values are not allowed. If it is omitted, then it sets to the value 1. flex-shrink: It is the positive unitless number that determines the flex shrink factor. It specifies how much the item will shrink compared to the other flexible-items. Negative values are not allowed. If it is omitted, then it sets to the value 1. flex-basis: It is the length in relative or absolute units that defines the initial length of the flex-item. It is used to set the length of the flex-item. Its values can be auto, inherit, or a number followed by the length units such as em, px, etc. Negative values are not allowed. If it is omitted, then it sets to the value 0. auto: This value of the flex property is equivalent to 1 1 auto. none: This value of the flex property is equivalent to 0 0 auto. It neither grows nor shrinks the flex-items. initial: It sets the property to its default value. It is equivalent to 0 0 auto. inherit: It inherits the property from its parent element. ExampleIn this example, there are three containers, each having three flex-items. The width and height of the containers are 300px and 100px. We are applying the flex: 1; to the flex-items of the first container, flex: 0 50px; to the flex-items of the second container, and flex: 2 2; to the flex-items of the third container. Test it NowOutput ExampleIn this example, there are two containers, each having three flex-items. The width and height of the containers are 200px and 100px. We are applying the flex: auto; to the flex-items of the first container, flex: 0 1 30px; to the flex-items of the second container. Test it NowOutput Next TopicCSS Media Queries |