CSS: Element's relative size makes background-image disappear - html

I have a background-image that is 800x480 pixels. When my element has a fixed size I see the background-image, but not when the element has a relative size or a max-width.
Working CSS script
.audio-container, .settings-container {
max-width:800px;
height:480px;
position:absolute;
background-image:url("../../public/images/Audio/I_Audio_BGK.png");
background-repeat: no-repeat;
background-size: 100% 100%;
}
CSS script with no background image showing
.audio-container, .settings-container {
width:100%;
/* Same result with max-width */
height:100%;
position:absolute;
background-image:url("../../public/images/Audio/I_Audio_BGK.png");
background-repeat: no-repeat;
background-size: 100% 100%;
}
What can I do to show the background-image yet have the element sizes relative to the browser window?
By request, here are the parent DIVs
<div ng-controller="MainController" class="main-guy">
<div class="screen-inside">
<div class="audio-container" ng-controller="AudioController">
</div>
</div>
</div>
Here are the parent DIV CSS styles
.main-guy {
position:absolute;
/* Same result if width and height are set to a fixed number */
width: 100%;
height: 100%;
margin: auto;
}
.screen-inside {
margin:auto;
position:relative;
height:60%;
width:66.66%;
}

You have to change the position:absolute in .settings-container to position:relative as your image in this case act as a Child for .settings-container and the image should be according to its parent. So Position:absolute will not work.
Check the snippet
.main-guy {
position:absolute;
width: 100%;
height: 100%;
margin: auto;
background:#999;
}
.screen-inside {
margin:auto;
position:relative;
height:60%;
width:66.66%;
background-color:blue;
}
.audio-container, .settings-container {
width:100%;
/* Same result with max-width */
height:100%;
background-image:url(http://reservations.life/css/images/bg-01.jpg);
background-repeat: no-repeat;
background-size: cover;
position:absolute;
}
<div ng-controller="MainController" class="main-guy">
<div class="screen-inside">
<div class="audio-container" ng-controller="AudioController">
</div>
</div>
</div>

Using the following HTML:
<div class="settings-container"></div>
With the following CSS:
html, body { height: 100%; margin: 0; padding: 0; }
.settings-container {
width: 100%;
height: 100%;
text-align: center;
background-image: URL("your-image-here");
background-repeat: no-repeat;
background-size: cover;
}
Results in a background taking up 100% of the width and height of the viewport. It's difficult to solve your question properly without seeing the whole picture, but my guess is that you will need to apply height somewhere else in your document.
You may also run into issues with using position: absolute, but again that largely depends on the broader picture of how you're applying this to your site/application/whatever.

Related

How do I make text appear on top of background image?

I have used this approach https://stackoverflow.com/a/22211990
Only problem is that as soon as I enter text/content in div like this:
<div>abc</div>
That text appear under the image.
Code: https://codepen.io/labeeb/pen/JMxzQY
* {
padding: 0px;
margin: 0px;
}
.image {
background: url('https://i.pinimg.com/originals/4f/d6/ef/4fd6ef1f078ca5e229ce5925c10f194a.jpg') no-repeat;
background-size: 100%;
background-size: contain;
height: 0;
padding-top: 100.44%; /* (bg image width/ bg image height) * 100*/
}
<div class="image">aaa</div>
You can give the image element position:relative and wrap the text in an element with position:absolute and top:0
*{
padding:0px;
margin:0px;
}
.relative {
position:relative;
}
.text {
position: absolute;
color: red;
top:0;
}
.image{
background:url('https://i.pinimg.com/originals/4f/d6/ef/4fd6ef1f078ca5e229ce5925c10f194a.jpg') no-repeat;
background-size:100%;
height:0;
padding-top:100.44%; /* (bg image width/ bg image height) * 100*/
}
<div class="image relative">
<div class="text">aaa</div>
</div>
For me : you choice is a bad strategy, because de the padding-top will always impact your content. It is not the background job.
My solution : combine height 100vh and background-size cover.
*{
padding:0px;
margin:0px;
}
.image{
color: white;
height: 100vh;
background:url('https://i.pinimg.com/originals/4f/d6/ef/4fd6ef1f078ca5e229ce5925c10f194a.jpg') no-repeat;
background-size:cover;
}
<div class="image">aaa</div>

CSS background image and height

I have this code:
.main {
min-height:100%;
width:100%;
position: relative;
background-image: url('onepage_restaurant.jpg');
background-repeat: no-repeat;
background-size: cover;
display: block;
}
but the div of class .main isn't showing up. Why?
Your code works fine unless you make sure that your parent element has a height greater than zero - otherwise your .main element will have no height either.
Also make sure that the file path to your image is correct. You can use the developer tools of your browser to check the height of your container and the image url.
html, body {
width: 100%;
height: 100%;
}
.main {
min-height: 100%;
width: 100%;
position: relative;
background-image: url(//placehold.it/500);
background-repeat: no-repeat;
background-size: cover;
display: block;
}
<div class="main"></div>
You want to set the min-height of that element to 100%. When you want to set the height of an element with percentage you need to set the height of the parent element to a specific value.
Set the height in pixels. Example.
height: 100px;
Could be either background image wrong path or, if you have no content, html, body height set to zero.
try this in your css
html, body {
width:100%;
height:100%;
margin:0;
padding:0;
}

images in a banner disappear when I use max-width or width: auto

I have rotating banner images which I'd like to work (scale to fit) in any screen size.
When I use the following, it works:
.banner{
position:absolute;
width: 80%;
height: 30%;
top:5%;
left:20%;
background:#FFF;
border:hidden;
}
However, when I try to change the width to for example 40%, the images truncate rather than scale down.
When I tried to use, for example, max-width: 80%, or width: auto, the images totally disappear, even if I use a high z-index.
Setting both width and height on your images, will not care about aspect ratio. Just use width = 100%, and leave the height related to it (with the technique below).
And then set the container width to whatever you want:
#banner {
width: 100%;
height: 0;
padding-top: 30%;
background: red;
}
#banner-container {
width: 400px;
}
<div id="banner-container">
<div id="banner"></div>
</div>
If you want to show an image inside it, use CSS background-image with background-size: cover:
#banner {
width: 100%;
height: 0;
padding-top: 30%;
background: gray;
background-position: 50% 50%;
background-size: cover;
background-repeat: no-repeat;
}
#banner-container {
width: 400px;
}
<div id="banner-container">
<div id="banner" style="background-image: url('http://placekitten.com/800/500');"></div>
</div>

