居中设置

Center Align - Using margin

Setting the width of a block-level element will prevent it from stretching out to the edges of its container. Use margin: auto;, to horizontally center an element within its container.

The element will then take up the specified width, and the remaining space will be split equally between the two margins:

<!DOCTYPE html>
<html>
<head>
<style>
.center {
margin: auto;
width: 60%;
border: 3px solid #73AD21;
padding: 10px;
}
</style> <script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?73c27e26f610eb3c9f3feb0c75b03925";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
</head>
<body> <div class="center">
<p><b>Note: </b>Using margin:auto will not work in IE8, unless a !DOCTYPE is declared.</p>
</div> </body>
</html>

Tip: Center aligning has no effect if the width property is not set (or set to 100%).

Tip: For aligning text, see the CSS Text chapter.

Left and Right Align - Using position

One method for aligning elements is to use position: absolute;:

<!DOCTYPE html>
<html>
<head>
<style>
.right {
position: absolute;
right: 0px;
width: 300px;
border: 3px solid #73AD21;
padding: 10px;
}
</style> <script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?73c27e26f610eb3c9f3feb0c75b03925";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
</head>
<body> <div class="right">
<p>In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.</p>
</div> </body>
</html>

Note: Absolute positioned elements are removed from the normal flow, and can overlap elements.

Tip: When aligning elements with position, always define margin and padding for the <body> element. This is to avoid visual differences in different browsers.

There is also a problem with IE8 and earlier, when using position. If a container element (in our case <div class="container">) has a specified width, and the !DOCTYPE declaration is missing, IE8 and earlier versions will add a 17px margin on the right side. This seems to be space reserved for a scrollbar. So, always set the !DOCTYPE declaration when using position:

Left and Right Align - Using float

Another method for aligning elements is to use the float property:

<!DOCTYPE html>
<html>
<head>
<style>
.right {
position: absolute;
right: 0px;
width: 300px;
border: 3px solid #73AD21;
padding: 10px;
}
</style> <script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?73c27e26f610eb3c9f3feb0c75b03925";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
</head>
<body> <div class="right">
<p>In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.</p>
</div> </body>
</html>

最新文章

  1. TypeScript的4种编译方式
  2. 关于DOS与cmd(windows系统)
  3. 【转】Linux查看内存大小和插槽
  4. mount: unknown filesystem type &#39;ntfs&#39;
  5. 一行代码解释.net事件与委托
  6. Chrome 开发工具 Javascript 调试技巧
  7. javascript操作JSON对象,增加 删除 修改
  8. AngularJS2之Angular正式初探
  9. LeetCode(56)-Add Binary
  10. .asmx支持post请求或者get请求调用(WebService &quot;因 URL 意外地以 结束,请求格式无法识别&quot; 的解决方法)
  11. sunset
  12. Rocketlab公司火箭Electron介绍
  13. Echars折线配置详解
  14. Linux基础学习(8)--权限管理
  15. Python异步IO
  16. Go语言实践_实现一(客户端)对一(服务器端)聊天室
  17. SpringBoot------Servlet3.0的注解自定义原生Servlet
  18. ORACLE INSTANCE与EM系统
  19. ionic toggle点击返回true/false支持自定义
  20. gridview空间使用

热门文章

  1. LeetCode(101)Symmetric Tree
  2. 用spring annotation声明的bean,当打包在jar中时,无法被扫描到
  3. 【Ajax 2】封装Ajax的核心对象:XMLHttpRequest对象
  4. [luoguP1627] 中位数(模拟?)
  5. Linux(1):基本配置
  6. SQL Prompt 5.1使用
  7. 骑士精神 (codevs 2449)
  8. 虫食算(codevs 1064)
  9. CodeForces - 320B Ping-Pong (Easy Version)
  10. msp430项目编程17