1

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);
    }
}
?>
1
  • Load your helpers in your autoload.php file (in config folder) or in the controller. not in the view. @ichadhr answer will solve your dilemma. Commented Aug 20, 2015 at 14:51

3 Answers 3

1

Try to load your style sheet like this:

    <link rel="stylesheet" href="<?= base_url('TecNet/standard.css') ?>">
Sign up to request clarification or add additional context in comments.

8 Comments

didn't work. It gave me the same link. Thanks anyway
have you removed 'index.php from your url? If so, have you also added are rewrite rule for *css? Your css files should be in CI project root as well.
I added a route to the default controller $route['default_controller'] = 'tecnet/view'; but i didn't add a rewrite rule. The file is in my document root. Alongside with the index.php. Not under /system.
can you edit your question and add an example of your url structure?
@b_dubb please read this carefully even PHP ini file disabled "short tags" CodeIgniter will optionally rewrite short tags on-the-fly.
|
1

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') ?>">

Comments

0

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

Comments

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.