作为一个初级的前端工程师,在开发的过程中遇到了许多问题,其中使元素垂直居中这个问题难住了我,可能在大家看来这是一个非常小的问题,但是却困扰了我很长时间,于是决定做一个总结!!!

废话不多说,直接上代码,里面有我的思考过程

案例一

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>div实现垂直居中</title>
</head>
<style>
.abc {
width: 200px;
height: 200px;
background: green;
position: absolute;
left: ;
top: ;
bottom: ;
right: ;
margin: auto;
} .box {
position: relative;
width: 500px;
height: 500px;
background: red;
display: inline-block;
}
</style>
<div class="box">
<div class="abc"> </div>
</div> <body>
</body> </html>

案例二(文字的水平垂直居中)

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>Document</title>
</head> <body>
<style>
div {
height: 300px;
width: 200px;
display: table;
background: #;
} span {
display: table-cell;
vertical-align: middle;
}
</style>
<div>
<span>我是span</span>
</div>
</body> </html>

案例三

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>div实现垂直居中</title>
</head>
<style>
.abc {
width: 200px;
height: 200px;
background: green;
position: absolute;
left: %;
top: %;
margin-left: -100px;
margin-top: -100px;
} .box {
position: relative;
width: 500px;
height: 500px;
background: red;
display: inline-block;
}
</style>
<div class="box">
<div class="abc"> </div>
</div> <body>
</body> </html>

案例四

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>div实现垂直居中</title>
</head>
<style>
/*
table-cell实现居中
将父盒子设置为table-cell元素,设置
text-align:center,vertical-align: middle;
子盒子设置为inline-block元素
*/ .ok {
width: 200px;
height: 200px;
background: red;
display: table-cell;
/*这个必须是table-cell*/
/*父级是一个小表格,表格默认是放文字的,子集是一个小果冻元素,给父级设置vertical-align:middle使元素垂直居中*/
text-align: center;
vertical-align: middle;
} .innerBox {
width: 100px;
height: 100px;
background: green;
display: inline-block;
/*注意:里面的元素必须是inline-block*/
}
</style> <body>
<div class="ok">
<div class="innerBox">
</div>
</div>
</body> </html>

案例五

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>Document</title>
</head> <body>
<style>
/* 注意:该方法值适合文字的水平垂直居中;
* 父级高度固定,嵌套行内元素
*关键属性:父级:diaplay:tabel; 子集:display:tabel-cell; vertical-align:middle;
*/ .div {
height: 300px;
width: 200px;
display: table;
/*这么理解:父级是一个固定宽度高度的大表格*/
background: #;
} .span {
display: table-cell;
/* 子集是父级表格里面的一个小格,设置display:table-cell,给父级设置垂直居中*/
vertical-align: middle;
}
</style>
<div class="div">
<div class="span">sddddd</div>
</div>
</body> </html>

