简体   繁体   English

在段落内浮动块级元素

[英]Floating a block-level element inside a paragraph

I'm writing a compiler from a high-level document to HTML. 我正在编写从高级文档到HTML的编译器。 Basically, I want: 基本上,我想要:

Because blah blah{{margin-note|...}}, it yada yada.

to compile to the following paragraph: 编译为以下段落:

Because blah blah¹, it yada yada.

and with a box floating on the right of the paragraph in the same level as the margin note number. 并在该段落的右侧浮动一个框,该框与边注编号相同。

The way I approached this is to compile it to the following HTML: 我采用的方法是将其编译为以下HTML:

<p>
  Because blah blah<span class="mg-note-num">1</span><span class="mg-note-box">...</span>, it yada yada.
</p>

where mg-note-box shifts the element to the right of the paragraph. 其中mg-note-box将元素移到段落的右侧。 This works really well, except that the margin note box couldn't support block-level elements, like ul . 这确实非常有效,除了页边空白注释框不支持块级元素(如ul When I put ul inside the margin note box, the ul element will be thrown outside of the span tag. 当我将ul放在边距注释框中时, ul元素将被抛出到span标签之外。

The obvious fix is to change span to div so that ul can stay inside the div , but this is not an option either because div can't stay inside p . 明显的解决方法是将span更改为div以便ul可以留在div ,但这不是一个选择,因为div不能留在p Doing so will break the paragraph into two. 这样做会将段落分为两部分。 Instead of: 代替:

Because blah blah¹, it yada yada.

we would have this instead 我们将有这个

Because blah blah¹

, it yada yada.

I guess another option is to have a collection phase in my compiler, and move all margin note boxes outside of paragraph. 我猜另一种选择是在编译器中有一个收集阶段,并将所有边距注释框移到段落外。 This is also undesirable because for a long paragraph, the position of the margin note boxes will no longer match margin note numbers. 这也是不希望的,因为对于较长的段落,边注栏的位置将不再与边注号匹配。

How should I approach this? 我应该如何处理?


Update: I should have mentioned that my experiments with inline-block also don't work, but perhaps I did it wrong. 更新:我应该提到我的inline-block实验也不起作用,但是也许我做错了。 Here are what I did: 这是我所做的:

Let's start with a reference layout (which functions properly but doesn't support block-level elements) 让我们从参考布局开始(它可以正常运行,但不支持块级元素)

<p>
  before
  <span style="float: right;">Hello world</span>
  after
</p>

This results in 这导致

| before after             Hello world |

With div and inline-block : 使用divinline-block

<p>
  before
  <div style="float: right; display: inline-block;">Hello world</div>
  after
</p>

This results in: 结果是:

| before                        |
|                               |
| after             Hello world |

With span and inline-block : 使用spaninline-block

<p>
  before
  <span style="float: right; display: inline-block;">
    <ul><li>1</li></ul>
  </span>
  after
</p>

This results in: 结果是:

| before                        |
|                               |
| - 1                           |
|                               |
| after                         |

So, no, inline-block doesn't seem to work. 因此,不, inline-block似乎不起作用。

The requirement to handle block elements within a paragraph (this is the important part) is the root of the issue. 处理段落中的块元素的要求(这是重要的部分)是问题的根源。

Nesting a block element in a paragraph is not possible. 不能在段落中嵌套块元素。 Paragraphs are block-level elements, and automatically close if another block-level element is parsed before the closing </p> tag. 段落是块级元素,如果在</p>标记之前解析了另一个块级元素,则段落会自动关闭。 For example, this HTML: 例如,以下HTML:

<p>Before<div>Block</div>After</p>

results in: 结果是:

<p>Before</p>
<div>Block</div>
<p>After</p>

which causes the break around the block element. 这会导致块元素周围的断裂。

The only HTML structure that will achieve the result you want is to use an inline element like a <span> (without any nested block elements). 唯一可以实现所需结果的HTML结构是使用内联元素(例如<span> (不带任何嵌套的块元素)。 Here's an example: 这是一个例子:

 p { font-family: Arial; position: relative; padding-right: 120px; } .note-num { font-size: 10px; margin: 0 1px 0 -3px; } .note-content { position: absolute; right: 0; font-size: 12px; max-width: 100px; } 
 <p> 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. <sup class="note-num">1</sup> <span class="note-content"> Inline Note </span> 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. </p> 

However.... 然而....

There's one way we can kinda achieve the result you want, which is to use a <div> in place of the wrapping <p> . 我们可以通过一种方法来获得所需的结果,即使用<div>代替包装的<p> Like this: 像这样:

 p, .p { font-family: Arial; position: relative; padding-right: 120px; } .note-num { font-size: 10px; margin: 0 1px 0 -3px; } .note-content { position: absolute; right: 0; font-size: 12px; max-width: 100px; } .note-content ul { margin: 0; padding: 0; } 
 <div class="p"> 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. <sup class="note-num">1</sup> <span class="note-content"> <ul><li>Block Note</li></ul> </span> 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> 

My friend, Pavel Panchekha , found another method that doesn't need to change <p> to <div> : use <object> instead of <span> : 我的朋友Pavel Panchekha发现了另一种不需要将<p>更改为<div> :使用<object>而不是<span>

 p { font-family: Arial; position: relative; padding-right: 120px; } .note-num { font-size: 10px; margin: 0 1px 0 -3px; } .note-content { position: absolute; right: 0; font-size: 12px; max-width: 100px; } .note-content ul { margin: 0; padding: 0; } 
 <p> 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. <sup class="note-num">1</sup> <object class="note-content"> <ul><li>Block Note</li></ul> </object> 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. </p> 

Try adding display: inline-block to your <span> 's style... 尝试将display: inline-block添加到您的<span>的样式中...

See: https://www.w3schools.com/cssref/pr_class_display.asp 请参阅: https//www.w3schools.com/cssref/pr_class_display.asp

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

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