简体   繁体   English

CSS流体图像替换?

[英]CSS fluid image replacement?

Using CSS to replace text with an image is a well known practice. 使用CSS将文本替换为图像是众所周知的做法。 CSS-Tricks has a museum of some techniques ( http://css-tricks.com/examples/ImageReplacement/ ). CSS-Tricks有一些技术博物馆( http://css-tricks.com/examples/ImageReplacement/ )。

But none of these allows for replacement with a fluid image (for example, a logo that stretches across 100% of a fluid page layout). 但是这些都不允许用流体图像替换(例如,横跨100%流体页面布局的徽标)。 Is it possible to use CSS to do a fluid image replacement? 是否可以使用CSS进行流体图像替换?

Almost all image replacement techniques use a background-image. 几乎所有图像替换技术都使用背景图像。 And I know that you can set background-size: 100% . 我知道你可以设置background-size: 100% But it's not straightforward to get the height of the text element to scale with it's width because the browser doesn't consider the background image as part of the content. 但是,将文本元素的高度与其宽度一起缩放并不是直截了当的,因为浏览器不会将背景图像视为内容的一部分。

I understand that any of the common image replacement techniques could be easily combined with media queries to incrementally change the size of the text element to specific height x width ratios that work. 据我所知,任何常见的图像替换技术都可以轻松地与媒体查询相结合,逐步将文本元素的大小更改为特定的高度x宽度比率。 But that is incremental, not fluid. 但这是增量的,而不是流动的。

I did find a blog post that discusses this ( http://viljamis.com/blog/2011/fluid-image-replacement.php ). 我找到了一篇讨论此内容的博客文章( http://viljamis.com/blog/2011/fluid-image-replacement.php )。 But it turns out thay method actually requires putting an image in the html content. 但事实证明,实际上需要在html内容中放置一个图像。 I'm looking for real text replacement. 我正在寻找真正的文本替代品。

Took some fiddling, but I figured out a way. 采取了一些摆弄,但我想出了办法。 The key is to use padding percentage to set the height, because padding-top and padding-bottom percentage is linked to container width (unlike height , which is linked to container height). 关键是使用填充百分比来设置高度,因为padding-toppadding-bottom百分比与容器宽度相关联(与height不同,它与容器高度相关联)。

html HTML

<h1 class="logo">The Logo</h1>

css CSS

h1.logo {
    background-image: url('logo.png');
    background-size: 100%;
    width: 100%;
    padding-top: 29.8%;
    height: 0;
    text-indent: -9999px;
}

Where padding-top is calculated by dividing the image height by width. 通过将图像高度除以宽度来计算padding-top

Working example: http://jsfiddle.net/bXtRw/ 工作示例: http//jsfiddle.net/bXtRw/

I'll note that using overflow: hidden instead of text-indent: -9999px should also work. 我会注意到使用overflow: hidden而不是text-indent: -9999px也应该有效。 But I get unstable behavior in Firefox. 但是我在Firefox中遇到了不稳定的行为。

Also, using font-size: 0 instead of height: 0 produces unstable behavior in Firefox. 此外,使用font-size: 0而不是height: 0会在Firefox中产生不稳定的行为。

On the div that contains the background-image: 在包含background-image的div上:

div {
    max-width: 100%;
    width: 100%;
    min-height: 300px; //Adjust this number accordingly
    height: auto;
}

I use a method identical to @Warren Whipple, but I usually use / . 我使用的方法与@Warren Whipple相同,但我通常使用 / If you're not limited to using vanilla CSS, this method nicely abstracts a few pieces: 如果你不仅限于使用vanilla CSS,那么这个方法很好地抽象了几个部分:

// Only works in Compass/Sass – not regular CSS!
h1.logo {

    $header-logo-image: "logo.png";

    background: image-url($header-logo-image) no-repeat;
    background-size: 100%;
    height: 0;
    padding-top: percentage( image-height($header-logo-image) / image-width($header-logo-image) );
    overflow: hidden;
    width: 100%;
}

You should just have to replace the $header-logo-image variable with the name of your image. 您只需要将$header-logo-image变量替换为$header-logo-image名称即可。


In addition, I sometimes add: max-width: image-width($header-logo-image); 另外,我有时会添加: max-width: image-width($header-logo-image); , which will prevent the h1 from being sized any larger than its background image. ,这将防止h1大小任何大于其背景图像。

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

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