【css基础修炼之路】— 谈谈元素的垂直水平居中的更多相关文章

  1. 【CSS基础】实现 div 里的内容垂直水平居中

    方案一: 所有内容垂直水平居中 兼容性:IE8+. 条件:内容宽度和高度可以未知,可以是多行文字.图片.div. 描述:使用table-cell + vertical-align , html代码见文 ...

  2. HTML&CSS基础-内联和块元素

    HTML&CSS基础-内联和块元素 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.html源代码 <!DOCTYPE html> <html> ...

  3. js实现未知宽高的元素在指定元素中垂直水平居中

    js实现未知宽高的元素在指定元素中垂直水平居中:本章节介绍一下如何实现未知宽高的元素在指定元素下实现垂直水平居中效果,下面就以span元素为例子,介绍一下如何实现span元素在div中实现水平垂直居中 ...

  4. css中各种情况下的元素的垂直和水平居中的问题

    问题:外边是一个容器,容器中还有一个容器,那么请问怎么让里边的容器垂直水平居中显示?? No1: 外边的容器宽度和高度确认,里边是行内元素 .container{width:200px; height ...

  5. CSS样式—— 字体、元素的垂直水平居中

    1.CSS样式与HTML中标签属性的区别: 标签的属性是采用 属性名=“属性值” 表示的 CSS样式是采用名值对 属性名:属性值: 表示的 2.内联元素(行内元素)与块元素 (1)内联元素及其特点: ...

  6. 探究css中各种情况下的元素的垂直和水平居中的问题(面试题)

    今天各种纠结,真的是不想写东西(ps 我比较懒)但是老是有人问这个问题,于是我就本着分享精神还是整理一下,好了废话不多说 开始上代码 问题:外边是一个容器,容器中还有一个容器,那么请问怎么让里边的容器 ...

  7. css盒模型与bfc与布局与垂直水平居中与css设计模式等

    一.css盒子与布局相关 盒子内部的布局 盒子之间的布局visual formatting 脱离正常流normal flow的盒子的布局 absolute布局上下文下的布局 float布局上下文下的布 ...

  8. 【js基础修炼之路】- 手把手教你实现bind

    手写bind前我们先回顾一下bind有哪些特性,以便更好的理解bind和实现bind. bind的特性 var obj = { a: 100, say(one, two) { console.log( ...

  9. 【js基础修炼之路】- 微任务,宏任务和Event-Loop

    一段代码让你了解Event-Loop console.log(1); setTimeout(() => { console.log(2); }, 0); new Promise((resolve ...

随机推荐

  1. HDU3555 区间的数里面有49的个数(数位dp)

    题目:区间的数里面有49的个数 分析: dp[pos][0]:长度为pos的数中,不包含49的,前一位不为4的有多少个:dp[pos][1]:长度为pos的数中,不包含49的,前一位为4的有多少个:d ...

  2. Codeforces Round #482 (Div. 2) C 、 Kuro and Walking Route(dfs)979C

    题目链接:http://codeforces.com/contest/979/problem/C 大致题意 给出n个点,有n-1个边将他们链接.给出x,y,当某一路径中出现x....y时,此路不通.路 ...

  3. Newtonsoft.Json 自定义序列化格式转化器

    public static class JsonHelper { static JsonHelper() { Newtonsoft.Json.JsonSerializerSettings settin ...

  4. 06-图2 Saving James Bond - Easy Version (25 分)

    This time let us consider the situation in the movie "Live and Let Die" in which James Bon ...

  5. Git for Linux and Windows

    1.在liunx中安装 1.1yum安装 [root@node1 ~]# yum install git –y [root@node1 ~]# git version git version 1.8. ...

  6. Go语言基础之5--数组(array)和切片(slince)

    一.数组(array) 1.1 数组定义 1)含义: 数组是同一类型的元素集合. 数组是具有固定长度并拥有零个或者多个相同数据类型元素的序列. 2)定义一个数组的方法: var 变量名[len] ty ...

  7. hive复杂格式array,map,struct使用

    -- 创建数据库表,以array作为数据类型 drop table if exists person; create table person( name string ,work_locations ...

  8. vue 中使用driver.js来进行页面分步引导

    Driver.js 推荐15款最佳的 jQuery 分步引导插件 11 个超棒的 jQuery 分步指引插件

  9. java编程--04比较几个常用的日期时间相关类的区别

    第一篇,介绍日期的比较 第二篇,介绍日期的格式化 第三篇,介绍关于日期常用的计算 第四篇,比较几个常用的日期时间相关类的区别 第五篇,jdk9对日期类进行了更新,写一些i自己的学习心得. 下面以一组思 ...

  10. Quadtrees UVA - 297

    题目链接:https://vjudge.net/problem/UVA-297 题目大意:如上图所示,可以用一个四分树来表示一个黑白图像,方法是用根节点表示整副图像,然后把行列各等分两等分,按照图中的 ...