关于负margin的一些应用
author:一佰互联 2019-04-21   click:185

最近听大家在讨论 负margin的一些应用,所以自己研究一下,下面做一些记录。

1. 实现左右两列布局

方法一:

XML/HTML Code复制内容到剪贴板
  1. <div>  
  2.      <div style="float:left;width:200px;">左侧</div>  
  3.      <div style="margin-left:200px;background:#999;">右侧</div>  
  4. </div>  

方法二:

XML/HTML Code复制内容到剪贴板
  1. <div>  
  2.             <div style="width:200px;height:200px;">左侧</div>  
  3.             <div style="margin-left:200px;margin-top:-200px;">右侧</div>  
  4.         </div>  

对比两种方法,第一种用到float,弊端就是应用float在IE中可能引起很多bug,第二种的话用-margin,本事还是比较赞的,暂时没发现没事问题;

2.导航栏

XML/HTML Code复制内容到剪贴板
  1. <!doctype html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title>Document</title>  
  6.     <style type="text/css">  
  7.         *{   
  8.             margin: 0;   
  9.             border: 0;   
  10.             padding: 0;   
  11.         }   
  12.         ul{   
  13.             overflow: hidden;   
  14.         }   
  15.         ul li{   
  16.             background: #999;   
  17.             border-left: 1px solid #000;   
  18.             width:60px;   
  19.             height:30px;   
  20.             float: left;   
  21.             list-style: none;   
  22.             text-align: center;   
  23.             margin-left:-1px;   
  24.         }   
  25.     </style>  
  26. </head>  
  27. <body>  
  28.     <div style="margin-left:20px;">  
  29.     <ul>  
  30.         <li>1</li>  
  31.         <li>2</li>  
  32.         <li>3</li>  
  33.     </ul>  
  34.     </div>  
  35. </body>  
  36. </html>  

或者:

XML/HTML Code复制内容到剪贴板
  1. <!doctype html>  
  2. <html lang="en">  
  3. <head>  
  4.     <meta charset="UTF-8">  
  5.     <title>Document</title>  
  6.     <style type="text/css">  
  7.         *{   
  8.             margin: 0;   
  9.             border: 0;   
  10.             padding: 0;   
  11.         }   
  12.         ul{   
  13.                
  14.         }   
  15.         ul li{   
  16.             background: #999;   
  17.             border-left: 1px solid #000;   
  18.             border-right: 1px solid #000;   
  19.             width:60px;   
  20.             height:30px;   
  21.             float: left;   
  22.             list-style: none;   
  23.             text-align: center;   
  24.             margin-left:-1px;   
  25.         }   
  26.     </style>  
  27. </head>  
  28. <body>  
  29.     <div style="margin-left:20px;">  
  30.     <ul>  
  31.         <li>1</li>  
  32.         <li>2</li>  
  33.         <li>3</li>  
  34.     </ul>  
  35.     </div>  
  36. </body>  
  37. </html>  
合理使用overflow以及-margin 。

以上这篇关于负margin的一些应用就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持网页设计。

原文地址:http://blog.csdn.net/fanhaiwang520/article/details/8950189