Can the bleeding of :root styles into a shadowed component be avoided?

After creating a web component using Stencil that utilizes the shadow DOM and requires ignoring any externally added CSS (thanks to its own stylesheet), I employed the common fix for the component stylesheet:

:host {
  all: initial;
}

This code effectively resets any base styles set on the page via the <style> tag or an external stylesheet. Nonetheless, issues arise when dealing with inherited styles like font-size from the :root or html elements, which override calculated values such as 1rem, disregarding my specified font sizes in the component's CSS.

For instance, despite implementing the CSS reset mentioned above and aiming for a base size of 16px, my development tools reveal that a font-size: 160px value applied to the :root is utilized when computing the final font size for an element designated with font-size: 0.875rem.

https://i.sstatic.net/LERrN.png

I have delved into various resources regarding styles attributed to the :root element, yet finding a solution remains elusive. Is there a method to override styles impacting relative values when they originate from the :root element?

Answer №1

Check out this fun coding playground: https://jsfiddle.net/WebComponents/L5kbf8qm/

<style>
  :root {
    font-size: 15px;
  }
</style>
<my-element all="initial"></my-element>
<my-element all="inherit">See how the UA 8px margin is restored here</my-element>
<script>
  customElements.define("my-element", class extends HTMLElement {
    connectedCallback() {
        let all = this.getAttribute("all");
      this.attachShadow({mode:"open"})
          .innerHTML = `<style>`+
                       `  :host{all:${all}}` +
                       `  div{font-size:2rem;background:pink}`+
                       `</style>` +
                       `<div>Component all:${all}</div>`+
                       `<span id=result ></span><slot></slot>`;
      const showFontSize = (name) => {
        let size = window.getComputedStyle(this.shadowRoot.querySelector(name))["font-size"];
        this.shadowRoot.querySelector("#result").append(`${name} font-size is: `, size, document.createElement("br"));
      }
      showFontSize("div");
      showFontSize("span");
    }
  })
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Strategies for Handling Various Screen Resolutions in HTML

When developing a web page on a PC with a 1366 * 768 resolution, I encountered issues with layout distortion when viewed on a 1920*1080 resolution screen. How can CSS be managed to ensure consistent display across PCs with varying resolutions? <html> ...

Using jQuery to center the target ID element of an href on the page with scrollIntoView

I'm interested in how to use the scrollIntoView method to scroll an element with a specific target ID to the center of the page when clicking on a link. Here's the code I currently have: $("a").click(function () { var target = $($( ...

Having trouble with the Jquery Slider script?

I recently integrated a slider from into my website, following the installation instructions. However, I seem to be facing some conflicts with another script that controls animated div movements on the same page. Upon loading the page, all elements of th ...

What would cause a flex-basis calculation to throw an error in SASS?

Today, I was working on a flex-grid generator in scss, which is something I do often. However, I encountered an unexpected error this time. I am using a 12-col-grid system and my approach should work fine with the code flex: 0 0 #{100 / 12 / $i}%;. There m ...

The table is too wide for its parent mat-nav-list, stretching to 100%

Here is the code snippet I am using to display a table inside a mat-nav-list: <mat-nav-list> <table> <tr>Node Information</tr> <tr> <td>Name</td> <td *ngIf="activeNod ...

Tips for vertically aligning input field text and checkboxes

I need help aligning a checkbox vertically in the middle of an input field and checkbox that are in the same row. Here is the current code snippet: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" in ...

The benefits of utilizing `calc()` compared to `transform` for positioning elements

When deciding between using calc() or the transform property for placing an element, is there a specific reason to choose one over the other when the size of the element is already known? For instance... .element { bottom: 100%; transform: translateY ...

Customize Material-Table: Adjusting Detail Panel Icon Rotation upon Row Expansion

I've made a modification to the layout of the detail panel icon (mui ChevronLeft) so that it now appears on the right side of the table by using the options={{detailPanelColumnAlignment: "right"}} property. TableIcons : DetailPanel: for ...

What is the method to apply custom styles (CSS) to individual options within a jQuery UI selectmenu?

When working with an HTML form that includes a select element, I encountered a design challenge. <select id='myselect'> <option value='1'>1</option> <option value='2'>2</option> ... <option va ...

Creating a responsive layout with two nested div elements inside a parent div, all styled

I'm currently facing an issue with using padding in my CSS to make the two divs inside a parent div responsive at 50% each. However, when combined, they exceed 100%. I suspect this is due to the padding, but I'm unsure how to resolve it. .column ...

Customizing HTML forms using WordPress Divi's custom CSS techniques

I have a WordPress website that I'm customizing with the Divi theme. I've successfully added an HTML form to a 'Code' module and it's functioning well. However, I am struggling to determine where I should place the CSS code. I&apos ...

Adjust the width of the table data cells dynamically in HTML and CSS based on the screen resolution and content

My React JS app has a page that features a table with tbody, tr, and td elements. The table consists of only two columns represented by the td tags. I am trying to make the first column adjust automatically based on screen resolution and content. I have ex ...

Can the CSS file be modified while a MVC site is running?

My MVC website has a black and white color scheme with specific design elements in blue color. I handle all the coloring through CSS. I would like to change the color of these elements from blue to a different color occasionally. However, when using jQuer ...

How can I incorporate margins or adjust scaling within dompdf?

After creating a PDF using dompdf with the paper size set to A6, I am experiencing issues when trying to print it on A4 and letter paper sizes. The left and top borders are being cut off. Is there a way to generate a PDF that is compatible with A4, A6, l ...

Are you seeking precise parsing for PHP, JavaScript, and CSS?

As I work on developing bottom-up parsers for PHP, JavaScript, and CSS, I aim to create a universal parser that can handle all three languages. I've come across information suggesting that JavaScript could be parsed using an LALR(1) parser (please cor ...

jscrollpane does not work properly in Firefox, but it functions correctly in Chrome

I am currently utilizing jScrollpane to display a combination of images and a div in a horizontal format. While I have successfully implemented it on Chrome, it appears that Firefox is not applying the CSS correctly to prevent images from wrapping, resulti ...

Issue with Spyscroll functionality

Having some trouble getting Spyscroll to work. Can anyone help me identify the issue? I've been at it all day... I've attempted both the javascript and html+css setups, but neither seem to be functioning correctly. Manually adding the "active" c ...

Understanding the reasons for floated elements impacting the heights of non-floated elements (D7 views blocks using the zen theme)

Currently working on updating a website that was originally built on D7 with a 7.x-56 zen sub theme. The layout of the theme consists of responsive horizontally stacked content regions. Recently, I created a custom blog content type along with three views ...

The navigation bar is smaller in size compared to the brand logo

Is there a way to create a larger navbar-brand (bootstrap 4) that overlaps the navbar itself, instead of increasing the height of the navbar to fit the brand? https://i.sstatic.net/5H4UM.png This is what my HTML currently looks like: <nav class=" ...

Unique behavior and design specifically tailored for Safari viewing

Hey there! So I've been working on a Gatsby application and ran into an issue with Font Awesome Icons when viewed in Safari. The icons seem to lose their shape and proportions compared to how they appear in Google Chrome. Safari https://i.sstatic.ne ...