1

I a large HTML structure on my page, and I want one of the elements (id="myStyle") to be unaffected by all CSS from outside. The inner HTML of this element is loaded from a custom editor (ckeditor).

HTML:

body, html, *{margin:0px; padding:0px;}
ul{list-style: none;color:red;}
<html>
 <head>
  <title>MY Page</title>
  
 </head>
 <body>
  <div>
   <ul>
    <li> Style apply 1</li>
    <li> Style apply 2</li>
    <li> Style apply 3</li>
    <li> Style apply 4</li>
   </ul>
 <div id="myStyle">
              <ul>
                <li> Style none 1</li>
                <li> Style none 2</li>
                <li> Style none 3</li>
                <li> Style none 4</li>
              </ul>
   </div>
  </div>
 </body>
</html>

This is an example, not my actual HTML. Here the ul style applies for both inside or outside of id="myStyle".

I have seen lots of solutions. One of the best is to apply parallel CSS to #myStyle.

But I am trying to create a CSS boundary by using an iframe:

body, html, *{margin:0px; padding:0px;}
   ul{list-style: none;color:red;}
<html>
 <head>
  <title>MY Page</title>
  <style>
   body, html, *{margin:0px; padding:0px;}
   ul{list-style: none;color:red;}
  </style>
 </head>
 <body>
  <div>
   <ul>
    <li> Style apply 1</li>
    <li> Style apply 2</li>
    <li> Style apply 3</li>
    <li> Style apply 4</li>
   </ul>
   <div id="myStyle">
    <iframe onload='javascript:resizeIframe(this);' scrolling="no" id="iframe" src="javascript: '<!DOCTYPE html><html>
     <body>
      <ul>
       <li> Style none 1</li>
       <li> Style none 2</li>
       <li> Style none 3</li>
       <li> Style none 4</li>
      </ul>
      </body>
     </html>'" style="border: none; width:50%;">
    </iframe>
   </div>
  </div>
 </body>
</html>

This is working fine, but I have question: Is there any risk or any performance issue? Or is this considered alright?

Siguza
  • 21,155
  • 6
  • 52
  • 89
Sachink
  • 1,425
  • 10
  • 22
  • Do you want to "not apply" only your own CSS or some arbitrary third-party CSS? – Siguza May 13 '15 at 07:48
  • @Siguza I having template with css files like `style.css, layout.css. ...` and I don't want to apply these css inside the `#myStyle` div I used inline iframe, it's working fine, but is there any risk ? if it's okay then I will go forward. – Sachink May 13 '15 at 08:37

3 Answers3

1

First:

Is there [...] any performance issue?

I guess an iframe has a larger overhead than a simple div DOM-wise, but I don't know anything about CSS implementations, so I might be totally wrong here.

Second, validate your code.
Yields two errors, one of which is the src attribute of your iframe.

Also, src="javascript:'...'"? Is that really valid? I couldn't find any documentation mentioning it. On top of that, JavaScript does not support multi-line strings.
And you have another two errors:

onload='javascript:resizeIframe(this);'
  1. onload contains JavaScript already, so the javascript: should trigger a syntax error, but it seems that at least Chrome ignores it.
  2. resizeIframe is not defined anywhere, causing a JavaScript ReferenceError.

I must say, I'm highly surprised it works at all.

The "proper way" to create an inline frame (if there is such a thing) is to use the data: URI scheme, like

<iframe style="border: none; width:50%;" scrolling="no" id="iframe" src="data:text/html;charset=UTF-8,%3C!DOCTYPE%20html%3E%3Chtml%3E%3Cbody%3E%3Cul%3E%3Cli%3EStyle%20none%201%3C/li%3E%3Cli%3EStyle%20none%202%3C/li%3E%3Cli%3E%20Style%20none%203%3C/li%3E%3Cli%3E%20Style%20none%204%3C/li%3E%3C/ul%3E%3C/body%3E%3C/html%3E">

or

<iframe style="border: none; width:50%;" scrolling="no" id="iframe" src="data:text/html;base64,PCFET0NUWVBFIGh0bWw+PGh0bWw+PGJvZHk+PHVsPjxsaT5TdHlsZSBub25lIDE8L2xpPjxsaT5TdHlsZSBub25lIDI8L2xpPjxsaT4gU3R5bGUgbm9uZSAzPC9saT48bGk+IFN0eWxlIG5vbmUgNDwvbGk+PC91bD48L2JvZHk+PC9odG1sPg==">

You can get the part after the , with JavaScript by using either uncodeURI(str) or btoa(str), where str is a string containing the contents of the iframe.

Note that in HTML 5, scrolling has been removed and its sort-of replacement seamless (which is only supported by one single android browser at the time) will not really do what you want, because it will inherit CSS from the parent page (see also this question).

So, for your question

Is there any risk [...]?

