简体   繁体   English

CSS div 内容和字体图标缩放

[英]CSS div content and font icon scaling

This question has 2 parts.这个问题有两部分。

Part 1第1部分

I have the situation where I want to show an indicator:我有一种情况,我想显示一个指标:

HTML HTML

<div class="alert-icon alarm">
     <div class="hmi-icon-alarm"></div>
</div>

hmi-icon-alarm is an icon from a font file. hmi-icon-alarm 是字体文件中的图标。

CSS CSS

.alert-icon.alarm {
    background-color: #c4262e;
    border-style: solid;
    border-color: #ffffff;
}

.alert-icon.alarm .hmi-icon-alarm {
    margin: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

The above code works fine, but ideally to simplify things I would like to update the HTML to this:上面的代码工作正常,但理想情况下,为了简化事情,我想将 HTML 更新为:

<div class="alert-icon alarm"></div>

and based on the "alert-icon.alarm" class selector I would like to add the child div that sets the font icon.并基于“alert-icon.alarm”类选择器,我想添加设置字体图标的子div。 Similar to doing something like this:类似于做这样的事情:

.alert-icon.alarm::after {
    content: '<div class="hmi-icon-alarm"></div>';
}

I know that this is not possible using content, but is there another (browser supported) way of doing this?我知道使用内容这是不可能的,但是还有另一种(支持浏览器的)方法吗?

Part 2第2部分

Given the same html and css I want to be able to control the size of the font icon so that it is proportional to the width defined in the class .alert-icon (width and height will always be the same), so it can be scaled up and down and look the same (albeit a different size)给定相同的 html 和 css,我希望能够控制字体图标的大小,使其与类 .alert-icon 中定义的宽度成比例(宽度和高度将始终相同),因此可以放大和缩小并看起来相同(尽管大小不同)

HTML HTML

<div class="alert-icon alarm">
     <div class="hmi-icon-alarm"></div>
</div>

hmi-icon-alarm is an icon from a font file. hmi-icon-alarm 是字体文件中的图标。

CSS CSS

.alert-icon {
    width: 200px;
    height: 200px;
}

.alert-icon.alarm {
    background-color: #c4262e;
    border-style: solid;
    border-color: #ffffff;
}

.alert-icon.alarm .hmi-icon-alarm {
    margin: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
.alert-icon {
    --size: 200; /* Icon width */
    --scale-factor: 0.25 /* Icon size relative to container */;
    width: calc(var(--size) * 1px)
}
.alert-icon.alarm::after {
    margin: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    display: block;
    font-size: calc(var(--size) / var(--scale-factor) * 1px);
    transform: translate(-50%, -50%);
    /* All other properties belonging to .hmi-icon-alarm */ 
}

NOTE: Maybe it will be not supported in IE.注意: IE 可能不支持它。

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

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