CSS make background image overlap container - html

I was wondering how to make an image overlap the container width it is placed in.
So, for example:
<div class="container">
<div class="background-image"></div>
</div>
And the CSS
.container {
max-width: 1170px;
}
.background-image {
background: url(/img/my-image.jpg) no-repeat center center;
}
So on a 1663px wide screen, the .container class is 1170px wide. The background image is 1170px wide as well.
What I'm trying to achieve, is make the image full-width (overlap the .container class so it's 1663px wide) without adjusting the HTML.
Thank you in advance.

For a fixed width of 1663px...
.container {
max-width: 1170px;
background: salmon;
padding: 50px 0;
margin: 0 auto;
}
.background-image {
background: url(https://www.fillmurray.com/800/300) no-repeat center center;
background-size: cover;
height: 300px;
/* The important part... */
width: 1663px;
max-width: 100vw;
position: relative;
left: 50%;
transform: translateX(-50%);
}
<div class="container">
<div class="background-image"></div>
</div>
(remove the max-width: 100vw; if you don't want the width to adjust with the window)
Or if you would rather full width...
.container {
max-width: 1170px;
background: salmon;
padding: 50px 0;
margin: 0 auto;
}
.background-image {
background: url(https://www.fillmurray.com/800/300) no-repeat center center;
background-size: cover;
height: 300px;
/* The important part... */
width: 100vw;
position: relative;
left: 50%;
transform: translateX(-50vw);
}
<div class="container">
<div class="background-image"></div>
</div>

If I understand your question correctly, You need to background image with full width.
.container {
max-width: 500px;
min-height: 500px;
background: blue;
margin: 0 auto;
color: red;
}
.background-image {
background: url(https://www.w3schools.com/cssref/paper.gif) repeat center center;
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
opacity: 0.6;
z-index: -1;
}
<div class="container">
<div class="background-image"></div>
container
</div>

If I understand your question correctly, you are looking for the property background-size: cover which will create the desired effect, i have added a code snippet so you can see.
.container {
max-width: 1170px;
}
.background-image {
background: url(/img/my-image.jpg) no-repeat center center;
background-size: cover;
/* This will create the effect you are looking for */
background-color: #000000;
/* This can be removed, for illustration purposes only*/
height: 500px;
/* This can be removed, for illustration purposes only*/
}
<div class="container">
<div class="background-image"></div>
</div>

Related

How to position element's background-image relative to document / body with overflow: hidden set by parent?

Problem
I want a background-image that is a div.child of a div.parent to be relatively positioned to the body, with overflow: hidden functioning for div.parent. Also, I want that top: 0 of div.child applies to body not div.parent so that it doesn't matter where div.parent is positioned on the Y-axis (thus, top: of div.child doesn't need to be adjusted).
Demonstration
body {
position: relative;
margin: auto;
height: 1200px;
width: 100%;
background-image: url("https://images.unsplash.com/photo-1531251445707-1f000e1e87d0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=562&q=80");
background-size: 100% auto;
background-position: top center;
}
.parent {
width: 80%;
height: 300px;
margin: 0 auto;
margin-top: 200px;
outline: 2px solid white; /* !!! Not visible anymore !!! */
overflow: hidden; /* !!! Ignored by .child !!! */
}
.child {
position: absolute;
z-index: -1;
width: 100%;
height: 600px;
top: -200px;
left: 0;
background-image: url("https://images.unsplash.com/photo-1531251445707-1f000e1e87d0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=562&q=80");
background-size: 100% auto;
background-position: top center;
filter: invert(1);
}
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
There are two background-images that match perfectly (the same, one is inverted). Nonetheless, overflow: hidden applied to div.parent doesn't work anymore.
The div.parent box is indicated by a white outline.
Solution needed
I need a .parent box that holds the same background-image as the body and no matter where this .parent is positioned always shows the excerpt of the underlaying background-image applied to the body. Important: Having .parent with a background: transparent doesn't work. This is because, I will later use the same background-image as applied to body, but one that is edited (like an inverted in the example fiddle).
If you know the different values applied to the parent element you can easily calculate the background properties and keep the child relative to its parent:
body {
position: relative;
height: 1200px;
margin:0;
background-image: url("https://images.unsplash.com/photo-1531251445707-1f000e1e87d0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=562&q=80");
background-size: 100% auto;
background-position: top;
overflow:auto;
}
.parent {
width: 80%;
height: 300px;
margin: 0 auto;
margin-top: 200px;
outline: 2px solid white;
position:relative;
overflow:hidden;
box-sizing:border-box;
}
.child {
position: absolute;
z-index: -1;
/* same as border */
top:-2px;
left: -2px;
right:-2px;
bottom:-2px;
/**/
background-image: url("https://images.unsplash.com/photo-1531251445707-1f000e1e87d0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=562&q=80");
background-size: calc(100%/0.8) auto; /*0.8 = 80% of the width */
background-position: top -200px center; /* Equal to margin*/
filter: invert(1);
}
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
You need to add position:relative to the parent div. Check the below fiddle. Hope it helps.
body {
position: relative;
margin: auto;
height: 1200px;
width: 100%;
background-image: url("https://images.unsplash.com/photo-1531251445707-1f000e1e87d0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=562&q=80");
background-size: 100% auto;
background-position: top center;
}
.parent {
width: 80%;
height: 300px;
margin: 0 auto;
margin-top: 200px;
outline: 2px solid white; /* !!! Not visible anymore !!! */
overflow: hidden; /* !!! Ignored by .child !!! */
/*Add Relative to parent*/
position: relative;
}
.child {
position: absolute;
z-index: -1;
width: 100%;
height: 600px;
top: -200px;
left: 0;
background-image: url("https://images.unsplash.com/photo-1531251445707-1f000e1e87d0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=562&q=80");
background-size: 100% auto;
background-position: top center;
filter: invert(1);
}
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>

40/60 split screen with container

I am wondering what is the best way to achieve two background divs (blue and gray) with a container over the top of them (red):
http://s22.postimg.org/44kcq1cqp/screenshot_413.png
I would create two divs for the background colours and 100vh but how would I overlay a container on top so I can make the login area? I'm trying to achieve something like this design:
http://s22.postimg.org/584h1zxdt/screenshot_414.png
Thanks in advance!
here is how I would do that :)
https://fiddle.jshell.net/okjn0oca/
* {
margin: 0;
padding: 0;
}
.content{
width: 100vw;
height: 100vh;
}
.left{
position: absolute;
top: 0;
left: 0;
width: 40vw;
height: 100vh;
background-color: blue;
}
.right {
position: absolute;
right: 0;
top: 0;
height: 100vh;
width: 60vw;
background-color: green
}
.menu {
width: calc(100% - 30px);
height: 50px;
background-color: red;
margin: 15px auto;
}
.logo {
width: 60px;
height: 30px;
background-color: red;
margin: 15px;
}
.text{
position: absolute;
width: 30vw;
height: 100px;
left: 5vw;
top: 50%;
transform: translateY(-50%);
background-color: yellow;
}
<div class="content">
<div class="left">
<div class="logo">
</div>
<div class="text">
</div>
</div>
<div class="right">
<div class="menu">
</div>
</div>
</div>
You could use a 1px background image on the body:
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAFeCAYAAABEunUfAAAAG0lEQVR42mMUYPj/n3GUGCVGiVFilBglhgMBAK5H0KGRPNKKAAAAAElFTkSuQmCC) no-repeat;
background-size: 40% 100%;
Here's a Codepen with the example
With the background size you force the pixel to cover 40% of the width and 100% of the height.
The 1px background is generated with png-pixel.com.
Not sure if you have actually tried anything, but here's something you can try.
Assuming you only want to have split colors for the background, you don't have to use two divs for that. You can do that with gradient (don't forget vendor prefixes). Then it's just a matter of positioning the "overlay" div as you want.
.bg {
width: 100%;
height: 100vh;
position: relative;
background: linear-gradient(to right, rgba(0,56,199,1) 0%, rgba(0,56,199,1) 40%, rgba(74,74,74,1) 40%, rgba(74,74,74,1) 100%);
}
.overlay {
width: 60%;
height: 25%;
position: absolute;
top: 25%;
left: 10%;
background: white;
}
<div class="bg">
<div class="overlay"></div>
</div>
Use a single container.
<style>
.main
{
background:white;
}
.login
{
background:blue;
}
</style>
<div class="main">
<div class="col-md-5 no-margin">
leave it blank
</div>
<div class="col-md-7 no-margin login">
put your page contents here
</div>
</div>

