3

I develop an HTML widget that gets injected (as plain HTML, not iframe) to several websites.

The problem is that in some sites the style of my elements gets affected by the site's CSS (text alignment, underline, text spacing...).

Is there any way to prevent the site's CSS to affect the style of injected elements?

4

6 回答 6

2

如果你想重置所有 CSS 属性,你可以这样做。这是一个大文件,可能不是最好的解决方案,但它确实有效。

.yourContainer * {
  -webkit-align-content: stretch;                        -moz-align-content: stretch;                        align-content: stretch;
  -webkit-align-items: stretch;                          -moz-align-items: stretch;                          align-items: stretch;
  -webkit-align-self: stretch;                           -moz-align-self: stretch;                           align-self: stretch;
  -webkit-animation-delay: 0s;                           -moz-animation-delay: 0s;                           animation-delay: 0s;
  -webkit-animation-direction: normal;                   -moz-animation-direction: normal;                   animation-direction: normal;
  -webkit-animation-duration: 0s;                        -moz-animation-duration: 0s;                        animation-duration: 0s;
  -webkit-animation-fill-mode: none;                     -moz-animation-fill-mode: none;                     animation-fill-mode: none;
  -webkit-animation-iteration-count: 1;                  -moz-animation-iteration-count: 1;                  animation-iteration-count: 1;
  -webkit-animation-name: none;                          -moz-animation-name: none;                          animation-name: none;
  -webkit-animation-play-state: running;                 -moz-animation-play-state: running;                 animation-play-state: running;
  -webkit-animation-timing-function: ease;               -moz-animation-timing-function: ease;               animation-timing-function: ease;
  -webkit-app-region: no-drag;                           -moz-app-region: no-drag;                           app-region: no-drag;
  -webkit-appearance: none;                              -moz-appearance: none;                              appearance: none;
  -webkit-backface-visibility: visible;                  -moz-backface-visibility: visible;                  backface-visibility: visible;
  -webkit-background-clip: border-box;                   -moz-background-clip: border-box;                   background-clip: border-box;
  -webkit-background-composite: source-over;             -moz-background-composite: source-over;             background-composite: source-over;
  -webkit-background-origin: padding-box;                -moz-background-origin: padding-box;                background-origin: padding-box;
  -webkit-background-size: auto;                         -moz-background-size: auto;                         background-size: auto;
  -webkit-border-fit: border;                            -moz-border-fit: border;                            border-fit: border;
  -webkit-border-horizontal-spacing: 0px;                -moz-border-horizontal-spacing: 0px;                border-horizontal-spacing: 0px;
  -webkit-border-image: none;                            -moz-border-image: none;                            border-image: none;
  -webkit-border-vertical-spacing: 0px;                  -moz-border-vertical-spacing: 0px;                  border-vertical-spacing: 0px;
  -webkit-box-align: stretch;                            -moz-box-align: stretch;                            box-align: stretch;
  -webkit-box-decoration-break: slice;                   -moz-box-decoration-break: slice;                   box-decoration-break: slice;
  -webkit-box-direction: normal;                         -moz-box-direction: normal;                         box-direction: normal;
  -webkit-box-flex: 0;                                   -moz-box-flex: 0;                                   box-flex: 0;
  -webkit-box-flex-group: 1;                             -moz-box-flex-group: 1;                             box-flex-group: 1;
  -webkit-box-lines: single;                             -moz-box-lines: single;                             box-lines: single;
  -webkit-box-ordinal-group: 1;                          -moz-box-ordinal-group: 1;                          box-ordinal-group: 1;
  -webkit-box-orient: horizontal;                        -moz-box-orient: horizontal;                        box-orient: horizontal;
  -webkit-box-pack: start;                               -moz-box-pack: start;                               box-pack: start;
  -webkit-box-reflect: none;                             -moz-box-reflect: none;                             box-reflect: none;
  -webkit-box-shadow: none;                              -moz-box-shadow: none;                              box-shadow: none;
  -webkit-clip-path: none;                               -moz-clip-path: none;                               clip-path: none;
  -webkit-color-correction: default;                     -moz-color-correction: default;                     color-correction: default;
  -webkit-column-axis: auto;                             -moz-column-axis: auto;                             column-axis: auto;
  -webkit-column-break-after: auto;                      -moz-column-break-after: auto;                      column-break-after: auto;
  -webkit-column-break-before: auto;                     -moz-column-break-before: auto;                     column-break-before: auto;
  -webkit-column-break-inside: auto;                     -moz-column-break-inside: auto;                     column-break-inside: auto;
  -webkit-column-count: auto;                            -moz-column-count: auto;                            column-count: auto;
  -webkit-column-gap: normal;                            -moz-column-gap: normal;                            column-gap: normal;
  -webkit-column-progression: normal;                    -moz-column-progression: normal;                    column-progression: normal;
  -webkit-column-rule-color: rgb(0, 0, 0);               -moz-column-rule-color: rgb(0, 0, 0);               column-rule-color: rgb(0, 0, 0);
  -webkit-column-rule-style: none;                       -moz-column-rule-style: none;                       column-rule-style: none;
  -webkit-column-rule-width: 0px;                        -moz-column-rule-width: 0px;                        column-rule-width: 0px;
  -webkit-column-span: none;                             -moz-column-span: none;                             column-span: none;
  -webkit-column-width: auto;                            -moz-column-width: auto;                            column-width: auto;
  -webkit-filter: none;                                  -moz-filter: none;                                  filter: none;
  -webkit-flex-basis: auto;                              -moz-flex-basis: auto;                              flex-basis: auto;
  -webkit-flex-direction: row;                           -moz-flex-direction: row;                           flex-direction: row;
  -webkit-flex-grow: 0;                                  -moz-flex-grow: 0;                                  flex-grow: 0;
  -webkit-flex-shrink: 1;                                -moz-flex-shrink: 1;                                flex-shrink: 1;
  -webkit-flex-wrap: nowrap;                             -moz-flex-wrap: nowrap;                             flex-wrap: nowrap;
  -webkit-flow-from: none;                               -moz-flow-from: none;                               flow-from: none;
  -webkit-flow-into: none;                               -moz-flow-into: none;                               flow-into: none;
  -webkit-font-kerning: auto;                            -moz-font-kerning: auto;                            font-kerning: auto;
  -webkit-font-smoothing: auto;                          -moz-font-smoothing: auto;                          font-smoothing: auto;
  -webkit-font-variant-ligatures: normal;                -moz-font-variant-ligatures: normal;                font-variant-ligatures: normal;
  -webkit-grid-after: auto;                              -moz-grid-after: auto;                              grid-after: auto;
  -webkit-grid-auto-columns: auto;                       -moz-grid-auto-columns: auto;                       grid-auto-columns: auto;
  -webkit-grid-auto-flow: none;                          -moz-grid-auto-flow: none;                          grid-auto-flow: none;
  -webkit-grid-auto-rows: auto;                          -moz-grid-auto-rows: auto;                          grid-auto-rows: auto;
  -webkit-grid-before: auto;                             -moz-grid-before: auto;                             grid-before: auto;
  -webkit-grid-columns: none;                            -moz-grid-columns: none;                            grid-columns: none;
  -webkit-grid-end: auto;                                -moz-grid-end: auto;                                grid-end: auto;
  -webkit-grid-rows: none;                               -moz-grid-rows: none;                               grid-rows: none;
  -webkit-grid-start: auto;                              -moz-grid-start: auto;                              grid-start: auto;
  -webkit-highlight: none;                               -moz-highlight: none;                               highlight: none;
  -webkit-hyphenate-character: auto;                     -moz-hyphenate-character: auto;                     hyphenate-character: auto;
  -webkit-hyphenate-limit-after: auto;                   -moz-hyphenate-limit-after: auto;                   hyphenate-limit-after: auto;
  -webkit-hyphenate-limit-before: auto;                  -moz-hyphenate-limit-before: auto;                  hyphenate-limit-before: auto;
  -webkit-hyphenate-limit-lines: no-limit;               -moz-hyphenate-limit-lines: no-limit;               hyphenate-limit-lines: no-limit;
  -webkit-hyphens: manual;                               -moz-hyphens: manual;                               hyphens: manual;
  -webkit-justify-content: flex-start;                   -moz-justify-content: flex-start;                   justify-content: flex-start;
  -webkit-line-align: none;                              -moz-line-align: none;                              line-align: none;
  -webkit-line-box-contain: block inline replaced;       -moz-line-box-contain: block inline replaced;       line-box-contain: block inline replaced;
  -webkit-line-break: auto;                              -moz-line-break: auto;                              line-break: auto;
  -webkit-line-clamp: none;                              -moz-line-clamp: none;                              line-clamp: none;
  -webkit-line-grid: none;                               -moz-line-grid: none;                               line-grid: none;
  -webkit-line-snap: none;                               -moz-line-snap: none;                               line-snap: none;
  -webkit-locale: auto;                                  -moz-locale: auto;                                  locale: auto;
  -webkit-margin-after-collapse: collapse;               -moz-margin-after-collapse: collapse;               margin-after-collapse: collapse;
  -webkit-margin-before-collapse: collapse;              -moz-margin-before-collapse: collapse;              margin-before-collapse: collapse;
  -webkit-marquee-direction: auto;                       -moz-marquee-direction: auto;                       marquee-direction: auto;
  -webkit-marquee-increment: 6px;                        -moz-marquee-increment: 6px;                        marquee-increment: 6px;
  -webkit-marquee-repetition: infinite;                  -moz-marquee-repetition: infinite;                  marquee-repetition: infinite;
  -webkit-marquee-style: scroll;                         -moz-marquee-style: scroll;                         marquee-style: scroll;
  -webkit-mask-box-image: none;                          -moz-mask-box-image: none;                          mask-box-image: none;
  -webkit-mask-box-image-outset: 0px;                    -moz-mask-box-image-outset: 0px;                    mask-box-image-outset: 0px;
  -webkit-mask-box-image-repeat: stretch;                -moz-mask-box-image-repeat: stretch;                mask-box-image-repeat: stretch;
  -webkit-mask-box-image-slice: 0 fill;                  -moz-mask-box-image-slice: 0 fill;                  mask-box-image-slice: 0 fill;
  -webkit-mask-box-image-source: none;                   -moz-mask-box-image-source: none;                   mask-box-image-source: none;
  -webkit-mask-box-image-width: auto;                    -moz-mask-box-image-width: auto;                    mask-box-image-width: auto;
  -webkit-mask-clip: border-box;                         -moz-mask-clip: border-box;                         mask-clip: border-box;
  -webkit-mask-composite: source-over;                   -moz-mask-composite: source-over;                   mask-composite: source-over;
  -webkit-mask-image: none;                              -moz-mask-image: none;                              mask-image: none;
  -webkit-mask-origin: border-box;                       -moz-mask-origin: border-box;                       mask-origin: border-box;
  -webkit-mask-position: 0% 0%;                          -moz-mask-position: 0% 0%;                          mask-position: 0% 0%;
  -webkit-mask-repeat: repeat;                           -moz-mask-repeat: repeat;                           mask-repeat: repeat;
  -webkit-mask-size: auto;                               -moz-mask-size: auto;                               mask-size: auto;
  -webkit-nbsp-mode: normal;                             -moz-nbsp-mode: normal;                             nbsp-mode: normal;
  -webkit-order: 0;                                      -moz-order: 0;                                      order: 0;
  -webkit-perspective: none;                             -moz-perspective: none;                             perspective: none;
  -webkit-perspective-origin: 640px 345px;               -moz-perspective-origin: 640px 345px;               perspective-origin: 640px 345px;
  -webkit-print-color-adjust: economy;                   -moz-print-color-adjust: economy;                   print-color-adjust: economy;
  -webkit-region-break-after: auto;                      -moz-region-break-after: auto;                      region-break-after: auto;
  -webkit-region-break-before: auto;                     -moz-region-break-before: auto;                     region-break-before: auto;
  -webkit-region-break-inside: auto;                     -moz-region-break-inside: auto;                     region-break-inside: auto;
  -webkit-region-overflow: auto;                         -moz-region-overflow: auto;                         region-overflow: auto;
  -webkit-rtl-ordering: logical;                         -moz-rtl-ordering: logical;                         rtl-ordering: logical;
  -webkit-shape-inside: outside-shape;                   -moz-shape-inside: outside-shape;                   shape-inside: outside-shape;
  -webkit-shape-margin: 0px;                             -moz-shape-margin: 0px;                             shape-margin: 0px;
  -webkit-shape-outside: auto;                           -moz-shape-outside: auto;                           shape-outside: auto;
  -webkit-shape-padding: 0px;                            -moz-shape-padding: 0px;                            shape-padding: 0px;
  -webkit-svg-shadow: none;                              -moz-svg-shadow: none;                              svg-shadow: none;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0.180392);  -moz-tap-highlight-color: rgba(0, 0, 0, 0.180392);  tap-highlight-color: rgba(0, 0, 0, 0.180392);
  -webkit-text-combine: none;                            -moz-text-combine: none;                            text-combine: none;
  -webkit-text-decorations-in-effect: none;              -moz-text-decorations-in-effect: none;              text-decorations-in-effect: none;
  -webkit-text-emphasis-color: rgb(0, 0, 0);             -moz-text-emphasis-color: rgb(0, 0, 0);             text-emphasis-color: rgb(0, 0, 0);
  -webkit-text-emphasis-position: over;                  -moz-text-emphasis-position: over;                  text-emphasis-position: over;
  -webkit-text-emphasis-style: none;                     -moz-text-emphasis-style: none;                     text-emphasis-style: none;
  -webkit-text-fill-color: rgb(0, 0, 0);                 -moz-text-fill-color: rgb(0, 0, 0);                 text-fill-color: rgb(0, 0, 0);
  -webkit-text-orientation: vertical-right;              -moz-text-orientation: vertical-right;              text-orientation: vertical-right;
  -webkit-text-security: none;                           -moz-text-security: none;                           text-security: none;
  -webkit-text-stroke-color: rgb(0, 0, 0);               -moz-text-stroke-color: rgb(0, 0, 0);               text-stroke-color: rgb(0, 0, 0);
  -webkit-text-stroke-width: 0px;                        -moz-text-stroke-width: 0px;                        text-stroke-width: 0px;
  -webkit-transform: none;                               -moz-transform: none;                               transform: none;
  -webkit-transform-origin: 640px 345px;                 -moz-transform-origin: 640px 345px;                 transform-origin: 640px 345px;
  -webkit-transform-style: flat;                         -moz-transform-style: flat;                         transform-style: flat;
  -webkit-transition-delay: 0s;                          -moz-transition-delay: 0s;                          transition-delay: 0s;
  -webkit-transition-duration: 0s;                       -moz-transition-duration: 0s;                       transition-duration: 0s;
  -webkit-transition-property: all;                      -moz-transition-property: all;                      transition-property: all;
  -webkit-transition-timing-function: ease;              -moz-transition-timing-function: ease;              transition-timing-function: ease;
  -webkit-user-drag: auto;                               -moz-user-drag: auto;                               user-drag: auto;
  -webkit-user-modify: read-only;                        -moz-user-modify: read-only;                        user-modify: read-only;
  -webkit-user-select: text;                             -moz-user-select: text;                             user-select: text;
  -webkit-wrap-flow: auto;                               -moz-wrap-flow: auto;                               wrap-flow: auto;
  -webkit-wrap-through: wrap;                            -moz-wrap-through: wrap;                            wrap-through: wrap;
  -webkit-writing-mode: horizontal-tb;                   -moz-writing-mode: horizontal-tb;                   writing-mode: horizontal-tb;

    alignment-baseline: auto;
  background-attachment: scroll;
  background-clip: border-box;
  background-color: rgba(0, 0, 0, 0);
  background-image: none;
  background-origin: padding-box;
  background-position: 0% 0%;
  background-repeat: repeat;
  background-size: auto;
  baseline-shift: baseline;
  border-bottom-color: rgb(0, 0, 0);
  border-bottom-left-radius: 0px;
  border-bottom-right-radius: 0px;
  border-bottom-style: none;
  border-bottom-width: 0px;
  border-collapse: separate;
  border-image-outset: 0px;
  border-image-repeat: stretch;
  border-image-slice: 100%;
  border-image-source: none;
  border-image-width: 1;
  border-left-color: rgb(0, 0, 0);
  border-left-style: none;
  border-left-width: 0px;
  border-right-color: rgb(0, 0, 0);
  border-right-style: none;
  border-right-width: 0px;
  border-top-color: rgb(0, 0, 0);
  border-top-left-radius: 0px;
  border-top-right-radius: 0px;
  border-top-style: none;
  border-top-width: 0px;
  bottom: auto;
  box-shadow: none;
  box-sizing: content-box;
  caption-side: top;
  clear: none;
  clip: auto;
  clip-path: none;
  clip-rule: nonzero;
  color: rgb(0, 0, 0);
  color-interpolation: srgb;
  color-interpolation-filters: linearrgb;
  color-rendering: auto;
  cursor: auto;
  direction: ltr;
  display: block;
  dominant-baseline: auto;
  empty-cells: show;
  fill: #000000;
  fill-opacity: 1;
  fill-rule: nonzero;
  filter: none;
  float: none;
  flood-color: rgb(0, 0, 0);
  flood-opacity: 1;
  font-family: sans-serif;
  font-size: 16px;
  font-style: normal;
  font-variant: normal;
  font-weight: normal;
  glyph-orientation-horizontal: 0deg;
  glyph-orientation-vertical: auto;
  height: auto;
  image-rendering: auto;
  kerning: 0;
  left: auto;
  letter-spacing: normal;
  lighting-color: rgb(255, 255, 255);
  line-height: normal;
  list-style-image: none;
  list-style-position: outside;
  list-style-type: disc;
  margin-bottom: 0px;
  margin-left: 0px;
  margin-right: 0px;
  margin-top: 0px;
  marker-end: none;
  marker-mid: none;
  marker-start: none;
  mask: none;
  mask-type: luminance;
  max-height: none;
  max-width: none;
  min-height: 0px;
  min-width: 0px;
  opacity: 1;
  orphans: auto;
  outline-color: rgb(0, 0, 0);
  outline-offset: 0px;
  outline-style: none;
  outline-width: 0px;
  overflow-wrap: normal;
  overflow-x: visible;
  overflow-y: visible;
  padding-bottom: 0px;
  padding-left: 0px;
  padding-right: 0px;
  padding-top: 0px;
  page-break-after: auto;
  page-break-before: auto;
  page-break-inside: auto;
  pointer-events: auto;
  position: static;
  resize: none;
  right: auto;
  shape-rendering: auto;
  speak: normal;
  stop-color: rgb(0, 0, 0);
  stop-opacity: 1;
  stroke: none;
  stroke-dasharray: none;
  stroke-dashoffset: 0;
  stroke-linecap: butt;
  stroke-linejoin: miter;
  stroke-miterlimit: 4;
  stroke-opacity: 1;
  stroke-width: 1;
  tab-size: 8;
  table-layout: auto;
  text-align: start;
  text-anchor: start;
  text-decoration: none;
  text-indent: 0px;
  text-overflow: clip;
  text-rendering: auto;
  text-shadow: none;
  text-transform: none;
  top: auto;
  transition-delay: 0s;
  transition-duration: 0s;
  transition-property: all;
  transition-timing-function: ease;
  unicode-bidi: normal;
  vector-effect: none;
  vertical-align: baseline;
  visibility: visible;
  white-space: normal;
  widows: auto;
  width: auto;
  word-break: normal;
  word-spacing: 0px;
  word-wrap: normal;
  writing-mode: lr-tb;
  z-index: auto;
  zoom: 1;
}
于 2013-08-27T11:35:36.617 回答
1

