|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
flex-wrap. Поддержка браузерами flexFlexbox - Learn web developmentA new technology, but with support now fairly widespread across browsers, Flexbox is starting to become ready for widespread use. Flexbox provides tools to allow rapid creation of complex, flexible layouts, and features that historically proved difficult with CSS. This article explains all the fundamentals. Why Flexbox?For a long time, the only reliable cross browser-compatible tools available for creating CSS layouts were things like floats and positioning. These are fine and they work, but in some ways they are also rather limiting and frustrating. The following simple layout requirements are either difficult or impossible to achieve with such tools, in any kind of convenient, flexible way:
As you'll see in subsequent sections, flexbox makes a lot of layout tasks much easier. Let's dig in! Introducing a simple exampleIn this article we are going to get you to work through a series of exercises to help you understand how flexbox works. To get started, you should make a local copy of the first starter file — flexbox0.html from our github repo — load it in a modern browser (like Firefox or Chrome), and have a look at the code in your code editor. You can see it live here also. You'll see that we have a <header> element with a top level heading inside it, and a <section> element containing three <article>s. We are going to use these to create a fairly standard three column layout. Specifying what elements to lay out as flexible boxesTo start with, we need to select which elements are to be laid out as flexible boxes. To do this, we set a special value of display on the parent element of the elements you want to affect. In this case we want to lay out the <article> elements, so we set this on the <section> (which becomes a flex container): The result of this should be something like so: So, this single declaration gives us everything we need — incredible, right? We have our multiple column layout with equal sized columns, and the columns are all the same height. This is because the default values given to flex items (the children of the flex container) are set up to solve common problems such as this. More on those later. Note: You can also set a display value of inline-flex if you wish to lay out inline items as flexible boxes. An aside on the flex modelWhen elements are laid out as flexible boxes, they are laid out along two axes:
Bear this terminology in mind as you go through subsequent sections. You can always refer back to it if you get confused about any of the terms being used. Columns or rows?Flexbox provides a property called flex-direction that specifies what direction the main axis runs in (what direction the flexbox children are laid out in) — by default this is set to row, which causes them to be laid out in a row in the direction your browser's default language works in (left to right, in the case of an English browser). Try adding the following declaration to your section rule: flex-direction: column;You'll see that this puts the items back in a column layout, much like they were before we added any CSS. Before you move on, delete this declaration from your example. Note: You can also lay out flex items in a reverse direction using the row-reverse and column-reverse values. Experiment with these values too! WrappingOne issue that arises when you have a fixed amount of width or height in your layout is that eventually your flexbox children will overflow their container, breaking the layout. Have a look at our flexbox-wrap0.html example, and try viewing it live (take a local copy of this file now if you want to follow along with this example): Here we see that the children are indeed breaking out of their container. One way in which you can fix this is to add the following declaration to your section rule: flex-wrap: wrap;Try this now; you'll see that the layout looks much better with this included: We now have multiple rows — as many flexbox children are fitted onto each row as makes sense, and any overflow is moved down to the next line. The flex: 200px declaration set on the articles means that each will be at least 200px wide; we'll discuss this property in more detail later on. You might also notice that the last few children on the last row are each made wider so that the entire row is still filled. But there's more we can do here. First of all, try changing your flex-direction property value to row-reverse — now you'll see that you still have your multiple row layout, but it is started from the opposite corner of the browser window and now flows in reverse. flex-flow shorthandAt this point it is worth noting that a shorthand exists for flex-direction and flex-wrap — flex-flow. So for example, you can replace flex-direction: row; flex-wrap: wrap;with flex-flow: row wrap;Flexible sizing of flex itemsLet's now return to our first example, and look at how we can control what proportion of space flex items take up. Fire up your local copy of flexbox0.html, or take a copy of flexbox1.html as a new starting point (see it live). First, add the following rule to the bottom of your CSS: This is a unitless proportion value that dictates how much of the available space along the main axis each flex item will take up. In this case, we are giving each <article> element a value of 1, which means they will all take up an equal amount of the spare space left after things like padding and margin have been set. It is a proportion, meaning that giving each flex item a value of 400000 would have exactly the same effect. Now add the following rule below the previous one: article:nth-of-type(3) { flex: 2; }Now when you refresh, you'll see that the third <article> takes up twice as much of the available width as the other two — there are now four proportion units available in total. The first two flex items have one each so they take 1/4 of the available space each. The third one has two units, so it takes up 2/4 of the available space (or 1/2). You can also specify a minimum size value inside the flex value. Try updating your existing article rules like so: article { flex: 1 200px; } article:nth-of-type(3) { flex: 2 200px; }This basically states "Each flex item will first be given 200px of the available space. After that, the rest of the available space will be shared out according to the proportion units." Try refreshing and you'll see a difference in how the space is shared out. The real value of flexbox can be seen in its flexibility/responsiveness — if you resize the browser window, or add another <article> element, the layout continues to work just fine. flex: shorthand versus longhandflex is a shorthand property that can specify up to three different values:
We'd advise against using the longhand flex properties unless you really have to (for example, to override something previously set). They lead to a lot of extra code being written, and they can be somewhat confusing. Horizontal and vertical alignmentYou can also use flexbox features to align flex items along the main or cross axes. Let's explore this by looking at a new example — flex-align0.html (see it live also) — which we are going to turn into a neat, flexible button/toolbar. At the moment you'll see a horizontal menu bar, with some buttons jammed into the top left hand corner. First, take a local copy of this example. Now, add the following to the bottom of the example's CSS: div { display: flex; align-items: center; justify-content: space-around; }Refresh the page and you'll see that the buttons are now nicely centered, horizontally and vertically. We've done this via two new properties. align-items controls where the flex items sit on the cross axis.
You can override the align-items behavior for individual flex items by applying the align-self property to them. For example, try adding the following to your CSS: button:first-child { align-self: flex-end; }Have a look at what effect this has, and remove it again when you've finished. justify-content controls where the flex items sit on the main axis.
We'd like to encourage you to play with these values to see how they work before you continue. Ordering flex itemsFlexbox also has a feature for changing the layout order of flex items, without affecting the source order. This is another thing that is impossible to do with traditional layout methods. The code for this is simple: try adding the following CSS to your button bar example code: button:first-child { order: 1; }Refresh, and you'll now see that the "Smile" button has moved to the end of the main axis. Let's talk about how this works in a bit more detail:
You can set negative order values to make items appear earlier than items with 0 set. For example, you could make the "Blush" button appear at the start of the main axis using the following rule: button:last-child { order: -1; }Nested flex boxesIt is possible to create some pretty complex layouts with flexbox. It is perfectly ok to set a flex item to also be a flex container, so that its children are also laid out like flexible boxes. Have a look at complex-flexbox.html (see it live also). The HTML for this is fairly simple. We've got a <section> element containing three <article>s. The third <article> contains three <div>s. : section - article article article - div - button div button div button button buttonLet's look at the code we've used for the layout. First of all, we set the children of the <section> to be laid out as flexible boxes. section { display: flex; }Next, we set some flex values on the <article>s themselves. Take special note of the 2nd rule here — we are setting the third <article> to have its children laid out like flex items too, but this time we are laying them out like a column. article { flex: 1 200px; } article:nth-of-type(3) { flex: 3 200px; display: flex; flex-flow: column; }Next, we select the first <div>. We first use flex:1 100px; to effectively give it a minimum height of 100px, then we set its children (the <button> elements) to also be laid out like flex items. Here we lay them out in a wrapping row, and align them in the center of the available space like we did in the individual button example we saw earlier. article:nth-of-type(3) div:first-child { flex:1 100px; display: flex; flex-flow: row wrap; align-items: center; justify-content: space-around; }Finally, we set some sizing on the button, but more interestingly we give it a flex value of 1. This has a very interesting effect, which you'll see if you try resizing your browser window width. The buttons will take up as much space as they can and sit as many on the same line as they can, but when they can no longer fit comfortably on the same line, they'll drop down to create new lines. button { flex: 1; margin: 5px; font-size: 18px; line-height: 1.5; }Cross browser compatibilityFlexbox support is available in most new browsers — Firefox, Chrome, Opera, Microsoft Edge and IE 11, newer versions of Android/iOS, etc. However you should be aware that there are still older browsers in use that don't support Flexbox (or do, but support a really old, out-of-date version of it.) While you are just learning and experimenting, this doesn't matter too much; however if you are considering using flexbox in a real website you need to do testing and make sure that your user experience is still acceptable in as many browsers as possible. Flexbox is a bit trickier than some CSS features. For example, if a browser is missing a CSS drop shadow, then the site will likely still be usable. Not supporting flexbox features however will probably break a layout completely, making it unusable. We'll discuss strategies for overcoming tricky cross browser support issues in a future module. SummaryThat concludes our tour of the basics of flexbox. We hope you had fun, and will have a good play around with it as you travel forward with your learning. Next we'll have a look at another important aspect of CSS layouts — grid systems. developer.mozilla.org Flexbox - Learn web developmentA new technology, but with support now fairly widespread across browsers, Flexbox is starting to become ready for widespread use. Flexbox provides tools to allow rapid creation of complex, flexible layouts, and features that historically proved difficult with CSS. This article explains all the fundamentals. Why Flexbox?For a long time, the only reliable cross browser-compatible tools available for creating CSS layouts were things like floats and positioning. These are fine and they work, but in some ways they are also rather limiting and frustrating. The following simple layout requirements are either difficult or impossible to achieve with such tools, in any kind of convenient, flexible way:
As you'll see in subsequent sections, flexbox makes a lot of layout tasks much easier. Let's dig in! Introducing a simple exampleIn this article we are going to get you to work through a series of exercises to help you understand how flexbox works. To get started, you should make a local copy of the first starter file — flexbox0.html from our github repo — load it in a modern browser (like Firefox or Chrome), and have a look at the code in your code editor. You can see it live here also. You'll see that we have a <header> element with a top level heading inside it, and a <section> element containing three <article>s. We are going to use these to create a fairly standard three column layout. Specifying what elements to lay out as flexible boxesTo start with, we need to select which elements are to be laid out as flexible boxes. To do this, we set a special value of display on the parent element of the elements you want to affect. In this case we want to lay out the <article> elements, so we set this on the <section> (which becomes a flex container): section { display: flex; }The result of this should be something like so: So, this single declaration gives us everything we need — incredible, right? We have our multiple column layout with equal sized columns, and the columns are all the same height. This is because the default values given to flex items (the children of the flex container) are set up to solve common problems such as this. More on those later. Note: You can also set a display value of inline-flex if you wish to lay out inline items as flexible boxes. An aside on the flex modelWhen elements are laid out as flexible boxes, they are laid out along two axes:
Bear this terminology in mind as you go through subsequent sections. You can always refer back to it if you get confused about any of the terms being used. Columns or rows?Flexbox provides a property called flex-direction that specifies what direction the main axis runs in (what direction the flexbox children are laid out in) — by default this is set to row, which causes them to be laid out in a row in the direction your browser's default language works in (left to right, in the case of an English browser). Try adding the following declaration to your section rule: flex-direction: column;You'll see that this puts the items back in a column layout, much like they were before we added any CSS. Before you move on, delete this declaration from your example. Note: You can also lay out flex items in a reverse direction using the row-reverse and column-reverse values. Experiment with these values too! WrappingOne issue that arises when you have a fixed amount of width or height in your layout is that eventually your flexbox children will overflow their container, breaking the layout. Have a look at our flexbox-wrap0.html example, and try viewing it live (take a local copy of this file now if you want to follow along with this example): Here we see that the children are indeed breaking out of their container. One way in which you can fix this is to add the following declaration to your section rule: flex-wrap: wrap;Try this now; you'll see that the layout looks much better with this included: We now have multiple rows — as many flexbox children are fitted onto each row as makes sense, and any overflow is moved down to the next line. The flex: 200px declaration set on the articles means that each will be at least 200px wide; we'll discuss this property in more detail later on. You might also notice that the last few children on the last row are each made wider so that the entire row is still filled. But there's more we can do here. First of all, try changing your flex-direction property value to row-reverse — now you'll see that you still have your multiple row layout, but it is started from the opposite corner of the browser window and now flows in reverse. flex-flow shorthandAt this point it is worth noting that a shorthand exists for flex-direction and flex-wrap — flex-flow. So for example, you can replace flex-direction: row; flex-wrap: wrap;with flex-flow: row wrap;Flexible sizing of flex itemsLet's now return to our first example, and look at how we can control what proportion of space flex items take up. Fire up your local copy of flexbox0.html, or take a copy of flexbox1.html as a new starting point (see it live). First, add the following rule to the bottom of your CSS: article { flex: 1; }This is a unitless proportion value that dictates how much of the available space along the main axis each flex item will take up. In this case, we are giving each <article> element a value of 1, which means they will all take up an equal amount of the spare space left after things like padding and margin have been set. It is a proportion, meaning that giving each flex item a value of 400000 would have exactly the same effect. Now add the following rule below the previous one: article:nth-of-type(3) { flex: 2; }Now when you refresh, you'll see that the third <article> takes up twice as much of the available width as the other two — there are now four proportion units available in total. The first two flex items have one each so they take 1/4 of the available space each. The third one has two units, so it takes up 2/4 of the available space (or 1/2). You can also specify a minimum size value inside the flex value. Try updating your existing article rules like so: article { flex: 1 200px; } article:nth-of-type(3) { flex: 2 200px; }This basically states "Each flex item will first be given 200px of the available space. After that, the rest of the available space will be shared out according to the proportion units." Try refreshing and you'll see a difference in how the space is shared out. The real value of flexbox can be seen in its flexibility/responsiveness — if you resize the browser window, or add another <article> element, the layout continues to work just fine. flex: shorthand versus longhandflex is a shorthand property that can specify up to three different values:
We'd advise against using the longhand flex properties unless you really have to (for example, to override something previously set). They lead to a lot of extra code being written, and they can be somewhat confusing. Horizontal and vertical alignmentYou can also use flexbox features to align flex items along the main or cross axes. Let's explore this by looking at a new example — flex-align0.html (see it live also) — which we are going to turn into a neat, flexible button/toolbar. At the moment you'll see a horizontal menu bar, with some buttons jammed into the top left hand corner. First, take a local copy of this example. Now, add the following to the bottom of the example's CSS: div { display: flex; align-items: center; justify-content: space-around; }Refresh the page and you'll see that the buttons are now nicely centered, horizontally and vertically. We've done this via two new properties. align-items controls where the flex items sit on the cross axis.
You can override the align-items behavior for individual flex items by applying the align-self property to them. For example, try adding the following to your CSS: button:first-child { align-self: flex-end; }Have a look at what effect this has, and remove it again when you've finished. justify-content controls where the flex items sit on the main axis.
We'd like to encourage you to play with these values to see how they work before you continue. Ordering flex itemsFlexbox also has a feature for changing the layout order of flex items, without affecting the source order. This is another thing that is impossible to do with traditional layout methods. The code for this is simple: try adding the following CSS to your button bar example code: button:first-child { order: 1; }Refresh, and you'll now see that the "Smile" button has moved to the end of the main axis. Let's talk about how this works in a bit more detail:
You can set negative order values to make items appear earlier than items with 0 set. For example, you could make the "Blush" button appear at the start of the main axis using the following rule: button:last-child { order: -1; }Nested flex boxesIt is possible to create some pretty complex layouts with flexbox. It is perfectly ok to set a flex item to also be a flex container, so that its children are also laid out like flexible boxes. Have a look at complex-flexbox.html (see it live also). The HTML for this is fairly simple. We've got a <section> element containing three <article>s. The third <article> contains three <div>s. : section - article article article - div - button div button div button button buttonLet's look at the code we've used for the layout. First of all, we set the children of the <section> to be laid out as flexible boxes. section { display: flex; }Next, we set some flex values on the <article>s themselves. Take special note of the 2nd rule here — we are setting the third <article> to have its children laid out like flex items too, but this time we are laying them out like a column. article { flex: 1 200px; } article:nth-of-type(3) { flex: 3 200px; display: flex; flex-flow: column; }Next, we select the first <div>. We first use flex:1 100px; to effectively give it a minimum height of 100px, then we set its children (the <button> elements) to also be laid out like flex items. Here we lay them out in a wrapping row, and align them in the center of the available space like we did in the individual button example we saw earlier. article:nth-of-type(3) div:first-child { flex:1 100px; display: flex; flex-flow: row wrap; align-items: center; justify-content: space-around; }Finally, we set some sizing on the button, but more interestingly we give it a flex value of 1. This has a very interesting effect, which you'll see if you try resizing your browser window width. The buttons will take up as much space as they can and sit as many on the same line as they can, but when they can no longer fit comfortably on the same line, they'll drop down to create new lines. button { flex: 1; margin: 5px; font-size: 18px; line-height: 1.5; }Cross browser compatibilityFlexbox support is available in most new browsers — Firefox, Chrome, Opera, Microsoft Edge and IE 11, newer versions of Android/iOS, etc. However you should be aware that there are still older browsers in use that don't support Flexbox (or do, but support a really old, out-of-date version of it.) While you are just learning and experimenting, this doesn't matter too much; however if you are considering using flexbox in a real website you need to do testing and make sure that your user experience is still acceptable in as many browsers as possible. Flexbox is a bit trickier than some CSS features. For example, if a browser is missing a CSS drop shadow, then the site will likely still be usable. Not supporting flexbox features however will probably break a layout completely, making it unusable. We'll discuss strategies for overcoming tricky cross browser support issues in a future module. SummaryThat concludes our tour of the basics of flexbox. We hope you had fun, and will have a good play around with it as you travel forward with your learning. Next we'll have a look at another important aspect of CSS layouts — grid systems. developer.mozilla.org flex-wrap - CSS: Cascading Style SheetsThe CSS flex-wrap property specifies whether flex items are forced into a single line or can be wrapped onto multiple lines. If wrapping is allowed, this property also enables you to control the direction in which lines are stacked. The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request. See Using CSS flexible boxes for more properties and information. Syntaxflex-wrap: nowrap; /* Default value */ flex-wrap: wrap; flex-wrap: wrap-reverse; /* Global values */ flex-wrap: inherit; flex-wrap: initial; flex-wrap: unset;The flex-wrap property is specified as a single keyword chosen from the list of values below. ValuesThe following values are accepted: nowrap The flex items are laid out in a single line which may cause the flex container to overflow. The cross-start is either equivalent to start or before depending flex-direction value. This is the default value. wrap The flex items break into multiple lines. The cross-start is either equivalent to start or before depending flex-direction value and the cross-end is the opposite of the specified cross-start. wrap-reverse Behaves the same as wrap but cross-start and cross-end are permuted.Formal syntaxnowrap | wrap | wrap-reverseExamplesHTML<h5>This is an example for flex-wrap:wrap </h5> <div> <div>1</div> <div>2</div> <div>3</div> </div> <h5>This is an example for flex-wrap:nowrap </h5> <div> <div>1</div> <div>2</div> <div>3</div> </div> <h5>This is an example for flex-wrap:wrap-reverse </h5> <div> <div>1</div> <div>2</div> <div>3</div> </div>CSS/* Common Styles */ .content, .content1, .content2 { color: #fff; font: 100 24px/100px sans-serif; height: 150px; text-align: center; } .content div, .content1 div, .content2 div { height: 50%; width: 50%; } .red { background: orangered; } .green { background: yellowgreen; } .blue { background: steelblue; } /* Flexbox Styles */ .content { display: flex; flex-wrap: wrap; } .content1 { display: flex; flex-wrap: nowrap; } .content2 { display: flex; flex-wrap: wrap-reverse; }ResultsSpecifications
Browser compatibility
1. Partial support due to large number of bugs present. See Flexbugs.
LegendFull support Full support Partial support Partial support Compatibility unknown Compatibility unknownSee implementation notes.See implementation notes.See alsoDocument Tags and ContributorsContributors to this page: chrisdavidmills, jmmarco, wbamberg, rachelandrew, fscholz, Jeremie, andavies, stephaniehobson, erikadoyle, Sebastianz, haoyuchen1992, so_matt_basta, duncanmcdonald, jigs12, Sheppy, tregusti, teoli, ziyunfei, yisi, SJW, kscarfone, ethertank, FredB, scottrowedeveloper.mozilla.org flex-direction - CSS: Cascading Style SheetsThe flex-direction CSS property specifies how flex items are placed in the flex container defining the main axis and the direction (normal or reversed). The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request. Note that the values row and row-reverse are affected by the directionality of the flex container. If its dir attribute is ltr, row represents the horizontal axis oriented from the left to the right, and row-reverse from the right to the left; if the dir attribute is rtl, row represents the axis oriented from the right to the left, and row-reverse from the left to the right. Syntax/* The direction text is laid out in a line */ flex-direction: row; /* Like <row>, but reversed */ flex-direction: row-reverse; /* The direction in which lines of text are stacked */ flex-direction: column; /* Like <column>, but reversed */ flex-direction: column-reverse; /* Global values */ flex-direction: inherit; flex-direction: initial; flex-direction: unset;ValuesThe following values are accepted: row The flex container's main-axis is defined to be the same as the text direction. The main-start and main-end points are the same as the content direction. row-reverse Behaves the same as row but the main-start and main-end points are permuted. column The flex container's main-axis is the same as the block-axis. The main-start and main-end points are the same as the before and after points of the writing-mode. column-reverse Behaves the same as column but the main-start and main-end are permuted.Formal syntaxrow | row-reverse | column | column-reverseExampleHTML<h5>This is a Column-Reverse</h5> <div> <div>A</div> <div>B</div> <div>C</div> </div> <h5>This is a Row-Reverse</h5> <div> <div>A</div> <div>B</div> <div>C</div> </div>CSS#content { width: 200px; height: 200px; border: 1px solid #c3c3c3; display: -webkit-flex; -webkit-flex-direction: column-reverse; display: flex; flex-direction: column-reverse; } .box { width: 50px; height: 50px; } #content1 { width: 200px; height: 200px; border: 1px solid #c3c3c3; display: -webkit-flex; -webkit-flex-direction: row-reverse; display: flex; flex-direction: row-reverse; } .box1 { width: 50px; height: 50px; }ResultSpecifications
Browser compatibility
1. Since Firefox 28, multi-line flexbox is supported. 2. From version 18 until version 20 (exclusive): this feature is behind the layout.css.flexbox.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config. 3. From version 44: this feature is behind the layout.css.prefixes.webkit preference (needs to be set to true). To change preferences in Firefox, visit about:config.
LegendFull support Full support Compatibility unknown Compatibility unknownSee implementation notes.See implementation notes.User must explicitly enable this feature.User must explicitly enable this feature.Requires a vendor prefix or different name for use.Requires a vendor prefix or different name for use.See alsodeveloper.mozilla.org |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|