惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
Cyberwarzone
Cyberwarzone
L
LINUX DO - 最新话题
N
News and Events Feed by Topic
T
Troy Hunt's Blog
Help Net Security
Help Net Security
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
M
MIT News - Artificial intelligence
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V2EX - 技术
V2EX - 技术
Y
Y Combinator Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
大猫的无限游戏
大猫的无限游戏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
T
Threatpost
Recent Commits to openclaw:main
Recent Commits to openclaw:main
S
SegmentFault 最新的问题
I
InfoQ
H
Hacker News: Front Page
D
Docker
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Blog — PlanetScale
Blog — PlanetScale
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
AWS News Blog
AWS News Blog
Know Your Adversary
Know Your Adversary
博客园 - 【当耐特】
T
Tor Project blog
U
Unit 42
H
Heimdal Security Blog
Microsoft Azure Blog
Microsoft Azure Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
P
Privacy & Cybersecurity Law Blog
PCI Perspectives
PCI Perspectives
美团技术团队
O
OpenAI News
T
Tailwind CSS Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog
GbyAI
GbyAI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
MyScale Blog
MyScale Blog

博客园 - feenan

AngularJS 源码分析3 AngularJS 源码分析2 AngularJS 源码分析1 javascript 中关于对象转换数字值的一些特点 转 CSS hack:针对IE6,IE7,firefox显示不同效果 CSS中一些不经意的细节问题1 (转)雅虎WEB前端网站优化 -- 34条军规 一个比较常用的关于php下的mysql数据操作类 Js中的一个日期处理格式化函数 Underscore.js 1.3.3 源码分析收藏 Backbone.js 0.9.2 源码分析收藏 Nodejs实现的一个静态服务器例子 Javascript MVC学习杂记3 Javascript MVC学习杂记2 Javascript MVC学习杂记1 C语言实现的一个简单的HTTP程序 C语言string.h中常用字符函数介绍 通过日期生成星期几 Javascript 模块化编程
CSS关于元素垂直居中的问题
feenan · 2013-11-06 · via 博客园 - feenan

   今天碰到了一个问题,给一个父容器和一个子元素,子元素不定高和不定宽,怎么让子元素居中在父容器中,比如下段代码

方法1:

<div class="div1">
    <div class="div2">
      <p>this is a test!</p>
    </div>
</div>  

保证div2居中在div1中,想了下,CSS代码如下

    *{
         margin: 0;padding: 0;
       }
       .div1{
          padding:20px 100px;
          margin: 20px;
          height: 600px;    
          width: 500px;    
          text-align: center;
          border: 1px solid #ccc;
       }
       .div1:before{
         content: ".";
         height: 100%;
         display: inline-block;
         vertical-align: middle;
         visibility: hidden;
       }
       .div2{
          border: 1px solid gray;
          display: inline-block;
          vertical-align: middle;
       }

可以利用 vertical-align:middle属性保证垂直居中,和父容器的text-align:center来保存水平居中,前面一个属性只能对display:inline-block有效,而且需要参照物,所以用

:before伪元素来实现。

方法2: 

<div class="div1">
   <div class="content">
        <img src="" alt="">
   </div>
</div>

实现上面的图片内容居中,可以用父容器的line-height来实现

.div1{
   margin:20px;
   line-height:500px;
   text-align:center;
}

.content{
   display:inline-block;
   vertical-align:middle;
   line-height:normal;
}

.content img{
   max-width:100px;
}

上面的方法也可以实现不定高宽的内容居中,而且里面还可以加别的元素。