让绝对定位的DIV绝对居中显示的方法

飘逸的风10年前 (2015-12-27)前端7227

         绝对居中:即在客户端上任何分辨率下纵横方向均居中

         

紫色的正方形为绝对定位的div

position:absolute; 
 top: 50%; 
 left: 50%; 只能把div定位在以红色圈为起点的位置,加上margin:-100px 0px 0px -100后,便可以让其移动到以红点为中心的位置。;

样例代码:

<html> 
<head> 
<title>Nice and Free CSS Template 11</title>
 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<style type="text/css"><!-- 
/*   body und schrift deffinitionen */ 
body { 
background-color: #e1ddd9; 
font-size: 12px; 
font-family: Verdana, Arial, Helvetica, SunSans-Regular, Sans-Serif; 
color:#564b47; 
padding:0px; 
margin:0px;
 
} 
#inhalt { 
position:absolute; 
height:200px; 
width:400px;
 
margin:-100px 0px 0px -200px; 
top: 50%; 
left: 50%;
 
text-align: left; 
padding: 0px; 
background-color: #f5f5f5; 
border: 1px dotted #000000; 
overflow: auto; 
} 
p, h1 { 
margin: 0px; 
padding: 10px; 

h1 { 
font-size: 11px; 
text-transform:uppercase; 
text-align: right; 
color: #564b47; 
background-color: #90897a; 

a { 
color: #ff66cc; 
font-size: 11px; 
background-color:transparent; 
text-decoration: none; 

--></style> 
</head> 
<body> 
<div id="inhalt">
 
<p><b>centered</b><br /><br /> 
This area should be horizontally and vertically centered.<br /> 
This text stays left aligned<br /> 
<b>ie mac doesn’t like this! </b><br /> 
<a href="/">more nice and free css templates</a> 
</p> 
<p> 
</div> 
</body> 
</html>

相关文章

25个实用的jQuery技巧和解决方案

1. 去除页面的右键菜单 $(document).ready(function(){ $(document).bind(“contextmenu”,function(e){returnfal...

实现jQuery扩展总结

开发自己需要的jQuery插件,看个示例说明 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://...

jQuery 滚动鼠标中键图片自动改变大小(统一控制)

今天维护一个项目,需要对内容里的图片通过图片中间控制大小。由于是以前完成的,客户之前也没有提出要求,所以对已经添加后的数据没办法在一个个地在图片上加函数。网上查找了一下控制中键的资料,终于找到解决办法...

JS键盘控制翻页代码

<script language="javascript"> <!-- document.onkeydown=nextpage var prevpage="prev.html"...

jQuery下拉框美化插件DropKick

jQuery下拉框美化插件DropKick

DropKick 是一个下拉框美化插件,使用非常简单,直接为下拉框 select 调用方法即可,瞬间让你摆脱繁琐。DropKick 有 3 种主题,你可以选择一种与你的页面更加搭配的,当然你也可以自己...

jQuery操作Select的方法集合

jQuery获取Select选择的Text和Value:  语法解释:  1. $("#select_id").change(function(){/...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。