CodeIgniter - Linking an exernal css not working

被刻印的时光 ゝ 提交于 2020-01-06 07:59:12

问题


Here is the hierarchy of my files

Codeigniter/
   applications/
       assets/
           styles.css
       ...
       views/
           welcome.php

   system/
       ...

under my style.css is this code

p {
    color: #3333ff;
    background-color: #66ff66;
}

and under the welcome.php is this one.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>test</title>

    <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>assets/styles.css" />
</head>
<body>
     <p>test</p>
</body>
</html>

I set my base_url as

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

I cant understand why the css file isn't working

I also tried to change link to

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

but didn't work also


回答1:


I have a similar local setup (XAMPP) and I have the the following file hierarchy:

public_html_local/
   applications/

   assets/
      css/
          styles.css
          ...
      js/
          jquery.js
          ...
      images/
          banner.jpg
          ...

   system/
       ...

In my config file:

    $config['base_url'] = "http://localhost/public_html_local/"

and in my pages, I access my CSS files as follows:

<base href="<?php echo base_url(); ?>">
<link href="assets/css/reset.css" rel="stylesheet">
<link href="assets/css/styles.css" rel="stylesheet">

For me, I found it easier to keep my CSS files outside of the application folder.

In your case, you might expect the path to your CSS files to be:

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

However, if you try to access the CSS file from your local server using that URI, it probably won't work unless you changed the .htaccess file.




回答2:


1-If you are not using .htaccess and you did not removed your index.php file than put index.php inside the path to you css file.

2-If you used .htaccess and removed your index.php than put your css folder name inside your .htaccess file that way it should be accessable. and my links are working fine here is and example:

<link rel="stylesheet" href="<?=base_url()?>bootstrap/css/bootstrap.min.css" />



回答3:


You have to define url in you constant.php paste following code in public_html_local/application/config/constants.php

define('URL','http://YOUR-HOST/APPLICATION-FOLDER/');

define('CSS',URL.'assets/css/');//make a new folder named as assets if you want css sources

now simply use following to use stylesheet.

<link rel="stylesheet" href="<?php echo(CSS.'simplereflect.css'); ?>">


来源:https://stackoverflow.com/questions/17384989/codeigniter-linking-an-exernal-css-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!