how to insert corretly my picture in css? - css

i have a problem to insert my picture, i have a 404 error, though i thought that my path was correct.
my files are :
main-menu
phone.png
_menu.scss
in my file _menu.scss, i have the next code :
.phone {
background-image: url(phone.png);
}
.phone is the name of my class. i work with drupal and twig.
thank you for you help and sorry for my english.

When added as background in div, 'height' and 'width' must be added for the background image to appear.
it is don't showing:
.phone {
background-image: url(https://picsum.photos/300/300);
}
<div class="phone"></div>
but it is showing:
.phone {
background-image: url(https://picsum.photos/300/300);
height:300px;
width:300px;
}
<div class="phone"></div>

If your file tree is right, then all you need to do is add a width and a length. You are not telling the browser how tall and wide to make the phone, so it will show nothing.
.phone {
background-image: url(phone.png);
width: 500px;
height: 600px;
}
<div class="phone"></div>
I don't have your phone.png, but it should be like so.

Solution
There are two ways of importing an image.
1. Using HTML tag.
2. Using CSS background.
When using css version, don't forget background-size and background-position.
Example:
.image {
background: url('path/to/img.png') no-repeat;
background-size: contain; /*This scales the image accoding to the div*/
background-position: center center; /*positions the image in the center*/
width: auto;
height: 50px;
}
<img src="path/to/img.png" />
<div class="image"></div>

sorry, but i have always the same problem:
my TWIG code:
<div class="col-6">
<div class="phone">
</div>
and my SASS code:
.phone {
background: url('phone.png') no-repeat;
background-size: contain; /*This scales the image accoding to the div*/
background-position: center; /*positions the image in the center*/
width: auto;
height: 50px;
}
and my files are always the same too:
main-menu
phone.png
menu.scss
i have always the 404 error
thank you

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

Related

Fully display background image

I'm setting up a site, and want to create a responsive background image but I'm getting only single line with background image. What property should i need to use to make the entire background image to fit?
I created 2 files,
index.html :
<div id="logo">Test</div>
style.css :
#logo {
background-image: url("bg.png");
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
}
I tried many times but only single line gets background image. I want the background image to be fully displayed on screen without using height property which i think makes site less responsive.
you should change the height of logo div and set it as full height by adding height:100% for the body and logo div, your code should look like below code
html, body
{
height: 100%;
}
#logo {
background-image: url("bg.png");
background-position: center center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size: cover;
height:100%;
}
If you want the image to have the same height as the screen you can use height:100vh. But if you want only the image full size you can do this like this :
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#image{
max-height: 100%;
max-width: 100%;
}
#text{
position: absolute;
color: white;
top: 0
}
<img id="image" src="https://images.unsplash.com/photo-1528920304568-7aa06b3dda8b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80">
<div id="text">test</div>

How to repeat an image on the right and left with CSS?

I'm trying to apply CSS transformations to an image to achieve the following result:
right half of the image - main image - left half of the image
so that after CSS is applied to the image it looks like a "ribbon" with the image repeating after and before but not fully.
Is there a way to do this without changing the structure of the page (that is without using a div and background property)?
Example:
image to apply CSS to
result I want to achieve
I tried using background and it kinda works
background-image: url(/image.png);
background-repeat-x: repeat;
background-size: 50%;
background-position-x: 50%;
background-repeat-y: no-repeat;
height: 50px;
but I would like to get the same effect without using background rules but rules that apply to the image directly.
I tried also to apply the background rules to the img directly and got this result which could be a good middle-ground solution except for the missing image icon overlapping. The removal of this icon could also do the trick.
result by using background on img
Unfortunately, I cannot change the structure of the page (e.g. by wrapping the image into a div)
If you want to show certain parts of an image it's good practice to go with the background-position property in combination with background-size.
Other method is to cut the images to fit your needs and then display them seperatly.
I suggest you to go with my example:
.image {
height: 400px;
}
.image>div {
vertical-align: middle;
display: inline-block;
height: 100%;
}
.left,
.right {
width: 150px;
}
.left {
background: url("https://news.nationalgeographic.com/content/dam/news/2018/05/17/you-can-train-your-cat/02-cat-training-NationalGeographic_1484324.jpg") no-repeat right;
background-size: cover;
}
.middle {
background: url("https://news.nationalgeographic.com/content/dam/news/2018/05/17/you-can-train-your-cat/02-cat-training-NationalGeographic_1484324.jpg") no-repeat center;
background-size: cover;
width: 300px;
}
.right {
background: url("https://news.nationalgeographic.com/content/dam/news/2018/05/17/you-can-train-your-cat/02-cat-training-NationalGeographic_1484324.jpg") no-repeat left;
background-size: cover;
}
<div class="image">
<div class="left">
</div>
<div class="middle">
</div>
<div class="right">
</div>
</div>
I hope you like cats. I do.

