What is the proper way to add my image to CSS?

I'm encountering an issue while trying to insert my picture - I keep receiving a 404 error even though I believe my path is correct. Here are my files:

  • main-menu
    • phone.png
    • _menu.scss

Within my _menu.scss file, the following code is present:

 .phone {
  background-image: url(phone.png);
}

The class name is .phone and I am working with Drupal and Twig.

Thank you for your assistance, and apologies for any language barriers in my message.

Answer №1

For a background image to appear in a div, both the 'height' and 'width' properties must be specified.

When not specified:

 .phone {
  background-image: url(https://picsum.photos/300/300);
}
<div class="phone"></div>

When specified:

 .phone {
  background-image: url(https://picsum.photos/300/300);
   height:300px;
   width:300px;
}
<div class="phone"></div>

Answer №2

If your file structure is correct, all you have to do is specify the width and height. Without specifying the dimensions, the browser won't know how big to make the phone image, resulting in it not being displayed.

 .phone {
  background-image: url(phone.png);
  width: 500px;
  height: 600px;
}
<div class="phone"></div>

I don't have access to your phone.png file, but it should look something like this.

Answer №3

Solution

There are a couple of ways to bring in an image.
1. Through the use of an HTML tag.
2. By utilizing CSS background.

When opting for the css approach, remember to include background-size and background-position properties.

Example:

.image {
  background: url('path/to/img.png') no-repeat;
  background-size: contain; /*This will adjust the image size based on the div*/
  background-position: center center; /*centers the image*/
  width: auto;
  height: 50px;
}
<img src="path/to/img.png" />

<div class="image"></div>

Answer №4

Regrettably, I keep encountering the same issue: this is my TWIG code:

<div class="col-6">
  <div class="phone">
  </div>

and here is my SASS code:

.phone {
  background: url('phone.png') no-repeat;
  background-size: contain; /*This scales the image according to the div*/
  background-position: center; /*positions the image at the center*/
  width: auto;
  height: 50px;
}

Furthermore, my files remain consistently unchanged:

  • main-menu
    • phone.png
    • menu.scss

I am continuously presented with a 404 error. Thank you for your assistance.

Answer №5

Why not give this a shot: ./mobile.png.

.cellphone {
  background: url('./mobile.png') no-repeat;
  background-size: contain; /*Adjusts the image size to fit the div*/
  background-position: center; /*Centers the image*/
  width: auto;
  height: 50px;
}

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

The sidebar in Semantic UI does not have a specified height for the form segment

Need help troubleshooting a button that slides out a vertical sidebar. I want to include a short form for users to input search queries within the sidebar. The issue seems to be with the heights of #search-sidebar and .ui.form.segment. Here is the code s ...

Do you know of any online platform that can help me convert W3-theme colors into RGB values?

As an instance, I am looking to convert the color w3-theme-d1 (a shade of grey) into its RGB code. ...

Python: Issue capturing screenshot of element with Selenium

I recently delved into the documentation and attempted to capture a screenshot of an element using the following code: wd = webdriver.Chrome(chrome_path, chrome_options=options) url = "https://results.vtu.ac.in/AS_CBCS/index.php" wd.get(url) img ...

Transform or warp image to align with Mercator projection

I recently acquired an image that is essentially a screenshot from Google Maps. You can view it here: Now, I also have another image that I want to superimpose onto my map. The catch is that the map is in Mercator projection, where the scale varies from ...

Arrange divs in a vertical stack while maintaining a layout with two columns

I am trying to create a layout with two stacked `div`s on one side, and then have a single column on the other side that is the same height as the left `div`s. Something similar to this: https://i.stack.imgur.com/ODsEd.png I currently have the two `div` ...

Tracking the progress of reading position using Jquery within an article

Here is an example of a reading progress indicator where the width increases as you scroll down the page: http://jsfiddle.net/SnJXQ/61/ We want the progress bar width to start increasing when the article content div reaches the end of the article c ...

Starting the download of the canvas featuring a stylish border design

I'm facing an issue where the canvas border is not showing up when I download the canvas with a border. The border appears on the screen, but it doesn't get included in the downloaded canvas. let canvas=qrRef.current.querySelector("canvas&qu ...

Having trouble with the Jquery Slider script?

I recently integrated a slider from into my website, following the installation instructions. However, I seem to be facing some conflicts with another script that controls animated div movements on the same page. Upon loading the page, all elements of th ...

Angular animation not firing on exit

I am encountering an issue with my tooltip component's animations. The ":enter" animation is working as expected, but the ":leave" animation never seems to trigger. For reference, here is a link to stackblitz: https://stackblitz.com/edit/building-too ...

Troubleshooting a problem with CSS Matrix animations

Lately, I've been working on creating some matrix animations. However, I noticed an unusual issue. The code below seems to function differently across Firefox, Safari, and Chrome: @-moz-keyframes matrix { from { -moz-transform: matri ...

When scrolling back to the top of the page, the data-spy feature does not re-highlight the "Home" anchor

After experimenting with Data-spy to change the active anchor while scrolling, I encountered an issue. Upon scrolling back up to the top of the page from the about section, the "Home" anchor failed to re-activate. How can this be fixed? I attempted to rem ...

Is it possible to modify a scss style in Prestashop?

I have encountered an issue while using a default theme in Prestashop 1.6 that I need assistance with. My goal is to move the navbar up by 25px. I understand that I should make changes to #block_top_menu { padding-top: 25px; } in blocktopmenu.scss, which ...

In order to enhance user experience, I would like the tabs of the dropdown in the below example to be activated by

function openCity(evt, cityName) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } ...

The :focus pseudo-class is failing to target the input element

I'm dealing with a simple input field of type text: <input matInput type="text" placeholder="{{placeholder}}" class="input-field" [ngClass]="{'error': hasErrors}" [readonly]="isReadonly&quo ...

What is the best way to split an input field into distinct fields for display on the screen?

Take a look at this image: https://i.stack.imgur.com/LoVqe.png I am interested in creating a design similar to the one shown in the image, where a 4 digit one-time password (OTP) is entered by the user. Currently, I have achieved this by using 4 separate ...

What is the process for adjusting the color of a particular date in the <input type=Date> field?

Is there a way to modify the styling, such as color, of the input type date in HTML 5? In HTML 5, we can display a calendar using <input type=date>. For example, if March 23rd is a holiday and I want to highlight this specific date in red, how can I ...

The issue with jspdf is that it is failing to generate PDF documents of

I'm currently developing a resume builder app using ReactJS. One of the functionalities I'm working on is enabling users to download their resumes as PDFs. However, I've encountered an issue with the generated PDFs when using jsPDF. The down ...

Update the text within a textarea by clicking with the power of jquery

Is there a way to clear the content of my textarea on click? Below is the code snippet: http://jsfiddle.net/QMfyP/ <textarea name="adventage" style="width:400px; border-radius: 3px; border-left: 4px solid #6C3" id="adventage" cols="45" rows="5">Sh ...

Explore Site Configuration

When it comes to creating a responsive site with the given layouts: If you have a fixed header (4rem), how can you set up the main container (C & D) to have a maximum height of 100% and scroll independently based on the overall height of the contents in C ...

designing a responsive form in the mobile environment

I have implemented a search form on my website with a button inside the text box. It works fine on desktop, but when I switch to mobile view, the positioning is off. I am currently using absolute positioning for the button to give it a fixed position. Is t ...