background-image doesn't appear if
is empty?

前端 未结 8 970
旧巷少年郎
旧巷少年郎 2020-12-07 01:11

I created a

first thing in the to draw a top line at the top of the page:


    
相关标签:
8条回答
  • 2020-12-07 01:34

    If it is the only div element in the body use the following style to to make it occupy the full-width.

    .bordertop  {
       position: absolute;
       top: 0;
       right: 0;
       bottom: 0;
       left: 0;
       background-image: 
       url('../images/top_border.png');
    }
    
    0 讨论(0)
  • 2020-12-07 01:37

    I couldn't get my background showing in the div even with the width set up. Turns out i had to put "../" in the url section then it showed the picture i was struggling for quite a while.

    left {
        width: 800px;
        height: auto;
        min-height: 100%;
        position: relative;
        background-image: url("../img/loginpic.jpg");
        background-size: cover;
        border-top-left-radius: 4px;
        border-bottom-left-radius: 4px;
        background-color: crimson;
    }
    
    0 讨论(0)
  • 2020-12-07 01:40

    As the answers above me suggest ^^' it's because it has virtually no size, you need either to put content inside to resize it or to set width/height or padding in css bordertop class, or you can put another empty inside it with set size. I was going to skip this answer since there are already answers but I just wanted to add that width/height is not your only option.

    On a side note, oh man, people here posting so fast I sometimes wonder if its a race and what is the prize, there must be some, I guess helping other is itself great prize. :) When I was starting to type this there was no answer yet.

    0 讨论(0)
  • 2020-12-07 01:44

    Since the div is empty, there's no content to push it "open" leaving the div to be 0px tall. Set explicit dimensions on the div and you should see the background image.

    .bordertop 
    {
        background-image: url(../images/top_border.png);
        background-repeat: repeat-x;    
        height: 100px;
        width: 100%; /* may not be necessary */
    }
    
    0 讨论(0)
  • 2020-12-07 01:49

    Give the div a height:1px. That should work. Otherwise your div is 0px high, meaning you won't see anything.

    You could also give it padding-top:1px

    Another thing you could do is to set the background-image of the line on the body in your CSS. This is assuming the line is the entire width of the body.

    See demo

    0 讨论(0)
  • 2020-12-07 01:50

    You might need to set the css width and height of your <div> element to whatever size you want

    .bordertop {
        background-image: url(../images/top_border.png);
        background-repeat: repeat-x;
        width: 200px;
        height: 100px;
    }
    
    0 讨论(0)
提交回复
热议问题