Home >Web Front-end >CSS Tutorial >How to introduce CSS styles in CI framework?
The introduction method of CSS styles in the CI framework requires specific code examples
CI (CodeIgniter) is a lightweight PHP framework that is widely used in Web applications development. In the CI framework, in order to make the web page have good style and layout, we need to introduce CSS styles. This article will introduce how to introduce CSS styles in the CI framework and provide specific code examples.
1. Directly introduce CSS styles into the view file
In the CI framework, the view file (View) is responsible for rendering the HTML page. We can introduce CSS styles directly into the view file to change the appearance and layout of the page. The following are the specific steps:
application/views
directory. Therefore, we need to create a new view file in this directory and name it example_view.php
. example_view.php
file, add the following code: <!DOCTYPE html> <html> <head> <title>示例页面</title> <link rel="stylesheet" type="text/css" href="<?php echo base_url('css/styles.css'); ?>"> </head> <body> <h1>这是一个示例页面</h1> <p>这是一段示例内容。</p> </body> </html>
In the above code, we used <link>
tag to introduce CSS style sheets. Among them, the value of the href
attribute is dynamically generated through the base_url()
function and points to the css/styles.css
file. It is assumed here that the CSS style files are stored in the css
folder in the root directory of the CI framework.
application/controllers
directory, such as Example.php
, and add the following code in the file: class Example extends CI_Controller { public function index() { $this->load->view('example_view'); } }
In the above code , the index()
method is used to load the example_view.php
view file.
http://localhost/ci/index.php/example
, you can see the CSS style Sample page. 2. Using the resource loader of the CI framework
In addition to directly introducing CSS styles into the view file, the CI framework also provides a resource loader (Loader) function. Used to load resources such as CSS style sheets and JavaScript files. The following are the specific steps:
application/config/autoload.php
file, find the following code and modify it to: $autoload['helper'] = array('url');
In the above code, we add the url
helper library to the automatically loaded helper list to use the base_url()
function in the view file.
<!DOCTYPE html> <html> <head> <title>示例页面</title> <?php echo link_tag('css/styles.css'); ?> </head> <body> <h1>这是一个示例页面</h1> <p>这是一段示例内容。</p> </body> </html>
In the above code, we use the link_tag()
function to dynamically generate the <link>
tag and load the css/styles.css
file.
Summary:
This article introduces two methods of introducing CSS styles into the CI framework: directly introducing CSS styles into the view file and using the resource loader of the CI framework. No matter which method is used, you can easily add styles and layouts to the pages of the CI framework. I hope this article will be helpful to you in dealing with CSS styles in CI framework.
The above is the detailed content of How to introduce CSS styles in CI framework?. For more information, please follow other related articles on the PHP Chinese website!