0

I am using this code for making background image fixed but image url is not working can you tell me what I am missing.

code is written in my view file :

html { 
     background: url(<?php echo base_url();?>assets/images/img.jpg) no-repeat  center center fixed; 
     -webkit-background-size: cover;
     -moz-background-size: cover;
     -o-background-size: cover;
      background-size: cover;
}

Why I am getting "304 error" on all these files? I'm using this code to link these files:

 <script type="text/javascript" src="<?php echo base_url(); ?>assets/jquery/jquery-3.2.1.min.js"></script> 
 <link href=" <?php echo base_url(); ?>assets/css/bootstrap.min.css" rel="stylesheet">
 <link href=" <?php echo base_url(); ?>assets/style/style.css" rel="stylesheet">
 <script type="text/javascript" src="<?php echo base_url();?>/assets/js/bootstrap.min.js"></script>

7
  • what was error or problem Commented Nov 30, 2017 at 9:37
  • try this, background: url(../assets/images/img.jpg) no-repeat center center fixed; Commented Nov 30, 2017 at 9:39
  • 1
    try with this background: url(../assets/images/img.jpg)no-repeat center; Commented Nov 30, 2017 at 9:40
  • background image is stretched i want to make it fixed Commented Nov 30, 2017 at 10:00
  • @Bhargav i tried this but it doesn't work Commented Nov 30, 2017 at 10:03

5 Answers 5

3

you are missing ""() double quotes.

Use this css for fixed background

body{
 background-attachment: fixed;
 background-image: url(<?php echo base_url();?>./image.jpg);
}

or else , try using this.

background-image: url("../image.jpg");
Sign up to request clarification or add additional context in comments.

2 Comments

body is used in css because body holds the backgound image
note this <?php echo base_url();?>. will not work in .css
0

Lets say your css file is in assets folder

application

assets

assets > css > example.css

assets > images

system 

index.php

Then try this in the CSS file

body {
  background: url('../images/img.jpg') no-repeat center center fixed;
  -moz-background-size: cover;
  -webkit-background-size: cover;
  -o-background-size: cover;
   background-size: cover;
}

Of course make sure you have set the base url in the config.php

/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
|   http://example.com/
|
| WARNING: You MUST set this value!
|
| If it is not set, then CodeIgniter will try guess the protocol and path
| your installation, but due to security concerns the hostname will be set
| to $_SERVER['SERVER_ADDR'] if available, or localhost otherwise.
| The auto-detection mechanism exists only for convenience during
| development and MUST NOT be used in production!
|
| If you need to allow multiple domains, remember that this file is still
| a PHP script and you can easily do that on your own.
|
*/

$config['base_url'] = 'http://localhost/yourproject/';

1 Comment

Sir I tried your suggestion but it didn't work, I have updated my question can you please check it I think because of that error it is not working.
0

Use like this:

<div class="profile-pic-feadback" style="background-image:url(<?php echo base_url('images/user.png') ?>)"></div>

you have to used background-image. then it will work. I used this way only.

3 Comments

have you try to give this as inline css. I was having same error. so put all image css code in css file and only setting image url path code as a inline css. and it work me.
Can you give me the whole code. Are you sure image is stored into the same path
See i have updated my answered so you understand in more better way.
0

Here's the way:

html { 
     background-image: url('<?= base_url("assets/images/img.jpg");?>') no-repeat center center fixed; 
     -webkit-background-size: cover;
     -moz-background-size: cover;
     -o-background-size: cover;
      background-size: cover;
}

But I have to say that wolfgang1983's answer is a better practice. Avoid using CSS in your views.

Comments

0
html {

background-image: url('../assests/images/img.jpg') no-repeat center center fixed;

}

1 Comment

Rather than just pasting code should have some text explaining what needs to be done etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.