父div浮动干扰子div的宽度
我想做的是有一个非常简单的工具提示,可以使用 jQuery 在悬停时隐藏/显示。
为此,我所做的就是在 li 上放置一个 div会有一个工具提示。这个div绝对位于每个li的顶部。工具提示 div 将包含不同长度的文本,因此我不能只预先定义其宽度。我只是希望宽度根据文本长度一直延伸。
我的问题是,当我浮动 li 时,div 也会浮动(或者看起来如此),因此采用浮动 li 的宽度。工具提示现在变得与 li 的宽度一样窄,这是我不喜欢发生的情况。
这是 HTML:
<ul id="images">
<li><img src="[some image]" /><div class="tooltip">Some text that is quite long.</div></li>
<li><div class="tooltip">Some text that is quite small.</div><img src="[some image]" /></li>
<li><img src="[some image]" /></li>
<li><img src="[some image]" /></li>
</ul>
这是 CSS:
#images li {
list-style: none;
margin-left: 10px;
position: relative;
}
.tooltip {
color: #FFF;
clear: both;
background: #000;
padding: 10px;
font: 11px Arial, Helvetica, sans-serif;
-moz-border-radius: 5px;
position: absolute;
top: -50px;
display: none;
border: 2px solid #999;
}
我希望有人能帮助我解决这个问题。
What I am trying to do is to have a very simple tooltip that hides/shows on hover using jQuery.
(jsFiddle demo of the problem)
To do this, what I did was to put a div on li's that will have a tooltip. This div is absolutely positioned on top of each li. The tooltip div will contain varying lengths of text so I cannot just pre-define its width. I just want the width to extend all the way depending on the text length.
My problem is that when I float the li, the div also gets floated (or so it seems) hence taking the width of the floated li. The tooltip now becomes as narrow as the width of the li, which I don't like to happen.
This is the HTML:
<ul id="images">
<li><img src="[some image]" /><div class="tooltip">Some text that is quite long.</div></li>
<li><div class="tooltip">Some text that is quite small.</div><img src="[some image]" /></li>
<li><img src="[some image]" /></li>
<li><img src="[some image]" /></li>
</ul>
This is the CSS:
#images li {
list-style: none;
margin-left: 10px;
position: relative;
}
.tooltip {
color: #FFF;
clear: both;
background: #000;
padding: 10px;
font: 11px Arial, Helvetica, sans-serif;
-moz-border-radius: 5px;
position: absolute;
top: -50px;
display: none;
border: 2px solid #999;
}
I hope someone would help me with this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您始终可以事先计算出准确的宽度。
例如,您在页面外部创建一个工具提示 div,例如
,
如果添加以下 js,它将显示
更新良好的小提琴 这里
You can always calculate the exact width before.
For example, you create a tooltip div outside of your page like
and
If you add the following js, it will show nicely
updated fiddle here
块元素扩展到其容器的宽度。浮动不会改变这一点。
如果您希望工具顶部有不同的宽度,请使用
width
并指定宽度以像素为单位。如果您想走更高级的路线,您可以定义宽度和高度,检查溢出并使用 JavaScript 使其更宽。
Block elements expand to the width of their containers. Float doesn't change this.
If you want a different width on the tool-top use
width
and specify the width in pixels.If you want to go a fancier route, you can define a width and height, check for overflow and make it wider using JavaScript.
我认为你需要稍微改变一下你的方法!您可以在具有绝对位置的 div 内使用其 HTML 内容,并根据悬停对象的位置设置其位置,而不是使用与实际工具提示相同的 div。
其中 #myTooltip 是在列表外部定义的具有绝对位置的 div。
I think you need to change your approach a bit! Instead of using the same div as the actual tooltip, you can use its HTML content inside a div that has an absolute position and you set its position based on the position of the object being hovered.
where #myTooltip is a div defined outside your list that has an absolute position.