官术网_书友最值得收藏!

Ordering your Flexbox

Flexbox is a really powerful module as it comes with several properties that you can customize. Let's quickly go over some more basics before we fully take the plunge and use Flexbox in Bootstrap. Let's start by talking about the order of child boxes. By default, they will appear in the order that you insert them in the HTML file. Consider the following code:

<div class="parent"> 
   <div class="child"> 
   1 
   </div> 
   <div class="child"> 
   2 
   </div> 
   <div class="child"> 
   3 
   </div> 
</div> 

A proper CSS will produce a layout that looks like this:

Here's the CSS to produce this layout if you are following along at home:

.parent { 
  display: flex; 
  background: #ccc; 
  padding: 10px; 
  font-family: helvetica; 
} 
 
.child { 
  padding: 10px; 
  margin: 10px; 
  background: #fff; 
  flex-grow: 1; 
  text-align:center; 
  height: 100px; 
  line-height: 100px; 
} 

Now using an order property we can reorder the children using some CSS. Let's put the third box at the beginning. If you are reordering some blocks, you need to define the position for each one; you can't simply enter the value for a single block. Add the following CSS to your style sheet:

.child:nth-of-type(1) { 
  order: 2; 
} 
.child:nth-of-type(2) { 
  order: 3; 
} 
.child:nth-of-type(3) { 
  order: 1; 
} 

I'm using the nth-of-type pseudo selector to target each of the three boxes. I've then used the order property to set the third box to the first position. I've also adjusted the other two boxes to move them over one space. Save the file and your boxes should now look like this:

As you can see, the third box has moved to the first position. It's as easy as that to rearrange blocks on boxes on a page. I think you'll likely see how this could be useful for coding up a web application dashboard.

Stretching your child sections to fit the parent container

Another important Flexbox feature is the ability to stretch the width of the child boxes to fit the full-width of the containing parent. If you look at the preceding CSS you'll notice a flex-grow property on the .child class. The property is set to 1 which means that the child boxes will stretch to equally fill their parent. You could also do something where one box is set to a different value, using the nth-of-type selector, and then it would be wider than the others. Here's the code to create equal-width columns as that is what you'll likely do in most cases:

.child { 
   flex-grow: 1; 
} 

Changing the direction of the boxes

By default in Flexbox, the child boxes will be in a row going left to right. If you like, you can change the direction using the flex-direction property. Let's try out a few different directions. First let's review our base HTML code again:

<div class="parent"> 
   <div class="child"> 
     1 
   </div> 
   <div class="child"> 
     2 
   </div> 
   <div class="child"> 
     3 
   </div> 
</div> 

Here's the base CSS we wrote a little earlier. However, this time we'll add the flex-direction property (with a value of row-reverse)to the .parent class. This will reverse the order of the boxes:

.parent { 
  display: flex; 
  flex-direction: row-reverse; 
  background: #ccc; 
  padding: 10px; 
} 
 
.child { 
  padding: 10px; 
  margin: 10px; 
  background: #fff; 
  flex-grow: 1; 
  text-align:center; 
  height: 100px; 
  line-height: 100px; 
} 

If you save the file and view it in a browser it should now look like this:

What if we wanted to order the boxes vertically so they were stacked on top of each other in descending order? We can do that by changing the flex-direction property to column:

.parent { 
   ... 
   flex-direction: column; 
} 

That configuration will produce a grid that looks like this:

Finally there is one more direction we can try. Let's do the same vertically stacked grid but this time we'll reverse it. We do that by switching the flex-direction property to column-reverse:

.parent { 
   ... 
   flex-direction: column-reverse; 
} 

That will produce a grid that looks like this:

主站蜘蛛池模板: 衡山县| 合水县| 忻州市| 邓州市| 莱芜市| 肥乡县| 桑日县| 眉山市| 龙口市| 偏关县| 成都市| 侯马市| 湛江市| 留坝县| 岢岚县| 凤翔县| 沙河市| 大兴区| 黎平县| 南安市| 宜丰县| 利辛县| 淮滨县| 苍梧县| 安西县| 长宁县| 乐山市| 通许县| 获嘉县| 龙泉市| 莱阳市| 罗甸县| 峨边| 山东| 北票市| 嘉兴市| 湘潭县| 南川市| 汕头市| 南乐县| 湖口县|