简体   繁体   English

当存在左浮动图像时,使用悬挂缩进格式化段落

[英]Format paragraphs with hanging indent when left-floated images are present

I've got the following HTML, generated by TYPO3 (simplified): 我有以下HTML,由TYPO3生成(简化):

<div class="image">
    <img src="some-image.jpg"/>
</div>
<div class="text">
<p>
    <b>foo</b> Some text of varying length, sometimes multiple lines, sometimes not. sit amet enim ultrices a tempus nulla lobortis.
</p>
<p>
    <b>bar</b> Short text.
</p>
<p>
    <b>baz</b> Another paragraph. sit amet enim ultrices a tempus nulla lobortis. Cum ociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam sed felis mauris, in rhoncus nisl.
</p>

</div>

The image div is floated left, and the paragraphs should be formatted as list (hanging indent) with the part in <b> -tags as bullet. 图像div向左浮动,段落应格式化为列表(悬挂缩进),其中<b> -tags中的部分为子弹。 So the result should look like this: 所以结果应该是这样的:

-------------------- foo Some text of varying length, sometimes
|                  |     multiple lines, sometimes not. sit amet enim
|                  |     ultrices a tempus nulla lobortis.
|      Image       | 
|                  | bar Short text.
--------------------
baz Another paragraph. sit amet enim ultrices a tempus nulla lobortis.
    Cum ociis natoque penatibus et magnis dis parturient montes, nascetur
    ridiculus mus. Nullam sed felis mauris, in rhoncus nisl.

How do I achieve this behaviour? 我该如何实现这种行为?

My attempt was to indent the whole paragraph with p {padding-left: 50px;} and unindent the bold tags with pb:first-child {margin-left: -50px;} . 我的尝试是用p {padding-left: 50px;}缩进整个段落,并用pb:first-child {margin-left: -50px;}粗体标记。 That works fine if there is no image or the image is floated to the right, but when the image is floated left, the "bullets" ( <b> -tags) are rendered on top of the image. 如果没有图像或图像向右浮动,那么工作正常,但当图像向左浮动时,“子弹”( <b> -tags)将呈现在图像的顶部。 I've tried other approaches (using text-indent and such things) as well, but the problem basically stays the same. 我也尝试过其他方法(使用text-indent等等),但问题基本保持不变。

The solution doesn't need to (:-)) work on IE6, and if it does not work in some other browser, that would probably be acceptable. 解决方案不需要 (:-))在IE6上工作,如果它在其他浏览器中不起作用,那可能是可以接受的。

EDIT: Ok, I've got a solution which uses TYPO3 facilities: Change the image appearance type of the content element from "in text, left" to "besides text, left". 编辑:好的,我有一个使用TYPO3设施的解决方案:将内容元素的图像外观类型从“in text,left”更改为“除了text,left”。 Then TYPO3 adds a large enough left margin to the div with class text , and everything works fine. 然后TYPO3为div添加了一个足够大的左边距和类text ,一切正常。 If the list is longer than the image, you've got to split the content element into two, to make the paragraphs below the image display left again. 如果列表长于图像,则必须将内容元素拆分为两个,以使图像下方的段落再次显示。

<div class="contain">
<div class="image">
<img src="some-image.jpg"/>
</div>
<div class="text">
<p style="margin-bottom:20px;text-indent:-20px;">
<b>foo</b> Some text of varying length, sometimes multiple lines, sometimes not. sit amet enim ultrices a tempus nulla lobortis.
</p>
<p style="text-indent:-20px;" >
<b>bar</b> Short text.
</p>
<p style="clear:both;text-indent:-20px;margin-left:20px;">
<b>baz</b> Another paragraph. sit amet enim ultrices a tempus nulla lobortis. Cum ociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nullam sed felis mauris, in rhoncus nisl.
</p>
</div>
</div>​

CSS: CSS:

.contain{
  width:400px;
 }
.image{
 margin-top:15px;
 margin-right:30px;
 float:left;
 width:120px;
 height:80px;
 border:thin red solid;
 }​

DEMO DEMO

I'm not quite sure this is what you are looking for, but I use clear: both; 我不太确定这是你在寻找什么,但我使用clear: both; in my CSS when I want to 'escape' from floating objects. 在我想要从浮动对象中“逃脱”的CSS中。

.clearfloat
{
    clear:both;
}

For example: 例如:

<img class="float"> ... </>
<h1 class="clearfloat"> Hello </h1>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM