Home > Web Front-end > HTML Tutorial > 常用布局 @mixin_html/css_WEB-ITnose

常用布局 @mixin_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:18:55
Original
1315 people have browsed it

单个块级元素的居中

@mixin center-block() {  display: block;  margin-left: auto;  margin-right: auto;}
Copy after login

多个块级元素占一行

用 display: inline-block 来实现

@mixin row($parent-selector, $item-selector, $font-size: 12px, $vertical-align: top) {  #{$parent-selector} {    font-size: 0;// 防止子元素的空白元素占据空间  }  #{$item-selector} {    display: inline-block;    vertical-align: $vertical-align;// 防止子元素高度不同导致的奇怪的对齐    font-size: $font-size;  }}
Copy after login

用 flex 来实现

@mixin row() {  display: flex;}
Copy after login

多个块级元素的居中

用 display: inline-block 来实现

@mixin center-blocks($parent-selector, $item-selector, $font-size: 12px, $vertical-align: top) {  @include row($parent-selector, $item-selector, $font-size: 12px, $vertical-align: top);  #{$parent-selector} {    text-align: center;  }}
Copy after login

用 flex 来实现

@mixin center-blocks($parent-selector) {  #{$parent-selector} {    @include row;    justify-content: center;  }}
Copy after login

两端对齐

用 display: inline-block 来实现

@mixin justify-blocks($parent-selector, $item-selector, $font-size: 12px, $vertical-align: top) {  @include row($parent-selector, $item-selector, $font-size: 12px, $vertical-align: top);  #{$parent-selector} {    text-align: justify;    &:after {      content: '';      display: inline-block;      width: 100%;    }  }}
Copy after login

用 flex 来实现

@mixin justify-blocks($parent-selector) {  #{$parent-selector} {    @include row;    justify-content: space-between;  }}
Copy after login

多个块级元素占一行,某个元素占据剩余部分

用 display: table 来实现

@mixin item-fill($parent-selector, $item-selector, $fill-item-selector, $vertical-align: top) {  #{$parent-selector} {    display: table; // 兼容性相当好。IE8+ 都支持 http://caniuse.com/#feat=css-table  }  #{$item-selector} {    display: table-cell; // 兼容性相当好。IE8+ 都支持 http://caniuse.com/#feat=css-table    vertical-align: $vertical-align;  }  #{$fill-item-selector} {    width: 100%;  }}
Copy after login

用 flex 来实现

@mixin item-fill($parent-selector,  $fill-item-selector) {  #{$parent-selector} {    @include row;  }  #{$fill-item-selector} {    -webkit-flex-grow: 1;    flex-grow: 1;  }}
Copy after login

完整源码

  • _layout.scss
  • flex 实现

参考链接

  • Almost complete guide to flexbox (without flexbox)
  • Flex 布局教程:语法篇

本文遵守创作共享CC BY-NC-SA 4.0协议网络平台如需转载必须与本人联系确认。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template