简体   繁体   English

如何使Fontawesome4微调器图标在dom上占据100%的大小?

[英]How to make fontawesome4 spinner icon take 100% size on the dom?

I know I can change the fontawesome4 icon by setting font-weight property. 我知道我可以通过设置font-weight属性来更改fontawesome4图标。 But how can I set the value to take 100% size of the dom. 但是如何设置该值以容纳100%的dom。 For example: 例如:

<i class="fa fa-spinner" />

the size of the <i> is calculated automatically. <i>的大小是自动计算的。 So how can I set the font-weight for the spinner icon based on its size? 那么如何根据微调器图标的大小设置其字体粗细?

This is an example on that: https://codepen.io/zhaoyi0113/pen/LJWVxg You will see if you open this codepen that the spinner is spinning in a very small size. 这是一个示例: https ://codepen.io/zhaoyi0113/pen/LJWVxg您将看到是否打开此Codepen,表明纺纱器正在以非常小的尺寸旋转。 And I want it spinning in the center of the <i> by itself. 我希望它本身在<i>的中心旋转。 It works fine if I remove the height and width from the css. 如果我从CSS移除高度和宽度,效果很好。

I know some classes like fa-5x works but I need to calculate the size of <a> at runtime. 我知道一些类似fa-5x类可以工作,但是我需要在运行时计算<a>的大小。 How can I calculate the spinner size programmically? 如何以编程方式计算微调器尺寸?

Problems 问题

Issue #1 第1期

“…the size of the “ ...的大小 <i> <div> is calculated automatically. <div> 是自动计算的。 So how can I set the 那么我该如何设置 font-weight font-size for the spinner icon based on its size?” 旋转图标的font-size 基于其大小?”

Issue #2 第2期

“And I want it spinning in the center of the <i> by itself.” “而且我希望它自己在<i>的中心旋转。”


The code provided in OP's question has a <div> with the <i> nested within; 提供的代码在OP的问题有一个<div><i>嵌套内; hence this edit references the <div> as the element that has it's size calculated at runtime. 因此,此编辑引用<div>作为具有在运行时计算出的大小的元素。

Omar's answer is on the right track — font-size is the CSS property that's needed to manipulate the icon fonts size. Omar的答案是正确的font-size是操纵图标字体大小所需的CSS属性。 The size of the actual <i> tag itself is irrelevant. 实际<i>标签本身的大小无关紧要。


Solutions 解决方案

Fix #1 修复#1

  • It wasn't mentioned as to how the element (ie <i> refer to the first reference: above ) obtains it's shifting size nor was the range of said sizes provided during runtime, thus the demo has 3 separate examples with varying font-size s and matching width values. 没有提及元素(即<i> 指的是第一个参考: )如何获得其移位大小,也没有在运行时提供所述大小范围的信息,因此该演示有3个单独的示例,它们带有不同的font-size s和匹配的width值。

  • Wrap a block element (ex. such as a <div> ) around the <i> . <i>周围包裹一个块元素(例如<div> )。

  • Each <div> should have matching font-size and width values. 每个<div>应该具有匹配的font-sizewidth值。

  • Each nested <i> will always be the same font-size as it's parent <div> because the font-size of 1em inherits from the parent. 每个嵌套<i>font-size始终与其父级<div>因为1emfont-size是从父级继承的。

Fix #2 修复#2

  • Due to all <i> being the exact font-size as it's parent <div> font-size , there is no remaining space within the parent <div> that allows an <i> to stray from the center. 由于所有<i>font-size与父代<div> font-size ,因此父代<div>中没有剩余空间,允许<i>从中心偏离。


Examples 例子

Details commented in the following demos. 详细信息在以下演示中进行了评论。

Demo #1 演示#1

