Stylish CSS design for enhancing the appearance of grip boxes

Using CSS, I am trying to create a unique "grip" background that signifies an element is draggable. You can refer to this link:

Currently, I have assigned a class called dnd to the item, and my CSS code looks like this:

.dnd {
  background: #99bbee; /* looking for dynamic change */
  padding: 1em;
  margin: 2em;
  border-radius: 0.25em;
  width: 14em;
  transition: all 0.25s;
}
.dnd:hover {
  box-shadow: 0.25em 0.25em 0.3em rgba(0,0,0,0.2);
  margin: 1.75em 2.1em 2.25em 1.9em;
  cursor: move;
  cursor: grab;
  cursor: -moz-grab;
  cursor: -webkit-grab;
}
.dnd:active { 
  cursor: grabbing;
  cursor: -moz-grabbing;
  cursor: -webkit-grabbing;
}
.dnd::before { 
  content: " ";
  display: inline-block;
  width: 1.5em;
  height: 2em;
  margin: -0.5em 0.75em -0.5em -0.5em; 

  background-color: transparent;
  background-size: 0.5em 0.5em;
  background-position: 0 0;
  
  background-image: 
    -webkit-linear-gradient(#99bbee 50%, transparent 50%),
    -webkit-linear-gradient(left, rgba(0,0,0,0.2) 50%, transparent 50%);
  background-image: 
    -moz-linear-gradient(#99bbee 50%, transparent 50%),
    -moz-linear-gradient(left, rgba(0,0,0,0.2) 50%, transparent 50%);
  background-image: 
    -o-linear-gradient(#99bbee 50%, transparent 50%),
    -o-linear-gradient(left, rgba(0,0,0,0.2) 50%, transparent 50%);
  background-image: 
    linear-gradient(#99bbee 50%, transparent 50%),
    linear-gradient(to right, rgba(0,0,0,0.2) 50%, transparent 50%);
}

While this setup creates background boxes on the grip, I want to make the grip pseudo-element more versatile without relying on a fixed background color like #99bbee; using rgba(0,0,0,0.2) is fine. Is there a way to modify the CSS background image to handle different background colors?

Here is a jsfiddle link for reference: https://jsfiddle.net/luken/r69wfwjd/5/


Update in 2021: With widespread support for CSS Variables, they might be the optimal solution.

Answer №1

Using an SVG image background could be the optimal choice.

For example, if you examine how stackoverflow utilizes an SVG background (.grippie) to expand the textarea for writing answers, you'll see the benefits of this approach in action through SVG backgrounds.

The concept involves applying the css background property with url(...) and a data uri as the url.

The SVG code used can be found here.

.grip {
  background-size: cover;
  background: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI1IiBoZWlnaHQ9IjUiPgo8cmVjdCB3aWR0aD0iMSIgaGVpZ2h0PSIxIiBmaWxsPSIjODg4Ij48L3JlY3Q+Cjwvc3ZnPg==");
  width: 10px;
  height: 15px;
  cursor: grab;
  cursor: -webkit-grab;
  margin: 0.5em;
}

.container {
  display: flex;
  align-items: center;
}
<div class="container" draggable="true">
  <div class="grip"></div>
  <div class="content">A thing with a grip</div>
</div>
<div class="container" draggable="true">
  <div class="grip"></div>
  <div class="content">A thing with a grip</div>
</div>

Answer №2

To achieve this unique effect, you can implement multiple shadows for the pseudo-element:

.custom-effect {
  background: teal;
  padding: 1em;
  margin: 2em;
  border-radius: 0.5em;
  width: 16em;
  transition: all 0.5s;
  font-family: Helvetica;
}

.custom-effect::before {
  content: " ";
  display: inline-block;
  width: 0.75em;
  height: 0.75em;
  margin: 0em 2em 0em -1.25em; 

  background-color: transparent;
  box-shadow: 0.75em -1em 0 gray,
              0.75em -0.5em 0 gray,
              0.75em 0em 0 gray,
              0.75em 0.5em 0 gray,
              1.5em -1em 0 gray,
              1.5em -0.5em 0 gray,
              1.5em 0em 0 gray,
              1.5em 0.5em 0 gray;
}
<div class="custom-effect">
Element with unique shadow
</div>

Here is a creative snippet where the grid showcases a subtle darkened overlay.

The design includes 2 diagonal gradients that converge to form an interesting square pattern.

.custom-effect {
  background: teal;
  padding: 1em;
  margin: 2em;
  border-radius: 0.5em;
  width: 16em;
  transition: all 0.5s;
  font-family: Helvetica;
}


.custom-effect::before {
  content: " ";
  display: inline-block;
  width: 1.75em;
  height: 2em;
  margin: -0.75em 0.875em -0.75em -0.5em; 

  background-color: transparent;
  background-size: 0.63em 0.63em;
  background-position: 0 0, 0.31em 0.31em;

  background-image: 
linear-gradient(45deg, rgba(0,0,0,0.7) 0.22em, transparent 0.21em),
linear-gradient(225deg, rgba(0,0,0,0.7) 0.22em, transparent 0.21em);
  

}
<div class="custom-effect">
Element with unique shadow
</div>

Answer №3

By taking a simple set of dots like ...., rotating them 90 degrees, and applying text-shadow to repeat them, you can achieve this effect using just a small amount of CSS.

i.grip::after
{
content: "....";
display: inline-block;
color: #AAA;
font-style: normal;
font-family: sans-serif;
font-size:15px;
font-weight: bold;
transform: rotate(90deg);
text-shadow: 0px -4px 0px #AAA;
cursor:move;
margin-right:5px;
}
<div><i class="grip"></i> Drag Me By My Handle</div>

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

Utilizing a combination of PHP and jQuery, certain checkbox options were dynamically deactivated

<input type="checkbox" name="pref[]" value="red//fuji" />Fuji <input type="checkbox" name="pref[]" value="red//pinklady" />Pink Lady <input type="checkbox" name="pref[]" value="red//reddelicious" />Red Delicious <input type="checkbox" ...

Stop the resizing of a div that contains an image when additional text is inserted into it

My website has a div that is centered and contains a picture. The div adjusts its height to fit the picture, but when I add text, the div unexpectedly resizes so that its height is taller than the picture itself. Here is an example: Html: <div class=" ...

Pass data from a child controller to its parent using $emit

My goal is to pass values from dropdown lists and input fields in the FirstController to the MainController, and values from datepickers in the SecondController to the MainController. I have been attempting to use $emit and $on but without success. It&apos ...

Tips for adjusting the icon size within the <IconContext.Provider> dynamically

Currently, I am using the 'IconContext.Provider' tag to style my icons from the 'react-icons' library. Are there any solutions available to dynamically change the size of the icon based on the size of my media? Is using the global styl ...

Elevating the Sidebar: Transforming Bootstrap 4 Sidebar into a Top

Recently, I delved into the topic of creating a responsive sidebar menu in Bootstrap 4. After exploring various solutions, I opted for an alternate version. Here's the HTML code snippet that I implemented: <div class="container-fluid"> < ...

Tips for transferring a value from a Swift UITextField to a text field within an HTML form in a WKWebView using Swift

Consider the following HTML form: <p> <form> <input type="text" id="result" name="Result" width="20"/> </form> </p> I'm currently working on passing a String value from my UITextField in Swift to this ...

Creating a mouse-resistant div with a magnet-like effect

Looking for a way to make a circle move like a magnet effect when the mouse is near, causing elements underneath it to be exposed. If you want to see an example, check out this fiddle link: http://jsfiddle.net/Cfsamet/5xFVc/1/ Here's some initial c ...

Is it feasible to customize the appearance of output text to resemble that of a password input field in Rich Faces?

Within our dataTable, we have a collection of attributes. Included in these are outputtext fields that contain passwords. Typically, password fields display as a series of black bullets if they are input fields (inputSecret). Currently, the outputText fi ...

Utilizing Javascript in Spotfire for enhanced functionality

Currently, I have a dropdown menu with two options ("START" & "END"). Depending on the selected value from this dropdown, I want to display a specific DIV element. I attempted to use the code below, but unfortunately, it is not functioning as expected ...

The mobile menu is not responding to the click event

Upon clicking the mobile menu hamburger button, I am experiencing a lack of response. I expected the hamburger menu to transition and display the mobile menu, but it seems that neither action is being triggered. Even though I can confirm that my javascrip ...

Tips for incorporating the <li> element into a versatile layout

My dilemma involves a div containing multiple ul and li elements that are struggling to fill the available space effectively. The issue arises when there is either excess space on the right side or one of the uls gets displaced below the others due to vary ...

JavaScript programming does not rely on using the onclick method to style HTML elements

For a recent project, I was tasked with creating a toggle-style button setup for the challenge on CodePen. To achieve a 3D appearance for the button, I wrote some JavaScript code that utilized the onclick event. var on = true const green = document.getElem ...

Ways to perfectly align three images side by side using CSS

I am trying to align 3 pictures horizontally, but they keep ending up on separate lines. Here is the code I have been using: HTML <section class="features"> <figure> <img src="....jpg" alt="..."> ...

Attempting to extract information from website, successful on majority of sites but encountering issues on this particular one

We are looking to extract dividend data specifically from the Nasdaq exchange similar to this Google Sheets example. While most other URLs work without any issues, the query for this one seems to be failing. <?php $ch = curl_init(); curl_setopt($ch, C ...

Unable to modify the appearance of an HTML element when injected via a Chrome extension

I am currently developing a unique chrome extension that uses Ajax to inject custom HTML into the current tab. This extension appends a <div> element to the body, and now I need to manipulate it using JavaScript. Specifically, I want it to dynamical ...

Adjust the element colors within a Vue loop with dynamic changes

Hey there! I'm currently working on achieving a unique design inspiration that involves colorful badges grouped together. Here's a visual reference: https://i.sstatic.net/5LDBh.png In the image, you can see these badges grouped in pairs, but the ...

Display Loading Spinner with Bootstrap on Form Submission

This Django project features a Bootstrap spinner within a button, as seen in the code snippet below: <form method="POST" id="pdfUploadForm" enctype="multipart/form-data"> {% csrf_token %} {{ form|crispy }} <b ...

Tips for ensuring your jQuery events always trigger reliably: [Issues with hover callback not being fired]

My background image animation relies on the hover callback to return to its original state. However, when I quickly move the mouse over the links, the hovered state sticks. I suspect that I am moving the mouse off before the first animation completes, caus ...

Ensure that the search button remains at the bottom of the view without overlapping its container

this codepen link will take you to the relevant content https://codepen.io/rad-mej/pen/qBKedwB I have a fixed search menu on the left, along with data displayed on the right. The search menu contains a search button that I want to be anchored to the bott ...

Error: The term "Particles" has not been defined

I'm attempting to integrate code from a website into my project, but encountered an error when the particles failed to run after adding it. I downloaded and installed particle.js from "https://github.com/marcbruederlin/particles.js/issues" for this pu ...