惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Commits to openclaw:main
Recent Commits to openclaw:main
L
LangChain Blog
月光博客
月光博客
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 【当耐特】
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
博客园_首页
T
Tailwind CSS Blog
P
Proofpoint News Feed
雷峰网
雷峰网
D
Darknet – Hacking Tools, Hacker News & Cyber Security
IT之家
IT之家
V
Vulnerabilities – Threatpost
阮一峰的网络日志
阮一峰的网络日志
C
CERT Recently Published Vulnerability Notes
Attack and Defense Labs
Attack and Defense Labs
S
Schneier on Security
Security Archives - TechRepublic
Security Archives - TechRepublic
L
Lohrmann on Cybersecurity
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
WordPress大学
WordPress大学
The Register - Security
The Register - Security
N
Netflix TechBlog - Medium
Hugging Face - Blog
Hugging Face - Blog
Project Zero
Project Zero
博客园 - 叶小钗
F
Full Disclosure
大猫的无限游戏
大猫的无限游戏
Latest news
Latest news
S
SegmentFault 最新的问题
C
Cyber Attacks, Cyber Crime and Cyber Security
Google Online Security Blog
Google Online Security Blog
Recorded Future
Recorded Future
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
L
LINUX DO - 最新话题
Google DeepMind News
Google DeepMind News
P
Privacy International News Feed
I
InfoQ
F
Fortinet All Blogs
Vercel News
Vercel News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Threatpost
T
Tenable Blog
B
Blog RSS Feed

Ahmad Shadeed

Better fluid sizing with round() Use Cases for Field Sizing The Basics of Anchor Positioning Item Flow CSS Relative Colors Balancing Text In CSS Should masonry be part of CSS grid? CSS display contents CSS Grid Areas CSS Cap Unit An Interactive Guide to CSS Container Queries CSS :has() Interactive Guide CSS Nesting UX in DevTools CSS Nesting Future CSS: State Container Queries Conditional CSS with :has and :nth-last-child CSS Text balancing with text-wrap:balance CSS Masking Do we need CSS flex-wrap detection? My CSS Wishlist Conditional CSS CSS Style Queries Inside the mind of a frontend developer: Article layout Inside the mind of a frontend developer: Hero section CSS container queries are finally here The CSS behind Figma First Look At The CSS object-view-box Property Learn CSS Subgrid CSS :has Parent Selector Aligning Content In Different Wrappers Flexbox Dynamic Line Separator Hello, CSS Cascade Layers Building UI Components With SVG and CSS A Deep CSS Dive Into Radial And Conic Gradients Defensive CSS Building Real-life Components: Facebook Messenger Conditional Border Radius In CSS CSS Container Query Units Less Absolute Positioning With Modern CSS Aligning a Button Label Vertically Comparing Design Mockups To Code Result Using HSL Colors In CSS Custom Scrollbars In CSS Let CSS Container Queries For Designers The State of CSS Cross-Browser Development Overflow Issues In CSS Inspect Element As A Way To Increase Your Curiosity Handling Text Over Images in CSS Digging Into CSS Logical Properties Clipping Scrollable Areas On The inline-start Side Understanding Clip Path in CSS The Art of Building Real-life Components Handling Short And Long Content In CSS CSS Scroll Snap A Deep Dive Into CSS Grid minmax() CSS Variables 101 Finding The Root Cause of a CSS Bug Learn CSS centering How to detect browser support for Flexbox Gap CSS Mistakes While On Autopilot Digging Into the Flex Property Understanding CSS Multiple Backgrounds Aligning Logo Images in CSS Grid for layout, Flexbox for components Colors in CSS Thinking About The In-between Design Cases min(), max(), and clamp() CSS Functions Image Techniques On The Web Everything About Auto in CSS Learn Box Alignment Let Learn CSS Positioning Intrinsic Sizing In CSS CSS Grid Template Areas In Action Hiding Elements On The Web Creating a Variable Color Font From Scratch Building a Football Ticket With CSS and SVG Blending Modes in CSS CSS Variables With Inline Styles Implementing Dark Mode For My Website Rebuilding Apple Music Header in HTML & CSS Accessible Checkbox Layout Flickering On Browser Resize Enhancing The Clickable Area Size Custom Underlines with SVG Part 3: The Process of Implementing A UI Design From Scratch Part 2: The Process of Implementing A UI Design From Scratch Building An Old Nav Design CSS Flexbox: 5 Real World Use Cases I Used CSS Inline Flex For The First Time The Process of Implementing A UI Design From Scratch Common CSS Issues For Front-End Projects Handling Long and Unexpected Content in CSS How to Build Web Form Layouts With CSS Grid Grid Layout Ah-ha Moment Enhancing Our Components with CSS :empty Building Resizeable Components with Relative CSS Units CSS Writing Mode The Journey of Learning Front End Web Development on a Daily Basis
Rebuilding a comment component with modern CSS
Ahmad Shadeed · 2023-06-13 · via Ahmad Shadeed

