在 HTML 中创建一个框并将其在普通文本中向左或向右对齐?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8572998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 12:06:05  来源:igfitidea点击:

Create a box in HTML and align it left or right inside a normal text?

html

提问by grady

lets say I have a normal text in a paragraph (<p>...</p>). Now I want to place a box with for example ads or additional information somewhere in the text to the right or left. It should NOT be above or below the text, the text should align itself around the box.

假设我在段落中有一个普通文本(<p>...</p>)。现在我想在文本右侧或左侧的某处放置一个带有例如广告或附加信息的框。它不应位于文本上方或下方,文本应围绕框对齐。

How would I do this? I tried with span and div but failed so far. Ideas?

我该怎么做?我尝试使用 span 和 div 但到目前为止都失败了。想法?

Thanks :-)

谢谢 :-)

回答by socha23

Use <div>with floatCSS property.

<div>floatCSS 属性一起使用。

<p>
    <div style="background-color: red; height: 100px; width: 100px; float: right"></div>
    XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX 
    XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX 
    XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX 
    XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX 
    XXXXXX XXXXXX XXXXXX XXXXXX XXXXXX 
</p>

回答by Steve Pugh

This isn't possible with HTML alone but with CSS you can float a child element to one side.

单独使用 HTML 是不可能的,但是使用 CSS 您可以将子元素浮动到一侧。

http://jsfiddle.net/9A2vB/

http://jsfiddle.net/9A2vB/