Setting background to 100% height and keep footer at the bottom of page

I know this question has been asked many times but none of the answers solved my situation.
Here is a the demo http://jsfiddle.net/eyuxt3zz/18/ for wich I need that the #marketing_logo div background image should extend to 100% and to be tangent to footer div.
If I set the #content div to 100% using dispaly:absolute then the footer will not keep his bottom position.
I tried some solution but I cannot figure it out how to keep the footer at the bottom of the page when another div is also set to position: absolute.
The height for the background is now set to 300 px so that the content div does not appear empty.
So the question is how to make #marketing_logo div background to size 100% and keep footer div at the bottom. The footer div is set to position absolute so that it always stays at the bottom of the page.
Thanks!
demo http://jsfiddle.net/eyuxt3zz/18/
html code
<body>
<div id="page" class="pageContainer">
<div class="ppContainer">
<div id="container">
<div id="marketing_logo"></div>
</div>
</div>
<div id="footer">Footer</div>
</div>
</body>
CSS code
html, body {
height:100%;
margin: 0;
}
.pageContainer {
min-height: 100%;
}
#marketing_logo {
background: url('http://www.canvaz.com/portrait/charcoal-1.jpg') no-repeat;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
height:300px
}
#footer {
width: 100%;
height: 100px;
position: absolute;
left: 0;
bottom: 0;
background: #BBBBBB;
text-align: left;
padding-top: 10px;
}
I would solve this with JavaScript/jQuery. Here is your updated JSFiddle.
The JavaScript code:
$(document).ready(function() {
// Calculate heigth
function calcHeight() {
var height = $(window).height() - $('#footer').height();
$('#marketing_logo').css('height', height);
}
// Auto call calcHeight on page load
calcHeight();
// Call calcHeight when the browser window resizes
$(window).resize(calcHeight);
});
I created a little function called calcHeight. I call this function on page load and when the user resizes the window. Inside this function, I calculate the height of the footer and the remaining height (height of the window - the height of the footer) to update the height of your marketing_logo.
Here is an example, i am not sure this is what you are looking for? Example codepen
body,html{
height:100%;
}
#wrapper{
min-height:100%;
height:auto !important;
height:100%;
margin:0 auto -150px; /* minus the height of the footer */
}
#marketing_logo{
background: url('http://www.canvaz.com/portrait/charcoal-1.jpg') no-repeat center center;
height:100vh;
width:100%;
background-size:100%;
}
#push, footer{
height:150px; /* height of the footer */
clear:both;
}
footer{
background:#BBBBBB;
}
Try this:
#marketing_logo {
background: url('http://www.canvaz.com/portrait/charcoal-1.jpg') no-repeat;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
margin: 0 -15px -150px -15px;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
position: fixed;
}

