• ......">
    All geek questions in one place

    Unable to set background image for specific div

    Some strange background image behavior

    HTML

    <body> <div id="divGaLL"> <ul id="ulGaLL"> <li>...</li> <li>...</li> </ul> </div> </body> 

    CSS

     body{ background:url(img/back01.jpg); //works } #divGaLL{ background:red; // works background:url(img/back01.jpg); //doesn't work } #ulGaLL{ background:url(img/back01.jpg); //works } 

    Why can't I set back01.jpg as the background for #divGaLL ?

    +4
    html css background-image
    bonaca May 21 '13 at 11:10
    source share
    7 answers

    This is because you are using the same property and different values. When you use background: you can write color and image in one line

     #divGaLL{ background:url(http://cdn1.iconfinder.com/data/icons/free-scuba-diving-icon-set/128/fish.png) red; } 

    Demo

    +4
    Sowmya May 21 '13 at 11:12
    source share

    The background property is a shorthand for setting a set of properties, including background-image . If you want to specify a background color or background image without overlapping anything else, you must specify background-color and background-image full:

     #divGaLL { background-color: red; background-image: url(img/back01.jpg); } 

    See "background" in MDN .

    +2
    Richiehindle May 21 '13 at 11:12
    source share

    Try

     background:red url(img/back01.jpg) no-repeat; 
    +2
    Shibin ragh May 21 '13 at 11:17
    source share

    You need to use

    background-image: url('img/back01.jpg');

    or for example

    background: red url('img/back01.jpg') left top no-repeat;

    Try to adhere to this order of "arguments" if possible;)

    I prefer to use the second method, however sometimes, when you just need to change one thing, it is better to use only the first approach.

    It is always useful to determine the height and width and update that this element is a block , not an inline style, etc., if to prevent unwanted behavior, use something like:

     display: block; width: 150px; height: 150px; background: red url('img/back01.jpg') left top no-repeat; 
    +2
    user2405063 May 21 '13 at 11:57
    source share

    If #ulGaLL has a background image that completely covers it, you will not see the #divGaLL background.

    +1
    Duncan beattie May 21 '13 at 11:14
    source share

    Decision:

     #divGaLL{ width:200px; /* Width of back01.jpg */ height:200px; /* Height of back01.jpg */ background-image:url('img/back01.jpg'); } 
    +1
    Touhid rahman May 21 '13 at 11:20
    source share

    If you put the image in your HTML, you can use something like this:

     #divGaLL img{ background-color:red;} 

    Instead of the whole div, only IMG will be used.

    And yes, not only use the background for definition, since the background is used for all kinds of different things, but only for img or color.

    Therefore, always use a background color, background image, or whatever you want with it

    +1
    Ryan de vries May 21 '13 at 11:23
    source share

    Source: https://habr.com/ru/post/1481931/

    More articles:

    • Ckeditor, how to apply a keyboard shortcut to a specific style - javascript
    • F # Pattern comparison by type - pattern-matching
    • How to get iframe content from remote page? - jquery
    • CKEditor how to create custom styles? - javascript
    • Turn off application notification - android
    • Filling gap in the priority column + maintenance order - php
    • How to delete rows using CTE and INNER JOIN? - sql
    • Using D for a real-time application? - garbage-collection
    • Received unexpected value in TDS response at offset - sql-server
    • Strange problem with Internet deployment - webdeploy

    All Articles

    Geek Questions | 2019