<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>使当前对象相对于上层DIV 水平.垂直居中定位</title> <meta name="keywords" content="" />…
引子 我们经常遇到需要把div中的内容进行水平和垂直居中.所以,这里介绍一种方法,可以使div水平居中和垂直居中. 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>div水平垂直居中</title> <style> *{ margin:0; padding:0; } div.box{…
文字以及div水平垂直居中.md <div class=”content”> <div class=”mydiv”> huangyingnin! </div></div> 文字居中 水平居中text-align: center; .mydiv{ text-align: center;} 垂直居中 vertical-align: middle; .mydiv{ height:400px; text-align: center; vertical-align:…
今天写的了百度前端学院春季班的任务:定位和居中问题  由于距离上次学习CSS有点久远了,加上以前木有记笔记的习惯,方法忘得只剩下一种,今天通过网上查阅资料总结了以下两种简单的方法让DIV水平垂直居中.=.= 先来个效果图: HTML代码: <div class="wrap"> <div class="main first"> <div id="left" class="yuan"></…
DIV水平垂直居中,非IE浏览器可以用CSS3来处理,IE浏览器中分别处理IE6和/IE7.IE8.IE9. 在IE低版本中,虽然大致上没有问题,但还是有一些细微的显示问题. 示例如下: <!DOCTYPE html> <html> <head> <title>DIV水平垂直居中 </title> <meta http-equiv="Content-Type" content="text/html; chars…
百度了很多种方法,很多是不起作用的.下面这些方法是我亲自试过的.希望对大家有帮助! 下面是一波代码水军. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>设置div水平垂直居中</title> <style> *{ margin: 0px; padding: 0px; } /*方法一*/ #c…
scss 是一个很好用的css预处理语言,有很多很好的特性. 比如 mixin. 我们可以像使用函数那样使用mixin. 比如写一个div水平垂直居中. 上代码. @mixin absolute_center_mixin($width, $height){ position: absolute; top: 50%; left: 50%; margin-left: -($width / 2); margin-top: -($height / 2); width: $width; height: $…
最全的CSS盒子(div)水平垂直居中布局,对CSS 布局掌握程度决定你在 Web 开发中的开发页面速度. 相对于屏幕 方法一:利用定位 <div class="box"></div> <style> body { background: green; } .box { position: fixed; top: 50%; left: 50%; margin: -150px 0 0 -150px; width: 300px; height: 300p…
中秋快到了,祝大家中秋快乐. 平时大家写bug过程中肯定会遇到让div框水平或者垂直居中,然而有时候能居中,有时候不能居中.我把平时遇到的一些方法写出来,如果对你有用,那便是晴天. 1.text-align: center; text-align: center; 这个是最简单的了,实现文本水平居中. 2.margin: 0 auto; 0为上下边距为0,auto为左右边距自适应,于是变实现了水平居中.高度固定时,加上line-height就能实现垂直居中.代码如下: margin: 0 aut…
必看参考: http://www.jb51.net/css/28259.html 让div居中对齐缩写形式为: .style{margin:0 auto;} 数字0 表示上下边距是0.可以按照需要设置成不同的值. 例子: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&…
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> </head> <body> <!--方法1:一个未知宽高的弹出框,水平垂直居中--> <div style="position:relative;width:100%;height:600px;border:1px solid #888">方法1 <div…
方案一: div绝对定位水平垂直居中[margin:auto实现绝对定位元素的居中], 兼容性:,IE7及之前版本不支持 div{ width: 200px; height: 200px; background: green; position:absolute; left:0; top: 0; bottom: 0; right: 0; margin: auto; } 方案二: div绝对定位水平垂直居中[margin 负间距]     这或许是当前最流行的使用方法. div{ width:20…
代码: <div class="father"> <div class="son"> </div></div> 方案一: div绝对定位水平垂直居中[margin:auto实现绝对定位元素的居中], 兼容性:,IE7及之前版本不支持   .father{ width:400px; height:400px;               background: red; position:relative;  /* 或者…
方案一: div绝对定位水平垂直居中[margin:auto实现绝对定位元素的居中], 兼容性:,IE7及之前版本不支持 div{ width: 200px; height: 200px; background: green; position:absolute; left:0; top: 0; bottom: 0; right: 0; margin: auto; } 方案二: div绝对定位水平垂直居中[margin 负间距]     这或许是当前最流行的使用方法. div{ width:20…
在平时,我们经常会碰到让一个div框针对某个模块上下左右都居中(水平垂直居中),其实针对这种情况,我们有多种方法实现. 方法一: 绝对定位方法:不确定当前div的宽度和高度,采用 transform: translate(-50%,-50%); 当前div的父级添加相对定位(position: relative;) 图片展示: 代码如下: div{ background:red; position: absolute; left:50%; top:50%; transform: translat…
方案一: div绝对定位水平垂直居中[margin:auto实现绝对定位元素的居中], 兼容性:,IE7及之前版本不支持 div{ width: 200px; height: 200px; background: green; position:absolute; left:0; top: 0; bottom: 0; right: 0; margin: auto; } 方案二: div绝对定位水平垂直居中[margin 负间距]     这或许是当前最流行的使用方法. div{ width:20…
最近,公司招了一批新人,吃饭的时候恰好碰到一个新同事,就跟他聊了起来.听他说了主管面试的时候出的一些问题,其中一个问题我印象特别深刻,因为,我当年进来的时候,也被问到这个问题.虽然这个问题已经问烂了,但是我还是有必要在这里总结一个这个问题. 利用 CSS 来实现对象的垂直居中有许多不同的方法,比较难的是选择那个正确的方法. 使用 CSS 实现水平居中很容易,但要实现垂直居中并不容易.而且有些方法在一些浏览器中无效.下面我们看一下使对象垂直集中的几种不同方法,以及它们各自的优缺点. 一:表格布局法…
我们平时常用的定高,top:50%:left:50%和margin-left负一半宽度margin-top负一半高度的居中方式暂不考虑,因为这种方式大家都会. 第一种绝对定位(absolute centering)居中: <div class="abs-center"></div> .abs-center{ height: 50%; width: 50%; background: red; margin: auto; position: absolute; le…
水平大家都知道,一般固定宽度给个margin:0 auto:就可以了.下面实现水平垂直都居中 HTML <div class="parent"> </div> css html,body{ width: 100%; height: 100%; } .parent{ width: 750px; height: 400px; background: orange; /*水平居中*/ margin: 0 auto; position: relative; top: 5…
方法-1 img { vertical-align: middle; } div:before { content: ""; display: inline-block; width: 0; height: 100%; vertical-align: middle; }  方法0 .item{ position:absolute top:50% left:50% transform:translate3D:(-50%,-50%,0)} 方法1:水平:margin:0 auto; 垂直:…
/*实现一.原理:要让div等块级元素水平和垂直居中,必需知道该div等块级元素的宽度和高度,然后设置位置为绝对位置,距离页面窗口左边框和上边框的距离设置为50%,这个50%就是指页面窗口的宽度和高度的50%,最后将该div等块级元素分别左移和上移,左移和上移的大小就是该div等块级元素宽度和高度的一半.该方法使用普遍,但是前提是必需设置div等块级元素的宽度和高度.如果当页面div等块级元素宽度和高度是动态的,比方说需要弹出一个div等块级元素元素层并且要居中显示,div等块级元素的内容是动态…
用到CSS3中display的新属性. HTML <div class="parent"> </div> CSS html,body{ width: 100%; height: 100%; } /*方法二*/ body{ display: flex; align-items: center;/****水平居中****/ justify-content: center;/*垂直居中*/ } .parent{ width: 750px; height: 400px;…
div中嵌套一个居中的div有很多方法,如果不想调整边距,有一个简单的方法: <div align="center" style="width: 200px;height: 200px;background: green;display: table-cell;vertical-align: middle;"> <div style=" width: 100px;height: 100px;background: blue;"&…
示例1: .div1{ width:200px; height:300px; border:1px solid #000; position: relative; } .div2{ width: 40px; height:40px; background:green; position: absolute; margin: auto; top:; left:; right:; bottom:; } 示例1 css部分 <div class="div1"> <div c…
<style>#info{height:0px; width:0px;top:50%; left:50%;position:absolute;}#center{background:#FFCC33;border:1px solid #0033FF; width:300px;height:300px;position:absolute; margin:-150px;}</style><div id="info"><div id="cen…
方法一 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> .modal-header { padding: 10px 20px; background: rgba(0,0,0,.25); color:#fff; } .modal-body{ pa…
<div class="father"> <div id="main"></div> </div> .father{ position:fixed; width:100%; height:100%; /* top:0; left:0; */ } #main{ position: absolute; top:0; left:0; bottom:0; right:0; width:60%; height:60%; marg…
<div style="position:absolute; width: 600px; height: 200px; left: 50%; top: 50%; margin-left:-300px; margin-top: -100px;"> 看我居中 </div>…
css <style> .main{ background: #999999; width: 600px; height: 400px; position: absolute; top: 50%; left: 50%; margin: -200px 0 0 -300px;/*此处的-200px是widthd的一半,-300px是height的一半*/ } </style> html <body> <div class="main"> &l…
Dom结构: <div class="box"> <div class="inner"> 123 </div> </div> 1, 伪元素 加 vertical-align .box { width: 300px; height: 300px; border: 1px solid red; text-align: center; } .box:before{ content: ''; height: 100%; wid…