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

Creating fluid layouts with Grid and Flexbox

When it comes to implementing the design principles set by Microsoft to build attractive, intuitive, and interactive Windows 8 apps, layout is very important. It is common to define a page layout using HTML structural elements such as <div> and <table>, and the positioning style rules.

But now, there is a more flexible way to do it with the CSS3 advanced layout features, namely Grid layout and Flexbox (Flexible box) layout. These layout systems allow you to easily implement an adaptive and fluid layout.

The Grid layout

It offers a very simple way to create fluid and adaptable layouts for a Windows 8 app. It is ideal for implementing a full screen UI since the grid can automatically expand to fill in all the space that is available. The Grid layout allows you to align and position its child elements as columns and rows, entirely using CSS, and is independent of their order in the HTML code. It enables more fluidity in layouts than what would be possible with the approach that uses floats or scripts.

The following example demonstrates how we traditionally used floats to position elements:

The HTML code is as follows:

<div class="container">
  <div class="leftDiv"></div>
  <div class="rightDiv"></div>
</div>

The CSS code is as follows:

.container { width: 200px; height:50px; border: 1px solid black; }
.leftDiv { float:left; width: 100px; height:50px;background-color:blue}
.rightDiv { float:right; width: 50px; height:50px;background-color:red}

The preceding code will result in the following multicolor box. The container has a black border surrounding the two divs inside, the blue div to the left and the red one to the right, and the white space in between is the remaining unoccupied space:

The Grid layout is specified by setting the display style rule property of an element to -ms-grid, or you can use the -ms-inline-grid property for an inline-level grid element. You may have noticed the vendor prefix -ms(Microsoft-specific), which is because the status of this CSS feature is still a Working Draft; adding this vendor prefix allows it to work with both Internet Explorer 10 and Windows Store apps using JavaScript in Windows 8. The following is an example:

.divGrid {
  display: -ms-grid;
  -ms-grid-columns: 120px 1fr;
  -ms-grid-rows: 120px 1fr;
}
.column1row1 {
  -ms-grid-column: 1;
  -ms-grid-row: 1;
}
.column2row1 {
  -ms-grid-column: 2;
  -ms-grid-row: 1;
}

The display: -ms-grid; property creates a grid; afterwards, we define the columns and rows and specify their sizes using the following properties: -ms-grid-column and -ms-grid-rows. The -ms-grid-columns property specifies the width of each column, and -ms-grid-rows specifies the height of each row, in that grid. The width and height values in these two properties respectively are separated by a space character. In the preceding example, the -ms-grid-columns: 120px 1fr; property creates two columns; the first one has a width of 120 px and the second one has a width value of 1 fr, that is, one fractional unit, which means that the width of the second column will automatically fill in all of the remaining available space. The same concept applies for rows. The remaining two classes in the preceding code snippet will position the elements in these classes into columns and rows of the grid using the -ms-grid-column and -ms-grid-row properties.

Note

The fraction units (fr) designate how the available space should be divided among the columns or rows according to their fractional values. For example, if we have a four-columns layout such as the following: -ms-grid-columns: 100px 100px 1fr 2fr;, column 3 takes one fraction and column 4 takes two fractions of the total remaining space. Hence, the total remaining space is now 3 fr; column 3 is set to 1 fr divided by the total (3), so both one-third of the remaining space and column 4 having 2 fr will be assigned two-thirds of the remaining space.

In the preceding example, we used px and fr units to specify the size of the columns and rows. Additionally, we can do so using standard length units (such as px or em), or the percentage of the element's width or height. Also, we can use the following keywords:

  • auto: This keyword makes the size of the column or row stretch to fit the content inside
  • min-content: This keyword sets the size of the column or row to the minimum size of any child element
  • max-content: This keyword sets the size of the column or row to the maximum size of any child element
  • minmax(a,b): This keyword sets the size of the column or row to a value between a and b as much as the available space allows

