两个div,不用float时,如何让他们水平布局,左边的div更宽些


在不使用`float`的情况下,有几种方法可以让两个`div`水平布局,并且让左边的`div`更宽。 方法一:使用`display: flex` ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> /* 父容器使用flex布局 */ .parent { display: flex; } /* 左边的div设置更宽的宽度 */ .left { width: 70%; background-color: lightblue; } /* 右边的div设置较窄的宽度 */ .right { width: 30%; background-color: lightgreen; } </style> </head> <body> <!-- 父容器 --> <div class="parent"> <!-- 左边的div --> <div class="left">左边的div更宽</div> <!-- 右边的div --> <div class="right">右边的div较窄</div> </div> </body> </html> ``` 代码解释: - `.parent`类设置`display: flex`,使其成为一个弹性容器,子元素会水平排列。 - `.left`类设置宽度为`70%`,`.right`类设置宽度为`30%`,从而让左边的`div`更宽。 方法二:使用`display: inline-block` ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> /* 父容器设置字体大小为0,避免inline-block元素之间的间隙 */ .parent { font-size: 0; } /* 左边的div设置更宽的宽度,同时设置为inline-block */ .left { display: inline-block; width: 70%; background-color: lightblue; font-size: 16px; } /* 右边的div设置较窄的宽度,同时设置为inline-block */ .right { display: inline-block; width: 30%; background-color: lightgreen; font-size: 16px; } </style> </head> <body> <!-- 父容器 --> <div class="parent"> <!-- 左边的div --> <div class="left">左边的div更宽</div> <!-- 右边的div --> <div class="right">右边的div较窄</div> </div> </body> </html> ``` 代码解释: - `.parent`类设置`font-size: 0`,是为了消除`inline-block`元素之间的间隙。 - `.left`和`.right`类设置`display: inline-block`,让两个`div`水平排列,同时分别设置不同的宽度。 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: 1. 使用`flex`布局时,要注意浏览器的兼容性,特别是一些较旧的浏览器可能不支持某些`flex`属性。 2. 使用`inline-block`布局时,要注意元素之间的间隙问题,通常可以通过设置父元素的`font-size: 0`来解决,但要记得在子元素中重新设置字体大小。 3. 在设置宽度时,要考虑到元素的内边距、边框等因素,避免出现宽度超出预期的情况。 [2025-09-05 11:38:37 | AI写代码神器 | 855点数解答]
- def fill_and_submit_form(row): print("填充表单并提交...") wait = webdriverwait(driver, 5) # 增加等待时间 try: product_name_input = wait.until(ec.presence_of_element_located((by.xpath, '//*[@id="wcontentpanel"]/div[3]/div/div[2]/form/div[1]/div[3]/div/div/div[1]/input'))) product_category_dropdown = wait.until(ec.element_to_be_clickable((by.xpath, '//*[@id="wcontentpanel"]/div[3]/div/div[2]/form/div[1]/div[4]/div/div/div/span/span/div/div[1]'))) time.sleep(2) # 等待完成(361点数解答 | 2024-11-11 21:56:42)186
- 求一个圆柱体的表面积和体积。从键盘输入圆柱体的半径和高,输出它的表面积和体积。 函数接口定义: float area(float r,float h); //求圆柱体的表面积 float bulk(float r,float h);//求圆柱体的体积 裁判测试程序样例: #include<stdio.h> #define pi 3.1415926 // 用 pi 表示 π 的值 float area(float r, float h); float bulk(float r, float h); main() {float r,h; //r表示底面积半径,h表示圆柱体高度 scanf("%f,%f",&r,&h); printf("surface area :%.2f, volume :%.2f\n",area(r,h),bulk(r,h)); } /* 你提交的代码将被嵌入到该行的下面*/(248点数解答 | 2024-12-15 23:04:16)189
- 本题要求实现一个计算输入的两数的和与差的简单函数。 函数接口定义: void sum_diff( float op1, float op2, float *psum, float *pdiff ); 其中op1和op2是输入的两个实数,*psum和*pdiff是计算得出的和与差。 裁判测试程序样例: #include <stdio.h> void sum_diff( float op1, float op2, float *psum, float *pdiff ); int main() { float a, b, sum, diff; scanf("%f %f", &a, &b); sum_diff(a, b, &sum, &diff); printf("the sum is %.2f\nthe diff is %.2f\n", sum, diff); return 0; } /* 你的代码将被嵌在这里 */ 用c语言(128点数解答 | 2024-12-01 21:36:04)154
- <!DOCTYPE html> <html> <head> {include file="common_header" /} {include file="common_top" /} </head> <body> <div class="layui-fluid"> <div class="layui-row layui-col-space15"> <div class="layui-col-md12"> <div class="layui-card"> <div class="layui-card-body"> <div class="layui-form" lay-filter="component-form-element"> <div class="layui-box layui-laypage layui-laypage-molv">{$page}</div> <table class="layui-table" lay-even="" lay(644点数解答 | 2025-03-11 15:42:55)135
- App.vue <template> <div style="padding: 100px 0 0 0"> <div id="header" class="header-v1" style="height: 100px;background-color:rgb(0 137 255 / 70%)"> <div class="header"> <div class="container" style="width: 1500px;height: 100px"> <div id="header-logo" class="right"> <div class="header-logo"><a href="#" id="logo"><img id="logo-default" src="" alt=""/><img style="display:none" id="logo-alternate" src="" alt=""/> </a></div> </div>(31点数解答 | 2025-05-14 09:58:18)139
- 有如下一段html代码,请用css实现鼠标移入a标签,自动隐藏class="api-name-div"的div <a href="#" class="api-block"> <img src="../image/muwu.jpg"> <div class="api-name-div">幸福就是可以一起睡觉</div> <div class="api-edit-div">编辑</div>(181点数解答 | 2023-10-26 09:52:31)289
- <template> <div class="login"> <div class="loginBox"> <h3>用户登录</h3> <span class="deadline"></span> <div class="form"> <div class="input"> <img src="../../assets/login/username.png" alt="" /> <input type="text" placeholder="请输入用户名" v-model="username" /> </div> <div class="input"> <img src="../../assets/login/password.png" alt="" /> <input type="password" placeholder="请输入密码" v-model="password" /> </div>(1402点数解答 | 2025-03-18 21:31:12)136
- <a href="#" class="api-block"> <img src="../image/muwu.jpg"> <div class="api-name-div">幸福就是可以一起睡觉</div> <div class="api-edit-div">编辑</div> </a>(182点数解答 | 2023-10-26 09:46:09)247
- <!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>品牌列表案例</title> <link rel="stylesheet" href="./lib/bootstrap.css"> <link rel="stylesheet" href="./css/brandlist.css"> </head> <body> <div id="app"> <!-- 卡片区域 --> <div class="card"> <div class="card-header"> 添加品牌 </div> <div class="card-body"> <!-- 添加品牌的表单区域 --> <!-- form 表单元素有 submit 事件 --> <form> <div cl(796点数解答 | 2024-11-18 20:40:15)175
- <div class="neon">致敬创作者严大帅,严大帅帅过彭于晏!</div> <div id="stage1" class="game-box"> <h3>第一关:镜像数字</h3> <p>请输入密码:</p> <input type="text" id="input1"> <div style="margin-top:15px"> <button onclick="check(1)">提交</button> </div> <p>提示:显示器里的镜像世界</p> </div>(201点数解答 | 2025-03-05 11:14:34)133
- <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>2048 Game</title> <link rel="stylesheet" href="style.css"> </head> <body> <header> <h1>2048</h1> <button id="newGameButton">New Game</button> <p>Score: <span id="score">0</span></p> </header> <div id="grid-container"> <div class="grid-cell" id="grid-cell-0-0"></div> <div class="grid-cell" id="grid-cell-0-1"></div> <div class="grid-cell" id="grid-cell-0-2">(138点数解答 | 2025-03-05 15:45:36)243
- <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>2048 Game</title> <link rel="stylesheet" href="style.css"> </head> <body> <header> <h1>2048</h1> <button id="newGameButton">New Game</button> <p>Score: <span id="score">0</span></p> </header> <div id="grid-container"> <div class="grid-cell" id="grid-cell-0-0"></div> <div class="grid-cell" id="grid-cell-0-1"></div> <div class="grid-cell" id="grid-cell-0-2">(117点数解答 | 2025-03-11 00:06:25)177