Exploring the power of nested SCSS pseudo selectors

I am working with a structure of pseudo elements, which looks like this:

&::before {
  content: '';
  position: absolute;
  height: 100%;
  width: 1px;
  background: ${(props) => props.theme.colors.link};
  right: 6px;
}
&:first-child:before {
   content: '';
   position: absolute;
   height: 50%;
   width: 1px;
   background: ${(props) => props.theme.colors.link};
   right: 6px;
   bottom: 0;
 }
&:last-child:before {
  content: '';
  position: absolute;
  height: 50%;
  width: 1px;
  background: ${(props) => props.theme.colors.link};
  right: 6px;
  top: 0;
}

While the code works perfectly fine, I am wondering if there is a way to make it look cleaner and more elegant. Despite trying different things, I haven't yet found an effective solution.

If anyone has any suggestions on how I can improve the appearance of this code while keeping its functionality intact, I would appreciate the insight.

Answer №1

Give this a shot

section {
  &::after{
    content: '';
    position: absolute;
    height: 100%;
    width: 2px;
    background: ${(props) => props.theme.colors.link};
    right: 8px;
  }
  &:first-child:after {
      height: 60%;
      bottom: 0;
  }
  &:last-child:after {
      height: 40%;
      top: 0;
  }
}

Answer №2

One effective way to create consistent styles is by using mixins and includes.

For instance, consider the following code snippet:

 @mixin common-styles($top,$bottom){
  content: '';
 position: absolute;
 height: 50%;
 width: 1px;
 background: ${(props) => props.theme.colors.link};
 right: 6px;
 top: $top;
 bottom:$bottom
}

&:last-child:before {
  @include common-styles(0 , unset);
}

&:first-child:before {
  @include common-styles(unset , 0);
 }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

problem with the height of the side navigation bar

Currently, I am in the process of creating a back-end system that includes a side navigation bar positioned on the left-hand side. To accomplish this, I have opted to utilize the Bulma CSS framework and integrate it with Laravel. Although I have implemen ...

How can I make a div using jQuery fade in when an input field is in

I want to create a tooltip that changes its innerHTML and fades in when an input on my form is focused. Additionally, I need to check if the tooltip has been faded in as it initially loads faded out. In essence, I am looking to switch the tooltip content ...

Arrange elements in a vertical flow based on the height of the container

I am attempting to alter the direction of Elements to be vertical. Here is an example: By default, HTML elements are displayed horizontally like this:- #container { position: absolute; width: 400px; height: 200px; border: 1px solid gree ...

What can I do to prevent my background images from overlapping each other?

I'm trying to get the background images to display side by side with equal sizes and no spacing between them. The current code achieves this, but the images end up overlapping slightly, not filling 100% of the container. Despite setting the backgroun ...

Warnings in Bootstrap and Jade

Can someone help me understand why the colors for 'success' and 'danger' are not showing up in my flash messages? The messages themselves appear as expected, but the associated colors are missing. Here is a snippet of my package.json: ...

The agony of CSS in Emacs on Auto Complete Mode, portrayed vividly!

Recently, I added Auto Complete Mode to my Emacs setup. Issue #1: When typing declarations, the auto-complete feature works fine until I hit ;: Unexpectedly, it automatically tries to complete something! Pressing Enter would accept this unwanted completi ...

Adjusting CSS using JQuery based on scroll position

I am attempting to dynamically change a CSS style depending on the viewer's position on the page. Despite researching numerous similar threads online, I have not been able to get this code to work as intended. Any assistance would be greatly appreciat ...

What sets flex-start apart from baseline?

When using the align-* properties in flexbox, what sets flex-start apart from baseline? In the code snippet below, both align-self: flex-start and align-self: baseline produce the same result. Can you identify scenarios where align-self: flex-start and a ...

Guide on organizing post titles into a two-column table

I need assistance with formatting post titles into a table with 2 columns. Here is an example layout I am aiming for: ------------------------------- post title 1 | post title 2 ------------------------------- post title 3 | post title 4 ----------- ...

Combination of written content and mathematical equations displayed on an HTML webpage

I've been tasked with creating something similar to the following on an HTML page: The math tag in HTML is not suitable because it struggles with handling the line break between parts "/begin()-/end()". To achieve the desired effect (succes ...

Transitioning from a Mobile-first design to a Desktop layout: Tips and tricks

I am seeking a way to transition image 1 from mobile view to desktop view seamlessly. Currently, I have a div set up for image 1 in the mobile layout and now I need to rearrange the order of the items. Would using Bootstrap be the most efficient solution f ...

Unique style pattern for parent links with both nested and non-nested elements

I am in the process of designing a website and I have a specific vision for how I want my links to appear, but I am unsure of how to achieve it. Here is the desired outcome: a red link a green link a red link a green link … Below is the HTM ...

Is there a rationale behind using intricate CSS classes such as page-left-column-container-box-element-header for technical purposes?

What is the reasoning behind using this specific CSS and HTML structure for styling web pages? <div class="page"> <div class="page-left-column"> <div class="page-left-column-container-box"> <div class="page-left-column-con ...

CSS deep level selectors

For the following HTML/CSS code, I am trying to target only level 1 "row" elements and not their nested counterparts. However, when using the child selector, it still selects the nested elements. How can I achieve selecting only level 1 "row" elements? ...

Struggling to integrate a Bootswatch theme with Bootstrap 4?

Interested in diving into Bootstrap 4 with SCSS, I have successfully set up Bootstrap 4 with SCSS but now I want to use a theme from . Transitioning from using themes with BS3's straightforward LESS files, I am facing some confusion with BS4 as they h ...

The notification panel pop-up box keeps moving away from the right side

I'm currently in the process of creating a straightforward vertical notification panel, similar to the one seen on Stack Overflow. However, I've encountered an issue where the pop-up is not staying positioned correctly to the right side. It behav ...

Experiencing difficulties with positioning text beside an image

Hello everyone, I'm a first-time poster seeking some assistance. I'm currently tackling an assessment and am struggling with the final part. The task involves creating a picture card with an image at the top, a circular image to the side, and tex ...

Video background in JavaScript

Can anyone help me with using the jQuery plug in videoBG to fill a top div with a video that has 100% width and height? I want the #top-video to fill the #top-content but nothing is showing up. Any suggestions would be appreciated. Thanks! <!DOCTYPE ...

Click event in jQuery triggers element movement

I'm having a tough time figuring this out. I initially thought it was the negative margin causing the issue, but even after removing it, the problem persists. Check out the link below to see what I mean when you click on one of the list items at the ...

Adjusting the CSS based on the screen's resolution and size

Currently, I am working with a client who needs interfaces to be adapted for two screens with a resolution of 2560x1600. The challenge is that both screens have the same resolution but different screen diagonals. One has a 27" diagonal and the other... on ...