[note]佈局:列等高,水平垂直居中,固定頁腳

等高列布局

兩列/marginLeft

html結構:.wrap>.container>.sidebar+.main
marginLeft,marginRight兩邊都加上跟borderWidth相等的負值,能夠實現邊框效果javascript

/*1: 等同*/
.container {
  border-left-width: 200px;/*1*/
}
.sidebar {
  float: left;
  width: 200px;/*1*/
  margin-left: -200px;/*1*/
  margin-right: -1px;
  border-right: 1px solid #888;
}
.main {
  float: left;
  margin-left: -1px;
  border-left: 1px solid #888;
}

http://codepen.io/zplus/pen/N...css

兩列/流體佈局-border

.main {
  width: 100%;
  border-right: 220px solid #f00;
  margin-right: -220px;
}
.sidebar {
  width: 220px;
  background: #f00;
}

http://codepen.io/zplus/pen/X...html

多列/(+padding)+(-margin)=0

padding,margin值要夠大
html結構:.container>.left+.main+.rightjava

.container {
  overflow: hidden;
}
.column {
  margin-bottom: -99999px;
  padding-bottom: 99999px;
}

http://codepen.io/zplus/pen/w...瀏覽器

三列固定寬度/border

html結構:.wrap>.container>.main+.left+.rightsass

.container {
  width: 520px;
  border-left: 220px solid #0f0; /*==leftWidth*/
  border-right: 220px solid #f00;/*==rightWidth*/
}
.left {
  float: left;
  width: 220px;
  margin-left: -220px;
}
.main {
  float: left;
  width: 520px;
  margin-right: -520px;
}
.right {
  float: right;
  width: 220px;
  margin-right: -220px;
}

http://codepen.io/zplus/pen/J...ide

三列自適應佈局(實用)

html結構:.container>.main+.left+.right佈局

.container {
  width: 100%;
  float: left;
  border-left: 220px solid #0f0;
  border-right: 220px solid #f00;
  display: inline; /*IE*/
}
.main {
  float: left;
  width: 100%;
  margin-right: -100%;
}
.left {
  float: left;
  width: 220px;
  margin-left: -220px;
}
.right {
  float: right;
  width: 220px;
  margin-right: -220px;
}

http://codepen.io/zplus/pen/b...flex

多列布局/table(現代瀏覽器)

html結構:.table>.table-row>.table-cell+.table-cell+.table-cellui

.table {
  display: table;
  &-row {
    display: table-row;
  }
  &-cell {
    display: table-cell;
    width: 33%;
  }
}

http://codepen.io/zplus/pen/b...

水平垂直居中

flexbox方式

body {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;/*firefox下須要*/
}

http://codepen.io/zplus/pen/m...

transform與絕對定位方式

img {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

https://codepen.io/zplus/pen/...

僞元素方式

運用inline-block配合空標籤,這裏使用僞元素替代

body {
  text-align: center;
  &:after {
    content: "";
    display: inline-block;
    vertical-align: middle;
    height: 100%;
    width: 0;
  }
}

https://codepen.io/zplus/pen/...

固定頁腳

模擬表格方式

html結構:.wrap>.container+.footer

.wrap {
  display: table;
  table-layout: fixed;
  > div {
      display: table-row;
      height: 1px;
  }
  .container {
    height: auto;
  }
}

http://codepen.io/zplus/pen/o...

負marginBottom方式

html結構:.wrap>.container+.footer

html, body {
    height: 100%;
  }
  #{$root-selector} {
    min-height: 100%;
    height: auto !important;
    height: 100%;
    margin-bottom: -$footer-height;
    &:after {/*可用空標籤替代*/
      content: "";
      display: block;
      height: $footer-height;
    }
  }
  #{$footer-selector} {
    height: $footer-height;
  }

http://codepen.io/zplus/pen/q...

padding+position方式

html結構:.wrap>.container+.footer

.wrap {
  position: relative;
}
.container {
  padding-bottom: 60px; /*==footerHeight*/
}
.footer {
  position: absolute; /*基於wrap的定位*/
  bottom: 0;
  height: 60px; /*footerHeight*/
}

http://codepen.io/zplus/pen/X...

負marginTop方式

html結構:.wrap>.container^+.footer

html, body {
  height: 100%;
}
.wrap {
  min-height: 100%;
  height: auto !important;
  height: 100%;
}
.container {
  padding-bottom: 60px; /*==footerHeight*/
}
.footer {
  margin-top: -60px; /*==footerHeight*/
  height: 60px;
}

http://codepen.io/zplus/pen/x...

藉助javascript

http://codepen.io/zplus/pen/O...

垂直居中

inline-block+僞元素

在垂直居中的元素上添加僞元素,設置僞元素的高==父級容器的高,而後爲文本添加vertical-align:middle

.ghost-center {
  position: relative;
  &::before {
    content: "";
    display: inline-block;
    height: 100%;
    width: 1%;
    vertical-align: middle;
  }
  p {
    display: inline-block;
    vertical-align: middle;
  }
}

http://codepen.io/zplus/pen/M...

Sass mixin方法

@mixin center($width: null, $height: null) {
  position: absolute;
  top: 50%;
  left: 50%;
  
  @if not $width and not $height {
    transform: translate(-50%, -50%);
  } @else if $width and $height {
    width: $width;
    height: $height;
    margin: -($width/2) #{0 0} -($height/2);
  } @else if not $height {
    width: $width;
    margin-left: -($width/2);
    transform: translateY(-50%);
  } @else {
    height: $height;
    margin-top: -($height/2);
    transform: translateX(-50%);
  }
}

/*flexbox方法*/
@mixin center-children {
  display: flex;
  justify-content: center;
  align-items: center;
}

http://codepen.io/zplus/pen/p...


參考網址:

再談等高列布局、水平垂直居中與置頂頁腳
八種建立等高列布局
CSS居中完整指南
使用Sass製做居中效果

相關文章
相關標籤/搜索