Image in src goes to background-image and does not resize

Asked

Viewed 1,204 times

1

I’m putting together a project for a friend of mine and I’m using the scrollfullpage that splits the page into parts. In the first part I’m putting a banner and a written with my friend’s logo.

The problem is that when I put the normal logo, like 'img src="link"' in HTML, at the time of visualization I notice that it leaves the image and goes to the background in background-image. Although I put it right is not going and it gets the image cut.

HTML:

<img id="logo" src="assets/img/logo.png">

CSS:

#logo {
 width: 300;
 height: 180px;
}

But the sizes (width and height) that I can move so much in CSS when HTML is just the size of the area or space the image is in (as if it were a div), I can’t move the size of the image itself. When I decrease the size the image is cropped.

Example with CSS above, image appears cropped:

In red, parts that are missing

Chrome code and scan on top of page Code and Analyze

Has anyone ever had a similar problem ?

Link: https://carioca.creativecode.art.br/

  • There’s something wrong with the code, isn’t there? The background-image is being used in the tag img? and in the src has a gif of 1x1

  • At least I didn’t do anything that made the SRC images turn background image, I had already noticed this, but I don’t know how to fix it because staying in image doesn’t let me configure as I want

  • Your CSS is misspelled, PX is missing after 300 should be width:300px You’re using some kind of compiled?

  • Hugo, despite being wrong there, the error persists, the main problem is **img SRC turns background-image ** this hinders a lot about the treatment of the image, I want to be able to work on it again as an image... I don’t know what is compiled kkk =P

  • much if speaking of absence of 'px', it does not mean anything, because the browser itself chooses the form, it understands as 'auto', this certainly is related to a javascript library or code compressor, in a way it makes sense to delete the tag to optimize, provided that css is also myridified.

3 answers

3

this alone is not possible, unless you are using a javascript code to make this conversion, surely there is on your site a code similar to this:

about date:url: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs

function getBase64Image(img) {
    // Create an empty canvas element
    var canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;

    // Copy the image contents to the canvas
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0);

    // Get the data-URL formatted image
    // Firefox supports PNG and JPEG. You could check img.src to
    // guess the original format, but be aware the using "image/jpg"
    // will re-encode the image.
    var dataURL = canvas.toDataURL("image/png");

    return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}

what happens there is that it takes the original image and saves in your browser a recoded copy (Base64), so do not need to load the image whenever the user access the site, is a good optimization method(see the documentation to better understand, the encoded file can get larger than the original if done in the wrong way), also serves to not take the image of your site and stay using in another, consuming the bandwidth of your server, with reservations, of course, an advanced user could find the corresponding image poking through the console, but it is difficult for most since they cannot use the data:url elsewhere.

Formatting data:[<mediatype>][;base64],<data>

data:image is a local and temporary path, so much so that you can access directly through the browser without using http(s), of course, will give an error page, because the path is not relative.

"Note: Data Urls are treated as opaque origins unique to modern browsers, rather than inheriting the origin of the settings object responsible for navigating."

this explains how it ended up in background-image, just remove this code or "library" that you included on the site and clear the cache, will return to normal then.

in relation to the size of the image, you cannot change the size for a matter of hierarchy, the values have somehow already been declared in css(the right is to remove from there), you can force it to render with the values you want using: <img src="" style="width:300px !important;">

ACCORDING TO MOZILA’S DOCUMENTATION

data Uris:

Uris defined by RFC 2397, allow content creators to embed small files embedded in documents.

Base64 coding schemes are commonly used when there is a need to encode binary data that needs to be stored and transferred by media designed to handle text data. This is to ensure that the data remains intact without modification during transport. Base64 is commonly used in various applications, including email via MIME and complex XML data storage .

In Javascript, there are two functions, respectively, for decoding and encoding Base64 strings :

  • atob()
  • btoa()

The atob() function decodes a sequence of data that was encoded using the base 64 encoding. On the other hand, the btoa() function creates an ASCII sequence encoded in base 64 from a "sequence" of binary data.

0

First look is missing 'px' in CSS own width. But as you explained even putting the image path in src "it automatically goes to BACKGROUND-IMAGE", then you have two possible solutions for this:

1#

img#logo{
  height: auto;
  width: 100%;
  object-fit: cover;
}

2#

img#logo {
   background-size: cover
}

0

Put it in percentage to see if it works. Example:

#logo {
 width: 60%;
 height: 50%;
}

Remember to test the sizes there, it won’t necessarily be 100% or 50%.

  • Nazareth, as I mentioned, there in CSS he’s changing the size of the "div" that the image would occupy, like, if I put it smaller than the image, he cuts the image, if I put it bigger, he enlarges the space but not the image and there’s no div in that image

  • Have you tried putting another image to test? Do a test, try putting another background image to see if the problem continues

  • Yes, I tried, but the issue is not the background image, the image I’m talking about would be the logo that is in the middle of the screen, but it’s not going as img SRC as normal, it automatically goes to BACKGROUND-IMAGE, even though I put IMG SRC and because of that I can’t touch the size of it, nor with width and height inside the img, it acts as if it were the div =(

  • I made the changes here and it worked. I edited the answer

  • The image remains cropped, only bigger, was in the same thing

  • I forgot to take the ! Mportant try now

  • Nazaré, I noticed that when I use the Google Developer Tools analyzer, I put the path inside the SRC according to image code and analysis that I put there on top, right, but by the code I can’t leave the SRC there, it always changes to date:image........

  • Sometimes I have some problems in my css, I change the codes and when I load the page nothing happens, but by Google parser I can make the changes. What I do to get back to work is create another file. css and copy and paste everything into it and start using the newest file, maybe yours is the same problem as mine

  • In the changes I put as response, the photo was like this https://imgur.com/a/aCIQZ

  • It was show Nazare, but now resizes the screen, you will see that the image will cut....

  • In img SRC with percentage of to take care of it, but in the background gets a shit... Why did this happen? Why can’t I edit normally as an image? ='(

  • You know the div #caption? Voce could try to increase the size of it, because I think that when you just enlarge the photo itself, it ends up getting out of the size that is the div #caption

  • You may also need to use media queries. Read about https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/CSS_Media_queries

  • I haven’t done the media query of this page, I’m going through it in online server that I use, because I’m at work, but the div caption doesn’t have much function inside this image and this is happening and the first time, I never had this problem of not being able to manipulate the image and this div would imagine it makes no sense

  • I haven’t done the media query of this page, I’m going through it in online server that I use, because I’m at work, but the div caption doesn’t have much function inside this image and this is happening and the first time, I never had this problem of not being able to manipulate the image and this div would imagine it makes no sense

  • Do you wear bootstrap? If yes, put the class img-Responsive like this: <img class= "img-Responsive "id="logo" src="Assets/img/logo.png"> E tries to use class and does not divide into "logo" to see

  • Use materializecss

  • I imagined. He can do it. But he managed to solve?

Show 13 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.