简体   繁体   English

CSS | 将div放在之后 <p> 标签

[英]CSS | Place div after <p> tag

I am trying to add this tooltip right after ap tag's text finishes. 我正在尝试在ap标签的文本完成后立即添加此工具提示

In this example the tooltip show an image when i hover it: 在此示例中,工具提示会在我将鼠标悬停时显示图像:

<p>This is some text in a paragraph.</p> 
<div class="help-tip">
    <p><img src="balloon.jpg" width="300" /></p>
</div>

How could i align the .help-tip right after the "This is some text in a paragraph" paragraph? 我如何在“这是段落中的某些文字”段落之后对齐.help-tip?

Regards 问候

First of all your elements dont align because the img is wrapped with a p tag, which automatically changes the line. 首先,您的元素未对齐,因为img用p标签包裹,该标签会自动更改行。

<p>This is some text in a paragraph.</p> 
<div class="help-tip">
    <img src="balloon.jpg" width="300" />
</div>

To solve this you have to keep in mind that your container has to be able to contain both elements. 为了解决这个问题,您必须记住您的容器必须能够包含这两个元素。 So if your img is 300 px wide and your p element is 150px you need a container larger than 450px.A solution would be to wrap the two elements with a wrapper div and and add inline display in css. 因此,如果您的img宽度为300像素,而p元素为150像素,则需要一个大于450像素的容器。解决方案是使用包装器div包装这两个元素并在CSS中添加内联显示。

HTML HTML

<div class="wrapper">
    <span class="foo">This is some text in a paragraph.</span> 
    <div class="help-tip">
         <img src="balloon.jpg" width="300" />
    </div>
</div>

CSS CSS

.foo{
 width:150px;
 positition:relative;
}
.wrapper{
     width:550px;
     positition:relative;
}
.help-tip{
    display:inline;   
}

UPDATE UPDATE

This was not working because the paragraph text was wrappped in p element and it was going below by default. 这是行不通的,因为段落文本被包装在p元素中,并且默认情况下位于下面。 If you have to use the p element you can use position:absolute for the image to align it, or use span rather than p and use this jsFiddle . 如果必须使用p元素,则可以对图像使用position:absolute使其对齐,或者使用span而不是p并使用此jsFiddle

Edit even better code: 编辑更好的代码:

CSS: CSS:

.help-tip {
    /* remove following css: */
    position: absolute;
    top: 18px;
    right: 18px;
    /* add following css: */
    display: inline-block;
    position: relative;
}

HTML: HTML:

<p>This is some text in a paragraph.</p>
<div class="help-tip">
    <p><img src="balloon.jpg" width="300" /></p>
</div>

Other previous method: 其他先前的方法:

.help-tip {
    /* remove following css: */
    position: absolute;
    top: 18px;
    right: 18px;
    /* add following css: */
    float: left;
    margin: -2px 10px;
}
p {
    /* add following css: */
    float: left;
}

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

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