在前端开发过程中,经常要对元素进行居中设置。一般有水平居中,和垂直居中。一般设置水平居中简单。基本是margin:0 auto,就可以了。但是垂直居中,我们有时会觉得使用vertical-align,但是这个属性对div元素不起作用。

目前通用的解决办法是,使用绝对定位,然后设置left和top为50%。然后通过margin来设置。下面具体代码演示了一个简单的登录页面,然后使登录界面水平居中和垂直居中。代码如下:

<html>
<head>
<title>div元素水平和垂直居中</title>
<style type="text/css">
#login-container
{ width:400px;
height:350px;
background-color:#ddd;
text-align:center;
position:absolute;
left:50%;
top:50%;
margin:-200px 0 0 -200px;
}
#login-container .login-title
{
padding-top:50px;
}
</style>
</head>
<body>
<div id="login-container">
<h3 class="login-title">用户登录</h3>
<div>用户名:<input type="text" value=""/></div>
<div>密&nbsp;&nbsp;&nbsp;码:<input type="text" value=""/></div>
</div>
</body>
</html>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

截图如下:

使用Chrome、Firefox和IE9以上浏览器中,改变浏览器的大小,div元素还是会垂直居中。所有不需要额外写JavaScript代码进行定位。后续会写到另外一个问题:在一个div中,不固定高度的图片如何垂直居中。另外jQuery UI的Dialog控件有一个bug就是,页面太长了,往下拉滚动条时,overlay的高度不能自动延伸。这里需要写代码进行控制,在全局js文件中,添加下面代码:

//hotfix.修正overlay在窗口发生onresize时,不调整位置
function update_widget_overlay_height() {
var height = $(window).height() + $(window).scrollTop();
$(".ui-widget-overlay").css({ "height": height });
} $(window).scroll(function () {
update_widget_overlay_height();
});

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

 

DIV元素水平和垂直居中的更多相关文章

  1. 让div等块级元素水平以及垂直居中的解决办法

    一.背景 我们在设计页面的时候,经常要把div等块级元素居中显示,而且是相对页面窗口水平和垂直方向居中显示,如让登录窗口居中显示.我们传统解决的办法是用纯CSS来让div等块级元素居中.在本文中,我将 ...

  2. 文字在div中水平和垂直居中的的css样式

    文字在div中水平和垂直居中的的css样式 text-align:center; /*水平居中*/ line-height: 20px; /*行距设为与div高度一致*/ 示例如下: HTML元素 & ...

  3. div的水平和垂直居中

    CSS实现div的水平居中 div的水平居中可以通过margin设置为0 auto实现. .myDiv { width: 200px; height: 100px; margin: 0 auto; } ...

  4. HTML技巧篇:实现元素水平与垂直居中的几种方式

    如何使用html+css实现元素的水平与垂直居中效果,这也是我们网页在编码制作中会经常用到的问题. 1)单行文本的居中 主要实现css代码: 水平居中:text-align:center;垂直居中:l ...

  5. 使文字在div中水平和垂直居中的的css样式为,四个边分别设置阴影样式

    text-align:center; /*水平居中*/ line-height: 20px; /*行距设为与div高度一致*/ HTML元素 <div>水平垂直居中</div> ...

  6. css 宽高自适应的div 元素 如何居中 垂直居中

    在我们 编写css 样式的时候经常会遇见一个问题 那就是一个 宽高未知的元素 要让他 垂直居中如何实现这个呢 下面是我常用的两种方法 上代码 下面的是 结构代码 <div class=" ...

  7. html,将元素水平,垂直居中的四种方式

    将元素垂直,水平居中分两种情况:一个是元素尺寸固定,二是元素尺寸不固定 一.尺寸固定 方法1:定位 ,50%,margin负距 .box{ width: 400px; height: 300px; b ...

  8. 元素水平垂直居中(transform,margin,table-cell,jQuery)

    1.水平居中 .div{ margin:0 auto; (或者 margin:auto;) width:500px; height:300px; } 2.使用margin水平垂直居中 方式一: .di ...

  9. css中元素水平垂直居中4种方法介绍

    table-cell轻松设置文本图片水平垂直居中 让一个元素垂直居中的思路:把这个元素的容器设置为table-cell,也就是具有表格单元格的特性,再使用vertical-align(这个属性对blo ...

随机推荐

  1. [RxJava]在学习RxJava中的错误理解

    关于RxJava语法的问题与理解   最近看到一个blog-RxJava 写到关于Observable数据异步加载的介绍.针对fromCallable和just操作符在使用上的问题来跟大家针对代码进行 ...

  2. linux shell重定向总结

    command-line1 [-n] > file或文件操作符或设备 command-line1 [-n] >> file或文件操作符或设备 >suc.txt >err. ...

  3. JavaScript_js模拟键盘输入

    function fireKeyEvent(el, evtType, keyCode) { var evtObj; if (document.createEvent) { if (window.Key ...

  4. linux工具

    sudo yum install yum-utils

  5. MemCache 启动

    这个MemCache在园子里面的文章也很多,这里只是我自己记录作下笔记 MemCache的官方网站为http://memcached.org/ 启动与关闭memCache # /usr/local/m ...

  6. iOS单元格高度计算

    // Created by mac on 16/6/29. // Copyright © 2016年 zcc. All rights reserved. // #import "HotCel ...

  7. Oracle查询和问题简记

    现在做两个版本的系统,一个用的数据库是Access,另一个就是Oracle了.每个数据库支持的的查询SQL语句都有所区别,这里主要针对Access和Oracle做 记录. 首先贴出遇到问题最多的一条语 ...

  8. IntelliJ添加Emacs编辑器

    Intellij只支持emacs as a external tool: https://www.jetbrains.com/help/idea/2016.2/tutorial-using-emacs ...

  9. 提取ecshop的mysql类

    在下一篇文章中,我还将介绍如何完善ecshop的mysql类,使用ecshop的数据库前缀 下载ecshop后,解压缩,进入目录upload/includes,复制里面的cls_mysql.php放进 ...

  10. android培训机构排名

    Android开发行业的热度打开了Android培训领域的市场,我们会发现有很多的Android培训机构都在大势宣传自己的师资.就业情况.教学环境等 ,然而到底要选择哪个呢?令很多的人困惑.2015年 ...