The Layout Maestro

I spent years teaching CSS layout on this blog. I put everything I know into The Layout Maestro course: 70+ lessons and 150+ interactive examples that teach you how to think CSS layouts, not just memorize syntax.

Get the course

As a part of my curious UI inspection, I thought about why not to look at popular websites and see how they implemented the comment component layout. At first, I thought this will be an easy task, but it wasn’t.

I spent a lot of time trying to understand how this works and how we can do it better with modern CSS like :has, size container queries, and style queries. In this article, I will walk you through my thought process and share my findings along the way.

Let’s dive in!

Article series

Table of contents

  • Introduction
  • Thinking about the layout
  • Comment wrapper layout - The padding solution
    • Using data attributes to handle the spacing
    • Using CSS style queries
  • Comment wrapper layout - CSS subgrid
  • Thinking about the connecting lines
    • Handling the line added to the main comment
  • The comment component
    • Changing the user avatar size
    • Dynamic text alignment with dir=auto
  • CSS Logical Properties
  • Disabling the lines
  • The emoji-only state
  • Conclusion

Introduction

Here is the layout that we will build. At first glance, it might be simple, but there are a lot of tiny details.

We have a comment that can be nested into two more levels. I will call those “depths” in the article.

Consider the following figure.

It shows how the depth is changed based on the level of nesting for each comment.

Thinking about the layout

Before diving into the fine details, I prefer to work on the layout first and make sure it works great. The goal of this practice is to explore the potential of modern CSS to solve that problem for me.

The first to keep in mind is the HTML markup. The structure of the comments is a good fit for an unordered list <ul>.

<ul>
  <li>
    <Comment />
  </li>
  <li>
    <Comment />
  </li>
</ul>

The <Comment /> acts as a placeholder for the comment component. That is the HTML for a list of two comments without any replies.

If we have a reply or more to one of the comments, then a new <ul> will be appended.

<ul>
  <li>
    <!-- Main comment -->
    <Comment />
    <ul>
      <!-- Comment reply -->
      <li>
        <Comment />
      </li>
    </ul>
  </li>
  <li>
    <Comment />
  </li>
</ul>

From a semantic point of view, the above sounds logical.

I called the title “comment wrapper” to not confuse the meaning of the comment component itself. In the following section, I will explain my thinking on building the layout to handle the indentation or spacing for the comment replies.

Consider the following markup:

<ul>
  <li>
    <!-- Main comment -->
    <Comment />
    <ul>
      <!-- Comment reply -->
      <li>
        <Comment />
      </li>
    </ul>
  </li>
  <li>
    <Comment />
  </li>
</ul>

We have two main comments and one reply. From a visual point of view, it will look like the following:

I prefer to keep all the spacing and indentation stuff to the <li> items only, as they act as a home for the comment component.

Using data attributes to handle the spacing

The first thing I thought about is using data attributes on the <ul> and <li> elements.

<ul>
  <li nested="true">
    <!-- Main comment -->
    <Comment />
    <ul>
      <!-- Comment reply -->
      <li>
        <Comment />
      </li>
    </ul>
  </li>
  <li>
    <Comment />
  </li>
</ul>

We can call the <ul> and do specific styling as needed. In the figure below, the first reply has a spacing on the left to indicate that it’s visually related to the first comment.

In CSS, we can do something like this:

