常见的div布局面试题
题目1:如何让一个子元素在父元素里水平垂直居中?
方法1
.box{width:400px;height:400px;background:#ccc;position:relative;}
.child{width:50px;height:50px;position: absolute;left:50%;top:50%;margin-left:-25px;margin-top:-25px;background:red;}
<div class="box">
<div class="child"></div>
</div>
方法2:
.box{width:400px;height:400px;background:#ccc;position:relative;}
.child{width:50px;height:50px;margin:auto;overflow:auto;position: absolute;left:0;top:0;right:0;bottom:0;background:blue;}
<div class="box">
<div class="child"></div>
</div>
方法3:
.box{width:400px;height:400px;background:#ccc;position:relative;}
.child{width:50px;height:50px;margin:auto;position: absolute;left:50%;top:50%;transform:translate(-50%,-50%);-webkit-transform:translate(-50%,-50%);-ms-transform:translate(-50%,-50%);background:pink;}
<div class="box">
<div class="child"></div>
</div>
方法4:
.box{width:400px;height:400px;background:#ccc;display: table-cell;text-align: center;vertical-align: middle;}
.child{width:50px;height:50px;display:inline-block;background:red;}
<div class="box">
<div class="child"></div>
</div>
方法5:
.box{width:400px;height:400px;background:#ccc;display: flex;justify-content:center;align-items:center;}
.child{width:50px;height:50px;background:red;}
<div class="box">
<div class="child"></div>
</div>
题目2:高度已知,分为三栏,左右各300px,中间自适应?
方法1:浮动布局
.box>div{height:100px;}
.box .left{width:300px;background:red;}
.box .center{background:pink;}
.box .right{width:300px;background:blue;}
<div class="box">
<div class="left"></div>
<div class="right"></div>
<div class="center">中间栏:浮动布局</div>
</div>
方法2:定位布局
.box{position:relative;}
.box>div{height:100px;position:absolute;}
.box .left{left:0;top:0;width:300px;background:red;}
.box .center{left:300px;top:0;right:300px;background:pink;}
.box .right{top:0;right:0;width:300px;background:blue;}
<div class="box">
<div class="left"></div>
<div class="right"></div>
<div class="center">中间栏:定位布局</div>
</div>
方法3:flex布局
.box{display: flex;}
.box>div{height:100px;position:absolute;}
.box .left{width:300px;background:red;}
.box .center{flex:1;background:pink;}
.box .right{width:300px;background:blue;}
<div class="box">
<div class="left"></div>
<div class="right"></div>
<div class="center">中间栏:flex布局</div>
</div>
方法4:表格布局
.box{display: table; width:100%;}
.box>div{display: table-cell;height:100px;}
.box .left{width:300px;background:red;}
.box .center{background:pink;}
.box .right{width:300px;background:blue;}
<div class="box">
<div class="left"></div>
<div class="right"></div>
<div class="center">中间栏:table布局</div>
</div>
方法5:网格布局
.box{display: grid;grid-template-rows:100px;grid-template-columns:300px auto 300px;}
.box>div{height:100px;}
.box .left{background:red;}
.box .center{background:pink;}
.box .right{background:blue;}
<div class="box">
<div class="left"></div>
<div class="right"></div>
<div class="center">中间栏:grid布局</div>
</div>
本人正在不断地学习摸索中,如有错误,欢迎指正,如有不同的解题思路也欢迎指教~
常见的div布局面试题的更多相关文章
- 常见的div布局
1.一列固定宽度且居中 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- div 布局
转:http://blog.csdn.net/mercop/article/details/7882000 HTML CSS + DIV实现整体布局 1.技术目标: 开发符合W3C标准的Web页面 理 ...
- DIV布局之道一:DIV块的水平并排、垂直并排
DIV布局网页元素的方式主要有三种:平铺(并排).嵌套.覆盖(遮挡).本文先讲解平铺(并排)方式. 1.垂直平铺(垂直排列) 请看如下代码 CSS部分: CSS Code复制内容到剪贴板 .lay1{ ...
- CSS常见的五大布局
本文概要 本文将介绍如下几种常见的布局: 一.单列布局 常见的单列布局有两种: header,content 和 footer 等宽的单列布局 header 与 footer 等宽,content 略 ...
- 几种常见的CSS布局
本文概要 本文将介绍如下几种常见的布局: 其中实现三栏布局有多种方式,本文着重介绍圣杯布局和双飞翼布局.另外几种可以猛戳实现三栏布局的几种方法 一.单列布局 常见的单列布局有两种: header,co ...
- 10个常见的Node.js面试题
如果你希望找一份有关Node.js的工作,但又不知道从哪里入手评测自己对Node.js的掌握程度. 本文就为你罗列了10个常见的Node.js面试题,分别考察了Node.js编程相关的几个主要方面. ...
- 小div布局之卡片堆叠(card-stacking)
前端的页面布局和各种效果真是让人眼花缭乱,公司的设计师恨不得在一个网站上把前端的布局和样式效果都用一遍. 如何实现下面这种布局效果?我给这种布局效果起了个名字,叫做小div布局之卡片堆叠.然后我百度了 ...
- [MVC] DIV 布局
[MVC] DIV 布局 <style> .top { background-color: red; height: 50px; top: 0px; position: absolute; ...
- 【html】【8】div布局[子div在父div居底]
从今天起 开始细话div布局 思路及要点: 父div的位置设置成相对的,即“position: relative;”. 而子div的位置设置成绝对的,并且下边缘设为0,即“position: ab ...
随机推荐
- 右上角鼠标滑过展开收缩动画效果js代码的演示页面
http://files.cnblogs.com/files/tanlingdangan/top_right.rar.gz 右上角鼠标滑过展开收缩动画效果js代码的演示页面http://www.51x ...
- ubuntu gitlab服务器搭建
gitlab服务器搭建 1.安装依赖包 sudo apt-get install curl openssh-server ca-certificates postfix 执行完成后,出现邮件配置,选择 ...
- python的writelines读空行
在文件中,如果遇到一个空白行,readline()并不会返回一个空串,因为每一行的末尾还有一个或多个分隔符,因此“空白行”至少会有一个换行符或者系统使用的其他符号.只有当真的读到文件末尾时,才会读到空 ...
- 简易html5贪吃蛇
1. [图片] E6~0%QPA46ER843UQJ$0Z`H.jpg 2. [文件] snake.html <!DOCTYPE html><html><head> ...
- Quartz.Net初探
想必大家在工作中经常会遇到这样类似的需求,在某个时间或者需要不间断的执行某个业务动作去满足任务需求.例如,我们写了一个job,定时去处理一些任务,在没有了解到Quartz.Net之前,我是这样做的,进 ...
- HihoCoder1652 : 三角形面积和2([Offer收割]编程练习赛38)(几何)(不会几何,占位)
描述 如下图所示,在X轴上方一共有N个三角形.这些三角形的底边与X轴重合,底边上两个顶点的坐标分别是(Li, 0)和(Ri, 0),底边的对顶点坐标是(Xi, Yi).其中Li ≤ Xi ≤ Ri 且 ...
- Python连接Mysql数据库_20160928
python版本 2.7.1,python 连接mysql需要安装MYSQLdb模块 安装方法一种是cmd pip命令安装 pip install MySQLdb 一种是网上下载python MYSQ ...
- EntityFramework Code First 构建外键关系,数据库不生成外键约束
创建 ExtendedSqlGenerator类 public class ExtendedSqlGenerator : SqlServerMigrationSqlGenerator { #regio ...
- exosip 和 pjsip 简介
oSIP oSIP的开发开始于2000年7月,第一个版本在2001年5月发 布,到现在已经发展到3.x了.它采用ANSI C编写,而且结 构简单小巧,所以速度特别快,它并不提供高层的SIP会话 控制 ...
- 贪心+等价转化 HDU 1489
等价转换,题意简单来讲如下:在一条直线均匀分布N个村庄,每个村庄要么买酒,要么卖酒,且村庄的买酒和卖酒供需平衡,总和为0,把k个单位的酒从一个村庄运到相邻的村庄需要k个单位的劳动力,输出最小的劳动力. ...