How to load external CSS via Codeigniter?

Asked

Viewed 583 times

3

I need to call this sheet external style, but it does not load at all, I saw a similar question here, but I did not notice a conclusive answer, since I am using Codeigniter 3 below.

The error that appears is this:

Failed to load Resource: the server responded with a status of 404 (Not Found)

How could I solve this problem?

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="utf-8"/>
    <title>RR Telecon</title>
    <link rel="shortcut icon" href="images/favicon.ico" />
    <link rel="icon" href="images/favicon.ico" type="image/x-icon" />
    <link href="<?php echo base_url(); ?>/assets/css/bootstrap-theme.css" rel="stylesheet" type="text/css"/>
    <link href="<?php echo base_url(); ?>/assets/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css"/>
    <link href="<?php echo base_url(); ?>/assets/css/bootstrap.css" rel="stylesheet" type="text/css"/>
    <link href="<?php echo base_url(); ?>/assets/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
    <link href="<?php echo base_url(); ?>assets/css/home.css" rel="stylesheet" type="text/css"/>
</head>
<body>
    <section class="reader">
        teste
    </section>
</body>
</html>
  • 1

    This is the link, but you didn’t resolve me no http://answall.com/questions/102191/como-inser-css-externo-no-codeigniter-3

  • Open the Inspector de elemento from your browser, go to Console and refresh the page, see if an error appears. If it appears, post here to try to help you.

  • Alisson Acioli updated the error that appears, is at the bottom of the question

  • Actually it’s just the home.css that isn’t loading or are all?

  • Everyone, it doesn’t load, it’s some rule in the new version of codeigniter I think, and from what I’ve read too, but I still can’t solve.

  • try like this <link href="<?php echo base_url('assets/css/bootstrap-theme.css'); ?>" rel="stylesheet" type="text/css"/>

Show 1 more comment

2 answers

1

Error 404 means that it is not finding the file in the specified location. In the browser console, on which line is the error? From what I saw there, there is a bar (/) missing after base_url when you call home.php. This may be your problem. replace by the code below and test.

<link href="<?php echo base_url(); ?>/assets/css/bootstrap-theme.css" rel="stylesheet" type="text/css"/>
<link href="<?php echo base_url(); ?>/assets/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css"/>
<link href="<?php echo base_url(); ?>/assets/css/bootstrap.css" rel="stylesheet" type="text/css"/>
<link href="<?php echo base_url(); ?>/assets/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<link href="<?php echo base_url(); ?>/assets/css/home.css" rel="stylesheet" type="text/css"/>

But you would have to see if your base_url has the bar (/) at the end. If you do, you will need to remove all the bars when you place the link. Staying that way:

    <link href="<?php echo base_url(); ?>assets/css/bootstrap-theme.css" rel="stylesheet" type="text/css"/>
<link href="<?php echo base_url(); ?>assets/css/bootstrap-theme.min.css" rel="stylesheet" type="text/css"/>
<link href="<?php echo base_url(); ?>assets/css/bootstrap.css" rel="stylesheet" type="text/css"/>
<link href="<?php echo base_url(); ?>assets/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<link href="<?php echo base_url(); ?>assets/css/home.css" rel="stylesheet" type="text/css"/>

Test both ways and see what happens :)

0

Use the directory inside the function base_url(), and to shorten the call a little more, use <?= and ?> in place of <?php echo and ; ?>.

Example:

<link rel="stylesheet" type="text/css" href="<?=base_url('assets/css/bootstrap-theme.css')?>">
<link rel="stylesheet" type="text/css" href="<?=base_url('assets/css/bootstrap-theme.min.css')?>">
<link rel="stylesheet" type="text/css" href="<?=base_url('assets/css/bootstrap.css')?>">
<link rel="stylesheet" type="text/css" href="<?=base_url('assets/css/bootstrap.min.css')?>">
<link rel="stylesheet" type="text/css" href="<?=base_url('assets/css/home.css')?>">

See how the code got cleaner and nicer to read.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.