I'm trying to align an image at the bottom of my DIV, which happens to be the background. Just to make the case even more real, I also added some overlay (as that is how I am doing it in my project).
https://jsfiddle.net/jy0w2jmr/
background-size: 100% 100%;
That will make the background size 100% of the DIV, but not stretched out, so it will be 300px tall (if my DIV is 300px tall).
How do I make it, so when I resize the DIV, the background image also resizes, but sticks to the bottom of the DIV and NEVER overflows the DIV? background-position: bottom; does not seem to stick it to the bottom of my DIV.
Does this work for you?
#container {
width: 100%;
height: 300px;
background: url("https://upload.wikimedia.org/wikipedia/commons/2/2f/Google_2015_logo.svg");
position: relative;
background-size: contain;
background-position: bottom;
background-repeat: no-repeat;
}
.overlay {
position: absolute;
width: 100%;
height: 100%;
background: #333;
opacity: 0.5;
}
#container {
width: 100%;
height: 300px;
background: url("https://upload.wikimedia.org/wikipedia/commons/2/2f/Google_2015_logo.svg");
position: relative;
background-size: contain;
background-position: bottom;
background-repeat: no-repeat;
}
.overlay {
position: absolute;
width: 100%;
height: 100%;
background: #333;
opacity: 0.5;
}
<div id="container">
<div class="overlay"></div>
</div>
Related
On a web page i have a div with background image that has width of browser window and fixed height as you can see in code below. I need to make small white squares in corners of background image, i wounder if it is possible to do it with CSS? any other ideas of how to do it are welcome.
.field-node-field-book-airport-transfer:before{
background-image: url(/img.jpg);
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
position:absolute;
left:0;
right:0;
content: "";
height: 25em;
background-color: black;
}
You can create something like this - codepen link
<div class="field-node-field-book-airport-transfer"><span class="topleft"></span>
</div>
.field-node-field-book-airport-transfer:before{
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJ0AAAB2CAMAAADle2GlAAAAYFBMVEXMzMzPz8/S0tLV1dXIyMhMTEzY2NhJSUlSUlKwsLC2traIiIjExMS/v7+fn59YWFhra2tkZGRERESVlZV9fX08PDwxMTFdXV1ycnIAAAAjIyOOjo6pqakeHh4pKSkZGRnpOLpJAAACh0lEQVR4nO2W13LbMBBFgUUl2MFqkXH+/y+DQkrWjEvGliYPuedBbLjay8UuQMYAAAAAAAAAAAAAAAAAAAAAAAAA8J/DA3S9OA4Ubn4uumrP068k3zNXKu97mS+oq2MEaow3K/s4GO1jfCEqeq/UEMeJRn0u+RZkx4rJIruj7cWF/6fR1kyW6kMRlxcrwqHxjrGqWym84xeS75krV8HOCeWF7Yp4+irDr+4Huht7ywvvyiXIxFRQnF0TEn6JEtFt95IfIifiRGfcqYruaEvTxp0X6a6r0nO+n8NEudZtcpfqQLQ1DesheaQ77qx2Q7mnvFDX6OSub5IPaWR2ZaM92hZxilrmoju+raEXxLBQkGbrpn6ku6ZclmHo52CPGisouuO2zqGOo2gmznXZnqmjuabkjvFRNXvXS87bIufXuAf2BQ0vuw6hG895bYgld0xlV2SPUKKYq20554zWgXh2R/vSLmYLpWezO1LFQ915HY96dkKFurrL3TWHwcXcXs01vWDZHY1hAeKyNIyelLtUzUzYvSxjvFR3y/627uLjzapzqa186E7uFh3WlSnd1Oug+6fUXaFy7nzRqcjFm1KU430DxoYopiOhTRrnf1kl9y4N4PuqT4l66IpSvcZ25Xyusts0s/UUz8U40mGuTT1TvXmrOLOhUnPXjCXVc1pb8ur5MGgzUmupjlU0d4XoO9J6/304btq8VU23yLnuhFq5FqIJr0ZhwwiSS/VekB/ZU8qcuwIfU1XT6K2x1VFp8thJ5E3FXd5iRtO2vk/L4Z3kcfakc+xaLefXQOVq+jSSvIqrPDBKnvCR8v53xd/GeWPoCdYAAAAAAAAAAAAAAAAAAAAAAACAf8gfFagYayDDn0YAAAAASUVORK5CYII=);
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
position:absolute;
left:0;
right:0;
content: "";
height: 25em;
background-color: black;
}
.field-node-field-book-airport-transfer {
position: Relative;
}
.field-node-field-book-airport-transfer span.topleft {
width: 100px;
height: 100px;
background: #ffffff;
position: absolute;
top: 0;
left: 0;
}
I have a page where I want a full-screen background image. This works fine when the the body fits within the viewport, I have html { height: 100%; } and body { min-height: 100%; padding-top: 70px; ... background-size: cover; } (the top padding is for a page header). The issue arises when the page becomes larger than the viewport. The body stretches to the correct height, however the background attachment never grows larger than the size of the viewport. Here is a sample fiddle [https://jsfiddle.net/xdsgek6t/]. In the live version there is also an image overlay, but in the fiddle you can easily see the line where the radial gradient ends, even though I've told it to cover the body, which in this fiddle is 3000px tall due to a child element.
html { height: 100%; box-sizing: border-box; }
body {
overflow-y: scroll;
padding-bottom: 30px;
padding-top: 70px;
background-color: #363636;
min-height: 100%;
background-color: #1976D2;
background-image: radial-gradient( circle at top right, #64B5F6 0%, #1976D2 90% );
background-repeat: no-repeat;
background-attachment: scroll;
background-position: right 70px;
background-size: cover;
margin: 0;
box-sizing: border-box;
}
div.something { height: 3000px; width: 10px; }
header { position: absolute; width: 100%; top: 0; left: 0; height: 70px; z-index: 500; background-color: #ddd; }
<body>
<header></header>
<div class="something"></div>
</body>
This ends up looking really strange when the page grows a tiny bit larger, and is really evident on mobile.
Remove height: 100%; from html and it will extend. And if you need min-height: 100% on body, you can use min-height: 100vh instead, and that will not rely on height: 100% on html
html { box-sizing: border-box; }
body {
overflow-y: scroll;
padding-bottom: 30px;
padding-top: 70px;
background-color: #363636;
min-height: 100vh;
background-color: #1976D2;
background-image: radial-gradient( circle at top right, #64B5F6 0%, #1976D2 90% );
background-repeat: no-repeat;
background-attachment: scroll;
background-position: right 70px;
background-size: cover;
margin: 0;
box-sizing: border-box;
}
div.something { height: 3000px; width: 10px; }
header { position: absolute; width: 100%; top: 0; left: 0; height: 70px; z-index: 500; background-color: #ddd; }
<header></header>
<div class="something"></div>
So basically I want to move the white background to the middle/center
with a width 65% and height 100%
.middlebg {
background-color: #FFFFFF;
background-position: center top;
background-repeat: no-repeat;
background-attachment: fixed;``
width: 65%;
height: 100%;
clear: both;
The key to this is setting the margin in the middlebg class to margin: 0 auto the first value being the vertical margin and the second being the horizontal margin. Setting the horizontal margin to auto will centre this element for you.
http://codepen.io/lostdino/pen/rOEEPr
body {
background: black;
position:relative;
width: 100%;
}
.middlebg {
margin: 0 auto;
background-attachment: fixed;
width: 65%;
height: 80px;
background:white;
}
repeat-x background image in 500px container div and how can I scale background image to page width. I am sorry for my bad English. Have a look at the following image to understand my case.
.container {
width: 500px;
height: 60px;
background-color:#f2d88c;
}
.menubg {
width: 100%;
height: 30px;
background-image: url(bg.jpg) center repeat-x;
}
http://jsfiddle.net/PjGqv/9/
Your div.menubg is a child of div.container The child element cannot be wider then its 500px width parent.
You can use absolute positioning if the parent elements position it is relative to has a 100% width. I've included a jsfiddle
However by using position: absolute; you are taking the child out of the parents container. Depending on your situation, you would have to adjust its position values.
.menubg {
position: absolute;
width: 100%;
height: 30px;
background-image: url(bg.jpg) center repeat-x;
}
You can use 3 elements and use this css
.container {
position: relative;
display: block;
height: 60px;
width: 100%;
text-align: center;
padding: 0;
}
.bg {
background-color: #f2d88c;
position: relative;
height: 60px;
width: 500px;
margin: 0 auto;
}
.menubg {
position: absolute;
width: 100%;
top: 15px;
height: 30px;
display: block;
text-align: center;
background-image: url(bg.jpg) center repeat-x;
}
<div class="container">
<div class="bg"></div>
<div class="menubg"></div>
</div>
Try adding:
background-size: cover;
to the .menubg
My footer div is overlapping the content in chrome browser eventhough I have cleared it. Can someonone poin t me in the right direction with the css. Should I use an if is chrome javascript thing?
http://archibaldbutler.com/projects/roaming-giraffe/html/
Thanks
If the footer is designed to be at the bottom of the page all the time, then do this:
.footer-bg {
clear: both;
bottom: 0px;
position: fixed;
width: 996px;
height: 192px;
background-image: url('../html/images/footer-bg.jpg');
background-position: center top;
background-repeat: no-repeat;
}
.content-bg2 {
clear: both;
float: left;
margin-top: 10px;
margin-bottom:192px;
width: 996px;
height: 369px;
background-image: url('../html/images/maincontent2bg.jpg');
background-position: center top;
background-repeat: none;
}
if it is supposed to just be at the bottom of that content area then do this:
.footer-bg {
clear: both;
bottom: 0px;
width: 996px;
height: 192px;
background-image: url('../html/images/footer-bg.jpg');
background-position: center top;
background-repeat: no-repeat;
}