内容太大时 div 不浮动

发布于 2024-12-03 14:50:48 字数 2212 浏览 1 评论 0原文

我正在尝试并排放置两个 div。左边有一张图像,右边有一些数据。我希望右边的那个在没有足够的空间时被截断。不幸的是,当没有足够的空间时,整个 div 并没有被截断,而是落到了左侧的 div 下方。下面的图片应该可以清楚地说明这一点。

这是我的 html。我抛出了溢出:隐藏;空白:nowrap;到处都是,但无济于事。当右侧 div 中的内容不太大时(或者当我调整弹出窗口的大小足够宽时),所有内容都会像我想要的那样浮动。

(这不是 MVC,如果是的话,我会热情地使用 Ravor 语法)

    <div id="tabInfo" class="tabDiv">
        <ul>
            <li><a href="#tabs-info">Book Info</a></li>
            <li><a href="#tabs-reviews">Reviews</a></li>
            <li><a href="#tabs-subjects">Related Subjects</a></li>
        </ul>
        <div id="tabs-info">
            <div style="float:left;">
                <img src="../books/covers/medium/<%= Book.medImgID %>.jpg" />
            </div>
            <div style="float:left; margin-left:10px; font-size:10pt; overflow:hidden; white-space: nowrap; text-overflow: ellipsis;">
                <div style="font-size:14pt; overflow:hidden; white-space: nowrap; text-overflow: ellipsis;">
                    <b><%= Book.Title %></b>
                </div>
                <div title='<%= PublisherDisplay %>' style='margin-top:5px; font-size:12pt'><%= PublisherDisplay %></div>

                <div style="margin-top:7px;">Author<%= Book.AuthorsArray.Length == 1 ? "" : "s" %></div>
                    <% foreach (string auth in Book.AuthorsArray) { %>
                        <div style="margin-left:10px;"><%= auth %></div>
                    <% } %>

                <div title='<%= Book.Pages ?? 0 %>' Pages' style='margin-top:6px;'><%= Book.Pages ?? 0 %> Pages</div>
                <div style="margin-top:5px; height:16px; margin-top:6px;">
                    <div style="vertical-align: middle; float: left;">Read?&nbsp;&nbsp;</div>
                    <img src='../Img/IsRead/<%= Book.IsRead ? "check" : "cross" %>.png' style='width: 16px; height: 16px;' />
                </div>
            </div>
        </div>

I'm trying to get two divs side by side. The left one will have an image, and the right one will have some data. I'd like the one on the right to just truncate when there's not enough room. Unfortunately, instead of truncating, the whole div is just dropping down under the one that would otherwise be on the left when there's not enough room. The picture below should make this clear.

Here's my html. I'm throwing overflow:hidden; white-space: nowrap; everywhere, to no avail. When the content in the right div is not too large (or when I resize the popup wide enough) everything floats like I want it.

(this is not MVC, if it was, I'd be using Ravor syntax with gusto)

    <div id="tabInfo" class="tabDiv">
        <ul>
            <li><a href="#tabs-info">Book Info</a></li>
            <li><a href="#tabs-reviews">Reviews</a></li>
            <li><a href="#tabs-subjects">Related Subjects</a></li>
        </ul>
        <div id="tabs-info">
            <div style="float:left;">
                <img src="../books/covers/medium/<%= Book.medImgID %>.jpg" />
            </div>
            <div style="float:left; margin-left:10px; font-size:10pt; overflow:hidden; white-space: nowrap; text-overflow: ellipsis;">
                <div style="font-size:14pt; overflow:hidden; white-space: nowrap; text-overflow: ellipsis;">
                    <b><%= Book.Title %></b>
                </div>
                <div title='<%= PublisherDisplay %>' style='margin-top:5px; font-size:12pt'><%= PublisherDisplay %></div>

                <div style="margin-top:7px;">Author<%= Book.AuthorsArray.Length == 1 ? "" : "s" %></div>
                    <% foreach (string auth in Book.AuthorsArray) { %>
                        <div style="margin-left:10px;"><%= auth %></div>
                    <% } %>

                <div title='<%= Book.Pages ?? 0 %>' Pages' style='margin-top:6px;'><%= Book.Pages ?? 0 %> Pages</div>
                <div style="margin-top:5px; height:16px; margin-top:6px;">
                    <div style="vertical-align: middle; float: left;">Read?  </div>
                    <img src='../Img/IsRead/<%= Book.IsRead ? "check" : "cross" %>.png' style='width: 16px; height: 16px;' />
                </div>
            </div>
        </div>

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只有一腔孤勇 2024-12-10 14:50:48

如果您只是在 div 上设置 max-width 属性,那么它们就不会超过设定的宽度,应该可以工作。


您需要更改此设置才能与您的代码一起使用;这是一个例子:)

div.left
{
    float: left;
    width: 100px;
}

div.right
{
    margin-left: 110px;
}

div
{
    border: 1px solid red;
}

It should work if you just set a max-width property on the divs, so they can't exceed a set width.


You'll need to change this to work with your code; this is an example :)

div.left
{
    float: left;
    width: 100px;
}

div.right
{
    margin-left: 110px;
}

div
{
    border: 1px solid red;
}
梦幻之岛 2024-12-10 14:50:48

我建议为左列和右列设置固定宽度,否则右列将低于左列,因为选项卡区域没有足够的空间。因此,选项卡内容区域也必须设置宽度值。

您不需要设置空白和文本溢出属性

http://jsbin.com/uvibiz

I would recommend to set fixed width for both left and right columns or the right column will goes down of the left one since it doesn't have enough space in the tab area. Thus, tab content area must be set width value as well.

You dont need to set white-space and text-overflow attributes

http://jsbin.com/uvibiz

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文