3

I'm trying to find a way to override an inline width on an img tag, using inline css.

  • I do NOT have access to an external style sheet.
  • I can't change the img tag with its inline CSS. I'm working with a template and it's populated later.

What I want is to place a container div around the image tag such that the max-height=180, and max-width=120. Is there some sort of CSS magic and or trickery that I can do this?

<div style="put something here to override the 320px below">
        <img src="path/to/image" style="width:320px">   <!-- fixed tag -->
</div>
1
  • please try to use !important in inline-style and say that works or not? Commented Aug 29, 2014 at 3:59

2 Answers 2

2

You can add a small style block inline (fiddle here):

<style type="text/css">
    div img {
        width: 120px !important;
    }
</style>
<div>
    <img src="http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png"
        style="width:320px"/>
</div>

You probably would want want to add an ID to the div and the style rule to prevent unwanted effects in the rest of your page.


Update

If style elements cannot be used, you could try the following, using a CSS transform (fiddle here):

<div style="display: inline-block;
            transform: translate(-100px, -30px) scale(0.375, 0.375);">
    <img src="http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png"
           style="width:320px"/>
</div>

Now, your mileage will obviously vary depending on the level of CSS3 support in the mail client's rendering engines.

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

10 Comments

this might be the best/easiest answer. I'm having a "duh" moment reading it. Giving it a try for my test case... :D
but remember the major role is playing here is !important.
This won't work as inline style important not work then it also not work, please read my answer properly what I'm saying to use id and apply the style for that which won't be overriden by some script because that script is using no class or id....
just saw your comments that's why I removed my comments
I'm not sure why this was given -1. The Fiddle demo works. Unfortunately, I can't use style, or script tags. The editor strips them out.
|
1

Could you use JQuery? if yes this is a solution:

$("img[style='width:320px']").css("width","100px");

Check JSFiddle Demo

3 Comments

I thought about trying this. It's for an email template, so I don't know the effects of using jQuery, or even if I can.
@SteveUser:No, if it's for an email template i don't think it's possible to use JQuery.
OK, I thought so. Also, the template editor strips out the script tag. Thanks for the help though!

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.