The following table lists the properties associated with the Grid layout:

Moreover, the Grid layout exposes a rich set of properties that allows you to easily cater to the changes in the view states and orientation of the app. We will discuss that later on when we get to the design of the app.

The Flexbox layout

The second layout model we have is the Flexbox mode, another recent addition in CSS3. Similar to the Grid layout, the Flexbox layout is enabled using the display property and also requires a Microsoft-specific vendor prefix as it is still a World Wide Web Consortium (W3C) Working Draft. The Flexbox layout is used to make the relative position and the size of elements stay constant, even if the window sizes of the screen and browser change. Compared to floats, Flexbox provides a better and easier control on the position and size of elements. The advantage you have with Flexbox layout is that it enables relative positioning and dimensions of the elements inside it, since it considers the available space. This allows you to create a fluid layout that maintains the position and size of the elements relative to each other; hence, it enables the elements inside a Flexbox container to resize and reposition themselves when the dimensions of the browser or app window change. A Flexbox layout would be ideal for building apps that present any digital print media, such as a newspaper or a magazine.

As with the Grid layout, it is quite easy to create a container with a Flexbox layout by setting the display property to -ms-flexbox. After creating a Flexbox container, we can start manipulating the elements inside it, using the following properties:

  • -ms-flex-direction: It specifies the orientation of the child elements using the following keyword values: row (initial value), column, row-reverse, and column-reverse. We will go over each one of the values, and show the effect it applies, in the following example. And what better way to explain it than actual code? So,o suppose we have the following HTML and CSS code snippets:
    <div class="flexit">
      <div>1</div>
      <div>2</div>
      <div>3</div>
    </div>
    
    .flexit {
      width:160px;
      height:100px;
      border:2px solid brown;
      display:-ms-flexbox;
      -ms-flex-direction: row;
    }
    .flexit div {
      background-color:red;
      width:50px;
      height:25px;
      text-align:center;
      color:white;
    }
    .flexit div:first-child {
      background-color:green;
      height:30px;
    }
    .flexit div:last-child {
      background-color:blue;
      height:30px;
    }

    The preceding syntax creates a Flexbox container with the flexit class that wraps in a Flexbox layout the child <div> elements marked with text 1, 2, and 3 for tracking. We apply some styles and background colors to mark the child elements.

    So the following values in the -ms-flex-direction property will give us the results in the following table. Notice how the order and the positioning of the elements change without adding anything to the markup:

  • -ms-flex-align: This property specifies the alignment of the child elements in a Flexbox container. It takes the following keyword values: start, end, center, stretch, and baseline. The alignment is always perpendicular to the layout axis defined in the -ms-flex-direction property; so, if the orientation is horizontal, it will set the alignment to vertical and vice versa. For example, if the orientation is row (horizontal), the value start will set the alignment to top (vertical).
  • -ms-flex-pack: This property specifies how the available space is divided between the child elements of the Flexbox container, parallel to the axis defined by the -ms-flex-direction property, unlike the alignment property described earlier. It takes the following keyword values: start, end, center, and justify.
  • -ms-flex-wrap: This property enables the child elements to overflow and wrap to the next line or columns, and specifies the direction of that flow. It takes the following keyword values: none, wrap, and wrap-reverse.
主站蜘蛛池模板: 洛浦县| 钟祥市| 屏东县| 河间市| 临夏县| 蓬溪县| 公主岭市| 曲沃县| 元阳县| 含山县| 开远市| 长子县| 开阳县| 绥芬河市| 都兰县| 古交市| 曲靖市| 鸡泽县| 湖州市| 临颍县| 娄底市| 永德县| 昌吉市| 辽阳市| 桐庐县| 安阳县| 年辖:市辖区| 玉溪市| 互助| 固安县| 宜兰县| 武威市| 体育| 武定县| 衡阳市| 东宁县| 镇沅| 雷山县| 南通市| 当雄县| 泌阳县|