资讯专栏INFORMATION COLUMN

[note]布局:列等高,水平垂直居中,固定页脚

Chao / 2445人阅读

摘要:等高列布局两列结构两边都加上跟相等的负值,可以实现边框效果等同两列流体布局多列值要够大结构三列固定宽度结构三列自适应布局实用结构多列布局现代浏览器结构水平垂直

等高列布局 两列/marginLeft

html结构:.wrap>.container>.sidebar+.main
marginLeft,marginRight两边都加上跟borderWidth相等的负值,可以实现边框效果

</>复制代码

  1. /*1: 等同*/
  2. .container {
  3. border-left-width: 200px;/*1*/
  4. }
  5. .sidebar {
  6. float: left;
  7. width: 200px;/*1*/
  8. margin-left: -200px;/*1*/
  9. margin-right: -1px;
  10. border-right: 1px solid #888;
  11. }
  12. .main {
  13. float: left;
  14. margin-left: -1px;
  15. border-left: 1px solid #888;
  16. }

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

两列/流体布局-border

</>复制代码

  1. .main {
  2. width: 100%;
  3. border-right: 220px solid #f00;
  4. margin-right: -220px;
  5. }
  6. .sidebar {
  7. width: 220px;
  8. background: #f00;
  9. }

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

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

padding,margin值要够大
html结构:.container>.left+.main+.right

</>复制代码

  1. .container {
  2. overflow: hidden;
  3. }
  4. .column {
  5. margin-bottom: -99999px;
  6. padding-bottom: 99999px;
  7. }

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

三列固定宽度/border

html结构:.wrap>.container>.main+.left+.right

</>复制代码

  1. .container {
  2. width: 520px;
  3. border-left: 220px solid #0f0; /*==leftWidth*/
  4. border-right: 220px solid #f00;/*==rightWidth*/
  5. }
  6. .left {
  7. float: left;
  8. width: 220px;
  9. margin-left: -220px;
  10. }
  11. .main {
  12. float: left;
  13. width: 520px;
  14. margin-right: -520px;
  15. }
  16. .right {
  17. float: right;
  18. width: 220px;
  19. margin-right: -220px;
  20. }

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

三列自适应布局(实用)

html结构:.container>.main+.left+.right

</>复制代码

  1. .container {
  2. width: 100%;
  3. float: left;
  4. border-left: 220px solid #0f0;
  5. border-right: 220px solid #f00;
  6. display: inline; /*IE*/
  7. }
  8. .main {
  9. float: left;
  10. width: 100%;
  11. margin-right: -100%;
  12. }
  13. .left {
  14. float: left;
  15. width: 220px;
  16. margin-left: -220px;
  17. }
  18. .right {
  19. float: right;
  20. width: 220px;
  21. margin-right: -220px;
  22. }

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

多列布局/table(现代浏览器)

html结构:.table>.table-row>.table-cell+.table-cell+.table-cell

</>复制代码

  1. .table {
  2. display: table;
  3. &-row {
  4. display: table-row;
  5. }
  6. &-cell {
  7. display: table-cell;
  8. width: 33%;
  9. }
  10. }

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

水平垂直居中 flexbox方式

</>复制代码

  1. body {
  2. display: flex;
  3. align-items: center;
  4. justify-content: center;
  5. width: 100%;/*firefox下需要*/
  6. }

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

transform与绝对定位方式

</>复制代码

  1. img {
  2. position: absolute;
  3. top: 50%;
  4. left: 50%;
  5. transform: translate(-50%, -50%);
  6. }

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

伪元素方式

运用inline-block配合空标签,这里使用伪元素替代

</>复制代码

  1. body {
  2. text-align: center;
  3. &:after {
  4. content: "";
  5. display: inline-block;
  6. vertical-align: middle;
  7. height: 100%;
  8. width: 0;
  9. }
  10. }

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

固定页脚 模拟表格方式

html结构:.wrap>.container+.footer

</>复制代码

  1. .wrap {
  2. display: table;
  3. table-layout: fixed;
  4. > div {
  5. display: table-row;
  6. height: 1px;
  7. }
  8. .container {
  9. height: auto;
  10. }
  11. }

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

负marginBottom方式

html结构:.wrap>.container+.footer

