题目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布局面试题的更多相关文章

  1. 常见的div布局

    1.一列固定宽度且居中 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  2. div 布局

    转:http://blog.csdn.net/mercop/article/details/7882000 HTML CSS + DIV实现整体布局 1.技术目标: 开发符合W3C标准的Web页面 理 ...

  3. DIV布局之道一:DIV块的水平并排、垂直并排

    DIV布局网页元素的方式主要有三种:平铺(并排).嵌套.覆盖(遮挡).本文先讲解平铺(并排)方式. 1.垂直平铺(垂直排列) 请看如下代码 CSS部分: CSS Code复制内容到剪贴板 .lay1{ ...

  4. CSS常见的五大布局

    本文概要 本文将介绍如下几种常见的布局: 一.单列布局 常见的单列布局有两种: header,content 和 footer 等宽的单列布局 header 与 footer 等宽,content 略 ...

  5. 几种常见的CSS布局

    本文概要 本文将介绍如下几种常见的布局: 其中实现三栏布局有多种方式,本文着重介绍圣杯布局和双飞翼布局.另外几种可以猛戳实现三栏布局的几种方法 一.单列布局 常见的单列布局有两种: header,co ...

  6. 10个常见的Node.js面试题

    如果你希望找一份有关Node.js的工作,但又不知道从哪里入手评测自己对Node.js的掌握程度. 本文就为你罗列了10个常见的Node.js面试题,分别考察了Node.js编程相关的几个主要方面. ...

  7. 小div布局之卡片堆叠(card-stacking)

    前端的页面布局和各种效果真是让人眼花缭乱,公司的设计师恨不得在一个网站上把前端的布局和样式效果都用一遍. 如何实现下面这种布局效果?我给这种布局效果起了个名字,叫做小div布局之卡片堆叠.然后我百度了 ...

  8. [MVC] DIV 布局

    [MVC] DIV 布局 <style> .top { background-color: red; height: 50px; top: 0px; position: absolute; ...

  9. 【html】【8】div布局[子div在父div居底]

    从今天起 开始细话div布局   思路及要点: 父div的位置设置成相对的,即“position: relative;”. 而子div的位置设置成绝对的,并且下边缘设为0,即“position: ab ...

随机推荐

  1. 简单通俗解释内外网IP与端口映射

    IP:分为外网IP和内网IP 也就是我们说的外网IP属于实体IP 实体IP,它是独一无二的,在网络的世界里,每一部计算机的都有他的位置,一个 IP 就好似一个门牌!例如,你要去百度的网站的话,就要去『 ...

  2. Linux学习之路(二)文件处理命令之下

    分区格式化: 一块分区想要使用的话,要格式化.格式化主要有两个工作,1,把分区分成等大小的数据块,每个数据块一般为4KB.2在分区之前建一个分区表,给第一个文件建一行相关数据,在分区表里保存了它的io ...

  3. BZOJ 3709 [PA2014]Bohater:贪心【反过来考虑】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3709 题意: 在一款电脑游戏中,你需要打败n只怪物(从1到n编号). 为了打败第i只怪物, ...

  4. linux 进程学习笔记-共享内存

    如果能划定一块物理内存,让多个进程都能将该内存映射到其自身虚拟内存空间的话,那么进程可以通过向这块内存空间读写数据而达到通信的目的.另外,和消息队列不同的是,共享的内存在用户空间而不是核空间,那么就不 ...

  5. poj1456 Supermarket[另类的并查集做法]

    1.Supermarket(题目地址) 跟很久以前模拟的打地鼠那题一样,贪心+优先队列.这次换用并查集做法. 还是基于贪心,但这次换一种策略,先选价值最大的, 同时使其尽可能晚的被选上(因为早选会将之 ...

  6. 洛谷P1525关押罪犯——并查集

    题目:https://www.luogu.org/problemnew/show/P1525 并查集+贪心,从大到小排序,将二人分在不同房间,找到第一个不满足的即为答案. 代码如下: #include ...

  7. 洛谷P1144——最短路计数

    题目:https://www.luogu.org/problemnew/show/P1144 spfa跑最短路的同时记录cnt数组表示到达方案数. 代码如下: #include<iostream ...

  8. 中国移动推出NB-IoT/eMTC/GSM多模通信模组Qualcomm调制解调器支持

    亚洲电子消费展(CES Asia)在上海举行.期间,中国移动正式推出NB-IoT/eMTC/GSM三模通信模组A9500.该通信模组采用Qualcomm MDM9206 LTE IoT调制解调器,具有 ...

  9. Spring框架中的@Import、@ImportResource注解

    spring@Import @Import注解在4.2之前只支持导入配置类 在4.2,@Import注解支持导入普通的java类,并将其声明成一个bean 使用场景: import注解主要用在基于ja ...

  10. 我对sobel算子的理解

    转自:http://blog.csdn.net/yanmy2012/article/details/8110316 索贝尔算子(Sobeloperator)主要用作边缘检测,在技术上,它是一离散性差分 ...