Alternative Image Classes in Markdown

Developer
Size
6,385 Kb
Views
30,360

How do I make an alternative image classes in markdown?

This is a kind-of "hack" to apply CSS to images in Markdown.. What is a alternative image classes in markdown? How do you make a alternative image classes in markdown? This script and codes were developed by Kevin Gimbel on 11 August 2022, Thursday.

Alternative Image Classes in Markdown Previews

Alternative Image Classes in Markdown - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Alternative Image Classes in Markdown</title> <style> /* NOTE: The styles were added inline because Prefixfree needs access to your styles and they must be inlined if they are on local disk! */ * { margin: 0; padding: 0; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; position: relative;
}
body { font: 1rem/1.6em sans; margin: 5em auto; width: 100%; max-width: 50em; padding: 1.5em; background: #ddd;
}
a { color: grey; text-decoration: none; border-bottom: 1px solid grey;
}
a:hover { color: black; border-bottom: none;
}
h1 { font: 2em/1.6em sans;
}
pre { background: #313131; padding: .5em; color: white; overflow-x: scroll;
}
h3 { padding: 1em 0 0 0;
}
p { padding: .5em 0 1em 0; line-height: 1.65em;
}
code { background: #313131; padding: .2em .4em; color: white;
}
img { width: 100%;
}
img:after { content: attr(alt); position: absolute; bottom: 0; left: 0; background: #313131; color: black;
}
/*IMG "Hack" */
img[title*="imgClass--small"] { width: 20em;
}
img[title*="imgClass--rounded"] { -moz-border-radius: 3px; -webkit-border-radius: 3px; border-radius: 3px;
}
img[title*="imgClass--shadow"] { -moz-box-shadow: 0 0 5px rgba(0, 0, 0, 0.8); -webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.8); box-shadow: 0 0 5px rgba(0, 0, 0, 0.8);
}
img[title*="imgClass--gray"] { -webkit-filter: grayscale(1); filter: grayscale(1);
}
img[title*="imgClass--float-right"] { width: 50%; float: right; margin-left: .5em; margin-bottom: .5em;
} </style> <script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
</head>
<body> <h1>Alternative image classes in Markdown</h1>
<h3>The Problem</h3>
<p>My main problem with Markdown is that I&#39;m unable to give classes to images. I like to have classes for everything. The benefits I have from giving classes to nearly every element is that I&#39;m in full control when I design a website. If I&#39;m using a lossy mix of direct selectors and classes it becomes messy.</p>
<p>However, let&#39;s assume I&#39;m writing an article with a lot of images. I don&#39;t want them all to just &quot;be&quot; there, I want to style them. I want them to float to the left and right, have borders or apply shadows or whatever else. I want to use the full potential of CSS. </p>
<h3>Markup</h3>
<p>The markup for images in Markdown is pretty simple as you can see below:</p>
<pre><code>![image alt text](http://url.to/img.jpg)
</code></pre>
<p>You can apply an alt Text by writing it between <code>[</code> and <code>]</code>, an title Attribute can also be set by writing it in quotes right after the URL or PATH of the image. The only way I found to apply a specific style to images in Markdown is &quot;hacking&quot; into the <code>title</code> or <code>alt</code> Attribute. I decided to do it with the <code>title</code>, it would work the same way with alt.</p>
<h4>Markdown</h4>
<p><img src="http://cdn.eyeem.com/thumb/h/800/605f2220bc97deaa5c22e9bf427893c2cfcc458c-1377867949" alt="Image of a Car.">
First we define an image in Markdown. It&#39;s the same you can see above. <em>Notice: It is already styled by CSS (see line 23)</em></p>
<pre><code>![Image of a Car.](http://cdn.eyeem.com/thumb/h/800/605f2220bc97deaa5c22e9bf427893c2cfcc458c-1377867949)
</code></pre>
<p>The next step is to apply a title Attribute to the image. </p>
<pre><code>![Image of a Car.](http://cdn.eyeem.com/thumb/h/800/605f2220bc97deaa5c22e9bf427893c2cfcc458c-1377867949 &quot;Hi, I&#39;m the title!&quot;)
</code></pre>
<p>We&#39;ll now use this title Attribute to style specific images.</p>
<h4>CSS</h4>
<p>To use our &quot;hacked&quot; class we now need to use a special CSS Selector Method: The Attribute Selector. With the Attribute Selector you can easily access an Attribute, similar to styling an <code>h2</code> an Attribute Selector Style applies to all matching elements. In our case we&#39;ll likely want to address images only.
The markup is pretty easy:</p>
<pre><code>img[title*=&quot;imgClass--rounded&quot;] { -webkit-border-radius:3px; -moz-border-radius:3px; border-radius:3px;
}
</code></pre>
<p>This small snippet simply points to &quot;an image with a title attribute that contains imgClass--rounded&quot;. There are other selectors like that, e.g. <code>a[href$=&quot;https&quot;]</code>,<code>input[type=&quot;text&quot;]</code>,<code>div[class^=&quot;whatever&quot;]</code> - I&#39;ll maybe cover them in a blog post.</p>
<p>To prevent conflicts I decided to name all &quot;classes&quot; I apply to the image <code>imgClass--</code> followed by the name. Below you can see some examples, hover them to get the Title attribute, the last word always represents the &quot;class&quot;.</p>
<p><img src="http://cdn.eyeem.com/thumb/h/800/605f2220bc97deaa5c22e9bf427893c2cfcc458c-1377867949" alt="Image of a Car." title="A beautiful car imgClass--rounded"></p>
<p><img src="http://cdn.eyeem.com/thumb/h/800/605f2220bc97deaa5c22e9bf427893c2cfcc458c-1377867949" alt="Image of a Car." title="A beautiful car imgClass--shadow"></p>
<p><img src="http://cdn.eyeem.com/thumb/h/800/605f2220bc97deaa5c22e9bf427893c2cfcc458c-1377867949" alt="Image of a Car." title="A beautiful car imgClass--gray"></p>
<p>Another use-case could be to float an image to the right or left as shown below. </p>
<p><img src="http://cdn.eyeem.com/thumb/h/800/605f2220bc97deaa5c22e9bf427893c2cfcc458c-1377867949" alt="Image of a Car." title="A beautiful car imgClass--float-right"></p>
<p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
<p>That&#39;s it. Suggestions can be sent directly via <a href="http://twitter.com/_kevinatari">twitter</a></p>
</body>
</html>

Alternative Image Classes in Markdown - Script Codes CSS Codes

* { margin: 0; padding: 0; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; position: relative;
}
body { font: 1rem/1.6em sans; margin: 5em auto; width: 100%; max-width: 50em; padding: 1.5em; background: #ddd;
}
a { color: grey; text-decoration: none; border-bottom: 1px solid grey;
}
a:hover { color: black; border-bottom: none;
}
h1 { font: 2em/1.6em sans;
}
pre { background: #313131; padding: .5em; color: white; overflow-x: scroll;
}
h3 { padding: 1em 0 0 0;
}
p { padding: .5em 0 1em 0; line-height: 1.65em;
}
code { background: #313131; padding: .2em .4em; color: white;
}
img { width: 100%;
}
img:after { content: attr(alt); position: absolute; bottom: 0; left: 0; background: #313131; color: black;
}
/*IMG "Hack" */
img[title*="imgClass--small"] { width: 20em;
}
img[title*="imgClass--rounded"] { -moz-border-radius: 3px; -webkit-border-radius: 3px; border-radius: 3px;
}
img[title*="imgClass--shadow"] { -moz-box-shadow: 0 0 5px rgba(0, 0, 0, 0.8); -webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.8); box-shadow: 0 0 5px rgba(0, 0, 0, 0.8);
}
img[title*="imgClass--gray"] { -webkit-filter: grayscale(1); filter: grayscale(1);
}
img[title*="imgClass--float-right"] { width: 50%; float: right; margin-left: .5em; margin-bottom: .5em;
}
Alternative Image Classes in Markdown - Script Codes
Alternative Image Classes in Markdown - Script Codes
Home Page Home
Developer Kevin Gimbel
Username kevingimbel
Uploaded August 11, 2022
Rating 3
Size 6,385 Kb
Views 30,360
Do you need developer help for Alternative Image Classes in Markdown?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Kevin Gimbel (kevingimbel) Script Codes
Create amazing marketing copy with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!