如何在不设置显式宽度的情况下使 div 的内容换行?

发布于 2024-10-18 05:41:30 字数 570 浏览 1 评论 0原文

我想要一系列带有标题和其他信息的帖子。问题是,根据包含的 div 有多少空间以及标题的长度,它导致我的“后摘要”(标题、日期等)div 换行到下一行,使其看起来像

:它在做什么: 在此处输入图像描述

这是我想要它执行的操作(注意标题已换行,但并没有导致整个“ post-summary" 换行到下一行,如前面的示例所示): 在此处输入图像描述

我不在乎标题换行多少或换行多少行。我只是不希望它像第一个示例中那样强制包含帖子信息的父 div 低于帖子编号。

我创建了一个示例 jsfiddle 来进一步说明我在做什么: http://jsfiddle.net/aZvVU/6/

如何使帖子标题简单当它没有足够的空间时换行并且不会导致它使整个“后摘要”div 下降到下一行?如果可以避免的话,我不想固定标题宽度。

I want to have a series of posts with titles and other information. The problem is, depending on how much space the containing div has and how long the titles are, it is causing my "post-summary" (title, date, etc) div to wrap to the next line making it look like:

Here is what it is doing:
enter image description here

Here is what I want it to do (note the title wrapped, but didn't cause the whole "post-summary" to wrap to the next line like in the previous example):
enter image description here

I don't care how much the title wraps or how many lines it wraps to. I just don't want it to force the parent div which also contains the post-info to drop below the post number like it does in the first example.

I've created an example jsfiddle to further illustrate what I'm doing:
http://jsfiddle.net/aZvVU/6/

How can I make the post-titles simply wrap when it doesn't have enough space and not cause it to make the whole "post-summary" div from dropping to the next line? I don't want to make the title width fixed if I can avoid it.

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

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

发布评论

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

评论(3

深爱成瘾 2024-10-25 05:41:30

您可以将此作为起点 http://jsfiddle.net/A8csU/

HTML:

<div id="parent">
    <div id="right-child">
        <p>Right stuff.</p>
    </div>
    <div id="left-child">
        <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It  has roots in a piece of classical Latin literature from 45 BC, making it  over 2000 years old. Richard McClintock, a Latin professor at  Hampden-Sydney College in Virginia, looked up one of the more obscure  Latin words, consectetur, from a Lorem Ipsum passage, and going through  the cites of the word in classical literature, discovered the  undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33  of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by  Cicero, written in 45 BC. This book is a treatise on the theory of  ethics, very popular during the Renaissance. The first line of Lorem  Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section  1.10.32.</p>
    </div>
</div>

CSS:

#left-child {
 border: 1px solid red;
    margin-right: 102px; /* 100px + 1px border + 1px border */
}

#right-child {
    width: 100px;
    border: 1px solid blue;
    float: right;
}

You can use this as a starting point http://jsfiddle.net/A8csU/

HTML:

<div id="parent">
    <div id="right-child">
        <p>Right stuff.</p>
    </div>
    <div id="left-child">
        <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It  has roots in a piece of classical Latin literature from 45 BC, making it  over 2000 years old. Richard McClintock, a Latin professor at  Hampden-Sydney College in Virginia, looked up one of the more obscure  Latin words, consectetur, from a Lorem Ipsum passage, and going through  the cites of the word in classical literature, discovered the  undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33  of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by  Cicero, written in 45 BC. This book is a treatise on the theory of  ethics, very popular during the Renaissance. The first line of Lorem  Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section  1.10.32.</p>
    </div>
</div>

CSS:

#left-child {
 border: 1px solid red;
    margin-right: 102px; /* 100px + 1px border + 1px border */
}

#right-child {
    width: 100px;
    border: 1px solid blue;
    float: right;
}
墨洒年华 2024-10-25 05:41:30

您只需对代码进行一些调整,如下所示:

.post-list
{
    float: left;
    margin-right: 250px;  /* make sure it doesn't touch your right-nav*/
}

.right-nav
{
    background-color: #c0c0c0;
    padding: 10px;
    min-height: 300px;
    width: 200px;
    position: absolute; right: 0px; top: 0px; /* position it */
}

这是一个工作示例,其中代码为 http://jsfiddle。 net/YumSP/

编辑以修复标题换行 http://jsfiddle.net/YumSP/1/< /a>

You can just make a few tweaks to your code like so:

.post-list
{
    float: left;
    margin-right: 250px;  /* make sure it doesn't touch your right-nav*/
}

.right-nav
{
    background-color: #c0c0c0;
    padding: 10px;
    min-height: 300px;
    width: 200px;
    position: absolute; right: 0px; top: 0px; /* position it */
}

Here is a working example, with your code http://jsfiddle.net/YumSP/

edited to fix title wrapping http://jsfiddle.net/YumSP/1/

余厌 2024-10-25 05:41:30

将浮动放在 .post-summary 上,您不需要它,它会导致您的换行。然后在.post-summary中添加margin-left: 40px;,确保有.post-number的空间,并给出错觉.post-number 的完整高度列,而不必破坏高度。

要整理导航栏,请从 .post-list 中删除浮动并删除您的清除

。如果您希望导航栏占据一整列,请向 .post-list 添加 margin-right:220px

例如:

Drop the floating on .post-summary, you don't need it and it is causing your wrapping. Then add margin-left: 40px; to .post-summary to ensure that there is room for the .post-number and give the illusion of a full height column for the .post-number without having to muck about the heights.

To sort out the navbar, drop the floating from .post-list and kill off your clearing <div>s. If you want the navbar to take up a whole column then add a margin-right:220px to .post-list.

For example:

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