Chevron Shape using css with ::after

Question:
How do you create a chevron Shape using css with ::after?

The ::after pseudo-element allows you to create decorative elements or shapes that can be positioned relative to the element they are applied to. In the case of creating a down chevron shape using borders, it works through the use of CSS borders to create triangles. 

Here’s a breakdown of how your example works:


Explanation of Chevron Shape with ::after

CSS Rules Breakdown

#section::after {
    position: absolute;              /* Positions the pseudo-element relative to the nearest positioned ancestor */
    border-left: 18px solid transparent; /* Creates left side of the triangle */
    border-right: 18px solid transparent; /* Creates right side of the triangle */
    border-top: 18px solid red;      /* Creates the visible top side of the triangle */
    bottom: -18px;                   /* Positions the triangle 18px below the bottom of the #section */
    content: "";                     /* Required to make the ::after element appear */
    height: 0;                       /* Sets height to zero to only use the border for height */
    left: 50%;                       /* Centers the triangle horizontally */
    margin-left: -18px;             /* Adjusts the position back to the left by half its width */
    width: 0;                        /* Sets width to zero to only use the border for width */
}

How It Works 

  1. Triangle Creation: 
     
    1. The triangle shape is created using borders. When you apply a solid border to one side of an element and transparent borders to the other sides, a triangular shape is formed. 
    2. In this case:
      1. border-left: 18px solid transparent; and border-right: 18px solid transparent; make the left and right sides of the triangle invisible while giving it width. 
      2. border-top: 18px solid red; creates the visible top side of the triangle. 
         
  2. Positioning:
     
    1. position: absolute; allows the chevron to be positioned relative to the nearest positioned ancestor (an element with a position value other than static). If no such ancestor exists, it will be positioned relative to the initial containing block (likely the viewport). 
    2. bottom: -18px; moves the triangle downward, positioning it exactly below the bottom edge of the container (the #section).
       
  3. Centering:
     
    1. left: 50%; positions the left edge of the triangle at the center of the container. 
    2. margin-left: -18px; shifts the triangle back to the left by half its width (in this case, 18 pixels), effectively centering it beneath the container. 
       
  4. Content and Size: 
     
    1. content: ""; is necessary for the ::after pseudo-element to be generated; without it, the element won't be rendered. 
    2. height: 0; and width: 0; remove any default dimensions of the pseudo-element, ensuring that only the borders are used to create the shape.

Visualization

Here's how the visual part works:

  • The transparent borders form the sides of the triangle while the red border forms the base, creating an inverted triangle shape (downward chevron).

Conclusion

Using the ::after pseudo-element along with borders is an effective method to create shapes like a down chevron in CSS. This technique is widely used for creating decorative elements, arrow shapes, and other similar graphical features without needing additional HTML elements or images.