1. Web Design
  2. HTML/CSS
  3. JavaScript for Designers

Quick Tip: Styling Restricted Content for Online Publishers

In this quick tutorial, we’ll cover some ideas for styling restricted content–just like techniques you’ll see used by big online newspapers.
Scroll to top

Many popular online newspapers (e.g. The Wall Street JournalLe Figaro, The Telegraph) offer some free content, but require users to purchase a subscription if they want unlimited reading.

And in a world where many people expect content on the internet to be free, finding the most suitable payment model is an ongoing challenge for online publishers.

“One of the most essential all-time challenges for journalism is its economic sustainability.” – Aurelija Gackaitė

Take for example a post from The Wall Street Journal, where users are permitted an excerpt of an article for free, but need to subscribe to gain access to the full piece:

A post from The Whole Street JournalA post from The Whole Street JournalA post from The Whole Street Journal

In this short tutorial, we’ll use JavaScript to mimic this behavior. First we’ll create a post with restricted content, masked by a gradient, and then we’ll let subscribed users reveal the full post.

Here’s our demo:

Please accept marketing cookies to load this content.

1. Begin With the Page Markup

We’ll start by using the article element to represent a typical blog post or newspaper article. We’ll also wrap it inside a .container for setting a maximum page width.

Here’s the relevant markup:

1
<div class="container">  
2
  <article class="post">
3
    <header>
4
      <h1 class="post-title">...</h1>
5
      <div class="post-meta">...</div>
6
    </header>
7
    <div class="post-content">...</div>
8
    <div class="actions">
9
      <p>...</p>
10
      <div class="links">...</div>
11
    </div>
12
  </article>    
13
</div>

2. Add the CSS

By default we’ll add a not-member class to the html element (here we’re using the Add Class tool provided by CodePen:

Add the not-member class to the html elementAdd the not-member class to the html elementAdd the not-member class to the html element

This ensures that only a small part of the post will initially be visible to the readers:

The restricted post contentThe restricted post contentThe restricted post content

More specifically we’ll show just the header, the first two paragraphs, and the call-to-action links. We do this by using the “general sibling combinator” to select everything after the second paragraph (p:nth-child(2) ~ *) where .not-member is present:

1
.not-member .post-content p:nth-child(2) ~ * {
2
  display: none;
3
}
4
5
.not-member .actions {
6
  display: block;
7
}

Next we’ll add a few styles to the last visible paragraph (second). We’ll gradually hide its contents and add a padlock icon on top of it. These styles will let readers know that access is restricted to the whole article.

Here’s the CSS needed for this bit:

1
.not-member .post-content p:nth-child(2) {
2
  position: relative;
3
}
4
5
.not-member .post-content p:nth-child(2)::before,
6
.not-member .post-content p:nth-child(2)::after {
7
  content: '';
8
  position: absolute;
9
  bottom: 0;
10
  left: 0;
11
  right: 0;
12
}
13
14
.not-member .post-content p:nth-child(2)::before {
15
  background: url(padlock.svg) no-repeat center;
16
  z-index: 1;
17
  height: 24px;
18
}
19
20
.not-member .post-content p:nth-child(2)::after {
21
  background: linear-gradient(rgba(255,255,255,0.18) 18%, rgba(255,255,255,0.36) 36%, rgba(255,255,255,0.54) 54%, rgba(255,255,255,0.72) 72%, rgba(255,255,255,0.9) 90%);
22
  height: 100%;
23
}

To round off these basic styling rules, we’ll style the call-to-action section:

1
.not-member .actions {
2
  font-weight: bold;
3
  text-align: center;
4
  margin-top: 2rem;
5
}
6
7
.not-member .links {
8
  display: grid;
9
  grid-gap: 1rem;
10
  grid-template-columns: 1fr 1fr;
11
  margin-top: 1rem;
12
}
13
14
.not-member .links a {
15
  background: firebrick;
16
  color: white;
17
  padding: 0.5rem 0;
18
  transition: background 0.3s;
19
}
20
21
.not-member .links a:hover {
22
  background: #9d1e1e;
23
}

3. Unlock Premium Content

For the purposes of our simple demo, let’s assume that a reader becomes a premium member each time he or she clicks on a call-to-action. 

In our case, at that point the not-member class is removed from the html element and the full post content becomes visible. 

Here's the JavaScript code responsible for that functionality:

1
const links = document.querySelectorAll(".not-member .links a");
2
3
for(const link of links) {
4
  link.addEventListener("click", e => {
5
    e.preventDefault();
6
    document.documentElement.classList.remove("not-member"); 
7
  });
8
}

By the time the full post appears, there’s no need to style the second paragraph differently as well as show the initial call-to-actions. So, let’s hide them all:

1
.post-content p:nth-child(2)::before,
2
.post-content p:nth-child(2)::after,
3
.actions {
4
  display: none;
5
}

Finally, we can restart the subscription/unsubscription process by clicking on the Unsubscribe/Sign Out link which can be found at the bottom of the post content:

UnsubscribeSign Out linkUnsubscribeSign Out linkUnsubscribeSign Out link

Here’s the required JavaScript code:

1
const unsubscribe = document.querySelector(".unsubscribe");
2
3
 unsubscribe.addEventListener("click", e => {
4
    e.preventDefault();
5
    document.documentElement.classList.add("not-member"); 
6
  });

Conclusion

In this quick tutorial, we built a simple demo that covered some ideas for styling restricted content. Similar techniques are used by big online newspapers and publishers, but you may find a use for this yourself.

We only simulated the subscription, so to implement this properly you would have to work out the signup process behind the scenes. And it’s important to note that the content we’ve hidden is only hidden visually–the page source still contains the restricted content. Bear this in mind if you’re publishing genuinely sensitive material.

As always, thanks for reading!

Read All About it!