Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

css - The meaning and benefits of flex: 1

I stumbled upon flex: 1 today in some code and googled it to get some information about what it does. w3schools succinctly states:

Let all the flexible items be the same length, regardless of its content.

But as I've seen it used for other cases, e.g. purposely pushing down sibling element to bottom, does anyone have the knowledge to list useful cases for flex: 1 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The CSS border, padding and margin properties are all shorthand properties. This means they consolidate multiple properties into one.

The CSS flex property is no different. It's simply a shorthand way to consolidate:

  • flex-grow
  • flex-shrink
  • flex-basis

So, use the flex property for the same reason you would use any other CSS shorthand property:

  • to minimize your code
  • reset/change default values

In terms of function, there's nothing unique about the flex property. Anything that the flex property can do, can also be done using a combination of the longhand properties.

For example:

flex: 2 1 250px

is exactly the same as:

  • flex-grow: 2
  • flex-shrink: 1
  • flex-basis: 250px

The flexbox spec then takes "shorthanding" a step further, defining an even shorter shorthand:

flex: <positive-number>

Equivalent to flex: <positive-number> 1 0.

Makes the flex item flexible and sets the flex basis to zero.

http://www.w3.org/TR/css-flexbox-1/#flex-common

Here's a list of commonly used flex shorthand rules:

The spec also makes this recommendation:

Authors are encouraged to control flexibility using the flex shorthand rather than with its longhand properties directly, as the shorthand correctly resets any unspecified components to accommodate common uses.

http://www.w3.org/TR/css-flexbox-1/#flex-components

Here's a related post with more info:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...