CSS Solution A: Assuming each <div> 's width , height , and font-size are all of equal value. CSS解决方案A:假设每个<div>widthheightfont-size都相等。

 /* ~REQUIRED~ All <i> have the font-size: 1em -- that particular value is an equivalent to the the same font-size as it's parent's font-size -- thus each <i> will conform to it's parent element's font-size. */ i { font-size: 1em; } /* ~OPTIONAL~ .bx is added to demonstrate that each icon is centered and rotating within it's perimeter instead of orbiting around it. */ .bx { border: 1px solid #000; } /* ~REQUIRED~ .box0, .box1, and .box2 are at arbitrary font-sizes and matching width. */ .box0 { font-size: 50px; width: 50px; } .box1 { font-size: 100px; width: 100px; } .box2 { font-size: 200px; width: 200px; } 
 <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"> <div class='bx box0'><i class="fa fa-spinner fa-spin"></i></div> <div class='bx box1'><i class="fa fa-spinner fa-spin"></i></div> <div class='bx box2'><i class="fa fa-spinner fa-spin"></i></div> 


Demo #2 演示#2

JavaScript Solution: Basically Demo #1 updated with JavaScript. JavaScript解决方案:基本上, 演示#1已使用JavaScript更新。 When the page loads: 页面加载时:

  • the width and font-size values of each <div> will be changed to the value of it's height 每个<div> widthfont-size值将更改为其height的值

  • each .vx is assigned a unique style values in order to demonstrate how the JavaScript can handle <div> s of different sizes 每个.vx都有一个唯一的样式值,以演示JavaScript如何处理不同大小的<div>

  • the result is that each <div> will be a square — it's size is determined by it's height (including padding-top/bottom ). 结果是每个<div>将是一个正方形-它的大小取决于它的height (包括padding-top/bottom )。

 // Collect all .vx into a NodeList then convert it into an array. const vxArray = Array.from(document.querySelectorAll('.vx')); // For each div.vx in the new array (ie vxArray.)... vxArray.forEach(function(div) { // ...get the current <div>'s height and... let vxH = div.clientHeight + "px"; // ...then assign that value to the <div>'s dimensions and... div.style.width = vxH; div.style.height = vxH; // ...change the <div>'s font-size as well. div.style.fontSize = vxH; }); 
 /* ~REFERENCE~ Details of property/values in Demo #1 that occur in Demo #2 can be found in Demo #1. */ i { font-size: 1em; } .vx { border: 1px solid #000; } .box3 { height: 42px; } .box4 { font-size: 89px; } .box5 { height: 4rem; } 
 <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"> <div class='vx box3'><i class="fa fa-spinner fa-spin"></i></div> <div class='vx box4'><i class="fa fa-spinner fa-spin"></i></div> <div class='vx box5'><i class="fa fa-spinner fa-spin"></i></div> 


Note 注意

“How can I make the spinner works if the height is not equal to its width?” “如果高度不等于宽度,如何使微调器起作用?”

If the <div> 's height differs from it's width it should still work. 如果<div>heightwidth不同,它仍然可以工作。 I recommend that the <div> s should be square so that the font icon can fit edge to edge without requiring any extra styling. 我建议<div>应该是正方形的,以便字体图标可以边到边而不需要任何额外的样式。 Keep in mind that the font icon will not stretch itself out of it's original shape in order to conform to wider or taller lengths of a rectangle § . 请记住,字体图标不会超出其原始形状,以适应矩形§更长或更宽的长度。

Demo #3 演示#3

CSS Solution B: Demo #1 applying <div> as rectangles. CSS解决方案B: 演示#1<div>为矩形。

 /* ~OPTIONAL~ Applied is a common default -- needed when layout elements have relative measurements. */ html, body { height: 100%; width: 100%; } i { font-size: 1em; } /* ~REQUIRED~ First 3 property/values centers <i> vertically and horizontally. */ .rx { display: flex; justify-content: center; align-items: center; border: 1px solid #000; } .rec0 { font-size: 50px; height: 50px; } /* ~OPTIONAL~ inline-block elements default: width:auto -- <div> width = width of <div>'s content. */ .rec1 { font-size: 100px; display: inline-block; } .rec2 { font-size: 20ch; height: 100%; } 
 <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"> <div class='rx rec0'><i class="fa fa-spinner fa-spin"></i></div> <div class='rx rec1'><i class="fa fa-spinner fa-spin"></i></div> <div class='rx rec2'><i class="fa fa-spinner fa-spin"></i></div> 


in Demo #2 each <div> has class: .vx . 演示#2各自<div>具有类: .vx

§ there are ways to stretch fonts, but it's not worth the trouble and it's also beyond the scope of the question. §有一些方法可以拉伸字体,但这不值得麻烦,而且也超出了问题的范围。


References 参考

.querySelectorAll()

Array.from()

.forEach()

.clientHeight

you can use css 你可以使用CSS

i {font-size: 100vw}

or u can access the svg's directly in the download 或者您可以直接在下载中访问svg

<img width="100%" src="assets/fontawesome-free-5.0.1/advanced-options/raw-svg/spinner.svg">

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

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