Background image not working although path is correct - html

I'm trying to clone a website in my local environment. For some reason, background image doesn't show even though the path is correct as shown in the jpg below.
The photo is in the same folder as index.html and style.css. The css is below:
.wallpaper {
background-image:url('jobbatical-1-wallpaper.jpg');
background-repeat: no-repeat;
background-position: top;
background-size: cover;
height: 100%;
}
.navbar {
overflow: hidden;
background-color: green;
position: fixed;
top: 0;
left: 0;
width: 100%;
z-index: 10;
}
.navbar a {
float: left;
display: block;
color: blue;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}

The problem is that 100% height is useless if the parent has no height.
What do height do you want the image to have? 100% of the viewport or just fill the page?
(100% of the viewport would mean that you have to scroll to see the full picture because of your navbar)
Here is the code of the image should copy the height of the viewport:
(EDIT: maybe you should set the height of the image to 100vh instead of bodys height)
html,
body
{
height: 100%;
margin: 0px;
}
.navbar
{
background-color:green;
height:50px;
}
.image
{
height: 100%;
background-image: linear-gradient(black,gray);
}
<div class="navbar"></div>
<div class="image"></div>
Here is the code if you want to fill the parent (using flex):
body
{
margin: 0px;
}
.viewport
{
height: 100vh;
display: flex;
flex-direction: column;
width: 100%;
}
.navbar
{
background-color: green;
height: 50px;
}
.image
{
background-image: linear-gradient(black,gray);
flex-grow: 1;/*This will make the image to fill the parents space*/
}
<div class="viewport">
<div class="navbar"></div>
<div class="image"></div>
</div>

Your div has no height. You can tell because you can see the position of the paragraph under it.
Since it has no height, there are no pixels to display the background image on.
You need to give it a height.
You have tried giving it height: 100% but a percentage height means height: auto (i.e. the height of the content, of which you have none) when the parent element's height is auto (which the body element is by default).
If you want to make a div take up the remaining vertical space after other elements have been accounted for, look at flexbox for layout. The Holy Grail layout is a fairly extreme example of that.

Space characters aren't allowed in url's.
So you either don't use spaces or encode it when calling it with %20 instead of the space.
I personally use - or _ instead.
Stackoverflow: "href syntax : is it okay to have space in file name"

Related

Responsive Padding with Background-Image

