CSS Sticky Footer 几种实现方式_CSS教程_CSS_网页制作

来源:脚本之家  责任编辑:小易  

www.zgxue.com防采集请勿采集本网。

什么是 “Sticky Footer”

所谓 “Sticky Footer”,并不是什么新的前端概念和技术,它指的就是一种网页效果: 如果页面内容不足够长时,页脚固定在浏览器窗口的底部;如果内容足够长时,页脚固定在页面的最底部。但如果网页内容不够长,置底的页脚就会保持在浏览器窗口底部。

先来看看下面的例子, 代码如下

<div class="header"> 顶部</div><div class="main"> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p></div><div class="footer"> <i class="fa fa-copyright" aria-hidden="true"></i> <div>底部</div></div>

.header { background-color: #3498DB; height: 50px; line-height: 50px; text-align: center; color: #fff;}.main { overflow: auto; box-sizing: border-box;}.footer { background-color: #ECF0F1; height: 50px; line-height: 50px; text-align: center;}

细心读者应该发现问题了,底部 footer 位置会随着主体内容高度变化自动变化,当主体内容小于视口的高度时, footer 并没有黏贴在底部. 那么解决这样问题尼?

negative margin

<div class="main"> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p></div><div class="footer"> <i class="fa fa-copyright" aria-hidden="true"></i> <div>底部</div></div>

html,body { height: 100%;}.header{ background-color: #3498DB; height: 50px; line-height: 50px; text-align: center; color: #fff; position: fixed; width: 100%;}.main { min-height: 100%; overflow: auto; box-sizing: border-box; padding-bottom: 50px; padding-top: 50px; margin-bottom: -50px;}.footer { background-color: #ECF0F1; height: 50px; line-height: 50px; text-align: center;}

固定高度解决方案

使用如下属性 min-height calc vh

calc() 是 CSS3引入的,让你在声明CSS属性值时可以执行一些计算.

它可以用在一些数值场合; 详细可以查阅这里MDN

vh(Viewport Height): 顾明思议,表示的是视口的高度.

详细信息以及兼容可以查阅这里: caniuse

针对上面的代码进行修改,如下

.main { min-height: calc(100vh - 50px - 50px);}

这样完成我们期望的,但是有个缺点就是每次我们都要去计算 header、footer 的高度.

这显然不完美, 假如DOM结构层级多的话,需要计算的内容更多.

absolute

absolute相信大家熟悉不过了,这里就不在啰嗦了; 这里注意这个就行, absolute 元素其位置是根据什么来进行计算并进行定位的?

<div class="header"> 头部</div><div class="main"> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p></div><div class="footer"> <i class="fa fa-copyright" aria-hidden="true"></i> <div>底部</div></div>

html{ position: relative; min-height: 100%;}body{ margin-bottom: 50px;}.header { background-color: #3498DB; height: 50px; line-height: 50px; text-align: center; color: #fff;}.main { overflow: auto;}.footer { position: absolute; bottom:0; width: 100%; background-color: #ECF0F1; height: 50px; line-height: 50px; text-align: center;}

代码是不是很简单,这里主要 position的应用:

1 默认情况下, 当给某个元素设置为 position:absolute 时, 在祖先元素没有设置 position: absolute or fixed or relative

时, 其默认相对于初始包含块( initial containing block ).

2 什么初始化包含块?

The containing block in which the root element lives is a rectangle called the initial containing block. For continuous media, it has the dimensions of the viewport and is anchored at the canvas origin;

这是w3c对包含块介绍, 这段话大概意思, 根元素(document)为默认为初始化包含块,其初始化块的大小为视口的大小.

理解这几个概念后,我们再来看上面的代码就一目了然了!

html{ position: relative; min-height: 100%; } body{ margin-bottom: 50px; } position:relative 改变包含块,让设置absolute元素根据html元素来进行定位. min-height: 保证在内容不足视口时, footer能黏贴在底部. margin-bottom 值为 footer 元素的高度,这样保证内容区域不会被footer遮住.

Flexbox

Flexbox是最完美的方案。只需要几行CSS代码就可以实现,而且像上面计算或添加额外的HTML元素。

修改代码如下:

<div class="container"> <div class="header"> 顶部 </div> <div class="main"> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> <p>中间部分</p> </div> <div class="footer"> <i class="fa fa-copyright" aria-hidden="true"></i> <div>底部</div> </div></div>

html,body { height: 100%;}.container { display: flex; flex-direction: column; min-height: 100%;}.header { background-color: #3498DB; height: 50px; line-height: 50px; text-align: center; color: #fff;}.main { overflow: auto; box-sizing: border-box; flex: 1;}.footer { background-color: #ECF0F1; height: 50px; line-height: 50px; text-align: center;}

最终效果如下:

negative =margin、固定宽度、absolute 都依赖底部高度为固定.

一般推荐使用 absolute 和 flex 实现方式; 具体用那种可以根据具体场景来使用.

In addition to the May Day tourism what else can I do?Tutoring tutoring Beauty to raise colour Household chores Work overtime requirementsCentral south university graduation design525)inverted siphon and cross the water tank:when water need across the roadbed,limited to the design elevation at the same time,can use a pipe or groove,from the bottom of the subgrade or upper overhead crossing,the former inverted siphon,the latter for crossing the sink,equivalent of culvert and cross the water bridge respectively,both belong to the special structure of subgrade surface drainage,and mostly needed for irrigation and water conservancy and use.6)evaporation pool:dry climate,drainage difficult location,available along the borrow pit or specifically set the surface water evaporation pool is ruled out.(2)the underground drainage facilitiesPerched water in embankment and slope soil,buried shallow diving known as groundwater,groundwater affect the roadbed strength and slope stability,should be set well drainage,sewer and inspection facilities such as groundwater.Common subgrade underground drainage facilities are:blind ditch,sewer and water seepage tunnel and pits,its characteristic is little displacement,mainly collected water seepage way,and came to the exclusion of roadbed scope.For larger flow of groundwater,should set up special underground pipeline is ruled out.Buried under the ground,due to the underground drainage equipment is not easy to repair,after the completion of subgrade and hard to find out the failure situation,therefore require strong effective underground drainage equipment.1)drainage:relative to the ground open ditch drainage,drainage is also called the blind ditch,has the meaning of concealed work.Due to a ditch filled with particles of different size materials,use of filler material permeability will converge in a ditch of groundwater,and along the ditch drainage to the designated place.2)sewer:adopting the infiltrators will converge in a ditch of groundwater,and through the bottom channel which terminates to their appointed places.Its role is to reduce or intercept groundwater,the hydraulic characteristics is turbulent.3)pits:the pits of horizontal underground drainage equipment,under the local existence of multilayer aquifer,which affect the roadbed with thin upper aquifer,displacement,and flush the sewer is hard to decorate,the vertical drainage,set up the pits,through the impermeable layer,the roadbed,within the scope of the upper groundwater is deeper aquifers,in order to reduce groundwater or all of the upper excluded.Central south university graduation design53Chapter 10 of subgrade slope protection10.1 overviewSubgrade slope surface protection mainly solve the roadbed construction,bare roadbed stability of slope and slope surface.Easy to erosion of soil slopes and easily weathered rock cutting slope,after construction if exposed for a long time,in the natural weathering under the action of stress and rain erosion,gully,slip will happen collapse collapse and falling,peeling,such as slope deformation.At the same time,flake or erosion debris often blocked side ditch,poor drainage,therefore,must take the corresponding protective measures as early as possible,otherwise will cause serious diseases.10.2 design principles1)easy to weathering or vulnerable to the rains washed out of rock and soil slope and severe broken rock slope,shall be protected in a timely manner.2)and soft hard rock cutting slope,should be used according to the strata conditions all protection or local protection.3)in rainy areas,with sand soil filling embankment,the shoulder and side slope surface is vulnerable to water erosion and slope protection will be according to the specific situation.4)slope protection structure is generally not considered slope or artificial filled soil at the bottom of the lateral pressure,the protection of the stability of the slope should be enough,but the wall can be used to limit the stability of the slope.10.3 commonly used slope surface protection typesA,plant protectionPlant protection is a kind of simple construction,low cost and good effect is slope protection measures.Plant can cover topsoil,prevent the rains washed out;Adjust soil moisture,prevent cracks;The consolidation of soil,prevent the slope surface weathering flake,at the same time also can rise to protect the environment,beautify the role of road capacity.In order to ensure the slope stability and extend the lifespan of the plant protection,general appropriate in cutting slope foot set 1~2 m high plasma build by laying bricks or stones flaky slope protection.Second,plaster(a)applicable conditions1.Without a serious of easy weathering of rock slope,but poorly by the coal measures strata and the diagenesis of red sticky clay rock slope is not applicable.2.The slope is not restricted,but should slope surface is relatively dry.Central south university graduation design543.Use fixed number of year is shorter,the general is 8~10 years.(2)the construction points for attention1.The front should be the slope surface of weathering rock flush with clean with clean water to rinse the slope;Application large concave slope pulp build by laying bricks or stones flaky inlaying,wide fissure grouting.2.Using lime plaster slag pulp,after the mortar wipe on a bit dry namely to ram,until pulp out surface,then wipe flat coated with hardener.3.The plaster is unfavorable in winter season,rain and sunshine strongly during construction,the suitable temperature for 4 °~30 ℃,and pay attention to cover grass water keeping in good health.Three,shotcrete and shotcrete(a)applicable conditions1.Easy but not serious weathering of rock slope,the slope surface is relatively dry.2.Of high and steep slope,the broken upper strata and complete the lower strata and need large area protection slope,slope using this type is economic.3.The difference of diagenesis clay rock slope is unfavorable use.(2)the construction points for attention1.The shotcrete and shotcrete aggradation rubble slope should be cleared before and wash it.2.Mechanical injection should be done before operation to try spraying,moderate to adjust the water cement ratio,the injection of gray body paste,smooth surface,the aggregate distribution uniform,less amount of springback.3.The spraying operation should be bottom-up hierarchical.Gray body achieve initial set immediately after the water keeping in good health,last 7~10 days.It is strictly prohibited in frozen season or the rain spraying operations.Four,plasma build by laying bricks or stones flaky revetment(a)applicable conditions1.Various easy weathering of rock slope and soil slopes.If,when used on embankment slope should be heavy fact after the construction of embankment.2.The slope is not greater than 1 to 1.Plasma build by laying bricks or stones,flaky slope protection generally USES such as cross section,depending on the height of slope,the thickness of general is 0.3~0.4 m.When the slope protection area is larger,and the slope is steep,in order to enhance the stability of slope protection,can use the type of slope protection.中南大学毕业设计525)倒虹吸与渡水槽:当水流需要横跨路基,同时受到设计标高的限制,可以采用管道或沟槽,从路基底部或上部架空跨越,前者称倒虹吸,后者为渡水槽,分别相当于涵洞和渡水桥,两者属于路基地面排水的特殊结构物,并且多半是配合农田水利所需而采用。6)蒸发池:气候干旱、排水困难地段,可利用沿线的集中取土坑或专门设置蒸发池排除地表水。(2)地下排水设备路基及边坡土体中的上层滞水,活埋藏很浅的潜水称为地下水,当地下水影响路基路面强度或边坡稳定时,应设置暗沟、渗沟和检查井等地下水设施。常用的路基地下排水设备有:盲沟、渗沟、渗水隧洞和渗井等,其特点是排水量不大,主要是以渗流方式汇集水流,并就近排除路基范围以外。对于流量较大的地下水,应设置专用地下管道予以排除。由于地下排水设备埋置地面以下,不易维修,在路基建成后又难以查明失效情况,因此要求地下排水设备牢固有效。1)暗沟:相对于地面排水的明沟而言,暗沟又称盲沟,具有隐蔽..内容来自www.zgxue.com请勿采集。


  • 本文相关:
  • css sticky footer实现代码
  • css实现sticky footer的示例代码
  • 详解css经典布局之sticky footer布局
  • 详解sticky footer 绝对底部的两种套路
  • css sticky footer经典布局的实现
  • 基坑支护的英语文章
  • 网站首页网页制作脚本下载服务器操作系统网站运营平面设计媒体动画电脑基础硬件教程网络安全主页网页制作csscss教程cssstickycss sticky footer经典布局的实现css教程css3div+css教程web标准教程浏览器兼容教程css布局实例css控制ul li 的样式详解(推荐)html设置超链接字体颜色和点击后的字体颜色div水平垂直居中的完美解决方案css设置各种中文字体如雅黑、黑体、宋体、楷体等等css 文本字体颜色设置方法(css color)css 漂亮搜索框美化代码css自定义select下拉选择框的样式(不用其他标签模拟)css圆角效果 -webkit-border-radius(css3中border-radcss 中的background:transparent到底是什么意思有什么css 首行缩进两个文字css 实现渐变效果小结( linear-gradient线性渐变 和 r解决type=file 文件修改表单 名称不能正常回显的问题移动端前端适配方案(总结)css 横向进度条和竖向进度条实现代码css运用阿里巴巴矢量库快速在对应位置加上好看的图标详解盒模型大小取决于它的padding,margin,border数css+svg实现b站充电效果的示例代码css动画实现领积分效果的思路详解html/css中的空格处理及如何保留页面中的空格css实现气泡的小尖角效果
    免责声明 - 关于我们 - 联系我们 - 广告联系 - 友情链接 - 帮助中心 - 频道导航
    Copyright © 2017 www.zgxue.com All Rights Reserved