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

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.
主站蜘蛛池模板: 剑川县| 美姑县| 昌邑市| 黄石市| 双峰县| 布尔津县| 睢宁县| 苍山县| 隆安县| 内黄县| 芜湖市| 二连浩特市| 旌德县| 娱乐| 出国| 电白县| 肥东县| 西城区| 肃北| 灵川县| 麻栗坡县| 禄劝| 建始县| 股票| 临清市| 会理县| 邯郸市| 奉节县| 定西市| 莱芜市| 舟曲县| 金门县| 惠来县| 文昌市| 玛纳斯县| 拉孜县| 芷江| 哈尔滨市| 东安县| 英德市| 大兴区|