html - 我怎样才能让两个不同高度的 div 始终并排,文本直接出现在第一个 div 的下方并且从不与第二个 div 重叠?
问题描述
这有点繁琐,但我会尝试解释一下。我正在尝试使用 CSS 是:
- 页面顶部的两个 div(“A”和“B”),并排在一起,固定宽度为 400px(如果窗口太窄而无法同时看到,那么应该会出现滚动条,我不会想要一个 div 跳到另一个下面)
- 不过,这两个 div 可以有不同的高度 - 有时 div A 会更高,有时 div B。
- 我希望在 div A 正下方出现一些长文本。如果 div B 高于 div A,那么文本应该围绕 div B 流动。
- 文本的显示宽度不应超过 400 像素,但如果窗口较窄,则应根据需要尽可能窄地显示。文本不应宽于视口。
总而言之,我希望顶部的两个并排 div 以相同的方式显示,无论窗口宽度有多窄,但 div A 下面的文本响应窗口宽度(并且也不重叠 div B,如果 div B 比 div A 下降得更远)。
这是一些不是正确方法的代码,但部分显示了我的目标:
.all {
max-width:400px;
}
.header div{
display:inline-block;
vertical-align:top;
}
.header{
white-space: nowrap;
padding: 0px;
height: 36px;
}
.A{
background-color:red;
width: 195px;
margin-right: 5px;
}
.B{
width: 195px;
margin-left: 5px;
background-color:blue;
}
<div class="all">
<div class="header"><div class="A">Text1</div><div class="B">Text2<br/>Text2<br/>Text2<br/>Text2<br/></div></div>
This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.</div>
在这里不起作用的是长文本与 div B (蓝色的)重叠,而且它不允许 div A 具有不同的高度(它只显示在顶部下方 36px 处,而不是在 div A 下方,无论如何它的高度)。
任何帮助将非常感激。
编辑:这是第二个版本,它的行为如我所愿,除了我希望两个 div 在窗口太窄时保持并排:
.all {
max-width:400px;
}
.A{
background-color:red;
width: 190px;
margin: 5px;
float: right;
}
.B{
width: 190px;
margin: 5px;
background-color:blue;
float: right;
}
<div class="all">
<div class="B" style="float: right;">Text2<br/>Text2<br/>Text2<br/>Text2<br/></div>
<div class="A">Text1</div>
This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
This is some text. This is some text. This is some text.
</div>
解决方案
用于width:max-content;
避免缠绕。
调整屏幕大小以查看结果:
.all {
margin:5px;
}
.header {
width:max-content;
}
.A {
background-color: red;
width: 195px;
margin-right: 5px;
float:left;
}
.B {
width: 195px;
margin-left: 5px;
background-color: blue;
float: right;
}
.content {
clear:left;
max-width: 400px;
}
<div class="all">
<div class="header">
<div class="A">Text1</div>
<div class="B">Text2<br/>Text2<br/>Text2<br/>Text2<br/></div>
</div>
<div class="content">
This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This
is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text.
</div>
</div>
<div class="all">
<div class="header">
<div class="A">Text1<br/>Text2<br/>Text2<br/>Text2<br/></div>
<div class="B">Text2</div>
</div>
<div class="content">
This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This
is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text.
</div>
</div>
推荐阅读
- r - ShinyApp 工作,但 runApp 没有 R
- mysql - pymysql 读取 \n 个字符。如何停止在 .sql 文件中运行的 \n 字符?
- spring-boot - Spring Boot 应用程序 - JSTL 不起作用
- powershell - 如何比较两个自定义 ps 自定义对象并根据另一个结果更改一个的值?
- .htaccess - .htaccess 文件中的重定向指令在重定向到 https 之前重定向到 http 协议
- expression - 我需要这个字符串的最佳正则表达式,最后用逗号
- python - AttributeError:“LGBMRegressor”对象没有属性“feature_name_”
- amazon-web-services - 为什么从 gitlab ci 运行时,aws cli 正在执行旧的 docker 映像?
- javascript - 如何将 Shaka Player 与通过 xhr 或 websocket 到达的原始 mp4 视频数据一起使用?
- android - 属性 textIsSelectable 不适用于 TextView