How to crop not stretch a background-image

I want to use a background image to my section element with width:100% and height:40%.
So i used CSS3 and used this solution:
background-image: url(My_Local_Image);
background-repeat: no-repeat;
background-size: 100% 40%;
background-position: center top;
It worked nice!
My problem now is that i want the background-image to be cropped to fit the size i specify. Now image is streched to fit.
Is there ant way that i can achieve this?
FIDDLE
Unfortunately you cannot do something like
background-size: cover 40%;
cause you'll loose the 100%
the solution would be so make a separate image container, and after it an element for your (I suppose) text, setting simply background-size: cover; for the image container,
setting also width: 100%; and height : 40%; for the same.
But what you can do is
LIVE DEMO
<section>
<div class="sectionImage" id="first"></div>
<div class="sectionContent">1</div>
</section>
<section>
<div class="sectionImage" id="second"></div>
<div class="sectionContent">2</div>
</section>
<section>
<div class="sectionImage" id="third"></div>
<div class="sectionContent">3</div>
</section>
section{
background:#444;
position:relative;
margin:10px auto;
height:300px;
width:800px;
color:#fff;
}
.sectionImage{
width: 100%;
height:30%;
background: transparent none no-repeat center center;
background-size: cover;
}
.sectionContent{}
#first{
background-image: url('1.jpg');
}
#second{
background-image: url(2.jpg);
}
#third{
background-image: url(3.jpg);
}
If I understand what you're trying to do, simply remove the 40% from your background-size and the image will fill the div at the 800x300px size.
You must place the background container inside your main container. After that you must provide width and height of main containter and make overflow:hidden.
You can then play with main container's width and height to change crop size. (You can use width:40%; and height:100% too)
Here is JSFidde.
HTML:
<section id="first">
<div id="bg"></div>
</section>
CSS:
#first{
height:300px;
width:200px;
overflow:hidden;
}
#bg{
background-image: url('https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQOhLJod_xPxdgo339zfIJipPzOUZg9BunbT-ftIgDMiu2HLi0o');
background-repeat: no-repeat;
background-size: 100% 100%;
background-position: center top;
width:800px;
height:300px;
}
Use an inner div to get the crop effect:
Fiddle
CSS
#first{
height:300px;
width:800px;
}
#first div{
width:100%;
height:40%;
background-image: url('https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcQOhLJod_xPxdgo339zfIJipPzOUZg9BunbT-ftIgDMiu2HLi0o');
background-repeat: no-repeat;
background-size:100%;
background-position: 50% 50%;
}
Use the :before pseudo class
#first:before {
content: '';
position: absolute;
top: 0px;
left: 0px;
width: 100%;
height: 40%;
background-image: url(image.jpg);
background-repeat: no-repeat;
background-position: center center;
}
This will give you many CSS options to deal with both bigger/smaller images, stretching/cropping, etc., without messing with the html