需要连续 3 个 div,其中中心 div 填充页面宽度,无论内容长度如何
我正在尝试创建一个 html 界面,其中行将动态添加到我的网页中。
我目前的做法是使用嵌套 DIV,并将 CSS 显示样式设置为表格。
每行有 3 个 div。左侧 div 和右侧 div 具有固定宽度,而中间 div 应扩展以水平适应页面,无论其内容的长度如何。
我的问题是我不知道如何使中心 div 扩展页面的整个剩余宽度。使用下面的代码,中心 div 与内容一样小。
我尝试了一种解决方案,将左 div 向左浮动,右 div 向右浮动,但这不会让我正确选择一行文本。即,如果我开始选择右侧 div 的内容,然后向左拖动,则左侧和中心 div 将不会被选择。
该解决方案只需要针对基于 webkit 的引擎,因为我的代码将仅在基于 webkit 的环境中使用。
编辑!
我忘了提及我也尝试过使用表格。但是,我还需要避免当屏幕缩小时页面上出现水平滚动条。当我使用表格并缩小页面时,中心 div 在某个点停止缩小(由于我猜是固定宽度百分比)。
我的CSS代码:
.chatarea
{
display: table;
height = 100%;
padding-top:50px;
margin: 0px;
}
.row
{
#display: table-row;
}
.nick
{
display: table-cell;
width: 140px;
border-style: solid;
text-align: right;
}
.timestamp
{
display: table-cell;
width 50px;
border-style: solid;
}
.message
{
display: table-cell;
border-style: solid;
}
以及相关的html
<div class="chatarea">
<div class="row">
<div class="nick">
<p>Some Nick</p>
</div>
<div class="message">
<p>Some Message</p>
</div>
<div class="timestamp">
<p>Some Timestamp</p>
</div>
</div>
</div>
I am trying to create an html interface, where rows will be dynamically added to my web page.
The way I am currently doing it is by using nested DIVs with the CSS display style set to table.
Each row has 3 divs. The left div and the right div have a fixed width, while the middle div should expand to fit the page horizontally regardless of the length of it's content.
My problem is I'm not sure how to make that center div expand the entire remaining width of the page. With the code below, the center div is as small as the content.
I tried a solution that floated the left div left, and the right div right, however that would not let me select a row of text properly. i.e., if I started selecting the right div's content, then dragged towards the left, the left and center div would not be selected.
The solution only needs to target webkit based engines, as my code will only be used in a webkit based environment.
EDIT!
I forgot to mention that I also tried using tables. However I also need to avoid getting horizontal scroll bars appearing on the page when the screen is shrinking. When I use tables and shrink the page, the center div stops shrinking at a certain point (due to the fixed width percentages I guess).
My CSS code:
.chatarea
{
display: table;
height = 100%;
padding-top:50px;
margin: 0px;
}
.row
{
#display: table-row;
}
.nick
{
display: table-cell;
width: 140px;
border-style: solid;
text-align: right;
}
.timestamp
{
display: table-cell;
width 50px;
border-style: solid;
}
.message
{
display: table-cell;
border-style: solid;
}
And the relevant html
<div class="chatarea">
<div class="row">
<div class="nick">
<p>Some Nick</p>
</div>
<div class="message">
<p>Some Message</p>
</div>
<div class="timestamp">
<p>Some Timestamp</p>
</div>
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信这是一个解决方案:
border-collapse:collapse;
添加到 .chartarea 以删除双边框宽度。width: 100%
添加到 .chartarea 以覆盖窗口的整个宽度。width: 80%;
添加到 .message,使其随着窗口宽度的变化而增长。white-space: nowrap;
添加到 .nick 以控制换行。white-space: nowrap;
添加到 .timestamp 以控制换行。display: table-row
查看此
fiddle。注意:小提琴页面似乎已被服务器删除。
I believe this is a solution:
border-collapse: collapse;
to .chartarea to remove the double border width.width: 100%
to .chartarea to cover the entire width of the window.width: 80%;
to .message to have it grow as the window width changes.white-space: nowrap;
to .nick to control wrapping.white-space: nowrap;
to .timestamp to control wrapping.display: table-row
in .rowCheck out this
fiddle.Note: the fiddle page appears to have been removed by the server.
删除
显示
样式。然后只需使用:然后
.message
将占用剩余的宽度。或者,只需使用
元素。
Remove the
display
styles. Then just use:Then
.message
will take up the remaining width.Alternatively, just use a
<table>
element.如果你做不到,就假装去做。只需将
.nick
和.timestamp
放入.message
中,并将它们绝对定位即可。我检查了最近的 chrome 和 safari(在 Windows 上)中的 fiddle 并且选择文本没有问题。
If you can't do it fake it. Just put your
.nick
and.timestamp
inside.message
and position them absolutely.I've check the fiddle in recent chrome and safari (on windows) and had no problems with selecting text.