CSS等高布局的6种方式_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:19:00
Original
983 people have browsed it

× 目录 [1]边框模拟 [2]负margin [3]table [4]absolute [5]flex [6]js

前面的话

  等高布局是指子元素在父元素中高度相等的布局方式。等高布局的实现包括伪等高和真等高,伪等高只是看上去等高而已,真等高是实实在在的等高。本文将介绍边框模拟、负margin这两种伪等高以及table实现、absolute实现、flex实现和js判断这四种真等高布局

 

伪等高

边框模拟

  因为元素边框和元素高度始终是相同高度,用元素的边框颜色来伪装左右两个兄弟元素的背景色。然后将左右两个透明背景的元素使用absolute覆盖在中间元素的左右边框上,实现视觉上的等高效果

  [注意]左右两侧元素的内容高度不能大于中间元素内容高度,否则无法撑开容器高度

Copy after login

left

center

center

right

Copy after login

负margin

  因为背景是在padding区域显示的,设置一个大数值的padding-bottom,再设置相同数值的负的margin-bottom,使背景色铺满元素区域,又符合元素的盒模型的计算公式,实现视觉上的等高效果

  [注意]如果页面中使用锚点跳转时,将会隐藏部分文字信息

  [注意]如果页面中的背景图片定位到底部,将会看不到背景图片

Copy after login

left

center

center

right

Copy after login

真等高

table

  table元素中的table-cell元素默认就是等高的

Copy after login

left

center

center

right

Copy after login

absolute

  设置子元素的top:0;bottom:0;使得所有子元素的高度都和父元素的高度相同,实现等高效果

Copy after login

left

center

center

right

Copy after login
Copy after login

flex

  flex中的伸缩项目默认都拉伸为父元素的高度,也实现了等高效果

Copy after login

left

center

center

right

Copy after login
Copy after login

js

  当子元素高度不同时,进行js判断,增加较低子元素的padding-bottom,使得各个子元素实现等高效果

Copy after login

left

center

center

right

Copy after login

Copy after login

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!