Using linear-gradient() in the border-image property does not actually generate a border

I am attempting to add a linear gradient border around a dynamically created div using JavaScript. However, I am not seeing the border show up as expected and it is not displaying in black.

Check out my code snippet below:

const devlogList = document.createElement("ul");
devlogList.classList.add("cards");

const devlogs = [{
  name: "Upriver (placeholder)",
  gradientTopLeft: "red",
  gradientTopRight: "blue",
  imageSrc: "exampleimage.png",
  description: "description",
  status: "status",
}];

for (let i = 0; i < devlogs.length; i++) {
  const devlogItem = document.createElement("li");

  const devlogDiv = document.createElement("div");
  devlogDiv.classList.add("devlog");

  const devlogTitle = document.createElement("p");
  devlogTitle.style.color = devlogs[i].gradientTopLeft;
  devlogTitle.innerText = devlogs[i].name;

  const devlogStatus = document.createElement("p");
  devlogStatus.innerText = devlogs[i].status;

  devlogDiv.appendChild(devlogTitle);
  devlogDiv.appendChild(devlogStatus);

  devlogItem.appendChild(devlogDiv);

  devlogList.appendChild(devlogItem);
}

document.getElementById("devlogs").appendChild(devlogList);
.devlog {
  list-style-type: none;
  border-image: linear-gradient(to bottom right, blue, red) 1;
  width: 500px;
}

body {
  background-color: black;
  color: white;
}
<div id="devlogs"></div>

I am running Firefdox version 126.0.1 (64-bit), which should support the use of gradient with the border-image property from v29 onwards.

Answer №1

To add a border to your elements, you can specify the style in your .css file like this:

border-style: solid;

According to MDM, it is important to note that "some browsers may not render the border image if the border-style is set to none or the border-width is 0."

const devlogList = document.createElement("ul");
devlogList.classList.add("cards");

const devlogs = [{
  name: "Upriver (placeholder)",
  gradientTopLeft: "red",
  gradientTopRight: "blue",
  imageSrc: "exampleimage.png",
  description: "description",
  status: "status",
}];

for (let i = 0; i < devlogs.length; i++) {
  const devlogItem = document.createElement("li");

  const devlogDiv = document.createElement("div");
  devlogDiv.classList.add("devlog");

  const devlogTitle = document.createElement("p");
  devlogTitle.style.color = devlogs[i].gradientTopLeft;
  devlogTitle.innerText = devlogs[i].name;

  const devlogStatus = document.createElement("p");
  devlogStatus.innerText = devlogs[i].status;

  devlogDiv.appendChild(devlogTitle);
  devlogDiv.appendChild(devlogStatus);

  devlogItem.appendChild(devlogDiv);

  devlogList.appendChild(devlogItem);
}

document.getElementById("devlogs").appendChild(devlogList);
.devlog {
  list-style-type: none;
  border-image: linear-gradient(to bottom right, blue, red) 1;
  width: 500px;
  
  /* This attribute is necessary for proper rendering in some browsers */
  border-style: solid;
}

body {
  background-color: black;
  color: white;
}
<div id="devlogs"></div>

Answer №2

const devlogList = document.createElement("ul");
devlogList.classList.add("cards");

const devlogs = [{
  name: "Upriver (placeholder)",
  gradientTopLeft: "red",
  gradientTopRight: "blue",
  imageSrc: "exampleimage.png",
  description: "description",
  status: "status",
}];

for (let i = 0; i < devlogs.length; i++) {
  const devlogItem = document.createElement("li");

  const devlogDiv = document.createElement("div");
  devlogDiv.classList.add("devlog");

  const devlogTitle = document.createElement("p");
  devlogTitle.style.color = devlogs[i].gradientTopLeft;
  devlogTitle.innerText = devlogs[i].name;

  const devlogStatus = document.createElement("p");
  devlogStatus.innerText = devlogs[i].status;

  devlogDiv.appendChild(devlogTitle);
  devlogDiv.appendChild(devlogStatus);

  devlogItem.appendChild(devlogDiv);

  devlogList.appendChild(devlogItem);
}

document.getElementById("devlogs").appendChild(devlogList);


.devlog {
  list-style-type: none;
  border-image: linear-gradient(to bottom right, blue, red) 1;
  width: 500px;
  border-style: groove
}

body {
  background-color: black;
  color: white;
}


<div id="devlogs"></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

Access denied for generating login hint on target domain in javascript web application for Google sign-in

Utilizing the Google signin Javascript API with the gapi-signin-button on a webapp. The app is being served by a gulp server, binding to 0.0.0.0. Everything works fine during local development, but encountering issues when accessing the page through a publ ...

Tips for utilizing a personalized extension in Visual Studio Code