li[data-nested="true],
li[data-nested="true]
  li {
  padding-left: 3rem;
}

While that might work, we can do much better with CSS variables and style queries.

Using CSS style queries

We can check if the CSS variable --nested: true is added to the container and style the child items based on that.

Consider the following markup where I added an inline CSS variable --nested: true to the <ul> elements.

<ul>
  <li style="--nested: true;">
    <!-- Main comment -->
    <Comment />
    <ul style="--nested: true;">
      <!-- Comment reply -->
      <li>
        <Comment />
      </li>
    </ul>
  </li>
  <li>
    <Comment />
  </li>
</ul>

With style queries, I can check if the CSS variable is there and style the <li> element based on that. This is supported in Chrome Canary only, for now.

@container style(--nested: true) {
  /* Add spacing to the 2nd level <li> items. */
  li {
    padding-left: 3rem;
  }
}

Here is why I prefer style queries over data attributes:

  • It’s easier to understand. It reads @container, and the plain words say it. If the container has the --nested: true CSS variable, then apply the following stuff.
  • It’s possible to combine multiple style queries for more control over CSS.
  • If needed, we can use size container queries along with style queries, too.

Another solution is to use CSS subgrid to build the nested comments layout. Let me be honest with you, it will be more CSS code, but it’s cool to explore the potential of new CSS features.

I will explain about subgrid in a brief to give you an idea. Consider the following CSS grid:

<ul>
  <li class="main">
    <!-- Main comment -->
    <Comment />
    <ul>
      <!-- Comment reply -->
      <li>
        <Comment />
        <ul>
          <li>
            <Comment />
          </li>
        </ul>
      </li>
    </ul>
  </li>
</ul>
li.main {
  display: grid;
  grid-template-columns: 3rem 3rem 1fr;
}

This will be added to the first direct <li> element of the <ul> list. That grid will look like this:

Currently, in CSS grid, it’s not possible to pass the main grid to the child items. In our case, I want to pass the grid columns to the first <ul>, and then to the <li> of that <ul>.

Thanks to CSS subgrid, this is possible now. It’s available only in Firefox and Safari. Chrome is on the way!

Consider the following figure:

First, we need to set up the main grid as follows. We have 3 columns.

@container style(--nested: true) {
  li.main {
    display: grid;
    grid-template-columns: 3rem 3rem 1fr;

    .comment {
      grid-column: 1 / -1;
    }
  }
}

The .comment component will always span the full width. That’s why I added grid-column: 1 / -1. It means: “span the comment from the first to the last column”. This is helpful to avoid entering the column number manually for each depth in the nesting. Something like this:

/* Not good */
@container style(--nested: true) {
  li.main .comment {
    grid-column: 1 / 4;
  }

  ul[depth="1"] .comment,
  ul[depth="2"] .comment {
    grid-column: 1 / 3;
  }
}

Cool. The next step is to position the depth=1 comments within the main grid, and then add subgrid and position the inner <li>.

@container style(--nested: true) {
  ul[depth="1"] {
    grid-column: 2 / 4;
    display: grid;
    grid-template-columns: subgrid;

    > li {
      grid-column: 1 / 3;
      display: grid;
      grid-template-columns: subgrid;
    }
  }
}

Finally, we need to position the depth=2 list.

@container style(--nested: true) {
  ul[depth="2"] {
    grid-column: 2 / 3;
  }
}

That solution works, but I’m not a fan of all of that details. A simple padding can fix it.

Still not sure about how it works? See the following video which shows me inspecting the grid in the DevTools.

Check out the Codepen demo.

That’s it! Now we have comments with spacing that reflects their relationship.

Thinking about the connecting lines

To make it more clear that there is a reply to a comment, there are connecting lines between the main comment and replies. The Facebook team used a <div> to handle the lines. Can I do something different?

Consider the following figure:

At first, your mind might try to trick you into: “Hey, this looks like a rectangle with a left and bottom border and a border radius on the bottom left corner”.

li:before {
  content: "";
  width: 30px;
  height: 70px;
  border-left: 2px solid #ef5da8;
  border-bottom: 2px solid #ef5da8;
  border-bottom-left-radius: 15px;
}

The moment I typed height:.. in the above CSS, I realized that this won’t work. How would I know the height? It’s not possible with CSS, why? Because I need to make the bottom edge of the line aligned to the first reply avatar.

Then, I thought about using pseudo-elements for that purpose. What if that curved line can be divided into two parts?

The line can be added to the main comment, and the curved element is for the reply.

Well, what if we have another reply to the first reply? Here is a visual that shows how the lines will work:

In CSS, we need to use pseudo-elements to achieve the line effect. Before diving into the CSS to implement that, I want to highlight that the line or the curve will be positioned according to the full row.

Handling the line added to the main comment

This is the first challenge to tackle. We need to add a line to the ‌first comment if it has replies. How to do that? Thanks to CSS :has, we can check if an <li> contains a <ul>, and if yes, we apply the CSS needed.

Consider the following HTML:

<ul style="--depth: 0;">
  <li style="--nested: true;">
    <Comment />
    <ul style="--depth: 1;">
      <li>
        <Comment />
      </li>
    </ul>
  </li>
  <li>
    <Comment />
  </li>
</ul>

We need to style every <Comment /> with the following conditions:

  • it’s a direct child of an <li>
  • the <li> has a <ul> as a child
  • the parent is at depth: 0 or depth: 1

Here is how the above translate to CSS. CSS variables + style queries + has = a powerful conditional styling.

@container style(--depth: 0) or style(--depth: 1) {
  li:has(ul) > .comment {
    position: relative;

    &:before {
      content: "";
      position: absolute;
      left: calc(var(--size) / 2);
      top: 2rem;
      bottom: 0;
      width: 2px;
      background: #222;
    }
  }
}

The above is just an example of why I prefer using style queries over HTML data attributes for handling the depth of the comment and replies.

Next, we need the line and curved element for the depth: 1 replies. This time, the <li> will use both the :before and :after pseudo-elements.

@container style(--depth: 1) {
  li:not(:last-child) {
    position: relative;

    &:before {
      /* Line */
    }
  }

  li {
    position: relative;

    &:after {
      /* Curved element */
    }
  }
}

Finally, We need to add the curved element to each <li> of depth: 2, and the line to all <li>s except the last one. We need to do the following logic:

  • Add the curved element to each <li> in depth: 2.
  • Add the line to each <li> except the last one in depth: 2.

The curved line is a rectangle with a border and radius on the bottom left. Let me explain that:

@container style(--depth: 2) {
  li {
    position: relative;

    &:after {
      content: "";
      position: absolute;
      inset-inline-start: 15px;
      top: -2px;
      height: 20px;
      width: 28px;
      border-inline-start: 2px solid #000;
      border-bottom: 2px solid #000;
      border-end-start-radius: 10px;
    }
  }

  li:not(:last-child) {
    &:before {
      /* Line */
    }
  }
}

Notice that I’m using logical properties for the border and border-radius. This is useful to make the UI dynamically flip when the document language is RTL. I will come to this later in the article.

Check out the Codepen demo.

Disabling the lines

What if for some reason we need to hide connecting lines? Well, thanks to style queries, this is simple as toggling on and off a CSS variable.

By nesting all the depth related style queries within the --lines: true one, we can ensure that the limes will be shown only when the CSS variable is set.

@container style(--lines: true) {
  @container style(--depth: 0) {
  }

  @container style(--depth: 1) {
  }

  @container style(--depth: 1) {
  }

  @container style(--depth: 2) {
  }
}

Checkout the following video:

You might be thinking that all of the above is just for the main containing layout and lines. Yes, that’s correct! I haven’t even thought about the comment component yet.

Let’s talk a closer look:

At first glance, this is a perfect case for flexbox. We can have flexbox to display the avatar and comment box in the same line.

<div class="comment">
  <div class="user"></div>
  <!-- Because an additional wrapper doesn't hurt. -->
  <div>
    <div class="comment__body"></div>
    <div class="comment__actions">
      <a href="#">Like</a>
      <a href="#">Reply</a>
    </div>
  </div>
</div>

Please note that the HTML above is very basic. It doesn’t reflect a production-level code, and it is just there to help explain the CSS stuff.

.comment {
  --size: 2rem;
  display: flex;
  gap: 0.5rem;
}

.avatar {
  flex: 0 0 var(--size);
  width: var(--size);
  height: var(--size);
  border-radius: 50%;
}

That’s the basic layout, but we have a lot more than that. However, in this article, I will focus only on the things that are unique and important to explain.

Next are a few considerations for the comment body component.

This part alone of the comment component will need to handle:

  • Minimum width
  • Long content
  • Multilingual content (LTR vs RTL)
  • Context menu
  • Comment interactions
  • Editing state
  • Error state

I won’t be able to highlight all of the above in this article, as I might end up writing a book.

What I will highlight are some bets that I found interesting to be a good candidate for modern CSS.

Changing the user avatar size

When a reply is nested within a comment, the user avatar size will become smaller. This is good to make it visually easy to distinguish between a main comment and a reply.

Using style queries is perfect for that.

.user {
  flex: 0 0 var(--size);
  width: var(--size);
  height: var(--size);
}

.comment {
  --size: 2rem;

  @container style(--depth: 1) or style(--depth: 2) {
    --size: 1.5rem;
  }
}

Dynamic text alignment with dir=auto

A comment might contain a left-to-right (LTR) or right-to-left (RTL) language. The text alignment should be different based on the content language. Thanks to dir=auto HTML attribute, we can let the browser do that for us.

<div class="comment">
  <div class="user"></div>
  <div>
    <div class="comment__body">
      <p dir="auto"></p>
    </div>
    <div class="comment__actions"></div>
  </div>
</div>

CSS Logical Properties

By using CSS logical properties, we can build the comment component in a way that changes based on the document direction. The same applies to the connecting lines, too.

Check out the following video. Notice how it makes switching from LTR to RTL a breeze!

The emoji-only state

When the user adds a comment that consists of emojis only, the containing wrapper will change a bit:

  • No background
  • No padding

This is a perfect use-case for CSS :has.

.comment:has(.emjois-wrapper) {
  background: var(--default);
  padding: var(--reset);
}

Conclusion

It’s always thrilling of what can be done with modern CSS. Trying to think about a component or a layout that is already built in a new ways is a great way to learn new stuff. I learned a lot of new things and enjoyed the whole process.

Thank you for reading.