如何将动态文本插入具有绝对位置的 div 中?

发布于 2024-08-04 18:01:10 字数 357 浏览 1 评论 0原文

看看 http://www.barelyfitz.com/screencast/html -training/css/positioning/ 第 6 项。它说:

对于大多数设计来说,这不是一个可行的解决方案,因为我们通常不知道元素中有多少文本,或者将使用的确切字体大小。

为了将动态文本插入具有绝对位置的 div 中,我需要使用什么解决方法?

欢迎任何方法

Take a look at http://www.barelyfitz.com/screencast/html-training/css/positioning/ item 6. It says:

It is not a viable solution for most designs, because we usually do not know how much text will be in the elements, or the exact font sizes that will be used.

What workaround do I need to use in order to insert dynamic text into a div with absolute position?

Any approach is welcome

regards,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

他不在意 2024-08-11 18:01:10

如果您的主要目标是将 div 保持在其位置,而不根据文本量更改其高度或宽度,我会选择:

div {
    overflow: scroll;
}

另一个选项是缩小文本大小以适合 div,但那涉及一定量的模糊数学,您可能会面临文本太小而毫无意义的风险。

如果您希望 div 根据文本更改其高度,这还涉及一些模糊数学,但基本上,您可以通过以下方式获得文本的长度:

var sometext = "Hey, I'm some text!";
var textlength = sometext.length();

并使高度相对于该长度发生变化。您想玩弄数字,但它看起来像:

var div_height = 10 * textlength;
$("div").css("height,"+ div_height +"em");

If your primary goal is to keep the div in it's place, without changing it's height or width based on the amount of text, I'd go with:

div {
    overflow: scroll;
}

The other option is to have the text size shrink to fit into the div, but that involves a certain amount of fuzzy math and you run the risk of the text being so tiny it's pointless.

If you want the div to change it's height based on the text, this also involves some fuzzy math, but basically, you would get the length of the text with:

var sometext = "Hey, I'm some text!";
var textlength = sometext.length();

And make the height change in relation to that length. You'd want to play with the numbers, but it would look something like:

var div_height = 10 * textlength;
$("div").css("height,"+ div_height +"em");
陌路黄昏 2024-08-11 18:01:10

请参阅 W3C 网站的视觉效果部分此处

See Visual Effect section from W3C site here

装迷糊 2024-08-11 18:01:10

也许对动态文本容器使用 "overflow: auto" div.所以高度不是问题。

Maybe using "overflow: auto" for the dynamic-text-container div.So the height isn't a problem.

街道布景 2024-08-11 18:01:10

问题不在于将动态文本放在绝对定位的 div 中 - div 将扩展以适应其中的任何文本。在您的示例中,红色和绿色 div 没有定义高度。

绝对定位的 div 会从文档流中取出,因此 html 中出现在它们之后的任何内容都会表现得好像它们根本不存在一样。

使用绝对定位 div 的设计需要在包含的 div 上定义高度,以便绝对定位 div 不会与其他内容重叠。在您的示例中

定义的高度为 250px。将其更改为 100px,您将看到

移动到红色和绿色 div 下方。

因此,如果侧边栏中有一个绝对定位的 div,后面没有任何内容,则可以添加所需的所有动态文本。如果你的标题中有一个,它将使你的设计实现起来非常复杂。

The problem isn't putting the dynamic text in the absolutely positioned div - the div will expand to fit whatever text is in there. There are no heights defined on the red and green divs in your example.

Absolutely positioned divs are taken out of the flow of the document so anything that appears after them in the html will act like they aren't even there.

Designs that use absolutely positioned divs need to have a height defined on the containing div so the absolutely positioned divs don't overlap other content. In your example <div id="div-1"> has a height of 250px defined. Change that to 100px and you will see <div id="div-after"> move under the red and green divs.

So if you have a absolutely positioned div in a sidebar with nothing after it you can add all the dynamic text you want. If you have one in your header, it is going to make your design very complicated to implement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文