</>复制代码

  1. html, body {
  2. height: 100%;
  3. }
  4. #{$root-selector} {
  5. min-height: 100%;
  6. height: auto !important;
  7. height: 100%;
  8. margin-bottom: -$footer-height;
  9. &:after {/*可用空标签替代*/
  10. content: "";
  11. display: block;
  12. height: $footer-height;
  13. }
  14. }
  15. #{$footer-selector} {
  16. height: $footer-height;
  17. }

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

padding+position方式

html结构:.wrap>.container+.footer

</>复制代码

  1. .wrap {
  2. position: relative;
  3. }
  4. .container {
  5. padding-bottom: 60px; /*==footerHeight*/
  6. }
  7. .footer {
  8. position: absolute; /*基于wrap的定位*/
  9. bottom: 0;
  10. height: 60px; /*footerHeight*/
  11. }

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

负marginTop方式

html结构:.wrap>.container^+.footer

</>复制代码

  1. html, body {
  2. height: 100%;
  3. }
  4. .wrap {
  5. min-height: 100%;
  6. height: auto !important;
  7. height: 100%;
  8. }
  9. .container {
  10. padding-bottom: 60px; /*==footerHeight*/
  11. }
  12. .footer {
  13. margin-top: -60px; /*==footerHeight*/
  14. height: 60px;
  15. }

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

借助javascript

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

垂直居中 inline-block+伪元素

在垂直居中的元素上添加伪元素,设置伪元素的高==父级容器的高,然后为文本添加vertical-align:middle

</>复制代码

  1. .ghost-center {
  2. position: relative;
  3. &::before {
  4. content: "";
  5. display: inline-block;
  6. height: 100%;
  7. width: 1%;
  8. vertical-align: middle;
  9. }
  10. p {
  11. display: inline-block;
  12. vertical-align: middle;
  13. }
  14. }

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

Sass mixin方法

</>复制代码

  1. @mixin center($width: null, $height: null) {
  2. position: absolute;
  3. top: 50%;
  4. left: 50%;
  5. @if not $width and not $height {
  6. transform: translate(-50%, -50%);
  7. } @else if $width and $height {
  8. width: $width;
  9. height: $height;
  10. margin: -($width/2) #{0 0} -($height/2);
  11. } @else if not $height {
  12. width: $width;
  13. margin-left: -($width/2);
  14. transform: translateY(-50%);
  15. } @else {
  16. height: $height;
  17. margin-top: -($height/2);
  18. transform: translateX(-50%);
  19. }
  20. }
  21. /*flexbox方法*/
  22. @mixin center-children {
  23. display: flex;
  24. justify-content: center;
  25. align-items: center;
  26. }

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

参考网址:

再谈等高列布局、水平垂直居中与置顶页脚
八种创建等高列布局
CSS居中完整指南
使用Sass制作居中效果

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/115482.html

相关文章

  • 构建静态页面 之 [ 布局 ]

    摘要:布局描述表示对页面中的显示效果进行一些排列水平方向居中垂直方向居中居中布局水平方向居中第一种方式水平居中行内块级元素水平居中的第一种方法该方法需作用在父子结构中为父级设置属性为子级设置属性注意的问题属性是设置文本内容对齐方式的 布局 描述 表示对页面中的显示效果进行一些排列 水平方向居中 垂直方向居中 居中布局 水平方向居中 第一种方式 水平居中 + 行内块级元素(text-a...

    andot 评论0 收藏0
  • 构建静态页面 之 [ 布局 ]

    摘要:布局描述表示对页面中的显示效果进行一些排列水平方向居中垂直方向居中居中布局水平方向居中第一种方式水平居中行内块级元素水平居中的第一种方法该方法需作用在父子结构中为父级设置属性为子级设置属性注意的问题属性是设置文本内容对齐方式的 布局 描述 表示对页面中的显示效果进行一些排列 水平方向居中 垂直方向居中 居中布局 水平方向居中 第一种方式 水平居中 + 行内块级元素(text-a...

    JessYanCoding 评论0 收藏0
  • CSS 常用的定位和布局方法汇总(已添加源码地址)

    CSS-Layout 旨在打造详尽的前端布局代码学习库(自从用了框架开发,CSS生疏了不少,所以开这个库练练手)SF不能正确解析含有中文的网址,所以某些预览链接无法跳转,请访问我的博客阅读此文 常见定位方法 水平居中 子元素为行内元素还是块状元素,宽度一定还是宽度未定,采取的布局方案不同。 方案选择基本思路:子元素为 行内元素:对父元素设置text-align:center; 定宽块状元素: 设...

    loonggg 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<