markdown Flexbox的
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown Flexbox的相关的知识,希望对你有一定的参考价值。
**Create a Flex Container**
To define and access a container as a flex container you can use display: flex;.
If no additional rules are set, all direct children will be considered flex items
and will be laid horizontally, from left to right.
The width of flex items automatically adjusts to fit inside the container.
HTML
```html
<div class="flexcontainer">
<div class="flexitem1" style="background-color: lightblue;"> test1 </div>
<div class = flexitem2 style=background-color:lightgreen;> test2 </div>
<div class = flexitem3 style=background-color:lightslategray;"> test3 </div>
</div>
```
CSS
```css
.flexcontainer {
display: flex;
}
```
![Flex Container](https://lh4.googleusercontent.com/eejXc1oWSG0SAk3V2YxFxEdiSTjMqqiQ0mOk6JRy_2ZsSswfHs81lYhlwNgxJ9Bf19oTpXiIxwOxaWPIqPne=w1920-h938)
---
**Defining Flex Direction**
To put all flex items in one column, add flex-direction: column; to your CSS.
```css
.flexcontainer {
display: flex;
flex-direction: column;
}
```
![Flex Direction](https://lh4.googleusercontent.com/ztXquxCyZKLpYakkYnoCwnrObuTFsn2EFP1AiWwcooiYTlB3JBjN_-7oFwUJekn74LtvsWlem2dUQdLeDfvV=w1920-h938)
To reverse the column order of flex items, add `flex-direction: column-reverse;` to your CSS.
```css
.flexcontainer {
display: flex;
flex-direction: column-reverse;
}
```
![Flex Direction Row Reverse](https://lh6.googleusercontent.com/ycQSIbjo6dN_3ZDsXpz7582ga-I0u_NGTCAXyW23QvmFKDKktAYFgmf5qGKSwRU_1b9FohFLDXvdKFfdtf60=w1920-h938)
To reverse the row order of flex items, add `flex-direction: row-reverse;` to your CSS.
```css
.flexcontainer {
display: flex;
flex-direction: row-reverse;
}
```
![Flex Direction Column Reverse](https://lh5.googleusercontent.com/cyG8DYTewg53rXJWA7o_tv-aTt8B0reo1WRKtlkjYewuSJySY7uTTF18Aw_YoCskFNcaMKvBkpx3CKJvLn9n=w1920-h938)
---
**Align and Justify Flex Items**
If you want flex items aligned to the right:
```css
.flexcontainer {
display: flex;
justify-content: flex-end;
}
```
![Justify Content Flex End](https://lh4.googleusercontent.com/q8TD8v4Y6IH8Lcq98cO44bLGBQn-ePkknfee9zIHIkM68U0mPG_PVICwCpEpsOvtmN063GS43O8CepygXhEb=w1920-h938)
If you want flex items centered:
```css
.flexcontainer {
display: flex;
justify-content: center;
}
```
![Justify Content Center](https://lh6.googleusercontent.com/paPPI-Z9oE0h8CQNsUYS25ZPyeED2WRBqvivkQnr49H6AGPrzesnAvehha4AQL_bYm4v_08axEj_Dh18U4C-=w1920-h938)
If you want flex items separated:
```css
.flexcontainer {
display: flex;
justify-content: space-between;
}
```
![Justify Content Space Between](https://lh4.googleusercontent.com/8ecWL3E3LGr5wie5fw0JE3uGVSDudDRR13j0OcwX7AlDOibRq_P6ofSQvJj5IrBHnyqZwfC6uO8RAWWLQJpV=w1920-h938)
To center flex items both horizontally and vertically:
```css
.flexcontainer {
display: flex;
justify-content: center;
align-items: center;
}
```
---
**Wrapping**
Resized to fit into one column or one row when a flex container is not large enough, flex items are not able to wrap by default. By using flex-wrap: wrap;, any flex items causing overflow will be wrapped to the next line.
To allow wrapping:
```css
.flexcontainer {
display: flex;
flex-wrap: wrap;
}
```
![Wrapping Wrap](https://lh5.googleusercontent.com/EnlZXL88oNtct4FsWnZmsKY_Ko8FWKXQA2sYwqvUHclZevNF4mg16bjqky7iQQVDbSZ_4dQhAsoaGhmLQKTK=w1920-h938)
To allow reverse wrapping (overflow item(s) are sent to the line above):
```css
.flexcontainer {
display: flex;
flex-wrap: wrap-reverse;
}
```
![Wrapping Wrap Reverse](https://lh5.googleusercontent.com/p1EHhAC2j8lNLPPYOqVgVyktiXmB_4MNcokOs1tmX1oSe1gz3vL3vLixsRBoM4iLPpm4bMvThrdoKVVbRfuK=w1920-h938)
To control the positioning of flex items when there is wrapping (replace * with stretch, space-around, space-between, center, flex-end, or flex-start):
```css
.flexcontainer {
display: flex;
flex-wrap: wrap;
align-content: *;
}
```
---
**Expanding Flex Items**
When there is leftover space within a flex container, flex-grow can be used to dictate how each item fits in the remaining space.
To grow a flex item (default size is 0):
```css
.flexcontainer {
display: flex;
}
.flexitem1 {
flex-grow: 1;
border: 3px solid;
}
```
![Flex Grow](https://lh5.googleusercontent.com/BiyehuZqfuONB3r7Ig0w0wwDYsYbV4-BkcK8emqYf5veTrgsbcYNOQlfWpeqNVtgD25rSeS6BcM3wYMJckGe=w1920-h938)
*Only working when there is space remaining in a container*, `flex-grow` equals the ratio
of a container’s width/height that an item should stretch to fit to use up all leftover
container space, relative to the size of other children in the container. Similarly,
`flex-shrink` *only works when flex items are overflowing* the flex container due to insufficient space.
To shrink a flex item (default size is 0):
```css
.flexcontainer {
display: flex;
}
.flexitem1 {
flex-shrink: 1;
border: 3px solid;
}
```
To set the precise size of each flex item (default value is `flex-basis: auto;`):
```css
.flexcontainer {
display: flex;
}
.flexitem1 {
flex-basis: 25%; /*percentage value*/
}
.flexitem2 {
flex-basis: 50px; /*absolute value*/
}
.flexitem3 {
flex-basis: 5px;
}
```
![Flex Basis](https://lh5.googleusercontent.com/_AkidTIlGJQKrmpgTUFFdjCoOfa5UX-dQvTMj--IvXiqEqaX55M8fVrW1xN21yJS8zzyeNpnRgzoWxxboSlc=w1920-h938)
To use `flex-grow`, `flex-basis` and `flex-shrink` at the same time:
```css
.flexcontainer {
display: flex;
}
.flexitem1 {
flex: 2 1 100px;
}
.flexitem2 {
5 1 5%;
}
.flexitem3 {
flex: 1 5 5%;
}
```
---
**Additional Method to Access Flex Items**
Throughout the tutorial flex items were access directly via CSS, example:
```css
.flexitem1 {
flex-shrink: 1;
}
```
However, you may want to name all flex items the same, create groups of items, etc.
To make your code more usable, you can access flexbox items by their order number
(place the order number of the item you want to alter in parenthesis):
```css
.flexitem1:nth-child(1) {
flex-shrink: 1;
}
```
Simple to learn, flexbox is a quick way to make responsive page elements.
Unlike older layout methods, such as vertically-based block or horizontally-based inline,
flexbox was designed to be adaptable to both vertical and horizontal layouts.
Automatically adjusting to whatever type of device it’s being accessed from,
flexbox is vital to modern web development. Make your pages dynamic across mobile,
tablet and PC with flexbox.
以上是关于markdown Flexbox的的主要内容,如果未能解决你的问题,请参考以下文章