Arthur Rump

Sometimes you want to embed a Tweet in your blog posts. At least I do. With a bit of CSS, you won't have to depend on an external script to make your embedded Tweet recognizable as a Tweet.

Fallback styling for embedded Tweets

CSS, Twitter, Embed Tweet

Embedding a Tweet on your website is easy to do. Find the tweet, click on Embed Tweet, copy, paste, done.

Let's first take a look at the code you just copied:

<blockquote class="twitter-tweet" data-dnt="true">
    <p lang="en" dir="ltr">
        Announcing <a href=";ref_src=twsrc%5Etfw">#fsharp</a> 4.6 <a href=""></a>
    &mdash; Phillip Carter (@_cartermp) <a href="">2 april 2019</a>
<script async src="" charset="utf-8"></script>

Sidenote: please add the data-dnt="true" attribute to tell Twitter you don't want them to track your visitors.

Note how all the actual content of the Tweet is just text in a blockquote. That's great because if the script does not load, the content you wanted to share is still there and readable. This could happen in situations where Twitter is entirely blocked (whether by a company or a nation-state), the user has JavaScript disabled, or because the script is blocked by Content Blocking in Firefox. However, this means that Tweets will be rendered as blockquotes by default. Here's a screenshot from my website with two embedded tweets:

Two embedded tweets formatted with standard blockquote styling
Tweets styled like quotes with my old website theme

These are simply styled as every other blockquote, which is not a problem in itself, but it does not give the reader an obvious clue to the source of the content. Sure, they will probably figure it out from the use of links and @username, but I'm often a bit confused when I see a tweet presented like this, especially since you're expecting the Tweet to be displayed in the widget as created by Twitter's script.

This is the reason I decided to add some distinct styling for Tweets when redoing the design for this site. Here's a comparison between my styling and the widget as rendered by Twitter:

Comparison between my custom styling of the blockquote and Twitter's widget, with a Tweet by Scott Hanselman
My custom blockquote styling on the left, the Twitter widget on the right

Another comparison, this time by Immo Landwerth
My custom blockquote styling on the left, the Twitter widget on the right

We only need a little bit of CSS:

blockquote.twitter-tweet {
    background-image: url("/ionicons/logo-twitter.svg");
    background-repeat: no-repeat;
    background-position: top 0.5rem right 0.5rem;
    background-size: 1.25rem 1.25rem;
    padding: 1rem 2rem 1rem 1rem;
    border: 1px solid #e1e8ed;
    border-radius: 5px;

blockquote.twitter-tweet > p {
    margin: 0;

Restricting the selector to only elements of type blockquote makes sure to only apply the styling in case the script has not loaded. The script will replace the blockquote with an iframe containing Twitter's widget and I don't want to mess that up. The Twitter bird is added as a background image in the upper right corner and the text is padded to make sure it doesn't flow over the logo. The border and border-radius properties match the values the script would set.

Since I'm already using Ionicons on this site, I just linked to the logo provided by Ionicons (with a little modification to set the color), but if you want a pure CSS solution, you can of course embed the SVG directly:

background-image: url("data:image/svg+xml;charset=utf-8,");

(This is also what the Twitter widget does, this is the SVG they use for it.)

And there it is in practice:

Looks quite nice, I think.

Share this:
Read also: