How to apply an icon image using the background property in CSS in Vue 3

Need help setting background image from the public folder

|_public
| |_images
|   |_icon.png
|_src
  |_components
    |_TheHeader.vue

I'm having difficulty trying to set an image as a background in TheHeader.vue using CSS, but the image is located in the public folder.

**vue**

<template>
 <input type="text" :style="background">
</template>

<script>
export default {
 computed: {
  background() {
   return { 
    'background': `url(${require('/images/search-icon.png')}) 5px center no-repeat;` 
   }
  }
 }
}
</script>


Error: Module not found: Error: Can't resolve '/images/search-icon.png' in C:/user...

even absolute path doesnt work

<style scoped>
input {
 background: url('../../../public/search-icon.png') 5px center no-repeat;
}
<style>
Error: Module not found: Error: Can't resolve '../../../public/search-icon.png' in 'C:\user...

Answer №1

Two different paths are provided for the search icon:

'../../../public/search-icon.png'
and '/images/search-icon.png'. It is unclear if the search icon is located in an images folder or not. Assuming it is, the following code should resolve the issue:

background() {
  return {
    background: `url('/public/images/search-icon.png') 5px center no-repeat`
  }
}

or

input {
  background: url('/public/images/search-icon.png') 5px center no-repeat;
}

../../public/images/search-icon.png
should also work. It seems like you went up one directory level too far with your relative path.

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

Using Vue.js: Ways to recycle a template within a component without needing to export it as a separate component

At times, there may be a need to reuse a template within a component without being able to iterate through them due to non-sequential rendering. Take for example: <template> <div> <template> <span>{{yearly.tex ...

What is the best way to declare a variable within a VueJS v-if condition without it being visible?

In my code, I have a specific template that is displayed when a certain condition is met using a v-if statement. In this template, I need to set a variable to N/A, but I am struggling with how to accomplish this simple task. <span v-if="se ...

Conceal the object, while revealing a void in its place

Is there a way to hide an image but keep the containing div blank with the same dimensions? I want it to appear as if no content was there, maintaining the original width and height. For example: http://jsfiddle.net/rJuWL/1/ After hiding, "Second!" appea ...

The font size of the textarea dynamically adjusts based on the size of the screen

Some strange behavior is occurring in the Android default browser when I set the width and height of a textarea to 100%. The font size of the textarea seems to increase based on the screen size, and even though I attempted to alert the font-size using jQue ...

How should a Vue component be organized? What order should functions be implemented in?

What is the best practice for structuring Vue components? How should computed properties, methods, components, mounted lifecycle hook, and watch properties be ordered? ...

Modifying the appearance of Bootstrap button colors

I recently came across this code on the Bootstrap website. I am curious about how to change the text color from blue to black in this specific code snippet. Currently, the text is appearing as blue by default. For reference and a live example, you can vis ...

Sophisticated method for implementing dynamic components with props in Vue

In a specific scenario, I am using an API to output all the content for my Page, including its structure. To provide context, imagine a CMS with a page builder feature where authors can place components via drag and drop to create pages, which are then del ...

Using VueJS to pass objects as props

My current project involves building controllers for sets of items. I am creating a Javascript object with functions and storing it in the data of the Vue instance. This object is then passed using: <dynamic-table :table-object="objTable"></dynami ...

Issue with Transition-group functionality

Check out this CodePen link for reference. Despite setting up a transition-group, I am not experiencing the expected fade effect when clicking on the 'CONSTRUCTION PROGRESS' tab. <transition-group name="fade" class="row no-gutters" v-show=" ...

The page query functionality seems to be malfunctioning when implemented within a Gridsome component

While using <page-query> within the index works fine, I encounter an error of "edges undefined" when I try to use it inside a component. Can someone provide assistance with this issue? ...

Is it feasible to design a distinctive layout while utilizing a v-for loop in Vue 2?

I am currently in the process of designing a questionnaire. Within my array, each question is represented as an object. As I iterate through them using <component :is>, this component property guides how the question will be displayed - for example, ...

What functionalities are included in the vue-cli-plugin-vuetify package?

Recently I realized that my Vuetify project, which is not bootstrapped by vue-cli, has been running in production without the vue-cli-plugin-vuetify installed. After months of using Vuetify, I'm curious to know what exactly this plugin does? Can anyon ...

How to center a span using Twitter Bootstrap

As a newcomer to Twitter Bootstrap, I'm wondering how I can center a span within a parent row. Currently, I am working with Twitter Bootstrap Version 2.3 instead of 3.1. In version 3.1, there is a center-block feature that I cannot utilize in 2.3. Th ...

To ensure proper display, the canvas should be set to display block when its width is 100vw and height is 100vh

Can anyone explain why I need to include display: block when my canvas is full page? Without it, the canvas size appears larger even though the width and height values are the same. Appreciate any insights. Thanks! ...

Updating the styles of React Native components using stylesheets

I've created a unique React component with the following structure: import { StyleSheet } from 'react-native'; import { Input, Item } from 'native-base'; import Icon from 'react-native-vector-icons/FontAwesome'; import { ...

Comparison of utilizing PHP within CSS versus Implementation through JavaScript [General]

Curious about the incorporation of PHP in CSS, especially in relation to my current Wordpress Theme project. I am aiming for high levels of customization and have been using a JS file to change CSS Properties through PHP. However, I'm not entirely con ...

How can I display a button (hyperlink) on a new line using CSS or jQuery?

I am looking to create a new line after the last input of my form. On this new line, I want to add a "remove-link". This is my HTML: <div class="subform dynamic-form"> <label for="id_delivery_set-0-price"> Price: </label> <inp ...

What is the most effective way to ensure that a child component only executes when a link is clicked on the Vue component?

There are two components in my code The first component, which is the parent component, looks like this : <template> <ul class="list-group"> <li v-for="item in invoices" class="list-group-item"> <div class="ro ...

Vite 3: Including a global variable in the configuration results in an additional line break at the end of the string

Vite 3 offers the capability to define environment variables that will replace the key with the corresponding value during project build. The section of the configuration where the version is defined appears as follows: define: { __RELEASE_VERSION__: JS ...

Receiving an error in TypeScript stating that the property or data does not exist on the type for Vue props

I've recently integrated TypeScript into my Vue project, and now I'm encountering an error every time I try to access a value in props or data: 37:46 Property 'activity' does not exist on type '{ props: { activity: { type: ObjectC ...