How to center a bigger background - html

I've done plenty of research on this but I must be doing something wrong because it never works for me. What I want to do is center the Vignette.png to overlay my other background, which is a repeating pattern. Unfortunately I can't still post images because of my still very low reputation :-). But basically what I want is the vignette, which is an image with 4k resolution, to center itself on the page, an if you have a bigger screen, you will see the surplus. But what seems to happen is that the image starts rendering at the top left corner and i can't even see the center of the image, because my screen isn't big enough. Again, I wish i could put some images to clarify my problem, but hopefully i've explained it well.
This is my code:
/* CSS */
body {
background-image: url("Vignette.png"), url("Pattern.png");
background-repeat: no-repeat, repeat;
background-position: center, auto;
}
<!-- HTML -->
<!DOCTYPE html>
<html>
<head>
<title>WIP</title>
<meta charset="UFT-8">
<link rel="stylesheet" href="style.css">
</head>
</html>
Thanks for answering!

I don't fully understand yet what exactly you want to achieve. But chances are that you get it trying with background-size: cover or background-size: contain.
Here is an example of centered image that expands until its actual size is reached, and then the margins/background appear, so it never goes bigger than its resolution:
#page {
background-image: url('some-image.jpg');
background-repeat: no-repeat;
background-position: center center;
background-attachment: fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
More info: http://www.w3schools.com/cssref/css3_pr_background-size.asp
I hope it helps

You need to add background-attachment: fixed to your CSS, and provide one generic rule for the position:
body {
background-image: url("http://lorempixel.com/400/200/"), url("http://lorempixel.com/40/40/");
background-repeat: no-repeat, repeat;
background-attachment: fixed;
background-position: center center;
}
<!DOCTYPE html>
<html>
<head>
<title>WIP</title>
<meta charset="UFT-8">
<link rel="stylesheet" href="style.css">
</head>
<body></body>
</html>
In response to your comment:
The background-attachment: fixed property allows the image to maintain it's position on the page regardless of the other elements, rather than inheriting it's position and thus flowing with the page.
The 'generic rule' I referred to simple replaces your rule which was in fact invalid CSS with a valid rule, which specifies the position of the background image to be centred both horizontally and vertically (in that order): background-position: center center.

Use background-size: cover; for this. I included an example, is this what you were trying to reach?
Here is some information about the backround-size attribute.
http://devdocs.io/css/background-size
/* CSS */
body {
background-image: url("http://www.handleidinghtml.nl/html/afbeeldingen/voorbeelden/usa3.gif"), url("Pattern.png");
background-repeat: no-repeat repeat;
background-position: center center;
background-size: cover;
}
<!-- HTML -->
<!DOCTYPE html>
<html>
<head>
<title>WIP</title>
<meta charset="UFT-8">
<link rel="stylesheet" href="style.css">
</head>
</html>

Related

fixing the background image for a game I'm making using HTML

It's me again with another new project for finals.
I'm trying to make a game with HTML and I'm in the middle of making the main menu right now and I'm stuck on it.
The background image wouldn't fit in right and I've searched and tried every solution I can find in the internet but it just wouldn't come out right.
Here's what it looks like. As you can see, it doesn't fit in right. I was trying to make the bg image fit in without having to scroll. I used a 1920x1080 image
body {
background-image: url("main menu bg.png");
background-size: contain;
background-repeat: no-repeat;
background-size: 100% 100%;
background-position: center top;
}
<!DOCTYPE html>
<html>
<head>
<link rel = "stylesheet"
type = "text/css"
href = "stylemenu.css" />
<title> Trio of Battles </title>
</head>
<body>
<button button style="background-color: transparent;" onclick="myFunction()"><img src="start button.png"></button>
</body>
</html>
By changing body height to 100vh, you make sure that there's no scrollbar. And with contain, the image will always fit inside the screen.
body {
background: url("main menu bg.png")no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: contain;
overflow: auto;
margin: 0;
height: 100vh;
}

Image not loading in css

I have a problem in bootstrap/css. I want to load image from source folder and I typed in css this code:
background: url('../img/home.jpg') no-repeat;
But it doesn't show in my home section on the page.
https://codepen.io/write/image-not-loading
Perhaps try
background-size: cover;
Or as ZohirSalak CeNa said, resizing your element that is containing the background image unless it is your body.
HTML
<html>
<head>
<title>Your Title</title>
</head>
<body class="your-class">
</body>
</html
CSS
.your-class {
background-image: url('../img/home.jpg') no-repeat;
background-size: cover;
}
Also, Make sure you have the correct path to your image

