1. div高度自适应的情况

div在不设置高度的时候,会被里面的内容撑开,内容自动填充在div中,无论是一行内容还是多行内容,此时不需要设置垂直居中,内容自动在中间的,

想要看的更直观些,只需要加上padding元素,内容四周便会留下空白,实现水平垂直居中的效果

.demo{
width: 200px;
border: 1px solid red;
padding: 20px;
}
<div class="demo">
this is a test of margin
this is a test of margin
this is a test of margin
this is a test of margin
this is a test of margin
</div>

2.div设置具体高度

(1)内容只有一行

设置div的line-height和div的高度一样即可,这个大家都知道哒

(2)内容不确定有几行

这时候需要在div中再加一层结构,用p标签或者div都可以

*方法一:

.demo{
position: absolute;
width: 200px;
height: 200px;
border: 1px solid red;
}
p{
position: absolute;
width: 150px;
top: 50%;
left:50%;
transform: translate(-50%,-50%);
border: 1px solid black;
} <div class="demo">
<p>
this is a test of margin
this is a test of margin
this is a test of margin
this is a test of margin
</p>
</div>

*方法二:若是不想用position:absolute这样的脱离文档流的样式,那就可以尝试模拟表格的方法设置父元素display:table,设置子元素display:table-cell,并设置vertical-align:middle即可

.demo{
width: 200px;
height: 200px;
display: table;
border: 1px solid red;
}
p{
display: table-cell;
vertical-align: middle;
text-align: center;
border: 1px solid black;
}
<div class="demo">
<p>
this is a test of margin
this is a test of margin
this is a test of margin
this is a test of margin
</p>
</div>

此时子元素设置宽度是没用的,宽度始终和父元素一致;

但是如果子元素设置高度的话,若是高度小于父元素则无效果,若是高度大于父元素则父元素的高度也相应增加到和子元素一样的高度

*方法三:使用css3新增的flex布局完成,设置父元素display:box; box-pack:center; box-orient:vertical;即可,记得要在前面加上浏览器前缀哦

.box{
width: 200px;
height: 200px;
border: 1px solid red;
display: box;
box-pack:center;
box-orient:vertical;
display: -webkit-box;
-webkit-box-pack:center;
-webkit-box-orient:vertical;
} <div class="box">
<div>
this is a test
this is a test
this is a test
</div>
<div>
this is another test for the second div
</div>
</div>

div中的“内容”水平垂直居中的更多相关文章

  1. div中的内容水平垂直居中

    1. div高度自适应的情况 div在不设置高度的时候,会被里面的内容撑开,内容自动填充在div中,无论是一行内容还是多行内容,此时不需要设置垂直居中,内容自动在中间的, 想要看的更直观些,只需要加上 ...

  2. 让DIV中的内容水平和垂直居中

    让一个层水平垂直居中是一个非常常见的布局方式,但在html中水平居中使用margin:0px auto;可以实现,但垂直居中使用外边距是无法达到效果的.(页面设置height:100%;是无效的),这 ...

  3. [转]如何让div中的内容垂直居中

    转自:http://blog.163.com/yan_1990/blog/static/197805107201211311515454/ 虽然Div布局已经基本上取代了表格布局,但表格布局和Div布 ...

  4. 如何让div中的内容垂直居中

    虽然Div布局已经基本上取代了表格布局,但表格布局和Div布局仍然各有千秋,互有长处.比如表格布局中的垂直居中就是Div布局的一大弱项,不过好在千变万化的CSS可以灵活运用,可以制作出准垂直居中效果, ...

  5. div中的内容居中

    要使div中的内容居中显示,不仅div要设定“text-align:centr"  ,内置对象要添加margin:auto;属性才能使其在firefox等其他浏览器中也能居中.

  6. div中让内容能不换行就尽量不换行.【纯原】

    div中让内容能不换行就尽量不换行,部分左对齐,部分右对齐. <html> <head> <title>九歌·少司命</title> <style ...

  7. 怎样推断DIV中的内容为空

    怎样推断DIV中的内容为空 1.问题背景 推断div内部是否为空.假设为空,给出无数据提示:否则显示正常页面 2.设计源代码 <!DOCTYPE html PUBLIC "-//W3C ...

  8. div中的内容垂直居中的五种方法

    一.行高(line-height)法 如果要垂直居中的只有一行或几个文字,那它的制作最为简单,只要让文字的行高和容器的高度相同即可,比如: p { height:30px; line-height:3 ...

  9. html中div使用CSS实现水平/垂直居中的多种方式

    CSS中的居中,在工作中,会经常遇到.它可以分为水平居中和垂直居中,以下是几种实现居中的方式. git 查看源码 配合在线预览,效果更佳 以下例子中,涉及到的CSS属性值. .parent-frame ...

随机推荐

  1. Unable to resolve dependency for ':app@debug/compileClasspath' could not resolve com.android.support:design:28.0.0

    使用AndroidStudio3.2报这个错 配置 解决方法 1)去掉代理 gradle目录的下代理属性也 注销掉.   2)项目的gradle设定 3)设定项目的gradle-wrapper.pro ...

  2. mysql/mongodb监控之Percona Monitoring and Management (PMM) 2.1.0安装使用

    Percona Monitoring and Management (PMM)是Percona Server一款开源的用于管理和监控MySQL和MongoDB性能的开源平台,通过PMM客户端收集到的D ...

  3. 使用hdfs-mount挂载HDFS

    目录 1.特性(计划)简介 2.构建程序 3.使用hdfs-mount挂载HDFS hdfs-mount是一个将HDFS挂载为本地Linux文件系统的工具,使用go语言开发,不依赖libdfs和jav ...

  4. mysql5.7的主从切换

    mysql5.7的主从切换 主库: master 10.11.0.211 从库: slave 10.11.0.210 目标:主从切换,将slave切换为master,master切换为slave 一. ...

  5. [译]在Python中,如何拆分字符串并保留分隔符?

    原文来源:https://stackoverflow.com/questions/2136556/in-python-how-do-i-split-a-string-and-keep-the-sepa ...

  6. Python - Django - form 组件动态从数据库取 choices 数据

    app01/models.py: from django.db import models class UserInfo(models.Model): username = models.CharFi ...

  7. 对Dev的GridControl/GridView控件进行分组并展开操作

    今天在模块编写中碰到了对表格的分组,特意在这里把它记录下来. 一.背景:Dev14.1.3,GridControl,.NET4.0+C# 二.过程 1.GridControl设计 一共添加4列:在下面 ...

  8. [LeetCode] 901. Online Stock Span 线上股票跨度

    Write a class StockSpanner which collects daily price quotes for some stock, and returns the span of ...

  9. 【Spring Cloud学习之六】断路器-Hystrix

    环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 Spring Cloud 1.2 一.服务雪崩1.什么是服务雪崩分布式系统中经常会出现某个基础服务不可用造成整个系统不 ...

  10. python 的技巧

    pi = 0 n = 100 for k in range(n): pi+=1/pow(16,k)*(\ #一行不够写或不易读时用\,则多行与一行一样 4/(8*k+1)-2/(8*k+4)-\ 1/ ...