Remove attributes "height" and "width" of the image tag
Asked Answered
C

12

12

In order to create a responsive website with Typo3 and Twitter Bootstrap, I would like to remove the height and width attributs of images

Here's how images are generated in Frontend via content element of type text & image and image

<img src="typo3temp/pics/a625b79f89.jpg" width="300" height="226" alt="blabla" />

I would like to remove the dimension attributes and get this:

<img src="typo3temp/pics/a625b79f89.jpg" alt="blaba" />

Can anyone help me ?

Cystocarp answered 13/6, 2012 at 11:46 Comment(1)
if jquery is fine with you, then please see this api.jquery.com/removeAttrOperator
S
6

It's not possible to remove the height or width image attributes with typoscript.

But you could override it with CSS in order to create a responsive website.

img { height:100% !important; width:100% !important; }

Sidewinder answered 1/2, 2014 at 19:18 Comment(1)
I have come across the same issue in order to keep original size of the new image the width & height of old attributes to be removed jquery way is [here][1] by setting 100% !important in css you are telling browser to set the img size 100% relative to its parent irrespective of its original size which I guess is not the right way to do [1]: w3schools.com/jquery/html_removeattr.aspDoiron
L
10

Call an image tag in document.ready function.

$('img').removeAttr('width').removeAttr('height');
Legacy answered 13/6, 2012 at 12:56 Comment(1)
With Bootstrap you may want to also add the img-responsive class to the images at the same time. $('img').removeAttr('width').removeAttr('height').addClass('img-responsive');Jamiejamieson
R
6

FYI: There is no way to remove this with typoscript. The width and height attribute is hardcoded in sysext/cms/tslib/class.tslib_content.php function cImage. (Typo3 4.7)

Reveille answered 13/6, 2012 at 15:0 Comment(1)
A pity that it takes developers so long to get rid of web 1.0 habits.Encompass
S
6

It's not possible to remove the height or width image attributes with typoscript.

But you could override it with CSS in order to create a responsive website.

img { height:100% !important; width:100% !important; }

Sidewinder answered 1/2, 2014 at 19:18 Comment(1)
I have come across the same issue in order to keep original size of the new image the width & height of old attributes to be removed jquery way is [here][1] by setting 100% !important in css you are telling browser to set the img size 100% relative to its parent irrespective of its original size which I guess is not the right way to do [1]: w3schools.com/jquery/html_removeattr.aspDoiron
G
5

Use jquery

set an id to your image object:

<img src="typo3temp/pics/a625b79f89.jpg" width="300" height="226" alt="blabla" id="myimage" />

$('#myimage').removeAttr("height").removeAttr("width");

here is alternative javascript code:

var myImage= document.getElementById("myimage");
myImage.removeAttribute("heigth");
myImage.removeAttribute("width");
Goetz answered 13/6, 2012 at 11:49 Comment(2)
I would have preferred a solution via typoscript. But I will test it in jQuery. I had not thought of jQuery. I will test it.Cystocarp
i suggested to use jquery , wont be sorryGoetz
H
3

This will be possible with TYPO3 6.2 LTS. Checkout http://forge.typo3.org/issues/49723.

Hypophyge answered 12/9, 2013 at 18:3 Comment(0)
B
3

To remove the effects of fixed width and/or height attributes without actually removing these attributes you can set them back to "auto" in your CSS definitions:

img {
    height: auto !important;
    width: auto !important;
}

I suggest to do that only for the img tags where you really need the pictures to be fluid. This could be done by adding an id or class to the img tags or any surrounding tag.

E.g. if your fluid images are located in a wrapper div with class "fluid_pics" you could use:

.fluid_pics img {
    height: auto !important;
    width: auto !important;
}

Often you will only need to set the height back to auto, as the width is already overwritten to be 100% by your framework (e.g. Twitter Bootstrap).

Blackmail answered 2/10, 2014 at 17:45 Comment(1)
In my opinion this is the best approach. No javascript required, and things like auto-scaling in combination with max-width still work. Thanks!Mcdevitt
E
2

TypoScript to Remove "width" and "height" attributes from the source code. TYPO3 CMS 6.1+.

tt_content.image.20.stdWrap.parseFunc.nonTypoTagStdWrap.HTMLparser.tags.img.fixAttrib {
width.unset = 1
height.unset = 1
border.unset = 1
}

tt_content.textpic.20.stdWrap.parseFunc.nonTypoTagStdWrap.HTMLparser.tags.img.fixAttrib {
width.unset = 1
height.unset = 1
border.unset = 1
}

lib.parseFunc_RTE.nonTypoTagStdWrap.HTMLparser.tags.img.fixAttrib {
width.unset = 1
height.unset = 1
border.unset = 1
}

tt_news example: News List

plugin.tt_news.displayList.image.stdWrap.parseFunc.nonTypoTagStdWrap.HTMLparser.tags.img.fixAttrib {
width.unset = 1
height.unset = 1
border.unset = 1
}
Egin answered 6/4, 2015 at 3:47 Comment(0)
F
1

If your image has an id or another unique attribute assigned, you can easily do this:

var myImg=document.getElementById('myImage');
myImg && myImg.removeAttribute('height') && myImg.removeAttribute('width');
Furlana answered 13/6, 2012 at 12:22 Comment(0)
L
1

For Typo3 6.2 upwards you may set a custom image render layout (snippet belongs to TS setup):

tt_content.image.20.1 {
    layout {
        mylayout {
            # equals default rendering, except width and height removed
            element = <img src="###SRC###" ###PARAMS### ###ALTPARAMS### ###BORDER### ###SELFCLOSINGTAGSLASH###>
        }
    }
}

Enable the custom layout for css_styled_content (snippet belongs to TS constants):

styles.content.imgtext.layoutKey = mylayout

See https://docs.typo3.org/typo3cms/TyposcriptReference/ContentObjects/Image/Index.html#cobj-image-layoutkey for details on the IMAGE cObject.

Leatrice answered 14/9, 2015 at 7:36 Comment(2)
I am not able to get this to work with T3 7.6 - do both scripts go in the SETUP section of my template?Toothbrush
@Toothbrush I edited the answer with hints, where the snippets have to be placed.Leatrice
M
1

This is a VERY simple solution with jQuery. i found the answer at wpwizard.net.

Here's the URL: http://wpwizard.net/jquery/remove-img-height-and-width-with-jquery/

Here's the jQuery:

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function($){

    $('img').each(function(){ 
        $(this).removeAttr('width')
        $(this).removeAttr('height');
    });

});
</script>
Macaroni answered 11/3, 2016 at 14:32 Comment(0)
C
1

There is a solution for old TYPO3s with TYPOSCRIPT:

stdWrap {
    replacement {
        10 {
            search = /\s+(width|height)="[^"]+"/
            useRegExp = 1
            replace =
        }
    }
}

Use this stdWrap-TS where your image is rendered.

Chantalchantalle answered 4/10, 2016 at 12:51 Comment(0)
D
0

it dont work in 6.1 :/ but u can use CSS:

img {
    display: block;
    max-width: 100%;
    height: auto;
}
Dree answered 7/4, 2015 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.