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

What is the best way to create a layout for Flexbox project boxes with three boxes per row?

My project cards are displaying in a single horizontal line and expanding infinitely to the right, rather than appearing in rows of three as intended. This causes them to extend outside the visible area in a long horizontal strip. See the issue in action ...

Show a triangle positioned on the left side of the display

Looking for help with aligning a div to the left side of the screen CSS #nav { position: fixed; height: 50px; width: 50px; display: block; background-color: #000; } This particular div contains an icon that acts as a link HTML <div id="nav"> ...

What is causing this problem with my terminal when I attempt to install Sass?

I am having trouble with the installation of Sass using Terminal. When I try to run npm install -g sass, I am encountering an error in the log file. The response I get is as follows: npm WARN checkPermissions Missing write access to /Users/dagilo/desktop/n ...

If an element with a "hidden" display property is loaded in the browser window, will it be visible?

Is an element in a hidden display still using memory when the page is loaded? It's convenient to have many elements on a page, but if 99 elements are hidden and only 1 is displayed, does that impact the loading of the page? I'm curious if the pa ...

Getting the css property scaleX with JQuery is as simple as executing the

I am struggling to print out the properties of a specific div element using jQuery. My goal is to have the different css properties displayed in an information panel on the screen. Currently, I am facing difficulties trying to show scaleX. Here is my curr ...

CSS: Card elevates when keyboard is activated on a mobile device

[view image 1[view image 2] Image 1: Normal View, Image 2: When the keyboard is enabled, the card jumps up and when I close it's normal. Is this behavior normal? Or is there a fault in my CSS? I utilized styled-components for writing the CSS. CSS ...

What is causing concatenated string class names to fail in Tailwind CSS?

Whenever I find myself needing to style my React elements, I end up using the style attribute instead of className. None of the examples I've come across seem to apply styles that fit my specific needs. Can you shed some light on why this happens and ...

Which tools are best suited for Xamarin development?

Recently, I've been contemplating how to style my app. One common approach is using CSS, but the question that lingers in my mind is whether it's truly compatible and if I should begin utilizing CSS. Is there perhaps a different language that wou ...

Fixing the bug in the circle text animation

I followed the instructions in this tutorial and implemented the following code: Splitting(); * { margin: 0; padding: 0; box-sizing: border-box; font-family: monospace; } body { display: flex; justify-content: center; align-items: center; ...

Ways to align elements horizontally while preventing them from shifting positions

I'm faced with a layout challenge where I need to place 2 components side by side, but I want one component to stay on the right without affecting the orientation of the other component. Example: https://i.stack.imgur.com/T5Ram.png Currently, when I ...

Creating a Functional Right Arrow on a Pure CSS Select Menu/Dropdown

I have implemented a custom select/dropdown menu by following the solution provided here: The functionality works well, but I am facing an issue where the dropdown options only appear when clicking on the box. Clicking on the 'arrow' located on ...

Creating a personalized gradient using Tailwind CSS and Daisy UI framework

I’m attempting to incorporate a unique gradient into my next.js application with the help of Tailwind CSS and Daisy UI. Below is the specific code snippet I plan on using: background: linear-gradient(180deg, rgba(192, 192, 192, 0.04) 0%, rgba(201, 180, ...

Elevate Your Flipclock.js

I am currently working on a website that utilizes flipclock.js, but I am facing some challenges in enlarging it to occupy more space on the page. Is there anyone who knows how to upscale or increase its size? ...

Avoid displaying the image when encountering a 404 error, but sometimes a broken image may still appear momentarily

Here is the code snippet I'm currently using: <img ng-show="json.user.picture" ng-src="{{json.user.picture}}" ng-error="json.user.picture = false"> When accessing an image from an external website without permission, a 404 error code is return ...

Creating a repeated design using linear gradients in CSS

As I venture into the realm of Photoshop patterns for the first time, I am working on a webpage where I plan to incorporate these patterns to enhance the background. The pattern I have discovered measures 120x120 px. If I were to stop here, I would simpl ...

Adding the <a> tag causes Superfish to malfunction

I've been struggling to get the latest Superfish menu code working with lists that include the <a> tag. I've double-checked everything, but it seems like I'm missing something obvious. Can anyone help me figure out what's wrong? ...

Variety of formatting options for menu items when the menu is in its collapsed state

I am working with a Bootstrap nav-bar that collapses into a button by default. Within my nav-bar, I have opted for images instead of text links. However, when the nav-bar is collapsed, I would like these images to be smaller in size. Is there a way to cu ...

Connecting CSS and JS files in JSP was a bit of a challenge

Just starting out with jsp and using Glassfish server via netbeans. Struggling to link my jsp file with css and javascripts. Below is the structure of my files: Web Pages --Web-Inf --assets --css --style.css --js --jquery.js ...

Creating a Authentic Screw Top Appearance with CSS

I am striving to create a realistic screw head. Here is what I have done so far: <div class="screw"><div class="indent"></div></div> .screw { position: absolute; top: 10px; left: 49%; width: 30px; height: 30px ...

What is the correct way to customize colors for specific components in Material-ui?

Struggling with theming in Material-UI, particularly when it comes to customizing element colors. Some elements default to 'theme.palette.main.dark' and I want to override this behavior. For example, the TextField and SpeedDial components automa ...