通常没有,没有。归根结底,如果您将 HTML 代码放入其他人的站点,您就是在隐含地让他们控制该 HTML 的样式。

即使 using!important也可以用另一种!important样式覆盖,或者在最坏的情况下通过一点 Javascript 直接在元素上设置样式。(无论如何,!important这被认为是不好的做法,所以你可能不想这样做,对吧)

当然,您可以自己使用 Javascript 来设置样式,但是在您完成之后,网站所有者仍然容易受到同样的影响。

唯一真正可行的解决方案是将整个小部件放入 iframe。这就是谷歌为他们的广告等所做的事情。这将很好地将您的小部件代码与主站点隔离开来,并防止站点所有者无意中覆盖您的样式的任何可能性。他们可能仍然有办法解决这个问题,但是当您处理跨站点 iframe 时,它​​会变得更加困难,因为内置的浏览器安全限制开始发挥作用。

但真正的问题是:你真的要这样做吗?保持您定义的样式对您的样式有多重要?如果您的小部件使用该站点的默认字体呈现,也许您的小部件在特定站点上看起来更好?也许网站所有者对特定字体或颜色有公司要求?(有些公司可能对此非常挑剔)。当然,如果它在他们的网站上,他们至少应该有一定程度的控制权?如果他们完成后看起来很垃圾,那是他们的错。