How to center elements inside an element

I was wondering how to center 3 divs inside a div.
Here is my code example
body {
position: fixed;
height: 100%;
width: 100%;
}
#container {
border: 3px solid black;
height: 90%;
width: 90%;
}
.plaatje {
width: 30%;
height: 70%;
border: 2px solid black;
float: left;
text-align: center;
}
#plaatje1 {
background-image: url("http://image.prntscr.com/image/c3d5dbc04f664a3386b372d8e4ceb4c7.png");
background-size: 100% 100%;
background-repeat: no-repeat;
}
#plaatje2 {
background-image: url("http://image.prntscr.com/image/2bcfd124f98a448cbae822337818ff4e.png");
background-size: 100% 100%;
background-repeat: no-repeat;
}
#plaatje3 {
background-image: url("http://image.prntscr.com/image/e1b7059d626f47cb94535bbba9887cc1.png");
background-size: 100% 100%;
background-repeat: no-repeat;
}
<div id="container">
<div id="plaatje1" class="plaatje">
</div>
<div id="plaatje2" class="plaatje">
</div>
<div id="plaatje3" class="plaatje">
</div>
</div>
The problem is, there is still a white space on the right hand-side of the picture, I have marked it so you know what i'm talking about.
It also needs to scale, so if I resize the window, that the third image doesn't pops below the first or that the space exists when I resize it fully.
Any help is appreciated.
I have created a jsFiddle which demonstrates how you can do this using flexbox. It doesn't require floating the elements and gives you with exactly what you're looking for.
I have added a wrapper around the images (.images) and given it the flex properties required to align its contents, then removed the floats and a few other unnecessary things.
Here is the browser support for flexbox: caniuse:flexbox
body {
position: fixed;
height: 100%;
width: 100%;
}
#container {
border: 3px solid black;
height: 90%;
width: 90%;
}
.images {
height: 90%;
display: flex;
justify-content: center;
}
.plaatje {
width: 30%;
height: 70%;
border: 2px solid black;
background-size: 100% 100%;
background-repeat: no-repeat;
}
#plaatje1 {
background-image: url("http://image.prntscr.com/image/c3d5dbc04f664a3386b372d8e4ceb4c7.png");
}
#plaatje2 {
background-image: url("http://image.prntscr.com/image/2bcfd124f98a448cbae822337818ff4e.png");
}
#plaatje3 {
background-image: url("http://image.prntscr.com/image/e1b7059d626f47cb94535bbba9887cc1.png");
}
<div id="container">
<div class="images">
<div id="plaatje1" class="plaatje"></div>
<div id="plaatje2" class="plaatje"></div>
<div id="plaatje3" class="plaatje"></div>
</div>
</div>
You could just simply try adding text-align:center; to your container div
There are many ways to do this, and you should probably start with http://www.w3schools.com/css/css_align.asp - this elementary level question often gets flagged as not appropriate for SO.
But! Welcome. Here's one way you could do this - I've added comments to explain what's going on. Basically your float: left by definition made the .plaatjes impossible to center; and the text-align: center needs to be on the containing element
body {
position: fixed; /* probably don't actually want */
height: 100%;
width: 100%;
margin: 0; /* add */
}
#container {
border: 3px solid black;
height: 90%;
width: 90%;
margin-left: 5%;
text-align: center; /* add */
}
.plaatje {
width: 30%;
height: 70%;
border: 2px solid black;
/* float: left; // remove
text-align: center;*/
display: inline-block; /* add */
}
#plaatje1 {
background-image: url("http://image.prntscr.com/image/c3d5dbc04f664a3386b372d8e4ceb4c7.png");
background-size: 100% 100%;
background-repeat: no-repeat;
}
#plaatje2 {
background-image: url("http://image.prntscr.com/image/2bcfd124f98a448cbae822337818ff4e.png");
background-size: 100% 100%;
background-repeat: no-repeat;
}
#plaatje3 {
background-image: url("http://image.prntscr.com/image/e1b7059d626f47cb94535bbba9887cc1.png");
background-size: 100% 100%;
background-repeat: no-repeat;
}
<div id="container">
<div id="plaatje1" class="plaatje">
</div><div id="plaatje2" class="plaatje">
</div><div id="plaatje3" class="plaatje">
</div>
</div>
<!-- removed spaces between the divs -->

