代码如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> body{ margin: 0; } div{ width: 100px; height: 100px; background: red; margin-left: 50px; float: left; } </style> </head> <body> <div>div1</div> </body> </html>
解决方案:给元素加上display:inline。
div{
width: 100px;
height: 100px;
background: red;
margin-left: 50px;
float: left;
*display: inline;
}