前端面试题之CSS布局问题

前端面试题之CSS布局问题前端 CSS 布局问题垂直居中 DIV 两栏布局左边固定右边自适应三栏布局左右固定中自适应垂直居中 DIVHTML 部分 divclass father divclass son 我是垂直居中的 div 这里简单给出几种 1 绝对定位 盒子宽高已知 father position relative width 500 divclass son divclass father

垂直居中DIV

HTML部分

 <div class="father"> <div class="son">我是垂直居中的div</div> </div> 
 .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://javaforall.net/223768.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午1:26
下一篇 2026年3月17日 下午1:27


相关推荐

  • C++this指针

    C++this指针1)以下说法不正确的是:(括号内为个人理解) A.this指针就是指向成员函数所作用的对象的指针 B.每个对象的空间中都存放着一个this指针 C.类的非静态成员函数,真实的参数比所写的参数多1(多一个this指针) D.静态成员函数中不能使用this指针(因为static函数不属于某个对象) this指针是类的一个自动生成…

    2022年5月17日
    59
  • 腾讯官宣赞助 OpenClaw:我见证了全过程

    腾讯官宣赞助 OpenClaw:我见证了全过程

    2026年3月16日
    2
  • 智能体工具构建与集成实战:以HuggingFace Agents-Course项目为例

    智能体工具构建与集成实战:以HuggingFace Agents-Course项目为例

    2026年3月15日
    2
  • 阿里正式发布新一代基模千问3.5

    阿里正式发布新一代基模千问3.5

    2026年3月13日
    2
  • element-ui中表格获取当前行的索引index[通俗易懂]

    element-ui中表格获取当前行的索引index[通俗易懂]前言弄文件上传时,需要对上传列表的文件进行一定的操作,例如暂停/取消等等,因为我是使用element-ui中表格展示上传文件列表的,这时的操作却需要使用到当前行的索引下,如何获取索引就是我接下来要做的工作了:获取当前行的索引index使用scope.$index,scope.row即可实现获取索引<el-table-columnlabel=”排序”min-width=”100″><templateslot-scope=”scope”>{{sco

    2025年8月29日
    7
  • BEAM188能做屈曲分析吗

    BEAM188能做屈曲分析吗请问高手 BEAM188 189 能做长杆的屈曲分析吗 nbsp 特别要注意的是 一个非收敛的解 并不意味着结构达到了其最大载荷 它也可能是由于数值不稳定引起的 这可以通过细化模型的方法来修正 跟踪结构响应的载荷 变形历程 可以确定一个非收敛的载荷步 到底是表示了一个实际的结构屈曲 还是反映了其它问题 用户可以先用弧长法 ARCLEN 命令来进行一个预分析 以预测屈曲载荷 近似值 将此近似值

    2026年3月19日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号