10

I am doing a report and inside the report I have to render emails from different providers, this emails come with their own css (usually inline css but sometimes they apply general styles). I usually use iframes to encapsulate css so it doesn't breaks up mine but I can't use it now.

Is there a way to encapsulate css without the use of iframes?

here is an example of the problem I am having:

<html>
  <head>
    <style>
      // I enclose it to content so it doesn't override the email css 
      #my_content table, #my_content p {
        color: black;
      }
    </style>
   </head>
   <body>  
     <div id='my_content'>
      ... some stuff ...
     <div id='email'>
        <html>
          <head>
            <style>
              table {
                margin-left: 100cm; // screws up all my tables
              } 
              .... some styles that should only apply inside the email div ...
            </style>
          </head>
          <body>
            .... email content ...
          </body>
        </html>
      </div>
    </div>
  </body>
</html>

I could extract the html structure and just get what's on the body but then not all my emails will look as it should. Also the html must be valid so any suggestions would be great!

4
  • 2
    Yes! Use the scoped attribute. I don't believe it's supported anywhere yet, however. (Well, you can turn it on in Google Chrome in chrome://flags.) Commented Aug 28, 2012 at 20:14
  • I do use classes, let me explain better in the question how emails are breaking my css Commented Aug 28, 2012 at 20:30
  • ohhh the scoped attribute it's probably was I was looking for but it doesn't work for any major browser yet :'( w3schools.com/html5/att_style_scoped.asp Commented Aug 28, 2012 at 20:41
  • 6
    I wish I could simultaneously enter every single persons computer and block out w3schools from their search results. just saying Commented Aug 28, 2012 at 20:46

2 Answers 2

5

You can prepend #email to your css selectors, making them only apply to your div.

For example, change

.classname { display: block }

to

#email .classname { display: block }

Edit: If you have no control over the e-mail CSS as arxanas suggests, consider using LESS. Less is a CSS preprocessor that allows nesting of CSS selectors. If you include less.js, then you can do something like this:

#email {
    CSS goes here
}

less.js will parse this and convert it to CSS.

Sign up to request clarification or add additional context in comments.

4 Comments

I think the issue is that the styles are being loaded and he has no control over them.
Thanks arxanas for the clarification, I added a suggestion for this scenario.
yep I have no control over the css since is part of an email
Well, you did say you could process the HTML (or rather "extract the HTML structure"). If you can do that, then you can extract the rules from the <style> element, and prepend #email in front, or use less.js as @hayk.mart suggests. Currently the HTML presented is not valid.
1

You could try to check the E-Mail css part for collisions with Your own class names. I just found this post which could be helpful for You: Listing known CSS classes using Javascript

1 Comment

I see. He could try to replace those collisions with slightly modified identifiers. But that would't be as easy as to simply use css only like hayk.mart

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.