教你快速掌握两个div在同一行显示css如何实现
我们都知道div是一个块元素,块元素的特点是,独占一行,从上往下排列,但是有时候我们在页面排版的时候需要从左往右横着排列,想要实现这样的效果方法有很多,首先先来看一下,默认情况下的2个div的效果如下
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <meta http-equiv= "X-UA-Compatible" content= "IE=edge" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <title>Document</title> <style> *{ margin: 0; padding: 0; } .box1{ width: 200px; height: 200px; background-color: blue; } .box2{ width: 200px; height: 200px; background-color: deeppink; } </style> </head> <body> <div class = "box1" >盒子1</div> <div class = "box2" >盒子2</div> </body> </html> |
想要实现从左往右横着排列的效果,如下图
添加了浮动之后,2个盒子会横着从左往右排列.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <meta http-equiv= "X-UA-Compatible" content= "IE=edge" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <title>Document</title> <style> *{ margin: 0; padding: 0; } .box1{ width: 200px; height: 200px; background-color: blue; float : left; /*添加浮动*/ } .box2{ width: 200px; height: 200px; background-color: deeppink; float : left; /*添加浮动*/ } </style> </head> <body> <div class = "box1" >盒子1</div> <div class = "box2" >盒子2</div> </body> </html> |
方法2:转换为行内块元素,转换为行内块元素之后,因为行内块识别编辑器敲的回车,所以2个盒子水平之间有缝隙,不建议使用这个方法.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <meta http-equiv= "X-UA-Compatible" content= "IE=edge" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <title>Document</title> <style> *{ margin: 0; padding: 0; } .box1{ width: 200px; height: 200px; background-color: blue; display: inline-block; /*转换为行内块元素*/ } .box2{ width: 200px; height: 200px; background-color: deeppink; display: inline-block; /*转换为行内块元素*/ } </style> </head> <body> <div class = "box1" >盒子1</div> <div class = "box2" >盒子2</div> </body> </html> |
方法3:设置为弹性布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <meta http-equiv= "X-UA-Compatible" content= "IE=edge" > <meta name= "viewport" content= "width=device-width, initial-scale=1.0" > <title>Document</title> <style> *{ margin: 0; padding: 0; } body{ display: flex; /*设置为弹性布局,弹性布局中主轴默认是水平的,这个时候内部的子元素会横着排列*/ } .box1{ width: 200px; height: 200px; background-color: blue; } .box2{ width: 200px; height: 200px; background-color: deeppink; } </style> </head> <body> <div class = "box1" >盒子1</div> <div class = "box2" >盒子2</div> </body> </html> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端