带有背景图像的 CSS Div - 如何允许页面上的 div 扩展?

发布于 2024-11-11 00:22:48 字数 439 浏览 3 评论 0原文

我不确定如何表达这个主题的问题......抱歉。

我刚刚开始学习CSS。

我有一个带有背景图像的

,并且
中有文本。我读到,选择 em 字体大小是一个不错的选择,因为有些人可能需要在浏览器中使用更大的文本大小。因此使用 em 设置 font-size 可以更好地适应这些类型的用户。

但允许调整文本大小的问题是,在许多情况下,我的

中的文本将超出背景图像的大小,并使页面看起来很糟糕并且设计不好。

有没有办法使用CSS并允许背景图像'匹配''扩展'以适应更大的文本大小?

I wasn't sure how to word the question for this topic...sorry.

I'm just starting to learn CSS.

I have a <div> with a background image and there is text within the <div>. I read that choosing font sizes in em is a good choice because some people might require larger text sizes in their browsers. So setting the font-size with em would accommodate these types of users better.

But the problem with allowing the text to be resized, is that in many cases, the text within my <div> is going to go beyond the size of the background image and make the page look terrible and poorly designed.

Is there a way to use CSS and allow the background image to 'match' or 'expand' to accommodate to larger text size?

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

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

发布评论

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

评论(2

流年里的时光 2024-11-18 00:22:48

您可以将 divwidth 设置为 img width,这样它就不会扩展得更宽(超出图像) )。

当然,放大的文本会迫使 div 沿高度增长。

您还可以将 background-img 设置为重复(如果图像允许),以便当文本展开时,图像会重复。

background-image:url('whatever.png');
background-repeat:repeat-x;  

// x = horizontal, y = vertical

You could set the width of the div to the img width so that it never expands wider (beyond the image).

Of course, the enlarged text would force the div to grow height-wise.

You could also set the background-img to repeat (if the image allows for it), so that when the text expands, the image is repeated.

background-image:url('whatever.png');
background-repeat:repeat-x;  

// x = horizontal, y = vertical
浅忆 2024-11-18 00:22:48

由于您刚开始,您可能需要阅读 http://na.isobar.com/standards/# _pixels_vs_ems 其中他们说:

我们使用 px 测量单位
定义字体大小,因为它提供
对文本的绝对控制。我们意识到
使用 em 单位调整字体大小
曾经很流行,为了容纳
Internet Explorer 6 不调整像素大小
基于文本。然而,所有主要
现在浏览器(包括IE7和IE8)
支持以像素为单位调整文本大小
和/或整页缩放。由于 IE6 是
很大程度上被认为已弃用,像素
尺寸是首选。此外,
首选无单位行高
因为它没有继承
其父级的百分比值
元素,而是基于
字体大小的乘数。

正确:

#selector {
    font-size: 13px;
    line-height: 1.5;  /*  13 * 1.5 = 19.5 ~ Rounds to 20px. */
}

不正确:

/*  Equivalent to 13px font-size and 20px line-height, but only if the browser default text size is 16px. */
#selector {
    font-size: 0.813em;
    line-height: 1.25em;
}

Since you are starting out, you might want to read http://na.isobar.com/standards/#_pixels_vs_ems wherein they say:

We use the px unit of measurement to
define font size, because it offers
absolute control over text. We realize
that using the em unit for font sizing
used to be popular, to accommodate for
Internet Explorer 6 not resizing pixel
based text. However, all major
browsers (including IE7 and IE8) now
support text resizing of pixel units
and/or full-page zooming. Since IE6 is
largely considered deprecated, pixels
sizing is preferred. Additionally,
unit-less line-height is preferred
because it does not inherit a
percentage value of its parent
element, but instead is based on a
multiplier of the font-size.

Correct:

#selector {
    font-size: 13px;
    line-height: 1.5;  /*  13 * 1.5 = 19.5 ~ Rounds to 20px. */
}

Incorrect:

/*  Equivalent to 13px font-size and 20px line-height, but only if the browser default text size is 16px. */
#selector {
    font-size: 0.813em;
    line-height: 1.25em;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文