Adjust the width of a box-shadow using percentage values in CSS

As we work on developing our tool according to the provided design, we are facing a challenge in drawing a line within a table row using box-shadow. The requirement is to have the line cover only 30% of the row width, rather than spanning across the entire row.

We attempted to use the "width" property but faced difficulties in achieving the desired outcome.

For reference, here is the jsfiddle link showcasing our current implementation: https://jsfiddle.net/7va8t56z/4/

Below is the snippet of how we are applying the shadow to draw the line:

tbody tr { 
    box-shadow: inset 1px 2px 1px 0px rgb(82, 243, 109);
}

Is it possible to adjust the width of the green line without altering the overall length of the table or row? We aim to modify only the box-shadow width.

Here is a visual representation of the desired outcome: https://i.stack.imgur.com/sjMGJ.png

Thank you for your assistance.

Answer №1

If you're looking to enhance the first two columns only, you may want to consider using this style:

table { 
  border-spacing: 0;
}

tbody tr td:nth-child(1) {
  box-shadow: inset 1px 2px 0px 0px rgb(82, 243, 109);
}

tbody tr td:nth-child(2) {
  box-shadow: inset 0px 2px 0px 0px rgb(82, 243, 109);
}

Answer №2

When working with the box-shadow property as your best option, consider implementing multiple shadows for added effect.

table tr { 
  box-shadow: 
   /* In order to create a more dynamic visual effect, set the first shadow
   with a color that matches your background and adjust the initial values accordingly */
   inset -30vw -8px 0 10px #1a234b,
   inset -3px 2px 0 4px rgba(64, 210, 88)
}

Answer №3

If you're looking for a solution, I recommend this approach, complete with detailed explanations in both the CSS and JavaScript code snippets:

// We begin by selecting the first/only <button> element and adding an event listener to handle the 'click' event:
document.querySelector('button').addEventListener('click',
  // Inside the function, we locate the <tbody> element within the document and use Element.classList API to toggle the 'showHighlight' class on it:
  () => document.querySelector('tbody').classList.toggle('showHighlight')
);
body.dark-layout {
  background-color: #161d31;
  color: #fff;
}

/* To position the "highlighting" relative to the <tr> size rather than the <td> elements, we add position: relative to the <tr>: */
tbody tr {
  position: relative;
}

/* Using the "showHighlight" class-name as part of the selector ensures that the highlight appears only when the class is present. Adjust the inline-size to your preference: */
tbody.showHighlight tr td:first-child::before {
  /* Styling the borders and creating the desired half-shadow effect: */
  border: 2px solid hsl(120deg 60% 50% / 1);
  border-block-end-color: transparent;
  border-inline-end-color: transparent;
  content: '';
  block-size: 100%;
  inline-size: 30%; /* This can be adjusted */
  pointer-events: none;
  position: absolute;
}
<body class="dark-layout">
  <!-- Added button for toggling highlight applied: -->
  <button id="toggleHighlight">Toggle highlight</button>
  <table style="width: 100%;">
    <thead role="rowgroup">
      ...
    </table>

</body>

Check out the JS Fiddle demo here.

If you want to experiment more, here is another version with additional features:

...

// Adding event listener for input changes to dynamically update styles:
document.querySelector('form').addEventListener('input', (evt) => {
  let tbody = document.querySelector('tbody'),
    color = document.querySelector('input[type=color]'),
    inlineSize = document.querySelector('input[type=range]');
  [color, inlineSize].forEach(
    (el) => {
      let output = el.closest('label').querySelector('output');
      tbody.style.setProperty(`--${el.name}`, isNaN(el.value) ? el.value : `${el.value}%`);
      if (output) {
        output.textContent = el.value;
      }
    });
});
*,
::before,
 ::after {
  ...
}

@media screen and (width <=880px) {
  label {
    align-items: center;
    flex-direction: column;
    justify-content: start;
  }
  ...
}
...
  <form action="#">
    <fieldset>
      <label>
        <span class="labelText">Select highlight color:</span>
        <input name="color" type="color" value="#32CD32">
      </label>
      ...

</body>

Explore the updated JS Fiddle demo here.

For further details, refer to the following resources:

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

Cursor or caret bleeding through overlay on Internet Explorer

Currently, I am working on a pre-existing website called www.shopthethirdfloor.com. When using Internet Explorer, if you navigate to the products menu, select the search box, and then scroll down so that the search field goes underneath the menu overlay ...

Is there a way to adjust the font color when the expiration date passes?

My goal is to change the color of text to green when the expiration date has not been reached yet. If the expiration date has passed, then the text should be red. .curentDate { color: rgb(54, 168, 54) } .expirationDate{ color: red; } <div cl ...

Issue with IE11: Flexbox with absolute positioning not sizing correctly, fills entire width instead of individual content