how to make an image inside div fill the entire div?

Currently I'm using this code:
<style type="text/css">
.icondiv{
border:1px solid;
content: url(image.png) 100% 100%;
}
</style>
<div class="icondiv"></div>
The output is like, the image stays in 1/4 of the div. How can I make the image fill the whole? I already checked the image and it has no extra whitespace.
If you don't want to use background image
.container{
width: 400px;
height: 100px;
}
.container img{
width: 100%;
height: auto;
}
#supports(object-fit: cover){
.container img{
height: 100%;
object-fit: cover;
object-position: center center;
}
}
<div class="container">
<img src="http://i62.tinypic.com/2dh8y1g.jpg" alt="" />
</div>
But pay attention to the support: http://caniuse.com/#search=object-fit
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
.icondiv {
width: 400px;
height: 100px;
border: 1px solid #f00;
position: relative;
}
.icondiv img {
position: absolute; top: 0; left: 0;
width: 100%;
height: 100%;
}
<div class="icondiv">
<img src="https://encrypted-tbn1.gstatic.com/images?q=tbn:ANd9GcSL19OsbasMqU64_o3uoov5liyKmD8KMStU1OR8hXUtV4pwALr7Sg" alt="" />
</div>
You should use
<div class="container">
<img src="http://i48.tinypic.com/wrltuc.jpg" />
</div>
.container {
width: 700px;
height: 400px;
background: #444;
margin: 0 auto;
border: solid black 1px;
}
.container img{
width: 100%;
height: 100%;
}
Fiddle Here
Try this:
.icondiv{
border:1px solid;
background: url(yourimage.png) no-repeat center center;
background-size: cover;
width: 200px; // Adjust your needs
height: 200px; // Adjust your needs
}
You could create some css class like this:
full {
background-image: url(image_path('yourimage.jpg'));
background-repeat: no-repeat;
background-position: center;
background-attachment: fixed;
background-size: cover;
}
It looks like you're trying to add the image into the div using CSS rather than inline in the HTML... I will assume you've got a good reason for this and follow suit. Instead of using "content:" you can drop the image in as a background and make it spread to fill the container.
.container {
width: 700px;
height: 400px;
background:#f00 url(http://i48.tinypic.com/wrltuc.jpg) no-repeat center center;
background-size:cover;
margin: 0 auto;
border: solid black 1px;
}
<div class="container">
</div>
The benefit of using this "background-size:cover" technique is that your image will always fill the containing div regardless of its proportions.