Image declared in css is not showing

I want a image to fill the browsers and these are html side code
<!DOCTYPE html>
<html>
<head>
<title>Testing</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
</body>
</html>
since I want the image to be occupied fully in screen i declared it inside the body in css like this but sadly image is not appearing in the screen
body{
background-image:url('../img/bgmapsimage.png');
background-repeat:no-repeat;
background-size:contain;
background-position:center;
}
yes the image name is correct and location is correct as well.This is the link i refered link
Check out this link: https://css-tricks.com/perfect-full-page-background-image/
body{
background: url(images/bg.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
You can also use vh to have full height image no matter the screen size.
To test your CSS, try to change the URL in this line
background-image:url('../img/bgmapsimage.png');
to an absolute URL of which you know it will work, like
background-image:url('http://placehold.it/400x300');
If this image is shown, your original image path is the problem (the folder structure which your HTML, CSS and image files are in). If it's not shown, most likely your CSS code itself is the reason.
Your body element is empty and therefore has no height.
if you add a minimum height, you'll see the image:
body{
background-image:url('../img/bgmapsimage.png');
background-repeat:no-repeat;
background-size:contain;
background-position:center;
min-height: 200px;
}
Update your CSS as like below
body{
background: url('../img/bgmapsimage.png') no-repeat center center fixed;
-webkit-background-size: center;
-moz-background-size: center;
-o-background-size: center;
background-size: center;
}
OR
body{
background: url('../img/bgmapsimage.png') no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
You can also add css for html tag
Example
html{
/*Your CSS*/
}
Here is some CSS tricks for full screen background image CSS Tricks
body{
//backround image and all
background-size: cover;
}
Though it depends on the size of the image you are working with.

Background Image Stretch On Homepage

I'm currently trying to make my background image on my homepage to stretch across the whole page, but I'm having difficulties doing this because when I try and re-size the window it becomes smaller?
this is my code:
<style type="text/css">
body{
background: url(img/phantom410.jpg); repeat-y;
background-size:cover;
background-color:black;
}
</style>
Is there a way I can get an image for my website homepage and stretch it across the whole page like what twitter does with their background image; but not have it shrink once it starts getting smaller? thanks!
body {
background: url(https://css-tricks.com/examples/FullPageBackgroundImage/images/bg.jpg) no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
h1{
color:#FFF;
}
<html>
<head>
<title>Full Background</title>
</head>
<body>
<center><h1>Full Background Image</h1></center>
</body>
</html>

Why doesn't my background image show up in the html tag?

I'm having problems adding a background image to my background website in the html tag.
The problem is here:
html{
background:url('assets/img/jp-logo.png'); /*background image*/
background-image:url('assets/img/jp-logo.png'); /*background image*/
-webkit-background-size: cover;
-moz-background-size: cover;
}
I put in both background and background image because i testing whether either one would work along with the background size: cover but none of them did. I also checked my file directory paths, those are good too. I was wondering why as that's not supposed to happen. i tested it on a seaprate html and css test sheet and it worked, but why not here is suspicious to me. The full code here: http://jsfiddle.net/TheAmazingKnight/kjLg8/
This short sample I made worked:
<!doctype html>
<html lang="en">
<head>
<title>Test Page</title>
<link rel="stylesheet" href="test.css" type="text/css" media="screen" /> <!--CSS for desktop screens-->
</head>
<body>
<p>hello</p>
<img src="jp-logo.png" alt="jp-logo.png" />
</body>
</html>
html{
background-image:url('assets/img/jp-logo.png');
}
html has no semantic value. Use body instead. You may want to give your body a margin:0;.
I changed your background photo and placed into body element like this:
body{
background:url(http://technomarketer.typepad.com/technomarketer//Radiohead_wallpaper.jpg); /*background image*/
-webkit-background-size: cover;
-moz-background-size: cover;
and it worked.
So, maybe your path to the photo (assets/img/jp-logo.png) is wrong.
Did you check it?
html{
background:url("assets/img/jp-logo.png") no-repeat center center fixed;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
}
I found that this code works perfect and to give credit where credit is due:
http://css-tricks.com/perfect-full-page-background-image/
if you want to write css code inside html page be like this
<head>
<title>hello</title>
<style type="text/css">
html{
background: url(photo.jpg);
}
</style>
</head>
and the image url not between quotation mark 'image.jpg'
and it`s better to use body instead of html