简体   繁体   中英

PHP Codeigniter wont load CSS-File

Good evening,

I started using Codeigniter as Framework for my newest projekt... and already got problems on the first page.

I want to link a CSS file to my site. It looks perfectly good in the Code. But just nothing happens.

<html>
<head>
    <title>Tec.Net</title>
    <?php 
    $this->load->helper('html');
    echo link_tag($data['css']);
    ?>
</head>
<body>
    <h1><?php echo $title ?></h1>

This is the code of my header.php

body {
background-image: url("application/views/images/Console Background.png");
color: white;
}

The standard.css

<html>
<head>
    <title>Tec.Net</title>
    <link href="localhost/TecNet/standard.css" rel="stylesheet" type="text/css" />
</head>

<body>
    <h1>Home</h1>
    <h2>this is a home site</h2>    <em>&copy; 2015</em>
</body>
</html>

And at last. The code i receive from Firefox. The Link is perfectly fine. If i try to access it directly from my browser it opens the css file. As inline CSS the code works perfect just that link refuses to work

EDIT: my URL Structure for the controller.

http://localhost/TecNet/index.php/tecnet/view

And the controller itself

<?php
class Tecnet extends CI_Controller {

 public function view($page = 'home')
 {
    if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php')) {
            // Whoops, we don't have a page for that!
            show_404();
        }

    $data['title'] = ucfirst($page); // Capitalize the first letter

    $this->load->view('templates/header', $data);
    $this->load->view('pages/'.$page, $data);

    $this->load->view('templates/footer', $data);
    }
}
?>

尝试像这样加载样式表:

    <link rel="stylesheet" href="<?= base_url('TecNet/standard.css') ?>">

since CodeIgniter is based on the Model-View-Controller development pattern. Better you put resoures of css, img, js, etc.. in root folder instead on view like you did:

background-image: url("application/views/images/Console Background.png");

assume structures:

/standard.css
/assets/img/console-background.png

your standart.css file:

body {
background-image: url("assets/img/console-background.png");
color: white;
}

easy to call in view:

<link rel="stylesheet" href="<?= base_url('standard.css') ?>">

I use assests a bit differently in CI, load with controller dynamically, but maybe this can work for you:

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

just replace path, so they will match to yours

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM