我遇到了一个问题,在许多内容页面中有一个消息框。在此消息框中,您可以使用图像、文本、标题等。然而,每个消息框都会在右上角有一个图标。如果我使用position:absolute,则图像将很好地放置在框的顶部。但是,如果消息框具有标题或段落并填充了框的宽度,则文本将位于图像下方。
基本上,我需要一个围绕图像的包装器,其宽度使得文本只能显示到图像的边缘。我99%确定通过将absolute定位图像包装在div中并给其一些样式在firebug中实现了它。但今天似乎无法使其工作!
由于有数百个页面,因此无法移动HTML。图像目前没有包装器。所以我必须使用Jquery来包装图像。(如果这是答案的话)。
我知道position:absolute将元素从文档流中移出,但是否有什么办法?
无论如何,以下是我的代码:
JS会在图片周围包裹一个div标签。
CSS:
基本上,我需要一个围绕图像的包装器,其宽度使得文本只能显示到图像的边缘。我99%确定通过将absolute定位图像包装在div中并给其一些样式在firebug中实现了它。但今天似乎无法使其工作!
由于有数百个页面,因此无法移动HTML。图像目前没有包装器。所以我必须使用Jquery来包装图像。(如果这是答案的话)。
我知道position:absolute将元素从文档流中移出,但是否有什么办法?
无论如何,以下是我的代码:
<div class="message">
<h3>Some text, a header perhaps? But this is the next that will sit under the image, sometimes it's a p tag.</h3>
<img class="messageIcon" src="/link-to-icon/which-are-the-same-size" border="0" width="64" >
<p>Some more random text that would appear in the messagebox this could go on for a few lines.</p>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('img.messageIcon').wrap('<div class="messageIconWrap" />');
alert("this is a test");
});
</script>
JS会在图片周围包裹一个div标签。
CSS:
.messageIconWrap{
display: inline-block;
float:right;
height:60px;
width:60px;
}
div.message {
position: relative;
}
.message {
background: none repeat scroll 0 0 #393939;
clear: both;
}
.messageIcon {
position: absolute;
right: 20px;
top: 20px;
float: right;
}
JS Fiddle - http://jsfiddle.net/jdp7E/
float
到右侧吗? - CBroe