要使用CSS创建响应式浮动元素,代码如下-
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .floatRight, .floatLeft { color: white; font-size: 20px; padding: 10px; font-weight: 500; } .floatRight { background-color: rgb(0, 2, 95); float: right; } .floatLeft { background-color: rgb(85, 0, 74); float: left; } @media (max-width: 768px) { .floatRight { float: left; } .floatLeft { float: right; } } </style> </head> <body> <h1>Responsive Float Example</h1> <h2>The floats will swap their position on smaller screens</h2> <div class="clearfix"> <div class="floatRight">Float Right ></div> <div class="floatLeft">Float Left <</div> <br /> </div> </body> </html>
输出结果
上面的代码将产生以下输出-
在调整屏幕大小时,浮子将按以下方式交换位置-