Regarding your code, yes. Multiple, actually.

  • The risk of using invalid code is that browsers might not understand it.
  • There's a list of disadvantages frames have, see Wikipedia.
  • The risk of the user's browser not supporting frames (although I haven't heard of any such browser for a really long time now).
  • The risk of the user's browser not supporting JavaScript or having it disabled.

All those "risks" would be gone if you just had a div for which you manually reset (<name>: initial) those style attributes that are set.
I also consider this a cleaner solution, but that's just my opinion.

You might also want to have a look at Shadow DOM, although again browser support has not really arrived yet.

Community
  • 1
  • 1
Siguza
  • 21,155
  • 6
  • 52
  • 89
0

I'm a little confused with the example you have provided, but if I understand you correctly you want to set style to all divs EXCEPT the one with that ID.

If that is the case you can use this:

div:not(#myStyle) {
   ...code for all divs except #myStyle...
}

You could go as far as to have:

*:not(#myStyle) {
    ...code for ALL elements, except #myStyle...
}
Callum.
  • 146
  • 12
-2

The CSS3 keyword initial which will set the CSS3 property to the initial value as defined in the spec.

So until full support lands on all browsers here are some of the ways you can reset some CSS properties to their initial values with something like this :

.reset-this {
animation : none;
animation-delay : 0;
animation-direction : normal;
animation-duration : 0;
animation-fill-mode : none;
animation-iteration-count : 1;
animation-name : none;
animation-play-state : running;
animation-timing-function : ease;
backface-visibility : visible;
background : 0;
background-attachment : scroll;
background-clip : border-box;
background-color : transparent;
background-image : none;
background-origin : padding-box;
background-position : 0 0;
background-position-x : 0;
background-position-y : 0;
background-repeat : repeat;
background-size : auto auto;
border : 0;
border-style : none;
border-width : medium;
border-color : inherit;
border-bottom : 0;
border-bottom-color : inherit;
border-bottom-left-radius : 0;
border-bottom-right-radius : 0;
border-bottom-style : none;
border-bottom-width : medium;
border-collapse : separate;
border-image : none;
border-left : 0;
border-left-color : inherit;
border-left-style : none;
border-left-width : medium;
border-radius : 0;
border-right : 0;
border-right-color : inherit;
border-right-style : none;
border-right-width : medium;
border-spacing : 0;
border-top : 0;
border-top-color : inherit;
border-top-left-radius : 0;
border-top-right-radius : 0;
border-top-style : none;
border-top-width : medium;
bottom : auto;
box-shadow : none;
box-sizing : content-box;
caption-side : top;
clear : none;
clip : auto;
color : inherit;
columns : auto;
column-count : auto;
column-fill : balance;
column-gap : normal;
column-rule : medium none currentColor;
column-rule-color : currentColor;
column-rule-style : none;
column-rule-width : none;
column-span : 1;
column-width : auto;
content : normal;
counter-increment : none;
counter-reset : none;
cursor : auto;
direction : ltr;
display : inline;
empty-cells : show;
float : none;
font : normal;
font-family : inherit;
font-size : medium;
font-style : normal;
font-variant : normal;
font-weight : normal;
height : auto;
hyphens : none;
left : auto;
letter-spacing : normal;
line-height : normal;
list-style : none;
list-style-image : none;
list-style-position : outside;
list-style-type : disc;
margin : 0;
margin-bottom : 0;
margin-left : 0;
margin-right : 0;
margin-top : 0;
max-height : none;
max-width : none;
min-height : 0;
min-width : 0;
opacity : 1;
orphans : 0;
outline : 0;
outline-color : invert;
outline-style : none;
outline-width : medium;
overflow : visible;
overflow-x : visible;
overflow-y : visible;
padding : 0;
padding-bottom : 0;
padding-left : 0;
padding-right : 0;
padding-top : 0;
page-break-after : auto;
page-break-before : auto;
page-break-inside : auto;
perspective : none;
perspective-origin : 50% 50%;
position : static;
/* May need to alter quotes for different locales (e.g fr) */
quotes : '\201C' '\201D' '\2018' '\2019';
right : auto;
tab-size : 8;
table-layout : auto;
text-align : inherit;
text-align-last : auto;
text-decoration : none;
text-decoration-color : inherit;
text-decoration-line : none;
text-decoration-style : solid;
text-indent : 0;
text-shadow : none;
text-transform : none;
top : auto;
transform : none;
transform-style : flat;
transition : none;
transition-delay : 0s;
transition-duration : 0s;
transition-property : none;
transition-timing-function : ease;
unicode-bidi : normal;
vertical-align : baseline;
visibility : visible;
white-space : normal;
widows : 0;
width : auto;
word-spacing : normal;
z-index : auto;
}
  • This was a good answer for the question it as posted on, but it is too implausible for OP's problem I feel. Doesn't deserve the down votes though. – Callum. May 13 '15 at 08:59