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

Creating a dynamic grid design with images using the <ul><li></li></ul> HTML tags

Trying to create a responsive grid of images that adapts to changes in browser size. Is there a way to achieve this using a list? Ideally, I want the images to be evenly spaced in rows. For example, if there are three rows of four images on top and one r ...

When using Material UI GridTile, the actionIcon will not be shown if there is no title present

In my React application, I am utilizing Material-UI GridList to showcase some images. I am interested in adding an IconButton on top of the image in my GridTile component. Currently, I can achieve this by providing the button to the actionIcon prop. Howeve ...

Setting a fixed row height for Material-UI tables in ReactJS, even when the content is long

I'm currently working on a listing component using the material UI table in ReactJS. Within this table, I have a ResumeUrl field that can span over 3 or 4 lines, but I need to limit it to just 2 lines with the rest of the content being represented by ...

Stopping CSS animations at a specific point along the path without using timing functions can be achieved by implementing a targeted

Is there a way to pause an animation at the 50% mark for 2 seconds and then resume? P.S. The setInterval method is not recommended! function pauseAnimation() { document.getElementById("0").style.animationPlayState = "paused"; }; var pauseInterval = set ...

Keep an eye out for the Store to update by using an If statement and waiting for

Currently, I am utilizing a watch function to monitor any changes in appStore.storeID. When the value of storeID changes and is greater than zero, it should trigger a database Fetch using Supabase. <script setup> //import Store Data import { useAppSt ...

What is the most effective method for generating dial images through programming?

Currently, I am in the process of creating a website that makes use of variously sized and styled dials to indicate progress. The filled portion of the dial represents how close an item is to being 100% complete. I am seeking a universal solution that wil ...

Tips for setting height within a flexbox layout?

Is there a way to specify the height of rows in a flex wrap container so that text containers appear square on both smaller and larger screens? I want the sizing to be dynamic, adjusting as the window is resized. Check out this example on CodeSandbox Loo ...

In JavaScript, alert a message once all images have been clicked

I'm encountering a small issue with my javascript code. I am developing a game for a school project where the objective is to click (remove) fish using a fishing rod. However, the game does not have an end condition set up, so players cannot win. Belo ...

Styling your printed documents in Next.js 10

Within my Next 10 application, I am able to bring in global styles by importing them in _app.js in the following manner: import 'assets/css/app.css' I want to incorporate a print stylesheet as well. Instead of using a @media print { } query with ...

Proper method for positioning text in a line

Trying to recreate the image below, but facing alignment issues with the text in my code. How can I vertically align the text so that they are aligned like in the photo? Flexbox hasn't helped due to varying text lengths causing misalignment. const ...

Managing asynchronous data in various locations using Vuex modules

I'm struggling to grasp the correct usage of Vuex and whether it's necessary in my situation. Imagine I have a basic online shop consisting of two components: Product list. Product filtering. If I don't use Vuex: I can create Filtering as ...

Excessive width of Bootstrap container

Encountering a rather simple issue: the bootstrap container inside my navbar is too wide, causing an unwanted horizontal scrollbar as it expands the body. You can view the problematic page here. The theme used for this site can be found here. What's ...

The Toggle Functionality necessitates a double-click action

I'm currently working on implementing a menu that appears when scrolling. The menu consists of two <li> elements and has toggle functionality. Everything is functioning properly, except for the fact that the toggle requires two taps to activate ...

Issue with passing props from parent component to child component in Vue.js not functioning

As a newcomer, I have been assigned a project that involves using Vuejs. In this project, there is a page that utilizes a component called DashboardCard. Each DashboardCard receives 2 props - val and icon from the parent component. https://i.stack.imgur. ...

Generate HTML table with CSS dynamically applied through jQuery

After performing some calculations and applying CSS rules using the .css() function in jQuery to color the background of a table, I am facing an issue when trying to print the page. The printed page appears in black and white without retaining any styling ...

Please enter the date placeholder with today's date

I need help changing the placeholder to display the current date <input type="date" v-model="selectedDate" class="dateclass" placeholder="[function to retrieve current date]"> ...

Protecting Laravel 5 with Vue.js using CSRF tokens

I've been struggling to make this csrf token work with the vue.js example. Despite my efforts, it keeps indicating that I do not have a token. I've experimented with various approaches. Placing it at the bottom (not in the head, before the end ...

When a Mui Button is clicked, it changes the color of all tabs instead of just the active

My website footer contains 5 links represented by Mui Buttons with the component set to {Link} in order to transform them into clickable links. Currently, all the buttons are black and change to green when hovered over. However, I want the button color to ...

Utilize jQuery to dynamically add a CSS class to a button

When using jQuery to create dynamic buttons, is there a way to add a CSS class to the button during creation without needing an extra line of JavaScript to add the class afterwards? Below is a example code snippet: $.each(data, function (index, value) { ...

Ways to insert text inside a border

I'm struggling to figure out how to insert text within a border. Can someone guide me on how to accomplish this? Is there a way to place text in the center of a border? I have included a screenshot for reference. ...