I'm trying to make a full width and height responsive home page with an image. The problem I'm encountering are padding issues. I cannot get padding to work when I display an image in css under 'background-image: url();'. The only thing that works is the margin property but it is not responsive to the height and only shows the top and the rest as I scroll down but I am trying to have the padding be responsive to the resizing of the height of the page. To show you guys more of what I am trying to achieve, I included 2 examples, the top with what I want and the second with the problem I'm facing. I've managed to get responsive padding to work while I place the img tag in my HTML but I cannot do so with the background-image property as I'm trying to put text on it.
.test img{
width: 100%;
max-width: 100%;
height: 100vh;
padding: 10px;
}
.wrapper {
background-image: url(https://images4.alphacoders.com/432/43258.jpg);
height: 100vh;
width: 100%;
max-width: 100%;
background-size: cover;
background-position: center center;
}
<div class="test">
<img src="https://images4.alphacoders.com/432/43258.jpg" alt="">
</div>
<div class="main">
<div class="wrapper"></div>
</div>
https://jsfiddle.net/u9t4hqqq/
You can use margin, you just need to account for the vertical margin that will push your 100vh height out of 100vh, and you can do that with calc()
body {margin:0;}
div {
margin: 10px;
background: url('http://cdn.thedailybeast.com/content/dailybeast/articles/2015/03/31/neil-degrasse-tyson-defends-scientology-and-the-bush-administration-s-science-record/jcr:content/image.img.2000.jpg/1432067001553.cached.jpg') center top no-repeat / cover;
height: calc(100vh - 20px);
}
<div></div>
Or you can wrap the element in another element, apply padding to the outer element, and use border-box to keep the padding inside of 100vh.
body {margin:0;}
section {
height: 100vh;
box-sizing: border-box;
padding: 10px;
}
div {
background: url('http://cdn.thedailybeast.com/content/dailybeast/articles/2015/03/31/neil-degrasse-tyson-defends-scientology-and-the-bush-administration-s-science-record/jcr:content/image.img.2000.jpg/1432067001553.cached.jpg') center top no-repeat / cover;
height: 100%;
}
<section><div></div></section>
Padding does work, but you can't see it. If you put content within the div, you'd see the effects of any padding. What you want is to apply the padding to the parent, in this case .main. Padding by definition can not impact the background of the element it's applied to but rather where children sit in relation to the element's borders.
If that is somehow insufficient, you can simulate the look with box-sizing: border-box and use a 10px border that matches the body background.
Which raises the point that you may want to review the box model to learn better what margin and padding are and how they relate to elements:
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Introduction_to_the_CSS_box_model
madrougebeauty.com uses a "frame" that is layed on top of all elements; it has nothing to do with padding.
To achieve something like it, look at the following:
.wrapper {
background-image: url(https://images4.alphacoders.com/432/43258.jpg);
background-size: cover;
background-position: center center;
height: auto;
min-height: 100vh;
color: #fff;
box-sizing: border-box;
/* Give your content padding so nothing gets hidden under the frame */
padding: 2em;
}
.frame {
position: fixed;
z-index: 9999;
background-color: yellow;
}
.top, .bottom {
width: 100%;
height: 10px;
left: 0;
}
.left, .right {
width: 10px;
height: 100vh;
top: 0;
}
.top {
top: 0;
}
.right {
right: 0;
left: auto;
}
.bottom {
bottom: 0;
top: auto;
}
.left {
left: 0;
}
<!-- These 4 elements build a frame on top of the screen -->
<div class="frame top"></div>
<div class="frame right"></div>
<div class="frame bottom"></div>
<div class="frame left"></div>
<div class="wrapper">
<h1>Headline</h1>
<p>Your content here.</p>
</div>

Incorrect positioning of divs when using height in percentage

I'm not sure if this problem has been posted before, but I don't know how to ask this question effectively.
In my website I've created two large sections one after the other (not referring to the tag), one's height is set to 100% and the other is set to 90%. I've added a div directly underneath the second section. To keep the div stuck I've set "top" to 190% in order to simulate the length of the two sections. Although I've set a minimum height to each section, which makes the div crawl underneath the sections when they've stopped resizing.
How can I avoid this whilst using "position: absolute" for the elements?
html example (using one larger section):
<html>
<body>
<div class="section1"></div>
<div class="box"></div>
</body>
</html>
css example:
.section1 {
display: inline-block; width: 100%; height: 100%; min-height: 500px;
position: absolute;
}
.box {
width: 100%; height: 200px;
position: absolute; top: 100%; margin-top: 50px;
}
Thanks,
Jonathan
Just don't use position:absolute.
I'm assuming the reason you had it is because you needed height 100% of the viewport, without using JS. You could use the vh unit, but it doesn't have the best support/reliability.
The easiest way is to simply set html and body to height:100%;:
body,
html {
margin: 0;
height: 100%;
}
.full {
height: 100%;
background: teal;
}
.shorter {
height: 90%;
background: #fbfbfb;
}
footer {
background: #222021;
color: white;
text-align: center;
padding: 10px;
}
<section class="full"></section>
<section class="shorter"></section>
<footer>Made with love by a kitten</footer>
Note that I did add extra CSS for styling purposes.

Image overlapping Div when VH is added

I'm currently trying to set up div sections so they take up about 95vh of the web page.
The issue is that when I add vh to one of the sections, the image on the next section overlaps the previous section blocking off some of the content.
All of the divs have relative positioning and this only occurs when I add a vh to the div.
section#showcase{
height:92vh;
}
#contentShowcase{
height:92vh;
}
section#judging {
margin-bottom: 50px;
height: 90vh;
}
section#judging #contentEnter {
margin-top: 50px;
height:350px;
}
#judgingImg {
background: url('../images/beyond-2015-city.jpg') no-repeat 0 0;
background-size: cover;
height:50vh;
}
/* Section Content */
section#mainContent,
section#mainContentEnter,
section#mainContentAttend,
section#gobeyond,
section#event,
section#eventInfo,
section#enter,
section#attend,
section#judging,
section#sponsors,
section#venue,
section#showcase,
section#form,
#eventQuote,
#judgingImg,
#sponsorsImg,
.contentBlock {
width: 100%;
min-width: 100%;
display: block;
position: relative;
}
#scrolltoBeyond2015, #scrolltoEvent, #scrolltoShowcase, #scrolltoJudging, #scrolltoVenue {
padding-top: 68px;
margin-top: -68px;
display:block;
}
<section id="showcase" class="">
<a id="scrolltoShowcase"></a>
<div class="chevronDown chevDkBlue hidden-lg hidden-md"></div>
<div id="contentShowcase" class="row col-DarkBlue bkgrd-LtAccentBlue">
</div>
</div>
</section>
<section id="judging">
<a id="scrolltoJudging"></a>
<a class="chevronDown chevtntBlue" href="#scrolltoJudging"></a>
<div id="judgingImg"></div>
</section>
You could use the z-index to set things right in a kind of a layering style.
section#showcase {
height: 92vh;
z-index: 100;
}
#contentShowcase {
height: 92vh;
z-index: 80;
}
In this case, section#showcase would be at the top of the #contentShowcase for it has the higher value of z-index.
In the sample code the problem was caused due to this css -
#contentShowcase{
height:92vh;
}
you will see that the div#contentShowcase is inside section on which you have set height: 92vh but when you set height: 92vh on the child div #contentShowcase, this div goes out of the boundaries of section and hence the part of this div is below the next section
see this fiddle with your original code, I have added a green border to illustrate how it goes out of section
now see this fiddle in which I have removed height
so stop setting height in vh in div present inside section in which too you have set height in vh, use % instead.

