Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
天學(xué)會(huì)html+css,第九天固定定位。
Redmi手機(jī)電視筆記本。
今天的學(xué)習(xí)目標(biāo)是右側(cè)懸浮工具欄用固定定位實(shí)現(xiàn),它是相對(duì)于瀏覽器窗口的定位方式。
·盒子里的內(nèi)容用a標(biāo)簽,一個(gè)圖片加一行文字,此時(shí)它的位置在最底部。
·然后給它寫上固定定位樣式,右側(cè)距離0,下面距離70像素,加上背景顏色,看下效果。
·開始給a標(biāo)簽寫樣式,固定寬高,text-renderin默認(rèn)下劃線去掉,里面內(nèi)容居中,看下效果。
·圖片寫樣式之前也要加上這行代碼,然后讓它的尺寸變小一點(diǎn),并且左右居中,看下效果。
·文字的顏色、大小也調(diào)整一下。
·最后給a標(biāo)簽加上邊框、內(nèi)邊距,讓里面內(nèi)容往下挪一挪。
到此,今天的學(xué)習(xí)完成。
為教程,還是先科(啰)普(嗦)一下,然后再進(jìn)入正題。
CSS3 的 calc() 函數(shù)允許我們?cè)趯傩灾抵袌?zhí)行數(shù)學(xué)計(jì)算操作,它支持"+", "-", "*", "/" 運(yùn)算,遵循標(biāo)準(zhǔn)的數(shù)學(xué)運(yùn)算優(yōu)先級(jí)規(guī)則。例如,我們可以使用 calc() 指定一個(gè)元素的寬度總是比它的父元素寬度小 50px。
.foo {
width: calc(100% - 50px);
}
這意味著瀏覽器中的值可以更加靈活,能夠響應(yīng)視口的改變,用在流體布局上簡(jiǎn)直就是如虎添翼。calc是英文單詞calculate(計(jì)算)的縮寫,是css3的一個(gè)新增的功能。
特別需要注意的是:運(yùn)算符前后都需要保留一個(gè)空格,例如:width: calc(100% - 10px);
可以說,calc函數(shù)已經(jīng)得到了瀏覽器廠商的普遍支持,如下圖所示。
clac() 已經(jīng)得到普遍支持
對(duì)于不支持 calc() 的瀏覽器,整個(gè)屬性值表達(dá)式將被忽略。不過我們可以對(duì)那些不支持 calc()函數(shù)的瀏覽器,使用一個(gè)固定值作為回退。
.foo {
width: 90%; /* Fallback for older browsers */
width: calc(100% - 50px);
}
需求:比如,我們經(jīng)常需要固定一個(gè)操作面板在頁(yè)面底部,其它區(qū)域占滿屏幕剩余區(qū)域并隨視口變化而自適應(yīng)變化,且可以上下滾動(dòng)。
分析:我們能夠給要固定的元素設(shè)定一個(gè)高度,其值為視口的高度減去一個(gè)絕對(duì)值。那么我們可以做一個(gè)上下結(jié)構(gòu)的布局,上部為主區(qū)域,下部為底部區(qū)域。
HTML代碼:
<div id="main">主區(qū)域</div>
<div id="bottom">底部區(qū)域</div>
CSS代碼:
body {
margin: 0;
color: white;
text-align: center;
}
#main {
height: calc(100vh - 50px); /*視口高度 - 50px*/
overflow-y: auto;
background-color: blueviolet;
}
#bottom {
height: 50px;
background-color: black;
}
效果如下:無論窗口多大,底部始終保持50px高度,其余部分會(huì)隨著窗口變化而自適應(yīng)變化,當(dāng)主區(qū)域內(nèi)容很多時(shí),該區(qū)域會(huì)出現(xiàn)滾動(dòng)條。
【本文結(jié)束】
學(xué)習(xí)過程記錄,有需要的朋友可以參考。歡迎一鍵三連(點(diǎn)贊、關(guān)注、評(píng)論)。
絡(luò)發(fā)展到了今天,很多朋友對(duì)于網(wǎng)站已經(jīng)不陌生了,但是當(dāng)我們看網(wǎng)頁(yè)時(shí)你注意到網(wǎng)站的底部了嗎?雖然很少人會(huì)注意到他,但是如果不在底部的話,會(huì)很難看,今天小猿圈web前端講師就為你介紹網(wǎng)站頁(yè)面底部固定的方法。
方法一:footer高度固定+絕對(duì)定位
html
<div class="dui-container">
<header>Header</header>
<main>Content</main>
<footer>Footer</footer>
</div>
css
.dui-container{
position: relative;
min-height: 100%;
}
main {
padding-bottom: 100px;
}
header, footer{
line-height: 100px;
height: 100px;
}
footer{
width: 100%;
position: absolute;
bottom: 0
}
方法二:在主體content上的下邊距增加一個(gè)負(fù)值等于底部高度
html
<header>Header</header>
<main>Content</main>
<footer>Footer</footer>
css
html, body {
height: 100%;
}
main {
min-height: 100%;
padding-top: 100px;
padding-bottom: 100px;
margin-top: -100px;
margin-bottom: -100px;
}
header, footer{
line-height: 100px;
height: 100px;
}
方法三:將頁(yè)腳的margin-top設(shè)為負(fù)數(shù)
html
<header>Header</header>
<main>Content</main>
<footer>Footer</footer>
css
main {
min-height: 100%;
padding-top: 100px;
padding-bottom: 100px;
}
header, footer{
line-height: 100px;
height: 100px;
}
header{
margin-bottom: -100px;
}
footer{
margin-top: -100px;
}
方法四: 通過設(shè)置flex,將footer的margin-top設(shè)置為auto
html
<header>Header</header>
<main>Content</main>
<footer>Footer</footer>
css
body{
display: flex;
min-height: 100vh;
flex-direction: column;
}
header,footer{
line-height: 100px;
height: 100px;
}
footer{
margin-top: auto;
}
方法五: 通過函數(shù)calc()計(jì)算內(nèi)容的高度
html
<header>Header</header>
<main>Content</main>
<footer>Footer</footer>
css
main{
min-height: calc(100vh - 200px); /* 這個(gè)200px是header和footer的高度 */
}
header,footer{
height: 100px;
line-height: 100px;
}
方法六: 通過設(shè)置flexbox,將主體main設(shè)置為flex
html
<header>Header</header>
<main>Content</main>
<footer>Footer</footer>
css
body{
display: flex;
min-height: 100vh;
flex-direction: column;
}
main{
flex: 1
}
小猿圈提醒大家合理利用自己每一分每一秒的時(shí)間來學(xué)習(xí)提升自己,不要再用"沒有時(shí)間“來掩飾自己思想上的懶惰!趁年輕,使勁拼,給未來的自己一個(gè)交代!想學(xué)習(xí)IT編程類知識(shí)的可以到小猿圈網(wǎng)站去自學(xué),里面有最新最全的視頻以及專業(yè)的解答。
*請(qǐng)認(rèn)真填寫需求信息,我們會(huì)在24小時(shí)內(nèi)與您取得聯(lián)系。