How to Load CSS File in Codeigniter

How to load css in codeigniter

The function base_url() should return the base path (without index.php)

You may fix it by adding a backslash like:

<link rel="stylesheet" type="text/css" href="<? echo base_url();?>/css/all.css">

or remove the index.php from your config:

$config['base_url'] = 'http://webscarlets.com/ci/';

How can I load css or js file in codeigniter 4?

As app is sibling folder of public folder,

And as per codeIgniter 4 .

anything you put in public folder can be accessed directly.

so it should be something like below to get css applied to the view.

In views folder lets say there's a file as index.php

then code should be like below:

index.php

    <link rel="shortcut icon" type="image/png" href="/css/style.css"/>

How can I include css and js file in codeigniter

As far as i can see, you din't declared $config['css'] in your config file, so it's normal to get undefined variable error for css. But you shouldn't have problem with js.

When declaring your base_url use trailing slash at the end (eg. "http://localhost/fancysite/")

Also you can use CI's url helper to use functions like base_url() or site_url() and many more. (as @Likee suggested).

config.php

// this should be the first variable in CI's config.php
$config['base_url'] = "http://localhost/ASOFT/Projects/CI_search/";

// many more lines with other configuration variables
// ..................................................
// ..................................................
// ..................................................

// your own configuration variables at the end of file
$config['css'] = 'css/';
$config['js'] = 'assets/css/';
$config['image'] = 'images/';

controller

public function index() {

// loading url helper to use base_url() function in the view,
// if you load this helper in autoload.php you don't need to load it here again
$this->load->helper('url');

// if you didn't declare data as class property
// you can simply use
// $data = array(
// 'css' => $this->config->item('css'),
// 'js' => $this->config->item('js'),
// 'image'=>$this->config->item('image')
// );

$this->data = array(
'css' => $this->config->item('css'),
'js' => $this->config->item('js'),
'image'=>$this->config->item('image')
);

// you really don't need the line below
// $data = $this->data;
$this->data['error'] = '';

$this->load->view('index',$this->data);
}

view

<link rel="stylesheet" href="<?php echo base_url($css . 'style.css'); ?>">
<link rel="stylesheet" href="<?php echo base_url($css . 'bootstrap.min.css'); ?>">
<script src="<?php echo base_url($js . 'jquery.min.js'); ?>"></script>
<!-- do you really need to include jquery.js while you included jquery.min.js above? but here we go :) -->
<script src="<?php echo base_url($js . 'jquery.js'); ?>"></script>
<script src="<?php echo base_url($js . 'bootstrap.min.js'); ?>"></script>

Probably you will need to use url helper a lot. So you can autoload it in autoload.php file in config folder. it must be somewhere around line 90

$autoload['helper'] = array('url');

CSS File is not loading in my CodeIgniter PHP

Open your browser console by right clicking on your web page and select inspect.

Go to your network requests / console and check what URL you get for your linked files (CSS).

If you find out that the path you have given in your application if different then you need to change your code accordingly to get the appropriate path.

How can I include the CSS file in CodeIgniter?

define general css that used.
Open "config.php" within directory CodeIgniter\system\application\config.

$config['css'] = 'mystyles.css';

Create a file named mystyles.css within root application: \CodeIgniter.

$this->load->helper('url');       
$data['css'] = $this->config->item('css');

You can load css in views pages like this.

CodeIgniter - css file doesn't work

Put your asset folder in project root, out side application folder.

Change the link to,

<?php echo link_tag('assets/css/style.css')?>

CI Folder Structure

Sample Image

External css file not loading in Codeigniter view

For example your project name is "ProjectName",
Add folder assets and place css file inside of it.

Here in config change base_url

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

After that in view use this way

<link rel="stylesheet" type="text/css" href="<?php echo base_url('assets/testcss.css');?>">

I hope it will work for you.

If feel any issue please let me know. Thank you



Related Topics



Leave a reply



Submit