<p>I found this to work quite well for adding Bootstraps "img-fluid" class automatically:</p>

<pre><code>{{ entry.yourTextField|replace('&lt;img ', '&lt;img class="img-fluid" ')|raw }}
</code></pre>

CC BY-SA 4.0
2019-11-22T11:14:48.097Z
2019-11-22T11:14:48.097Z
Adrian
<p>There's a plugin that's supposed to do what you want: <a href="https://github.com/nilsenpaul/redactoraddattributes/" rel="nofollow">Redactor Add Attributes</a>, but unfortunately it doesn't work with Craft newer that 2.0 (which includes the completely refactored Redactor 10).</p>

CC BY-SA 3.0
2015-05-27T10:20:11.090Z
Nope, sorry for that. If anyone has the time to make the necessary adjustments, I'd be more than happy to update the plugin.
Paul
CC BY-SA 3.0
2015-05-27T09:48:23.433Z
2015-05-27T10:14:39.253Z
2015-05-27T10:14:39.253Z
carlcs
<p>I wrote a plugin called <a href="https://github.com/mmikkel/RetconHTML-Craft" rel="noreferrer">Retcon HTML</a> which will solve this nicely using PHP's <a href="http://php.net/manual/en/class.domdocument.php" rel="noreferrer">DOMDocument</a> class.</p>

<p>You'll want to use the <code>attr</code> filter, which is documented <a href="https://github.com/mmikkel/RetconHTML-Craft/wiki/Attr" rel="noreferrer">here</a>:</p>

<pre><code>{{ entry.richTextField | retconAttr( 'img', { 'class' : 'image fit' }) }}
</code></pre>

CC BY-SA 3.0
2015-10-19T09:11:31.393Z
This is a really clean solution, thank you very much.
Masiorama
CC BY-SA 3.0
2015-05-27T11:57:02.517Z
2015-05-27T13:20:18.477Z
2015-05-27T13:20:18.477Z
Mats Mikkel Rummelhoff
<p>How can I add a CSS class to all images inserted into a rich text field ?</p>

<p>ie. so an image inserted has style= "image fit" added to the img selector</p>

<redactor>
Adding class to images in rich text
# Adding class to images in rich text

(Asked by: username_0 on 2015-05-27)

How can I add a CSS class to all images inserted into a rich text field ?

ie. so an image inserted has style= "image fit" added to the img selector

**Tags:** redactor


---

## 3 answers

### Answer by username_4 on 2015-05-27. Score: 6 | Accepted answer

I wrote a plugin called Retcon HTML which will solve this nicely using PHP's DOMDocument class.
You'll want to use the attr filter, which is documented here:
{{ entry.richTextField | retconAttr( 'img', { 'class' : 'image fit' }) }}

> **Comments:**
> **username_5**: This is a really clean solution, thank you very much.  

---

### Answer by username_1 on 2019-11-22. Score: 3

I found this to work quite well for adding Bootstraps "img-fluid" class automatically:
{{ entry.yourTextField|replace('


---

### Answer by username_2 on 2015-05-27. Score: 1

There's a plugin that's supposed to do what you want: [Redactor Add Attributes](https://github.com/nilsenpaul/redactoraddattributes/), but unfortunately it doesn't work with Craft newer that 2.0 (which includes the completely refactored Redactor 10).

> **Comments:**
> **username_3**: Nope, sorry for that. If anyone has the time to make the necessary adjustments, I'd be more than happy to update the plugin.  

---

