Is there a way to add dynamic inline styles to the :after pseudo-element of an element in Vue.js?

I am trying to dynamically load a div called image-background. Can anyone guide me on how to achieve this?

<div
      class="sub-service-box"
      v-for="item in serviceCategories[itemIndex].services"
      :key="item._id"
    >
      <div
        class="background-img"
        :style="{ backgroundImage: 'url(' + item.imageUrl + ')' }"
      >
      </div>

    </div>

The array serviceCategories[itemIndex].services is populated by an API call.

Answer №1

Is the variable imageUrl pointing to a remote URL with https, or is it a path to a static asset within your project?

If it's a remote URL, your code appears to be correct.

If it's a static asset in your project, make sure to use require in this manner:

<div
  class="sub-service-box"
  v-for="item in serviceCategories[itemIndex].services"
   :key="item._id"
>
  <div
    class="background-img"
     :style="{ backgroundImage: 'url(' + require(item.imageUrl) + ')' }"
  >
  </div>
</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

Matching Heights for Carousel Images

My image/video carousel is causing trouble as the content within doesn't maintain a consistent height. Clicking on the thumbnails highlights a mismatch in the main image heights. Is there a way to ensure that all elements have the same height while k ...

Implement a grid layout for columns within the profile section

Each user on my website has a profile tab that displays their submitted posts. To showcase these posts, I utilize the following code: <?php while ($ultimatemember->shortcodes->loop->have_posts()) { $ultimatemember->shortcodes->loop-> ...

Challenges with the Placeholder and Text Alignment in Angular Material's Text Area

I am currently facing an issue with a textarea in Angular, which is displayed as shown below: https://i.sstatic.net/jjUlT.png My Angular code for the textarea is as follows: <div *ngIf="whetherDisplay()" class="textarea-text"> <mat-form-field ...

What steps can I take to correct this CSS code? I am looking to update the content using CSS

Here is the code I have for the specific page shown in the screenshot: https://i.sstatic.net/57o2Z.jpg I want to change 'Replay course' to 'Access course'. Can anyone help me figure out what I'm missing? a.course-card__resume { d ...

Ways to ensure two stacked divs have equal height

I'm in the process of designing a card with two stacked divs. The upper section of the card contains an image, while the lower part consists of text. How can I ensure that both the top and bottom divs have equal heights? I am hesitant to specify fixed ...

Mastering the use of v-if and v-else in Vue JS for <td> table elements

After exhausting my search efforts without finding a solution, I am turning to this platform for clarification on how to correctly implement the v-if and v-else directives within <td> elements in an HTML table in Vue. //assuming that the value of th ...

Tips on achieving consistent division through CSS where both columns and rows have equal spacing

In my website's code, I am trying to display two blogs per row. While the width is divided correctly, there is an issue with the height. This is because some blogs have large titles and others have small titles, resulting in uneven content layout. In ...

Error in Laravel 5.5 PusherBroadcaster.php at line 106

I am facing a frustrating issue with the BroadcastException in PusherBroadcaster.php (line 106) error while using Laravel 5.5 and Vue 2.0. Despite trying various solutions, I have been unable to resolve it. Desperately seeking assistance. Here's what ...

Choose the label by utilizing the CSS selector

I am currently using CSS to create tabs with radio buttons. However, I am struggling to figure out how to select the corresponding <label> for the radio button. To keep the labels separate from the content and display them as tabs, I have structured ...

What steps can be taken to resolve the link prefetch warning in a Next.js application?

I am currently working on a Next.js application using version 13.4, and I've encountered a warning in the console: After preloading the resource at <URL>, it was not used within a few seconds of the window's load event. Please ensure that i ...

Fluid and static dimensions in CSS

I am looking to have two divs in the same row in HTML, where one remains a constant width while the other adjusts its size as the end user increases the web page. My initial attempt was to define a parent div with two child divs like this: <div> ...

Turn off automatic scrolling on Safari iOS 16 and 15 with single tab functionality enabled

Hey there! I'm encountering a frustrating issue that's really boggling my mind. When using iOS 15 or 16 with Safari and the "Single tab" setting enabled (which places the Safari search bar at the top), I noticed a strange padding animation when ...

Guide to adding a Font to a server

Recently, I downloaded and installed a font on my computer and incorporated it into my project. While it appeared to work perfectly on my local system, I encountered issues when I uploaded the page to the server. How can I ensure that the font is properly ...

Mastering the utilization of React props within a Tailwind component

Looking to implement a straightforward button component in a Next App using Tailwind CSS, where I can pass values such as background color, text color, etc. through props and use them to render different types of buttons within my application. Take a look ...

Incorporating a divider into a Material-UI MenuItem causes an additional border

I needed a way to separate the MUI MenuItem using a divider, but it resulted in an extra border around the item where the divider was placed. The highlighted section in the image shows the item that has the divider= true setting and the extra border. Is th ...

Manipulate the CSS style of the body element in Angular with Typescript using the important tag

Within my Angular application I have implemented the following code to customize the body style: constructor(private elementRef: ElementRef) { } ngOnInit() { this.elementRef.nativeElement.ownerDocument.body.style.overflowY = 'hidden'; ...

Utilizing HTML and CSS to Position Text Adjacent to the Initial and Final Elements in a Vertical List

Exploring a simple number scale ranging from 1 to 10, I experimented with different ways to represent it. Here's my attempt: <div class="rate-container"> <p class="first">Extremely Unlikely</p> <a class=" ...

Using the <pre> tag with Vue.js in v-html allows for displaying pre

When using v-html to display content retrieved via AJAX, I encountered an issue with the "pre" tag not rendering as expected. Here is my component setup: <div v-html="content"></div> For example, the content includes: <h1>Title</h1 ...

"What are some creative ways to customize the appearance of a Material-UI Select-field Dropdown menu

I am currently utilizing the material-ui select-field component. <SelectField multiple={true} hintText="Checkbox Filters" dropDownMenuProps={{ iconButton:<ActionHome />, }} className="checkbox-com" underlineStyle={{display ...

Unable to alter Mui input label color upon focus using theme.ts

In my next.js app, I'm facing an issue with changing the color of the label in a Material UI input field using Mui. Despite updating the theme.ts file to change the border bottom color and label color, only the border bottom color is being applied. T ...