<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <!-- 左侧 --> <div style="width: 240px;float:left;height: 300px;background:#666; "> <button type="button" onclick="javascript:alert('test')">右侧按钮1</button> </div> <!-- 右侧 --> <div style="width:100%;float:right; margin-left:-250px;"> <div style="margin-left:250px; height:300px;background:#666;"> </div> </div> </body> </html>
該方式可以正常實現左右佈局,但存在一個問題:由於採用浮動疊加方式,導致左側div中的button無法點擊。
解決方法:浮動元素加入position屬性(如relative,absolute等)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <!-- 左侧 --> <div style="width: 240px;float:left;height: 300px;background:#666; position: relative;"> <button type="button" onclick="javascript:alert('test')">右侧按钮1</button> </div> <!-- 右侧 --> <div style="width:100%;float:right; margin-left:-250px;"> <div style="margin-left:250px; height:300px;background:#666;"> </div> </div> </body> </html>