Here's a simple case to reproduce the issue: <div style="display: inline-block; position: relative; border: 1px solid black;"> short <div style="display: flex; position: absolute; border: 1px solid black; top: 100%; lef ...

Applying media queries to incorrect screen sizes

My media query seems to be behaving oddly. @media only screen and (max-width: 1200px) { .tool-panel { margin: 0px 24px 0px 0px; } } It is getting applied at 1183.20px instead of 1200px. This discrepancy is consistent across all browsers ev ...

What is the best way to incorporate modal window parameters into this code snippet?

JavaScript function: function loadBlockEditor(block, username) { var blockInfo = $.ajax({ url: "in/GameElement/BlockEditor.php", type: "GET", data: 'block=' + block + '&nick=' + username, dataType: "html" }); b ...

I am experiencing a CSS display issue on the mobile version of my map leaflet where only the header and footer are visible

I am experiencing a display issue on Android and iPhone due to conflicting css commands. Here's what's happening: I am using React + React Leaflet with a header, main, and footer all set to width 100% within a parent div with body set to width an ...

JavaScript has the ability to manipulate the width of an element by using methods to both retrieve

I have a unique situation where I need to dynamically adjust the width of a ul list element based on the text content inside it. Specifically, I want the width of the ul list to be equal to the width of the first list item, which can change frequently. My ...

Exploring the latest features in Bootstrap 4 Alpha and enhancing your

Rumors have it that Bootstrap 4 will make its debut during the year 2016. When duty calls for an upgrade, is a complete re-write truly the best solution? Tackling such a project from Boostrap 2 to 3 was no small feat, and I find myself hesitant to take on ...

p5.js experiencing issue: Uncaught TypeError - Unable to access property 'transpose3x3' due to null value

I have a simple website built with Flask where I am running several p5.js sketch animations, and everything is working smoothly. However, when I try to add a new sketch that utilizes WEBGL, I encounter the following error: Uncaught TypeError: Cannot read p ...

Issue with Hiding Bootstrap Tabbed Content Correctly

I am currently struggling to make a Bootstrap tab field function correctly, encountering some obstacles along the way. While the content is successfully tabbing between each div, the issue I'm facing is that the content from each tab is actually bein ...

Achieving Perfect Vertical Alignment in Bootstrap 4 Grid Cells

I am currently working on an admin page that involves querying and reporting on the page weights of key pages on a website, such as landing pages. Bootstrap 4 is in the pipeline, and I have utilized it to design a layout that I am satisfied with. However, ...

The calculator I designed using HTML, CSS, and JavaScript is experiencing difficulty adjusting to various screen sizes

I recently built a calculator using HTML, CSS, and JavaScript. It works perfectly on PC or big screens, but when viewed on smaller devices like phones or tablets, the display gets cut off and does not adjust properly. Here are some example pictures for ref ...

Creating a static line or separator based on the content using HTML and CSS

Is there a way to adjust the height of a line or divider based on the content of either the left or right section? For instance, if we have a left panel and a right panel, and we draw a line or divider on the left side panel, is it possible for the line ...

Adjust CKEditor's height within Vue.js

I recently began experimenting with integrating ckeditor5 into a Vue.js project. However, I encountered an issue where I couldn't manually adjust its height. Below is the code snippet I used - could you please review it and let me know if there are an ...

The color of the navbar-toggler-icon in Bootstrap cannot be altered

While working on customizing my Bootstrap 5 navbar, I encountered an issue with changing the color of the navbar-toggler-icon. The preset colors, navbar-light and navbar-dark, didn't align with my theme, so I attempted to change the color to pure whit ...

What is Causing The Card to Not Expand?

I wanted to enhance my project by incorporating a responsive card template, so I turned to one of the templates on CodePen for help. However, after copying the code into my own platform, I encountered an issue where the card wouldn't expand when click ...

Guide to center align fields in Ionic and Angular

I am working on creating a login screen that resembles the image provided. I have managed to set the background image, input fields, and buttons successfully. However, I am encountering a few issues: The width of my input field is taking up the entire spa ...

the background image shivering with movement

While experimenting with some animations, I encountered a small issue: When trying to animate a div that has an image as its background, the image appears to shake, especially when expanding and shrinking the div from the center. Is there a way to prevent ...

Embed a script into an iframe from a separate domain

While experimenting, I attempted to inject a script inside an iframe element using the following method. However, since the child and parent elements do not belong to the same domain (and I am aware that XSS is prevented in modern browsers), I was wonder ...

The webkitTransitionEnd event fires prior to a repaint or reflow occurring

My goal is to create a progressBar that changes its width when an ajax request is made. I want the ajax callback to only execute after the animation of the progressBar is complete. Here is the code I am using: CSS: #progressBar{ position: fixed; ...