简体   繁体   English

仅附加了阴影根的自定义组件上的“box-shadow”应用,就好像组件是 0x0px

[英]'box-shadow' on a custom component with only a shadow root attached applies as if component is 0x0px

I have a custom component that has only the shadow root attached and no child elements inside it.我有一个自定义组件,它只附加了影子根,里面没有子元素。 In Chrome dev tools, hover over the custom component shows its actual size on screen with the correct pixel numbers.在 Chrome 开发工具中,将鼠标悬停在自定义组件上会在屏幕上以正确的像素数显示其实际大小。 However, when adding a box-shadow property to this component result in the shadow being applied to the top left corner of the component, as if the component itself is only 0px by 0px.但是,当向该组件添加box-shadow属性时,会导致阴影被应用到组件的左上角,就好像组件本身只有 0px × 0px 一样。

 customElements.define('my-comp', class extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }) const div = document.createElement('div') div.setAttribute('style', 'height: 100px; width: 100px; background: lime') this.shadowRoot.append(div) } })
 <my-comp style="box-shadow: 0px 0px 10px 10px brown"></my-comp>

Is this a known bug and is there a workaround?这是一个已知的错误,是否有解决方法? Or perhaps there's an error somewhere in my code?或者我的代码中某处可能有错误?

Your custom component will have an inline display and you are adding a block element inside it.您的自定义组件将具有内联显示,并且您正在其中添加一个块元素。 You are facing the behavior of "block element inside inline element"您正面临“内联元素内的块元素”的行为

Make your element inline-block to avoid dealing with such case使您的元素inline-block以避免处理这种情况

 customElements.define('my-comp', class extends HTMLElement { constructor() { super(); this.attachShadow({ mode: 'open' }) const div = document.createElement('div') div.setAttribute('style', 'height: 100px; width: 100px; background: lime') this.shadowRoot.append(div) } })
 <my-comp style="box-shadow: 0px 0px 10px 10px brown;display:inline-block"></my-comp>

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

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