How to center Data URI in background image?

It seems that background-position/background-cover is not working with data URI as background images? What I have is a preview of images to be uploaded. To show those previews I am using JS to get the data URI into CSS background images, which I hope to center.
But I notice the following code does not work with a data URI
div {
width: 200px;
height: 200px;
display: block;
background-size: cover;
background-repeat: no-repeat;
background-image: url(data:image/jpeg;base64,/9j/4AAQSkZJRg...);
}
https://jsfiddle.net/7pfc2vLx/
UPDATE
I notice like many mentioned I am missing background-position in my above example. But seems like even with that it does not work when its an inline style?
<div id="profile-avatar" style="width: 200px; height: 200px; background-position: center center; background-size: cover; background-repeat: no-repeat; background-image: url(data:image/png;base64,iVBORw0KGgoAAAA
https://jsfiddle.net/7pfc2vLx/4/
If you want to Center an image then try JSfiddle
div {
width: 100%;
height: 200px;
display: block;
background-size: contain;
background-repeat: no-repeat;
background-image: url(data:image/jpeg;base64,/9j/4AAQSkZJRgABAgEASABIAAD/4QBsRXhpZgAASUkqAAgAAAAD…y5eKCQaVqJ0y7W7JJJ2b6cxwd3MlGwz9mN4vrwL6sTjY+BzMiXWzjSpa2fJRFOu/auHjYv/9k=);
background-position: center center;
}
Maybe I'm missing something in your question, but shouldn't you just add the position?
background-position: center;
Your background already fit the div and if you want to make sure it's in the centre of the div, add:
background-position:center center;
But seem nothing change, because your div is not center itself, so add:
margin:0 auto;
Now they all in center posistion. https://jsfiddle.net/7pfc2vLx/2/

Image not resizing properly: image gets cut off when window is resized

I at least got the following to render the image, but when the window is resized past a certain point: part of the image gets cut off.
#header {
background-image: image-url('my_header.png'); #image-url is a helper in rails
background-repeat: no-repeat;
height: 100px;
background-size: 100%;
border-radius: 1em;
}
And then showing how I specify the image at the top of the body in application.html.erb:
<body>
<div id="header"></div>
</body>
What I want to happen is for the image to scale proportionality but not get cut off. I do not want any specific height set. I want it to automatically scale as needed (however, I wasn't able to get the image to render unless I specified the height with px).
#Pangloss deserves recognition for providing a fantastic answer at this jsfiddle which he referenced in the comments.
Here is his css:
#header {
background-image: url('http://i.imgur.com/zOZVQaf.jpg');
background-repeat: no-repeat;
background-size: contain;
border-radius: 1em;
border: 1px solid blue;
}
#header img {
display: block;
visibility: hidden; /*hidden but reserve the space*/
width: 100%;
height: auto;
}
And the html:
<div id="header">
<img src="http://i.imgur.com/zOZVQaf.jpg">
</div>
#Pangloss provided this answer in the comments. If/when he posts an answer to this question, I will switch it over to his answer.

css dynamic background image positioning

I have some dynamic images as a background for some divs. Those have variable widths and heights. In front of each such a div i have a radio button. There is a way to align through css each div to be exactly at the middle referring to the radiobutton in front of it? I cannot use fixed background height since there are dynamic and the height are different. I was trying to put background-size: 100% 100%; or background-size: auto;
Doesn't work
Any help is more than welcome, thanks!!!
Try:
background-position: center center;
=====
UPDATE:
It's a lot easier working with your fiddle!
Try this:
div.VISA_logo {
background: url("http://www.globalworkandtravel.com/sites/all/modules/customshare/includes/facebook.png") no-repeat;
}
div.VISA_CARTE_BLEUE_logo{
background: url("http://www.discoverireland.ie/App_Themes/DiscoverIreland/images/discoveries/button-add-trip-small.png") no-repeat;
}
div.MasterCard_logo{
background: url("https://s3.amazonaws.com/sugarstats/s3_avatars/7287/1234222094_norm.jpg") no-repeat;
}
div.AMEX_logo{
background: url("http://www.uappointment.com/images/google-small-icon.png") no-repeat;
}
div.PAYPAL_logo{
background: url("http://i.imgur.com/EqgOS.png") no-repeat;
}
div.VISA_logo,
div.MasterCard_logo,
div.PAYPAL_logo,
div.VISA_CARTE_BLEUE_logo,
div.MasterCard_DE_FR_UK_logo,
div.AMEX_logo {
width: 105px;
margin-left: 5px;
height: 42px;
float: right;
margin-right: 30px;
background-position:0 center;
}

Resources