现在我知道上面的段落并不适用于所有人。也许你问这个是因为你不希望用户隐藏你的版权文本,或者类似的东西。但是在某些情况下,您确实需要问自己一个问题,即您是否为此而施加限制;他们是否真的值得付出努力,或者他们是否可能只会惹恼想要做一些完全合法的事情的用户。

于 2013-08-27T11:44:30.220 回答
1

看看这里: http: //meyerweb.com/eric/tools/css/reset/。将代码粘贴到小部件样式表的顶部,它将重置几乎所有可能影响显示的 CSS 属性。如果你不想访问链接,这里是:

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
    background: none;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}
于 2013-08-27T11:32:06.273 回答
0

CSS的本质是继承。即,如果您不向元素发送属性,它将从其父元素中获取。据我了解,您正在将小部件添加为代码。我的意思是您的代码被注入到另一个页面中,并且不涉及 iframe。因此,您的样式应该是内联样式,例如:

<div class="widget" style="padding: 0;">
...
</div>

您可能尝试的第一个解决方案是将重置样式放在您的最顶层容器中。所以它的所有孩子都从那里继承了缺失的属性。然而这并不是那么好,因为有大量的属性并且直接将它们添加到标签中看起来很奇怪。

使用 !important 就足够了,但这仅在您知道哪种样式将被更改的情况下。

我认为,如果您不使用 iframe,则很难拥有绝对自定义外观的小部件。你可以尝试一些疯狂的事情,比如

  • 使用自定义标签并分别设置它们的样式
  • 使用更多图像而不是 CSS。图像看起来保持不变
于 2013-08-27T11:46:15.377 回答
-2

为封装注入的 div 创建一个 id,然后根据需要设置样式。这将始终保持完好无损。否则,你无法阻止事情的发生,只有保护。

于 2013-08-27T11:31:46.673 回答
-4

在你的 css 中尽可能具体。因此,假设您有以下 HTML

<div id="myWidget">
    <div class="someContent">test</div>
</div>

并且您想告诉 someContent 需要是红色的,请使用以下 css:

#myWidget div.someContent{#ff0000;}
于 2013-08-27T11:32:31.397 回答