面試 01-頁面佈局
阿新 • • 發佈:2020-12-15
01-頁面佈局
#前端面試(前言)
#面試基礎
-
頁面佈局
-
CSS盒模型:是CSS的基石。
-
DOM事件
-
HTTP協議
-
面向物件
-
原型鏈:能說出原型鏈的始末
#面試進階
-
通訊:普通的通訊、跨域通訊
-
安全:CSRF、XSS。
-
演算法
#回答問題時要注意的
(1)題乾的要求真的是字面要求的這麼簡單嗎?
(2)答案怎麼寫,技巧在哪裡
(3)如果想證明我的實力,應該有幾種答案?
本文來講一下頁面佈局。
#題目:頁面佈局
問題:假設容器的高度預設100px,請寫出三欄佈局,其中左欄、右欄的寬度各為300px,中間的寬度自適應。
分析:
初學者想到的答案有兩種:
-
方法1:浮動
-
方法2:絕對定位。
但要求你能至少寫出三四種方法,才算及格。剩下的方法如下:
-
方法3:flexbox。移動開發裡經常用到。
-
方法4:表格佈局 table。雖然已經淘汰了,但也應該瞭解。
-
方法5:網格佈局 grid。
下面分別講解。
#方法1 和方法2
方法1、浮動:
左側設定左浮動,右側設定右浮動即可,中間會自動地自適應。
方法2、絕對定位:
左側設定為絕對定位, left:0px。右側設定為絕對定位, right:0px。中間設定為絕對定位,left 和right 都為300px,即可。中間的寬度會自適應。
使用article
標籤作為容器,包裹左、中、右三個部分。
方法1 和方法2 的程式碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
html * {
padding: 0px;
margin: 0px;
}
.layout {
margin-bottom: 150px;
}
.layout article div { /*注意,這裡是設定每個小塊兒的高度為100px,而不是設定大容器的高度。大容器的高度要符合響應式*/
height: 100px;
}
/* 方法一 start */
.layout.float .left {
float: left;
width: 300px;
background: red;
}
.layout.float .right {
float: right;
width: 300px;
background: blue;
}
.layout.float .center {
background: green;
}
/* 方法一 end */
/* 方法二 start */
.layout.absolute .left-center-right {
position: relative;
}
.layout.absolute .left {
position: absolute;
left: 0;
width: 300px;
background: red;
}
/* 【重要】中間的區域,左側定位300px,右側定位為300px,即可完成。寬度會自使用 */
.layout.absolute .center {
position: absolute;
left: 300px;
right: 300px;
background: green;
}
.layout.absolute .right {
position: absolute;
right: 0;
width: 300px;
background: blue;
}
/* 方法二 end */
</style>
</head>
<body>
<!-- 方法一:浮動 start -->
<!-- 輸入 section.layout.float,即可生成 -->
<section class="layout float">
<!-- 用 article 標籤包裹左、中、右三個部分 -->
<article class="left-right-center">
<!-- 輸入 div.left+div.right+div.center,即可生成 -->
<div class="left">
我是 left
</div>
<div class="right">
我是 right
</div>
<div class="center">
浮動解決方案
我是 center
</div>
</article>
</section>
<!-- 方法一:浮動 end -->
<section class="layout absolute">
<article class="left-center-right">