在Grafana环境下如何将SCSS转换为HTML内联样式
Answer
Got it, let's break this down for you. Pure inline styles won't cover all your needs here (like pseudo-classes or font imports), so the best approach is to use a combination of an internal <style> tag in your HTML head plus minimal inline styles—this is fully supported by Grafana's HTML panel and will preserve all your original styling.
Step 1: Expand SCSS to Flat CSS
First, we'll unwrap the nested SCSS into standard CSS selectors so it can live in a <style> tag:
@import url("https://fonts.googleapis.com/css2?family=Roboto:wght@400&display=swap"); body { font-family: "Roboto", serif; background: #222; color: #fff; } .grid { display: flex; max-width: 1200px; margin: 0 auto; } .grid .product { width: 99%; max-width: 333px; margin-right: 1%; } .grid .product:last-child { margin-right: 0; } .grid .product .product-image { height: 100%; max-width: 100%; max-height: 225px; } .grid .product .product-image a img { width: 100%; height: 100%; background-size: cover; background-repeat: no-repeat; background-position: center; } .grid .product .product-description { display: block; margin-top: 20px; max-width: 100%; align-items: center; } .grid .product .product-description h1 { display: -webkit-box; font-size: 1rem; text-align: center; text-transform: uppercase; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; text-overflow: ellipsis; height: 40px; margin-bottom: 10px; } .grid .product .product-description .brand-wrapper { display: flex; flex-direction: column; -webkit-box-pack: center; -webkit-justify-content: center; justify-content: center; align-items: center; } .grid .product .product-description .brand-wrapper .brand { margin-bottom: 0.5rem; }
Step 2: Integrate into Your HTML
Now, add this CSS to a <style> tag inside the <head> of your HTML. We'll keep the inline background-image on the <img> since that's a dynamic value. Here's the final working code:
<!DOCTYPE HTML> <html> <head> <style> @import url("https://fonts.googleapis.com/css2?family=Roboto:wght@400&display=swap"); body { font-family: "Roboto", serif; background: #222; color: #fff; } .grid { display: flex; max-width: 1200px; margin: 0 auto; } .grid .product { width: 99%; max-width: 333px; margin-right: 1%; } .grid .product:last-child { margin-right: 0; } .grid .product .product-image { height: 100%; max-width: 100%; max-height: 225px; } .grid .product .product-image a img { width: 100%; height: 100%; background-size: cover; background-repeat: no-repeat; background-position: center; } .grid .product .product-description { display: block; margin-top: 20px; max-width: 100%; align-items: center; } .grid .product .product-description h1 { display: -webkit-box; font-size: 1rem; text-align: center; text-transform: uppercase; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; text-overflow: ellipsis; height: 40px; margin-bottom: 10px; } .grid .product .product-description .brand-wrapper { display: flex; flex-direction: column; -webkit-box-pack: center; -webkit-justify-content: center; justify-content: center; align-items: center; } .grid .product .product-description .brand-wrapper .brand { margin-bottom: 0.5rem; } </style> </head> <body> <div class="grid"> <div class="product"> <div class="product-image"> <a href="https://www.jdsports.se/product/nike-air-force-1-shadow-womens/393242_jdsportsse/" target="_blank"> <img style="background-image:url(https://i.imgur.com/9pdOpoF.png);" /> </a> </div> <div class="product-description"> <h1> Nike Air Force 1 Shadow Womens sfg sdfg sdf gdfs sdf </h1> <div class="brand-wrapper"> <span class="brand">(Jdsports)</span> <span class="id">ID 2200</span> </div> </div> </div> </div> </body> </html>
Why Not Pure Inline Styles?
- Pseudo-classes like
:last-childcan't be applied via inline styles—they require CSS selectors. - Font imports only work via
<style>or<link>tags, not inline. - Using inline styles for all these rules would create redundant, hard-to-maintain code.
This method stays within Grafana's restrictions while fully replicating your original SCSS's visual效果.
内容的提问来源于stack exchange,提问作者PythonNewbie