Why doesn't the background image show up without specific width and height?

Here's an example code
I've been wondering why the background-image doesn't show up unless I specific the image's width and height in pixels. I tried to specific only the width in percentage but it didn't work. The w3cschools.com was able to show the background image without specifying the width and height, but it works only in body background. Any explanation or solution workaround?
HTML
<div class="pic"></div>
CSS
.pic {
background: url("http://i.imgur.com/HIt6f8r.png") no-repeat;
display: block;
position: relative;
width: 244px;
height: 230px;
background-size: contain;
}
Your <div> element don't have any content, so the <div> height is 0px.
The width of the <div> is still 100%.
If you add any content to the div it will have some height and it will show a portion of image.
<body> by default has the height of the window, so you can see the background-image.
I found a great alternative without specifying the height, thanks to http://blog.brianjohnsondesign.com/maintain-aspect-ratio-for-html-element-using-only-css-in-a-responsive-design/.
HTML
<div class="pic"></div>
CSS
.pic {
background: url("http://i.imgur.com/HIt6f8r.png") no-repeat;
display: block;
position: relative;
width: 20%;
height: 0;
padding-bottom: 20%;
background-size: 100%;
}
All you need to do, assuming that it's a square, to match the padding-bottom to the width in css.
Update:
I also heard about another solution that may be useful. http://www.mademyday.de/css-height-equals-width-with-pure-css.html
CSS
.pic {
position: relative;
width: 50%;
}
.pic:before {
content: "";
display: block;
padding-top: 100%;
}
although I haven't tested it out yet....
<div class="pic"></div>
Div is container, it expects to have inner elements, when it's empty you must explicitly define height.
Your background image will not show because the div element has no content, this means that its height is 0.
You could use this jQuery code to make your div take the size of the window.
$(function () {
'use strict';
$('.div').height($(window).height());
$(window).resize(function () {
$('.div').height($(window).height());
})
});
If you don't specify height, the size of your div is given by the size of its contents, i.e. it's 0x0, so you don't have much chance of seeing a background image. Add
border: 1px solid red;
to see how large your div is (or isn't).
I am struggling with similar, trying to put text on top of image using css, but as I dont set height, it doesnt show. Have tried code above as well
.module5 {
background: url(image.jpg);
display: block;
background-size: 100%;
width: 100%;
height: 0;
position: relative;
float: left;
border: 1px solid red;
}
.mid h2 {
font-family: 'Roboto', sans-serif;
font-weight: 900;
color: white;
text-transform: uppercase;
margin: 0;
position: absolute;
top: 50%;
left: 50%;
font-size: 2rem;
transform: translate(-50%, -50%);
}
And where pointing it:
<div class="module5 mid" style="width: 100%;">
<h2>My Text</h2>
</div>
So unless I set a height of module, it just shows a red line(my border for testing)

Maximal height to a div that is children of 100% min-height body

At first I want to apologize for putting my code straight into the question, but I see no point putting a fiddle of that as the jsfiddle seems not to support my idea at all.
I have a document's scaffold like
<body>
<div id = "top" class = "top">
<div class = "container-pageblock">
<div class = "block-content">
<div class = "some-element">rolololo</div>
</div>
</div>
<div class = "footer-pageblock">
<div class = "block-content">foter</div>
</div>
</div>
</body>
with css rules that go
html
{
height: 100%;
}
body
{
font-family: "Segoe UI";
padding: 0;
margin: 0;
min-height: 100%
}
.top
{
width: 100%;
min-height: 100%;
background-color: #eee;
position: relative;
}
[class $= "-pageblock"]
{
width: 100%;
}
[class $= "-pageblock"] .block-content
{
width: 1080px;
margin-left: auto;
margin-right: auto;
}
.top, [class $= "-pageblock"]
{
min-width: 1080px;
}
.container-pageblock
{
background-color: #afa;
}
.footer-pageblock
{
text-align: center;
display: block;
background: #dde;
position: absolute;
bottom: 0;
}
.some-element
{
height: 500px;
}
My goal is to have the footer stuck to the bottom of the browser (with some blank space between .container-pageblock and .footer-pageblock when the content is too little) and as the content grows, the footer to be at the bottom of the document. Which basically means that I need the footer to be at most (not higher than) at the bottom of the browser window.
The problem seems to be that the div.top's min-height: 100% does not make its height as the body height. The effect is that when the browser height is (much) more than the contents' height, the .footer-pageblock's bottom is at the bottom of the .content-pageblock instead of the bottom of the browser.
Firebug indicates that body's height does have at least 100%.
I can't make it work for Firefox and IEs up to 9). However the code seems to work in latest Chrome and IE 10.
What is more, I really want to have this html preserved (that is keep the .footer-pageblock inside .top)
EDIT
According to this question and its answer, I just needed to specify height: 100% to body. Contrary to what I expected, this does not make .top's height to the browser's height. Solved.