简体   繁体   English

将跨度放在div中的特定位置

[英]Placing span at a specificr position in div

I have 3 span tags (x, y, and z) within a div tag, with span z being the last span in div. 我在div标签中有3个span标签(x,y和z),span z是div中的最后一个span。 I want the text within span z to be shown at lower right cornor of div. 我希望span z中的文本显示在div的右下角。 How can I do that? 我怎样才能做到这一点?

I have tried the following code but it does not work: 我尝试了以下代码,但它不起作用:

<span align="right" style="font-size: 0.76em;" >my text here</span>

span is not a block level element, so I don't think you can position it relative to another element. span不是块级元素,所以我认为你不能相对于另一个元素定位它。

Make it a div with style="position:absolute;right:0;bottom:0;text-align:right' 使用style="position:absolute;right:0;bottom:0;text-align:right'将其设为div

Make sure that the containing div has style="position:relative;" 确保包含div的style="position:relative;" to contain the absolute object. 包含绝对对象。

JSfiddle: http://jsfiddle.net/gHZqs/ JSfiddle: http//jsfiddle.net/gHZqs/

Edit 编辑

You can use the span tag instead of the div, as the others have said. 您可以使用span标记而不是div,正如其他人所说的那样。 However span tags are for inline content. 但是,span标记用于内联内容。 If you take the content out of the flow with absolute positioning then you should use a div IMO. 如果您使用绝对定位从流中取出内容,那么您应该使用div IMO。 If you're working within a framework or something and you need to use the span tag then I wouldn't lose any sleep over it. 如果你在一个框架或某个东西中工作而你需要使用span标记,那么我就不会失去任何睡眠。 Here's a link on a similar question: 这是一个类似问题的链接:

SPAN vs DIV (inline-block) SPAN与DIV(内联块)

Much the same as @RSG is saying however you can use a span instead of converting it to a DIV . 与@RSG大致相同但是你可以使用span而不是将其转换为DIV Assign the parent container position:relative and then assign the span.z with the following css: 分配父容器position:relative ,然后使用以下css分配span.z

span.z{
    position:absolute;
    bottom:0;
    right:0;
}

See working example here: http://jsfiddle.net/crYaw/1/ 请参阅此处的工作示例: http//jsfiddle.net/crYaw/1/

HTML: HTML:

<div id="container">
    <p>Test here. Test here. Test here. Test here. Test here. Test here. Test here. Test here. </p>
    <p>This is some <span class="x">more</span> text</p>
    <p>This <span class="y"is some</span> >more text</p>
        <span class="z">Float me bottom right</span>
</div>

CSS: CSS:

#container{
    position:relative;
    width:500px;
    height:200px;
    background-color:#eee;
}

span.x{
    font-weight:bold;
}

span.y{
    color:red;
}

span.z{
    position:absolute;
    bottom:0;
    right:0;
}

There is a text-align css value that can be used but it only applies to block level elements and the contents within the block level element (in this case your div). 有一个text-align css值可以使用,但它只适用于块级元素和块级元素内的内容(在本例中为div)。 Since you don't want all items in the div aligned to the right you can either float:right; 由于您不希望div中的所有项目都与右侧对齐,因此您可以float:right; the z span or wrap it in another div that has text-align:right; z span或将其包装在另一个具有text-align:right; div中text-align:right; . Here are both solutions: 以下是两种解决方案:

http://jsfiddle.net/y9pvv/1/ http://jsfiddle.net/y9pvv/1/

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

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