How to center fixed navbar in another div?

I am trying to have a fixed and centered navbar div which is within another div that's using a parallax effect. If I set the position to relative, the navbar will be centered within the other div however setting it to fixed will have a fixed navbar but not centered. I would like to avoid using margin left x amount of px because it doesn't seem consistent for cross resolutions. Is there anyway to keep the navbar fixed position but have it centered within the other div without mar?
This is my code:
#page-wrap {
position: relative;
min-width: 1366px;
max-width: 2048px;
margin: 0px auto;
width: 95%;
}
header {
background: url('../images/cover1.jpg') no-repeat;
background-size: 100% 90%;
width: 100%;
height: 1000px;
}
#nav {
background: #f0f;
margin: 0 auto;
height: 500px;
min-width: 980px;
max-width: 2048px;
position: fixed;
opacity: 1;
}
#main {
background: #fff;
position: relative;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="page-wrap">
<header>
<nav id="nav">Nav</nav>
</header>
<div id="main">Lorem ipsum flash.</div>
</div>
<script>
$(document).on('scroll', function(e) {
$('#nav').css('opacity', ($(document).scrollTop() / 500));
var st = $(window).scrollTop();
$('header').css({
'background-position-y': 0 + (st * .77) + "px"
});
});
</script>
You could keep your nav in center of screen, but you need to wrap it inside fixed container, working Fiddle. HTML is slightly different:
<div id="page-wrap">
<header>
<div class="nav_container">
<nav id="nav">Nav</nav>
</div>
</header>
<div id="main">Lorem ipsum flash.</div>
</div>
In CSS fixed element becomes .nav_container and nav must become inline-block - that way we can use text align:
#page-wrap {
position: relative;
min-width: 1366px;
max-width: 2048px;
margin: 0px auto;
width: 95%;
}
header {
background: url('http://placehold.it/500x500') no-repeat;
background-size:100% 90%;
width: 100%;
height: 500px;
}
.nav_container {
margin: 0 auto;
width: 100%;
position: fixed;
opacity: 1;
text-align: center;
}
.nav_container nav {
display: inline-block;
background: #f0f;
min-width: 100px;
max-width: 2048px;
height: 100px;
}
#main {
background: #fff;
position: relative;
height: 500px;
}
How about centering your nav using javascript?
$('#nav').css({'left': (($(document).width() - $('#nav').width())/2 )+"px"});
This should suffice for what you're trying to do. Let me know if this doesn't work.
#nav {
background: #f0f;
margin: 0 auto;
height: 500px;
min-width: 980px;
max-width: 2048px;
position: fixed;
opacity: 1;
left: 50%;
}