首页 > 其他分享 >前端面试题之CSS布局问题

前端面试题之CSS布局问题

时间:2022-08-27 14:11:53浏览次数:45  
标签:面试题 color 前端 height width background 200px CSS left

垂直居中DIV
HTML部分

<div class="father">
<div class="son">我是垂直居中的div</div>
</div>

这里简单给出几种
1.绝对定位(盒子宽高已知)

.father {
position: relative;
width: 500px;
height: 500px;
background-color: red;
}

.son {
position: absolute;
left: 50%;
top: 50%;
margin-left: -150px;(-盒子一半宽度)
margin-top: -150px;(-盒子一半高度)
width: 300px;
height:300px;
background-color: blue;
}

2.绝对定位(宽高已知)

.father {
position: relative;
width: 500px;
height: 500px;
background-color: red;
}

.son{
position:absolute;
margin:auto;
top:0; left:0; bottom:0;right:0;
width: 300px;
height:300px;
background-color: blue;
}

3.定位 (宽高未知)

.son {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: blue;
}

4.grid布局(父元素设置,宽高未知)(兼容性ie 10以上支持)

.father {
display: grid;
align-items: center;
justify-content: center;
width: 500px;
height: 500px;
background-color: red;
}

5.flex布局(父元素设置,宽高未知)(兼容性ie 11以上支持)

.father {
display: flex;
align-items: center;
justify-content: center;
width: 500px;
height: 500px;
background-color: red;
}

6.表格布局(父元素设置,宽高未知)(兼容性较好)

.father {
display:table-cell
text-align: center;
vertical-align: middle
width: 500px;
height: 500px;
background-color: red;
}
.son {
display: inline-block;
}

两栏布局左边固定右边自适应
这与右边固定左边自适应,上固定下自适应是一个道理。

HTML部分

<div class="father">
<div class="left"></div>
<div class="right"></div>
</div>

1.float布局

.left {
width: 200px;
height: 200px;
float: left;
background-color: blue;
}
.right {
margin-left: 200px;
height: 200px;
background-color: red;
}

2.绝对定位

.father {
position: relative;
height: 200px;
}

.left {
position:absolute;
width: 200px;
height: 100%;
float: left;
background-color: blue;
}

.right {
position:absolute;
height: 100%;
left:200px;
right: 0;
background-color: red;
}


3.flex布局

.father {
height: 300px;
width: 100%;
display: flex;
}

.left {
width: 300px;
height: 100%;
background-color: blue;
}

.right {
flex: 1;
height: 100%;
background-color: red;
}


三栏布局左右固定中自适应
这与左中固定右边自适应,中右固定左边自适应,以及上下固定中间自适应是一个道理

HTML部分

<div class="father">
<div class="left"></div>
<div class="right"></div>
<div class="main"> </div>
</div>

1.float布局

.father{
height: 50px;
div{
height: 100%;
}
}

.left {
width: 200px;
float: left;
background-color: red
}

.main {
margin-left: 200px;
margin-right: 200px;
background-color: blue
}

.right {
float: right;
width: 200px;
background-color: yellow
}


2.绝对定位

.father{
position: relative;
height: 50px;
div{
position: absolute;
height: 100%;
}
}

.left {
left: 0;
width: 200px;
background-color: red
}

.main {
left: 200px;
right: 200px;
background-color: blue
}

.right {
right: 0;
width: 200px;
background-color: yellow
}


3.flex布局
HTML部分

<div class="father">
<div class="left"></div>
<div class="main"> </div>
<div class="right"></div>
</div>

CSS部分

.father {
display: flex;
height: 50px;
div{
height: 100%;
}
}

.left {
width: 200px;
background-color: red
}

.main {
flex: 1;
background-color: blue
}

.right {
width: 200px;
background-color: yellow
}
原文链接:https://blog.csdn.net/Jet_Lover/article/details/115646489

标签:面试题,color,前端,height,width,background,200px,CSS,left
From: https://www.cnblogs.com/vant-xie/p/16630463.html

相关文章

  • 消息队列面试题
             ......
  • 前端——HTML5
    HTML5(超文本标记语言)1.HTML5,在2014年10月由万维网联盟(W3C)完成标准制定。2. HTML5的设计目的是为了在移动设备上支持多媒体。新特性:新增了语义化标签新增了表单元......
  • mybatis面试题
    mybatis中#{}和${}的区别是什么?#{}是预编译处理,会将sql中的#{}替换为?号防止sql注入${}是字符串替换。当实体类中的属性名和表中的字段名不一样,怎么办?......
  • 面试题做错记录(开卷)
    #JavaScript是一门单线程的静态类型语言错,是动态类型语言#浏览器中的Cookie只能由服务端写入,并且每次网络请求会自动携带Cookiecookie可以在本地用js方法新......
  • 前端05
    目录DOM文档对象模型获取标签值操作JS类属性操作JS样式操作事件jQuery类库作业DOM文档对象模型获取标签值操作1.操作标签:input、select、textarea2.语法结构:查找到的标......
  • 【2022-08-26】python前端开发(五)
    python前端开发(五)JS获取值操作普通数据(输入、选择) 标签对象.value文件数据(上传) 标签对象.files 标签对象.files[0]leti1Ele=document.getElementById('d1......
  • 从前端获取数据发送到后端 存入数据库
    前端{%extends'home.html'%}{%blockcontant%}<h1class="center">添加图书</h1>{##method一定要加上post并先在setting里注掉中间件#}<formacti......
  • 前端项目实战22-前端处理二进制流
    constdownURL=window.URL.createObjectURL(newBlob([data])); //data为获取到的二进制数据 constlistNode=document.createElement("a"); //这里注意:非同......
  • 前端项目实战23-前端上传文件进行数据绑定用fileList
    <Uploadname="file"action="xxxx"onChange={handleChange}......
  • 前端基础
    前端基础HTML书写页面 CSS页面美化 JS页面运动HTML超文本标记语言(HyperTextMarkupLanguage)基本元素p段落标签,独占一行&nbsp网页上显示一个空格i文字......