Home >Web Front-end >CSS Tutorial >Step-by-step guide: Applying CSS styles in Yii framework
In website development, CSS style is an essential part, it can add color to the website and improve user experience. As an excellent PHP framework, the Yii framework also supports the use of CSS styles. This article will teach you step by step how to use CSS styles in the Yii framework.
First, we need to introduce CSS styles into the view file. Normally, we store CSS files in a separate folder (such as web/css/), here is an example.
In the view file (usually a .php file), we can use the Yii::app() method to introduce it CSS file, the code is as follows:
Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/css/style.css');
Among them, Yii::app()->baseUrl
obtains the absolute path of the website root directory (for example, http://www.example .com/
). Here we connect it with the relative path of the CSS file /css/style.css
, so that we can accurately point to the CSS file. It should be noted that Yii::app()
here represents the current Yii application.
If you want to introduce a CSS file into the controller, you can use code similar to the following:
Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl.'/css/style.css');
In the file After the introduction is successful, we need to write the style in the CSS file. Here is a simple example:
/* style.css */ h1 { color: red; } p { font-size: 14px; }
Two styles are defined here: the text color of the h1 element is red, and the font size of the p element is 14 pixels.
The last step, we need to use these styles in the view file. Typically, you add a class attribute to a specified element and then define the style of this class in a CSS file. For example:
/* style.css */ .cool-h1 { color: blue; } /* view.php */ <h1 class="cool-h1">Hello World!</h1> <p>这是一个 Yii 网站</p>
Here we define a class named "cool-h1". In the view file, we add it to the <h1></h1>
element, so that The styles defined in the CSS file are applied.
Based on the above, we can get a complete sample code:
/* style.css */ h1 { color: red; } .cool-h1 { color: blue; } /* view.php */Hello World!
这是一个 Yii 网站
In the second line, use Yii::app()
to introduce the CSS file, and in the 7th line, use Class name .cool-h1
controls the style of the <h1></h1>
element.
Summary:
This article introduces the steps to use CSS styles in the Yii framework, including referencing CSS files, defining styles in CSS files, and using styles in view files. I hope it can help developers in need.
The above is the detailed content of Step-by-step guide: Applying CSS styles in Yii framework. For more information, please follow other related articles on the PHP Chinese website!