Embarking on my HTML journey, so please bear with me. I've been watching tutorial videos to get a better grasp of it, and one of the tutorials I'm following shows how to create a resume on GitHub using VS Code. Check out the video here At 3:44 ...

Increase the size of the image beyond the boundaries of its container

I am facing an issue with an image inside a div. Whenever I add a margin-top, the background of the div extends downwards, which is not the desired behavior. How can I fix this problem? Below is my code snippet: <div id="content"> <div class=" ...

Creating a responsive navbar with a custom width form

Just started working with bootstrap and playing around with some new UI concepts. I'm having an issue with a non-responsive form width within my navbar, even though the navbar itself is responsive. I've tried using different collapse classes, but ...

What is the solution to the error message "Cannot assign to read only property 'exports' of object '#<Object>' when attempting to add an additional function to module.exports?"

I've been struggling for the past 5 days with this issue. Despite numerous Google searches, I haven't found a solution that works for me. I have a file called utils.js which contains useful functions to assist me. However, when I include the func ...

unable to apply custom css to primefaces components

In my Java EE application, I am using a ready-made template with CSS and jQuery. All the PrimeFaces components are rendering properly except for the panelGrid control of PrimeFaces 3.2. It is being displayed with a border, but I want it without a border. I ...

When I click the mouse, my drawing function starts lines from the top left corner instead of the latest point

http://codepen.io/FreelanceDev/pen/kLpJSf?editors=1010 You can find my CodePen project through the provided link. While the HTML and CSS elements are working correctly, I am facing issues with the JavaScript functionality. The desired outcome should be d ...

Personalize the appearance of dynamically generated DIV elements

This script generates a random number of squares (ranging from 20 to 40) and adds text to each square. The script then calculates the width of each square so that they all fit in a single row. Here is the code snippet: var quantity = Math.floor(Math.ran ...

Challenges encountered in d3.js when parsing through data sets

For the past two days, I have been struggling to solve this error and I'm at a loss. Every time I try to add data to the visualization, I encounter the following error: Uncaught TypeError: Cannot read property 'length' of undefined It seem ...

The useEffect hook in ReactJs is triggering multiple times

Encountering challenges while developing an Infinite scroll using ReactJs and Intersection observer API. Upon initial application load, the API gets called twice instead of once. This behavior may be due to strict mode, although not confirmed. Additionall ...

How can I incorporate eventData when using .bind() for the "keydown" event type?

I want to achieve something like this: var keydownHandler = function (ev) { alert(ev.data.test); return false; } $(document).bind('keydown', {test: 'foo'}, keydownHandler); However, the .bind method doesn't seem to be wor ...

What is the best method for translating object key names into clearer and easier to understand labels?

My backend server is sending back data in this format: { firstName: "Joe", lastName: "Smith", phoneNum: "212-222-2222" } I'm looking to display this information in the frontend (using Angular 2+) with *ngFor, but I want to customize the key ...

Using the width property to style a div element

There are two div elements in my HTML, each with a specified width. However, I want both divs to take up the full space as designated in the width property, regardless of the content inside them. Currently, the divs adjust their size based on the content a ...

Problem with AJAX updating CSS class

My HTML table features a column that displays a Yes/No option. When a user switches the option from Yes to No or vice versa, it triggers an AJAX call to a PHP script which updates the corresponding record in the database. Everything is functioning correctl ...

Passing a class as a parameter in Typescript functions

When working with Angular 2 testing utilities, I usually follow this process: fixture = TestBed.createComponent(EditableValueComponent); The EditableValueComponent is just a standard component class that I use. I am curious about the inner workings: st ...

Is it possible for me to transfer a class attribute to a directive template in AngularJS?

I recently came across this directive in AngularJS: productApp.directive('notification', function($timeout) { return { restrict : 'E', replace : true, scope : { type: "@", message: "@ ...

As the browser window is resized, the gap between images widens while the images themselves decrease

Hey there, I'm encountering some trouble with the image links at the top of my page. As I resize the browser or change screen resolutions in media queries using percentages, the images are resizing accordingly. However, I'm noticing that as the i ...

What is the functionality of the icon `<i>` in Twitter Bootstrap?

After adding an icon to a webpage, I noticed that it replaced the old, semi-deprecated <i> tag and instead used a customized image tag. It almost seems like <i ...> is just a simpler version of <img ...>. I'm curious about how this ...

What causes the sudden disappearance of the returned value from AJAX?

This piece of code is what runs on my server: modify_emp.php <?php echo $_POST[id]; ?> Below is the Javascript code in my HTML page: <script> $(document).ready(function () { var alreadyClicked = false; $('.element').h ...

Retrieve all items from the firebase database

I have a query. Can we fetch all items from a particular node using a Firebase cloud function with an HTTP Trigger? Essentially, calling this function would retrieve all objects, similar to a "GET All" operation. My next question is: I am aware of the onW ...