This is what is happening with the code.
Here is the code...
<div style="background-image: url(img/background.jpg);background-repeat: no-repeat; height:250px;"></div>
The size of the image is 963w x 200h.
At the end of the day, i would want this to be 100% width, but also be responsive for all devices. How can I make that happen?
adding background-size:cover; wont do that trick.
Here is a fiddle: https://jsfiddle.net/t7ekj0mh/
Add the rule background-size: contain to your div.
<html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/3/w3.css">
<body>
<div style="background-image: url(https://i.ibb.co/2jCzQB6/background2.jpg);background-repeat: no-repeat; background-size: contain; height:250px;"></div>
</body>
</html>
Add background size contain for the image to scale horizontally
As I understand, this is what you are attempting to achieve.
div{
background: #E6DCDD url(https://i.ibb.co/2jCzQB6/background2.jpg) center bottom / contain no-repeat;
height: 300px;
border: 5px dashed red;
}
<div></div>
This will stretch the image horizontally and place it at the bottom, and by adding a color, it will seem as it is a one-piece thing.
Related
I have a div that has a certain fixed width and height. I want to fit inside an image (it's dimensions are previously unknown) without stretching it (keep aspect ratio). Object fit would work but for the sake of image being wrapped inside of another wrapper div (to apply shadow), so it cannot be used this time.
To make it clear: I can't use object-fit, nor making it a background image.
How is this done properly with CSS?
further images to demonstrate my intentions: (blue rectangle on the right represents the original image in its original size, on the left it is how it should display inside it's dashed parent)
If i understood correctly this should do it
object-fit: contain;
.section{
height:90vh;
width:100%;
border: 2px solid black;
}
.img{
height:100%;
width:100%;
background-image: url('https://images.unsplash.com/photo-1481349518771-20055b2a7b24?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1239&q=80');
background-position: center;
background-repeat: no-repeat;
background-size: contain;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="main">
<div class="section">
<div class="img"></div>
</div>
</div>
</body>
</html>
you can resize the browser and the aspect ration wont change, I hope this is what you are looking for
In my css file, I have an id named "homehero", which displays a background image.
#homehero
{
background-image: url("images/coast.jpg");
background-size: 100% 100%;
background-repeat: no-repeat;
}
In my html file, I have a div that uses this id to display the image; however, the image does not appear whatsoever.
<div id="homehero"> <!-- Home Page Image -->
<!-- <img src="images/coast.jpg" width="100%" height="100%" alt="A Sunny Coastline"> -->
<!-- Old line of html that displayed the same image, but converted to css id. -->
</div> <!-- End of Home Page Image -->
The full html file can be found here.
The full css file can be found here.
Edit:
The image is displayed when setting it as the background image for another element, it is only in this id where the issue occurs.
Please Put width and height
width:100%;
height:100vh;
#homehero
{
background-image: url("images/coast.jpg");
background-size: 100% 100%;
background-repeat: no-repeat;
width:100%;
height:100vh;
}
I would suggest below changes,
#homehero
{
background-image: url("images/coast.jpg");
width:300px;
height:300px;
background-size: cover;
background-repeat: no-repeat;
}
Where, your image will be cover and if you specifies width and height it would be good and also your image will not be repeat as no-repeat property is used.
These are a few things I recommend you try:
Try setting the background size. (Instead of %, try setting it to px.)
Most of the time, the background picture is applied, but because our div has
no dimension, we are unable to view it.
Double-check that your picture file is located in the images folder.
Check your image's extension and make sure you're using the correct one in your code.
Use the dev tools to inspect the element and see whether the background property is being overridden by another CSS rule.
If none of the above methods work, try pasting the picture's real URL by copying the image address from the internet rather than providing a folder path.
This method works 90% of the time.
i recommend to use inline scope css, i hope it works for you
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="#homehero {.css">
<style>
#homehero {
background-image: url("image/image.png");
background-size: 100% 100%;
background-repeat: no-repeat;
}
</style>
</head>
<body>
<div>
<div id="homehero">
</div>
</div>
</body>
</html>
this might be the reason why it can happen
https://www.geeksforgeeks.org/types-of-css-cascading-style-sheet/#:~:text=As%20Inline%20has%20the%20highest,sheets%20have%20the%20least%20priority.
It looks like you're entering the url in the background image, for background size using background-size: cover;
If your url address is correct, then try adding
width:100%;
height:100vh;
I am writing a website and I have gotten everything in place with the homepage the only problem is when I resize the browser window the background image moves and parts of it disappear as the window is resized. Id like it to stay fixed in the window just have the image shrink as the window is shrunk.
I'm sure what I am doing wrong is pretty simple but I'm pretty new to coding and can't for the life of me figure out what I need to do. Thanks to you all in advance.
<meta name="viewport" content="width=device-width, initial-scale=1">
body{
background: url(Home-background.jpg);
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
background-attachment: fixed;
}
when you want cover vertical and horizontal can use this code (pay attention may be deformed this background image)
body {
background: url(Home-background.jpg);
background-position: center center;
background-repeat: no-repeat;
background-size: 100% 100%;
background-attachment: fixed;
}
You can use bootstrap to do this. In between the head tags of your html file add this:
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous">
It should work. Hope it helps :)
should the image be full width? if yes, make sure you specify the width to be 100%
width: 100%;
should do the trick
you need to use width: 100%; height: 100%; of your body tag.
it should work.
In the HTML file:
<img id="background-image" src="Home-background.jpg">
In the CSS file:
body {
margin:0;
}
img#background-image {
position:absolute;
width:100%;
height:100%;
z-index:-1;
}
As a result, the background image will be fully stretched, vertically and horizontally.
I have these two PNG images (Image 1: eyemasktrans , Image 2:dialogue_ughhh) which I already edited in Photoshop to make them transparent. But the problem is when I try to view on Chrome, the supposed transparent images appeared to have white backgrounds. Is they any way to fix this using just HTML? Thanks in advance! Below is my current code:
Other image I use: girl_sleeping
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.parallax {
/* The image used */
background-image: url("girl_sleeping.jpg");
/* Set a specific height */
min-height: 200px;
/* Create the parallax scrolling effect */
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
</style>
</head>
<body>
<center> <img src="eyemasktrans.png" /> </center>
<center> <img src="dialogue_ughhh.png" /> </center>
<div class="parallax"></div>
</body>
</html>
Your eyemasktrans.png and your dialogue_ughhh.png images are fine. They have a proper alpha channel in the PNG.
I think the issue you're having is that the .parallax div is not sized corectly, and also has some weird background configurations with fixed and center, which cause it to appear like something is covering it while you scroll around the page.
I don't know what the desired effect is here, but if you size things correctly and set z-index as appropriate, you won't have a problem.
Additionally, I would suggest considering SVG for this task, since all your art is vector anyway. You'll have a much faster load time.
(Issue is reproduced here: http://jsfiddle.net/vr1qms9h/1/)
You can use the opacity attribute.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.parallax {
/* The image used */
background-image: url("http://thepotatoplace.ga/images/background.png");
/* Set a specific height */
min-height: 200px;
/* Create the parallax scrolling effect */
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
opacity: 0.65;
}
</style>
</head>
<body>
<center> <img src="eyemasktrans.png" /> </center>
<center> <img src="dialogue_ughhh.png" /> </center>
<div class="parallax"></div>
</body>
</html>
I have this CSS style here, but it spans across the whole screen rather than just repeating it within the 960px width of the body.
body
{
width:960px;
margin: 10px auto 10px auto;
background-image:url(logo.png),url(backgroundimage.jpg);
background-position: top center, top center;
background-repeat: no-repeat, repeat-x;
background-attachment: static;
}
logo.png is just an image of a company logo, while backgroundimage.jpg is something I want to span only within the 960px rather than across the whole page. How can I do this?
You will need to make a container class. This class will hold all inner div classes and content.
The code for this will be as follows:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style>
.container{
width: 960px;
background: url("logo.jpg");
height: 700px;
}
</style>
<body>
<div class="container">
</div>
</body>
</html>
An easier way to do this would be to use bootstrap. The Bootstrap framework provides a .container class premade with custom responsiveness built in.