當前位置:首頁>職場>css頁面布局面試題(CSS面試常見問題)
發布時間:2024-01-24閱讀(16)
本篇文章主要是總結幾個前端面試常見的CSS面試題,希望對大家的面試有所啟示。

一、CSS實現水平居中和垂直居中的方法有哪幾種?
1、水平居中
.son {display:inline-block;text-align:center;}
// 父布局的css 需要設置 overflow:hidden.father{width:100%;height:200px;overflow:hidden;//不可缺少否則margin-top不生效}.son{width: 100px;height: 100px;margin:50px auto ;}
display:table-cell; /*IE8以上及Chrome、Firefox*/vertical-align:middle; /*IE8以上及Chrome、Firefox*/
.father{display:flex;justity-content:center;align-items:center;}.father{display:flex;flex-direction:column;//改變主軸為cross axisjustity-content:center;}
.father{position:relative;width:60%;height:400px;}.son{width:100px;height:160px;position:absolute;left:50%;top:50%;margin-left:-50px;margin-top:-80px;}
2、垂直居中
.son { height:60px; line-height:60px; // line-height需要和height保持一致 display:inline-height;}
二、absolute與fixed共同點與不同點
absoluate和fixed都是position屬性的值類型。position規定元素的定位類型,取值類型如下:
fixed和absoluate相同點:
fixed和absoluate不同點:
三、清除浮動的方法
CSS中經常會出現使用了float浮動元素而導致頁面中某些元素不能正確地顯示。那么如何清除float的浮動帶來的副作用呢?
1、在父元素中添加子元素,并為添加的子元素中添加clear,清除浮動
clear的取值如下:
<style>.clearfix { clear: both; }</style><div > <div >content1</div> <div ></div></div><div > <div >content2</div></div>
2、偽元素清除clearfix
給父級元素添加了一個:after偽元素,通過清除偽元素的浮動,達到撐起父元素高度的目的。
.clearfix:after{ content:""; display:block; visibility:hidden; clear:both; }
3、使用BFC清除
通過給父元素設置:overflow:auto或者overflow:hidden。讓父元素高度被撐起來。
<style>.wrap{ width:500px; margin:0 auto; overflow:hidden;}.float{ width:200px; height:200px; float:left;}.nofloat{ width:300px; height:150px; overflow:hidden;}</style><div > <div >浮動</div> <div >清除浮動</div></div>
四、什么是BFC?
BFC的官方定義是:BFC(Block Formatting Context)塊格式化上下文, 是Web頁面的可視CSS渲染的一部分,是塊盒子的布局過程發生的區域,也是浮動元素與其他元素交互的區域。簡單點來說就是幫我們幫BFC內的元素和BFC外的元素進行隔離,使其不會影響到外部元素。
BFC類型:
五、CSS和sass、LESS有什么區別?
CSS(層疊樣式表)是一門非程序式語言,SASS是使用Ruby 編寫,是一種對css的一種擴展提升,增加了規則、變量、混入、選擇器、繼承等等特性。可以理解為用js的方式去書寫,然后編譯成css。LESS受Sass的影響較大,但又使用CSS的語法,讓大部分開發者和設計師更容易上手。簡單點說就是less、sass是屬于編譯型CSS,需要通過編譯最終生成CSS。區別如下:
六、rem 和 em 的區別?
em是一種相對長度單位,相對于自身元素的字號大小,如果沒有設置就參照父容器的字號大小或瀏覽器默認字號大小。rem是CSS3的新標準也是一種相對長度單位,其相對于html根標簽的字號大小。
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>html{ font-size: 20px;}#box{ width: 10rem; height: 10rem; background: red; font-size:12px;}.remFont { font-size:1rem;}.emFont { font-size:1em; }</style></head><body> <div id="box"> <div >字體大小20px</div> <div >字體大小12px</div> </div></body></html>
歡迎分享轉載→http://m.avcorse.com/read-234246.html
Copyright ? 2024 有趣生活 All Rights Reserve吉ICP備19000289號-5 TXT地圖HTML地圖XML地圖