我遇到了一个问题,即当浏览器宽度调整大小时,浮动div会在另一个浮动div下方移动。我在下面的jsfiddle中创建了这种行为的示例,其中使用了伪文本和伪表。如果打开jsfiddle,并调整显示区域的大小,您将看到最终右手div(表格)移到左手div(文本)下方。
https://jsfiddle.net/p7yr5t4u/23/
.view {
float: left;
position: relative;
height: 100%;
}
#A {
width: 200px;
}
理想情况下,我希望看到的是当浏览器宽度调整大小时,右div显示水平滚动条,并使它保持在右侧,而不是在左div下方移动。我尝试将div上的“ overflow-x:auto”设置为无效。如何使水平滚动条出现在右侧的表格中,而不是使其移动到左侧的文本下方?
我对此问题进行了研究,发现了this thread和this thread。我能收集到的唯一解决方案是float本身就是一个问题,并且在div浮动时很难获得所需的行为。有没有办法让我在保持浮动布局的同时获得期望的结果?还是浮动本身是罪魁祸首?
答案 0 :(得分:2)
一个简单的解决方法是将float转换为inline-block
并使用white-space:nowrap
:
.container {
white-space:nowrap;
}
.view {
white-space:normal;
display: inline-block;
vertical-align:top;
position: relative;
height: 100%;
}
#A {
width: 200px;
}
table {
font-family: arial, sans-serif;
border-collapse: collapse;
}
td,
th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
<div class="container">
<div id="A" class="view">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
<div id="B" class="view">
<table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
</div>
</div>
答案 1 :(得分:1)
如果您不介意#B下的另一个包装,并且您确实想使用浮点数,则可以这样做。自从您在示例中使用它以来,我就将这200px的#A视为“常量”。
.view {
float: left;
position: relative;
height: 100%;
}
#A {
width: 200px;
}
#B {
box-sizing: border-box;
margin-left: -200px;
padding-left: 200px;
width: 100%;
}
#C {
overflow-x: auto;
}
table {
font-family: arial, sans-serif;
border-collapse: collapse;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
<div>
<div id="A" class="view">
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
<div id="B" class="view">
<div id="C">
<table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
<div>
</div>
</div>
</div>