文章列表


magento2中的创建响应式移动主题以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在 Magento 2 中,您可以创建响应式移动主题,以便您的在线商店可以在移动设备上优雅地呈现。以下是在 Magento 2 中创建响应式移动主题的一些步骤和示例代码:</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">创建一个新的主题目录。在 Magento 2 中,主题通常存储在 app/design/frontend/&lt;Vendor&gt;/&lt;Theme&gt; 目录中。在该目录下创建一个新的子目录,例如 mobile。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在 mobile 目录中,创建一个名为 theme.xml 的文件。该文件应包含以下内容:</p><pre class="brush:as3;toolbar:false">&lt;theme&nbsp;xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&nbsp;xsi:noNamespaceSchemaLocation=&quot;urn:magento:framework:Config/etc/theme.xsd&quot;&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;Mobile&nbsp;Theme&lt;/title&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;parent&gt;Magento/blank&lt;/parent&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;media&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;preview_image&gt;media/preview.jpg&lt;/preview_image&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;/media&gt; &lt;/theme&gt;</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">上述 theme.xml 文件中,您需要定义主题的标题,指定父主题和指定预览图像的路径。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在 mobile 目录中,创建一个名为 registration.php 的文件。该文件应包含以下内容:</p><pre class="brush:as3;toolbar:false">use&nbsp;\Magento\Framework\Component\ComponentRegistrar; ComponentRegistrar::register( &nbsp;&nbsp;&nbsp;&nbsp;ComponentRegistrar::THEME, &nbsp;&nbsp;&nbsp;&nbsp;&#39;frontend/&lt;Vendor&gt;/mobile&#39;, &nbsp;&nbsp;&nbsp;&nbsp;__DIR__ );</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">这将注册您的主题。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在 mobile 目录中,创建一个名为 web 的子目录。在 web 目录中,创建一个名为 css 的子目录,并在其中创建一个名为 source 的子目录。在 source 目录中,您可以创建 CSS 文件,并使用响应式设计技术将其适应于不同的屏幕大小。例如,以下是一个名为 styles.css 的示例文件,它包含了一些简单的 CSS 样式:</p><pre class="brush:as3;toolbar:false">@media&nbsp;(max-width:&nbsp;767px)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Mobile-specific&nbsp;styles&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp;body&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;font-size:&nbsp;14px; &nbsp;&nbsp;&nbsp;&nbsp;} } @media&nbsp;(min-width:&nbsp;768px)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Tablet-specific&nbsp;styles&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp;body&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;font-size:&nbsp;16px; &nbsp;&nbsp;&nbsp;&nbsp;} } @media&nbsp;(min-width:&nbsp;992px)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Desktop-specific&nbsp;styles&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp;body&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;font-size:&nbsp;18px; &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在上述示例中,@media 声明用于指定屏幕大小范围,并在其中定义适用于不同设备的不同样式。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">最后,清除 Magento 的缓存,然后在后台选择您的新主题。您可以在 Magento 后台的“内容”&gt;“配置”&gt;“主题”部分中找到此选项。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">这就是在 Magento 2 中创建响应式移动主题的简单示例。通过适当地定义 CSS 样式和使用响应式设计技术,您可以轻松地为移动设备创建美观的主题。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p><br/></p>

magento2中的响应式网页设计中的JavaScript以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在 Magento 2 中,响应式网页设计中的 JavaScript 主要用于处理响应式布局,例如屏幕大小更改时调整页面元素的大小和位置。以下是在 Magento 2 中使用 JavaScript 的一些示例代码:</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">使主菜单在小屏幕上可见:在 Magento 2 中,主菜单默认隐藏在小屏幕上,以便节省空间。但是,您可以使用 JavaScript 让菜单在小屏幕上可见。以下是示例代码:</p><pre class="brush:as3;toolbar:false">require([ &nbsp;&nbsp;&nbsp;&nbsp;&#39;jquery&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&#39;domReady!&#39; ],&nbsp;function($)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$(document).ready(function&nbsp;()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(&#39;.nav-toggle&#39;).click(function&nbsp;()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(&#39;.nav-items&#39;).toggleClass(&#39;active&#39;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;}); });</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">上述示例中,nav-toggle 类指定了菜单开关按钮,nav-items 类指定了菜单元素。当按钮被单击时,active 类将在菜单元素上进行切换,从而使其可见或隐藏。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">调整页面元素的大小和位置:在响应式设计中,页面元素的大小和位置可能需要在不同的屏幕大小下进行调整。以下是示例代码:</p><pre class="brush:as3;toolbar:false">require([ &nbsp;&nbsp;&nbsp;&nbsp;&#39;jquery&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&#39;domReady!&#39; ],&nbsp;function($)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$(window).resize(function()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;windowWidth&nbsp;=&nbsp;$(window).width(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(windowWidth&nbsp;&lt;&nbsp;768)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(&#39;.page-title&#39;).insertBefore(&#39;.page-main&#39;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(&#39;.page-title&#39;).insertAfter(&#39;.page-breadcrumbs&#39;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}); });</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">上述示例中,resize 事件指定了在窗口大小更改时应执行的函数。在此示例中,当窗口宽度小于 768 像素时,页面标题将移到页面主要部分的前面,否则它将移到页面面包屑的后面。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;"><span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;PingFang SC&quot;;">这只是</span> Magento 2 <span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;PingFang SC&quot;;">中使用</span> JavaScript <span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;PingFang SC&quot;;">的一些简单示例。通过正确使用</span> JavaScript<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;PingFang SC&quot;;">,您可以使您的</span> Magento 2 <span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;PingFang SC&quot;;">网站更具交互性和响应性。</span></p><p><br/></p>

magento2中的响应式设计中的CSS以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在 Magento 2 中,响应式设计需要使用 CSS 来实现不同设备的布局和样式。以下是一些响应式设计中使用的 CSS 代码示例:</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">媒体查询:使用媒体查询可以根据不同设备的屏幕尺寸来设置不同的 CSS 样式。例如:</p><pre class="brush:as3;toolbar:false">/*&nbsp;对于屏幕宽度小于&nbsp;768&nbsp;像素的设备,设置导航栏为垂直布局&nbsp;*/ @media&nbsp;(max-width:&nbsp;768px)&nbsp;{ &nbsp;&nbsp;.navbar&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;flex-direction:&nbsp;column; &nbsp;&nbsp;} } 弹性盒子布局(Flexbox&nbsp;Layout):使用弹性盒子布局可以方便地实现响应式布局。例如: /*&nbsp;将容器设置为弹性盒子,并使其内部的项目水平居中&nbsp;*/ .container&nbsp;{ &nbsp;&nbsp;display:&nbsp;flex; &nbsp;&nbsp;justify-content:&nbsp;center; &nbsp;&nbsp;align-items:&nbsp;center; } 栅格系统(Grid&nbsp;System):栅格系统可以帮助我们将页面分成多个网格,并将内容放入这些网格中。例如: /*&nbsp;将页面分成三个网格&nbsp;*/ .container&nbsp;{ &nbsp;&nbsp;display:&nbsp;grid; &nbsp;&nbsp;grid-template-columns:&nbsp;repeat(3,&nbsp;1fr); } /*&nbsp;在第一个网格中放置一个图片&nbsp;*/ .image&nbsp;{ &nbsp;&nbsp;grid-column:&nbsp;1; } /*&nbsp;在第二个网格中放置一个标题和一些文本&nbsp;*/ .title&nbsp;{ &nbsp;&nbsp;grid-column:&nbsp;2; &nbsp;&nbsp;grid-row:&nbsp;1; } .text&nbsp;{ &nbsp;&nbsp;grid-column:&nbsp;2; &nbsp;&nbsp;grid-row:&nbsp;2; } /*&nbsp;在第三个网格中放置一个按钮&nbsp;*/ .button&nbsp;{ &nbsp;&nbsp;grid-column:&nbsp;3; }</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">以上是一些常见的响应式设计中使用的 CSS 代码示例。通过使用这些代码,可以实现在不同设备上呈现不同的布局和样式,提高用户体验和页面响应性。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p><br/></p>

magento2中的添加自定义 CSS 预处理器以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在 Magento 2 中添加自定义 CSS 预处理器可以帮助您更高效地管理和开发样式表。以下是使用自定义 CSS 预处理器的代码示例:</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">创建自定义 CSS 预处理器</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在 Magento 2 根目录中创建一个名为 app/design/frontend/&lt;Vendor&gt;/&lt;Theme&gt;/web/css/source/_custom.less 的文件,并添加以下代码:</p><pre class="brush:as3;toolbar:false">@primary-color:&nbsp;#007bff; body&nbsp;{ &nbsp;&nbsp;background-color:&nbsp;#f8f8f8; &nbsp;&nbsp;color:&nbsp;#333; } h1,&nbsp;h2,&nbsp;h3&nbsp;{ &nbsp;&nbsp;color:&nbsp;@primary-color; }</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">这个代码定义了一个名为 @primary-color 的 LESS 变量,并在样式表中使用它来设置标题的颜色。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">配置 Magento 2</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在 Magento 2 根目录中打开 app/design/frontend/&lt;Vendor&gt;/&lt;Theme&gt;/requirejs-config.js 文件,并添加以下代码:</p><pre class="brush:as3;toolbar:false">var&nbsp;config&nbsp;=&nbsp;{ &nbsp;&nbsp;map:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&#39;*&#39;:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;custom:&nbsp;&#39;js/custom&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;less:&nbsp;&#39;js/less&#39; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;}, &nbsp;&nbsp;paths:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;less:&nbsp;&#39;js/less&#39;, &nbsp;&nbsp;&nbsp;&nbsp;lessc:&nbsp;&#39;js/lessc.min&#39;, &nbsp;&nbsp;&nbsp;&nbsp;lessOpts:&nbsp;&#39;js/lessOpts&#39; &nbsp;&nbsp;}, &nbsp;&nbsp;shim:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;lessc:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exports:&nbsp;&#39;less&#39; &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;lessOpts:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deps:&nbsp;[&#39;lessc&#39;] &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;}, &nbsp;&nbsp;config:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;mixins:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;mage/apply/main&#39;:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;js/apply-mixin&#39;:&nbsp;true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;} };</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">这个代码将 less 和 lessc 库添加到 Magento 2 的 JavaScript 依赖项中,并配置了 apply-mixin。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">创建 LESS 编译器</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在 Magento 2 根目录中创建一个名为 app/design/frontend/&lt;Vendor&gt;/&lt;Theme&gt;/web/js/less.js 的文件,并添加以下代码:</p><pre class="brush:as3;toolbar:false">define([ &nbsp;&nbsp;&#39;jquery&#39;, &nbsp;&nbsp;&#39;lessc&#39;, &nbsp;&nbsp;&#39;lessOpts&#39; ],&nbsp;function($,&nbsp;lessc,&nbsp;lessOpts)&nbsp;{ &nbsp;&nbsp;&#39;use&nbsp;strict&#39;; &nbsp;&nbsp;$.widget(&#39;custom.less&#39;,&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;options:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lessUrl:&nbsp;&#39;/&lt;Vendor&gt;/&lt;Theme&gt;/en_US/css/custom.less&#39; &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;_create:&nbsp;function()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;self&nbsp;=&nbsp;this; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;lessUrl&nbsp;=&nbsp;this.options.lessUrl; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$.ajax({ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;url:&nbsp;lessUrl, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataType:&nbsp;&#39;text&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;success:&nbsp;function(data)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;lessc.render(data,&nbsp;lessOpts,&nbsp;function(css)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(&#39;head&#39;).append(&#39;&lt;style&gt;&#39;&nbsp;+&nbsp;css&nbsp;+&nbsp;&#39;&lt;/style&gt;&#39;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;}); &nbsp;&nbsp;return&nbsp;$.custom.less; });</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">这个代码定义了一个名为 less 的 JavaScript 模块,并使用它来加载、编译和注入自定义 LESS 样式表。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">添加 apply-mixin</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在 Magento 2 根目录中创建一个名为 app/design/frontend/&lt;Vendor&gt;/&lt;Theme&gt;/web/js/apply-mixin.js 的文件,并添加以下代码:</p><pre class="brush:as3;toolbar:false">define([ &nbsp;&nbsp;&#39;jquery&#39;, &nbsp;&nbsp;&#39;mage/apply/main&#39; ],&nbsp;function($,&nbsp;mageApply)&nbsp;{ &nbsp;&nbsp;&#39;use&nbsp;strict&#39;; &nbsp;&nbsp;return&nbsp;function(target)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;$.widget(&#39;mage.apply&#39;,&nbsp;target,&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_loadFiles:&nbsp;function(files,&nbsp;callback)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;lessFile&nbsp;=&nbsp;files.filter(function(file)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;file.indexOf(&#39;custom.less&#39;)&nbsp;!==&nbsp;-1; &nbsp;&nbsp;&nbsp;&nbsp;})[0]; &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(lessFile)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require([&#39;less&#39;],&nbsp;function(less)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require([&#39;lessOpts&#39;],&nbsp;function(lessOpts)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;less.render(&#39;@import&nbsp;&quot;&#39;&nbsp;+&nbsp;lessFile&nbsp;+&nbsp;&#39;&quot;;&#39;,&nbsp;lessOpts,&nbsp;function(css)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$(&#39;head&#39;).append(&#39;&lt;style&gt;&#39;&nbsp;+&nbsp;css.css&nbsp;+&nbsp;&#39;&lt;/style&gt;&#39;); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;callback(files); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}); &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;this._super(files,&nbsp;callback); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;} }); return&nbsp;target; }; });</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">这个代码添加了一个名为 `apply-mixin` 的 Magento 2 mixins,并使用它来加载、编译和注入自定义 LESS 样式表。</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">5. 保存文件并清除缓存</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">保存文件后,需要清除 Magento 2 的缓存,以便应用更改。可以通过在命令行中运行以下命令来清除缓存:</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">php bin/magento cache:clean</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Copy code</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;PingFang SC&quot;; white-space: normal;">现在,您可以使用自定义<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;"> CSS </span>预处理器来开发<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;"> Magento 2 </span>主题。</p><p><br/></p>

magento2中的CSS 关键路径以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>Magento 2 的 CSS 关键路径通常包括以下文件:</p><p><br/></p><ol class=" list-paddingleft-2" style="list-style-type: decimal;"><li><p>lib/web/css/source/lib/variables/_colors.less:定义了页面上使用的颜色变量。</p></li><li><p>lib/web/css/source/lib/variables/_responsive.less:定义了响应式设计所使用的媒体查询。</p></li><li><p>lib/web/css/source/lib/variables/_typography.less:定义了页面上使用的字体变量。</p></li><li><p>lib/web/css/source/lib/_responsive.less:定义了响应式设计的样式。</p></li><li><p>lib/web/css/source/_module.less:定义了 Magento 2 的基础样式和布局。</p></li><li><p>lib/web/css/source/_theme.less:定义了主题特定的样式。</p></li></ol><p>下面是一个简单的 Magento 2 CSS 代码示例,其中包含了一些常用的样式:</p><p><br/></p><p><br/></p><pre class="brush:as3;toolbar:false">//定义主题特定的颜色 @color-primary:&nbsp;#007bff; @color-secondary:&nbsp;#6c757d; //定义字体大小 @font-size-base:&nbsp;16px; //定义响应式设计媒体查询 @media&nbsp;(min-width:&nbsp;768px)&nbsp;{ //&nbsp;样式 } //定义页面元素的基本样式 body&nbsp;{ font-family:&nbsp;Arial,&nbsp;sans-serif; font-size:&nbsp;@font-size-base; color:&nbsp;#333; } h1&nbsp;{ font-size:&nbsp;2.5rem; color:&nbsp;@color-primary; } p&nbsp;{ font-size:&nbsp;1.6rem; line-height:&nbsp;1.4; } //定义按钮样式 .button&nbsp;{ display:&nbsp;inline-block; font-size:&nbsp;1.4rem; padding:&nbsp;0.8rem&nbsp;1.6rem; border:&nbsp;none; border-radius:&nbsp;4px; background-color:&nbsp;@color-primary; color:&nbsp;#fff; } //定义链接样式 a&nbsp;{ color:&nbsp;@color-primary; text-decoration:&nbsp;none; &amp;:hover&nbsp;{ color:&nbsp;@color-secondary; text-decoration:&nbsp;underline; } }</pre><p>注意,这只是一个简单的示例,Magento 2 的 CSS 文件包含了大量的样式和变量,实际使用时需要根据具体的需求进行定制。</p><p><br/></p>

magento2中的jQuery UI 样式以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>Magento 2 使用 jQuery UI 提供了一些基础的 UI 样式,包括按钮、表单、对话框等。</p><p>下面是一个简单的 Magento 2 jQuery UI 代码示例,其中包含了一些常用的 UI 组件:</p><pre class="brush:as3;toolbar:false">&lt;div&nbsp;class=&quot;ui-widget&quot;&gt; &lt;label&nbsp;for=&quot;datepicker&quot;&gt;选择日期:&lt;/label&gt; &lt;input&nbsp;type=&quot;text&quot;&nbsp;id=&quot;datepicker&quot;&gt; &lt;/div&gt; &lt;button&nbsp;id=&quot;btn-submit&quot;&nbsp;class=&quot;ui-button&nbsp;ui-widget&nbsp;ui-corner-all&quot;&gt;提交&lt;/button&gt; &lt;div&nbsp;id=&quot;dialog-confirm&quot;&nbsp;title=&quot;确认删除?&quot;&gt; &lt;p&gt;&lt;span&nbsp;class=&quot;ui-icon&nbsp;ui-icon-alert&quot;&nbsp;style=&quot;float:left;&nbsp;margin:0&nbsp;7px&nbsp;20px&nbsp;0;&quot;&gt;&lt;/span&gt;确认删除此项?&lt;/p&gt; &lt;/div&gt;</pre><p>上述代码使用了 jQuery UI 提供的日期选择器、按钮和对话框组件。需要注意的是,要正确引入 jQuery UI 样式,需要在 Magento 2 的布局文件中加入以下代码:</p><pre class="brush:as3;toolbar:false">&lt;head&gt; &lt;css&nbsp;src=&quot;Magento_Theme::jquery-ui.css&quot;/&gt; &lt;/head&gt;</pre><p>这样才能正确加载 jQuery UI 样式。</p><p>另外,如果需要对 jQuery UI 组件进行定制,可以参考 Magento 2 的源代码中 lib/web/jquery-ui.theme.css 和 lib/web/jquery-ui.structure.css 文件,这些文件包含了 jQuery UI 的基础样式和变量。</p><p><br/></p><p><br/></p>

magento2中的界面库以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p>Magento 2 使用了 Bootstrap 作为界面库,提供了一些常用的 UI 组件,如网格系统、表单、按钮等。</p><p>下面是一个简单的 Magento 2 Bootstrap 代码示例,其中包含了一些常用的组件:</p><pre class="brush:as3;toolbar:false">&lt;div&nbsp;class=&quot;container&quot;&gt; &lt;div&nbsp;class=&quot;row&quot;&gt; &lt;div&nbsp;class=&quot;col-md-6&quot;&gt; &lt;form&gt; &lt;div&nbsp;class=&quot;form-group&quot;&gt; &lt;label&nbsp;for=&quot;input-name&quot;&gt;姓名:&lt;/label&gt; &lt;input&nbsp;type=&quot;text&quot;&nbsp;class=&quot;form-control&quot;&nbsp;id=&quot;input-name&quot;&gt; &lt;/div&gt; &lt;div&nbsp;class=&quot;form-group&quot;&gt; &lt;label&nbsp;for=&quot;input-email&quot;&gt;邮箱:&lt;/label&gt; &lt;input&nbsp;type=&quot;email&quot;&nbsp;class=&quot;form-control&quot;&nbsp;id=&quot;input-email&quot;&gt; &lt;/div&gt; &lt;button&nbsp;type=&quot;submit&quot;&nbsp;class=&quot;btn&nbsp;btn-primary&quot;&gt;提交&lt;/button&gt; &lt;/form&gt; &lt;/div&gt; &lt;div&nbsp;class=&quot;col-md-6&quot;&gt; &lt;img&nbsp;src=&quot;example.jpg&quot;&nbsp;alt=&quot;Example&nbsp;image&quot;&nbsp;class=&quot;img-thumbnail&quot;&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;</pre><p>上述代码使用了 Bootstrap 的网格系统、表单和按钮组件。需要注意的是,在 Magento 2 的布局文件中,需要引入 Bootstrap 的 CSS 和 JavaScript 文件:</p><pre class="brush:as3;toolbar:false">&lt;head&gt; &lt;css&nbsp;src=&quot;Magento_Theme::css/bootstrap.min.css&quot;/&gt; &lt;css&nbsp;src=&quot;Magento_Theme::css/bootstrap-theme.min.css&quot;/&gt; &lt;script&nbsp;src=&quot;Magento_Theme::js/bootstrap.min.js&quot;/&gt; &lt;/head&gt;</pre><p>这样才能正确加载 Bootstrap 的样式和脚本。</p><p>另外,如果需要对 Bootstrap 组件进行定制,可以参考 Magento 2 的源代码中 lib/web/css/source/lib/bootstrap 文件夹,这些文件包含了 Bootstrap 的基础样式和变量。</p><p><br/></p>

magento2中的用 Grunt 编译 LESS以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p><span style="font-family: &quot;Helvetica Neue&quot;; font-size: 15px;">在 Magento 2 中,Grunt 是一个常用的任务运行器,用于执行一些常见的前端开发任务,例如编译 LESS 文件、压缩 JavaScript 文件等。下面是一个简单的 Magento 2 Grunt 编译 LESS 文件的代码示例:</span><br/></p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">首先,需要安装 Grunt 和相关插件。可以使用以下命令安装:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><pre class="brush:as3;toolbar:false"> npm&nbsp;install&nbsp;-g&nbsp;grunt-cli npm&nbsp;install&nbsp;grunt&nbsp;grunt-contrib-less&nbsp;grunt-contrib-watch&nbsp;--save-dev</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">接下来,在 Magento 2 的主题文件夹中创建一个 Gruntfile.js 文件,并添加以下代码:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">javascript</p><pre class="brush:as3;toolbar:false">module.exports&nbsp;=&nbsp;function(grunt)&nbsp;{ &nbsp;&nbsp;grunt.initConfig({ &nbsp;&nbsp;&nbsp;&nbsp;less:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;development:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;compress:&nbsp;true, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strictMath:&nbsp;true, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sourceMap:&nbsp;true, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;outputSourceFiles:&nbsp;true, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sourceMapURL:&nbsp;&#39;styles.css.map&#39;, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sourceMapFilename:&nbsp;&#39;css/styles.css.map&#39; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;files:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;css/styles.css&#39;:&nbsp;&#39;less/styles.less&#39; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;watch:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;less:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;files:&nbsp;[&#39;less/**/*.less&#39;], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks:&nbsp;[&#39;less&#39;] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;}); &nbsp;&nbsp;grunt.loadNpmTasks(&#39;grunt-contrib-less&#39;); &nbsp;&nbsp;grunt.loadNpmTasks(&#39;grunt-contrib-watch&#39;); &nbsp;&nbsp;grunt.registerTask(&#39;default&#39;,&nbsp;[&#39;less&#39;,&nbsp;&#39;watch&#39;]); };</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">上述代码定义了一个 less 任务和一个 watch 任务。less 任务用于编译 LESS 文件,并生成对应的 CSS 文件和 Source Map 文件。watch 任务用于监视 LESS 文件的变化,并在文件变化时重新编译 LESS 文件。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在上述代码中,LESS 文件位于 less/styles.less,编译后生成的 CSS 文件位于 css/styles.css,Source Map 文件位于 css/styles.css.map。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">最后,在终端中进入 Magento 2 的主题文件夹,运行以下命令即可开始监听 LESS 文件的变化并实时编译:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Copy code</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">grunt</p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">需要注意的是,如果 Magento 2 使用了缓存,可能需要在每次修改 LESS 文件后清除缓存才能看到效果。可以使用以下命令清除缓存:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><pre class="brush:as3;toolbar:false">php&nbsp;bin/magento&nbsp;cache:clean</pre><p><br/></p>

magento2中的CSS 和 LESS 预处理以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在 Magento 2 中,可以使用 CSS 和 LESS 两种方式来编写样式。LESS 是一种 CSS 预处理器,它提供了一些额外的功能,例如变量、嵌套、混合等,可以使样式的编写更加灵活和高效。下面是一个简单的 Magento 2 CSS 和 LESS 预处理的代码示例:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在 Magento 2 中,可以在主题文件夹中的 web/css/source 文件夹下创建 LESS 文件,例如 styles.less,并在该文件中编写样式,例如:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;"><br/></p><pre class="brush:as3;toolbar:false">@primary-color:&nbsp;#007bff; .page-title&nbsp;{ &nbsp;&nbsp;font-size:&nbsp;24px; &nbsp;&nbsp;color:&nbsp;@primary-color; } .btn-primary&nbsp;{ &nbsp;&nbsp;background-color:&nbsp;@primary-color; &nbsp;&nbsp;border-color:&nbsp;@primary-color; }</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在上述代码中,定义了一个变量 @primary-color,并在 .page-title 和 .btn-primary 选择器中使用了该变量。这样,如果需要修改主题的主色调,只需要修改 @primary-color 变量的值即可,所有使用该变量的样式都会自动更新。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">Magento 2 使用 Grunt 作为任务运行器来编译 LESS 文件,可以在主题文件夹中的 Gruntfile.js 文件中配置 Grunt 任务。下面是一个简单的 Grunt 配置示例:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;"><br/></p><pre class="brush:as3;toolbar:false">module.exports&nbsp;=&nbsp;function(grunt)&nbsp;{ &nbsp;&nbsp;grunt.initConfig({ &nbsp;&nbsp;&nbsp;&nbsp;less:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paths:&nbsp;[&#39;web/css/source&#39;], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strictMath:&nbsp;true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;files:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#39;web/css/styles.css&#39;:&nbsp;&#39;web/css/source/styles.less&#39; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}, &nbsp;&nbsp;&nbsp;&nbsp;watch:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;less:&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;files:&nbsp;[&#39;web/css/source/**/*.less&#39;], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tasks:&nbsp;[&#39;less&#39;] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;}); &nbsp;&nbsp;grunt.loadNpmTasks(&#39;grunt-contrib-less&#39;); &nbsp;&nbsp;grunt.loadNpmTasks(&#39;grunt-contrib-watch&#39;); &nbsp;&nbsp;grunt.registerTask(&#39;default&#39;,&nbsp;[&#39;less&#39;,&nbsp;&#39;watch&#39;]); };</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在上述代码中,less 任务用于编译 LESS 文件,watch 任务用于监听 LESS 文件的变化并自动重新编译。可以使用以下命令启动 Grunt:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;"><br/></p><p class="p3" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;PingFang SC&quot;; white-space: normal;">需要注意的是,在<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;"> Magento 2 </span>中,所有的样式文件都应该使用<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;"> .less </span>后缀,并位于主题文件夹的<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;"> web/css/source </span>文件夹下,<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">Grunt </span>任务会自动编译<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;"> LESS </span>文件并生成对应的<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;"> CSS </span>文件</p><p><br/></p>

magento2中的包含 CSS以及代码示例

<h5 style="color:red;">系统学习magento二次开发,推荐小册:<a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank">《Magento中文全栈二次开发 》</a></h5> <div class="image-container"> <p> <a style="color:blue;" href="https://www.maxiaoke.com/manual/magento_cn_dev.html" target="_blank"> <img src="https://www.maxiaoke.com/uploads/images/20230218/bb9c82995c24d1105676e02f373755f5.jpg" alt="Magento中文全栈二次开发"> </a> </p> </div> <div class="text-container" style="font-size:14px; color:#888"> <p>本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。</p> </div> <hr><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在 Magento 2 中,可以使用 @import 指令将多个 CSS 文件合并成一个文件。这种方式可以减少 HTTP 请求次数,加快页面加载速度。下面是一个简单的 Magento 2 包含 CSS 的代码示例:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;"><br/></p><pre class="brush:as3;toolbar:false">/*&nbsp;File:&nbsp;styles.css&nbsp;*/ body&nbsp;{ &nbsp;&nbsp;background-color:&nbsp;#f0f0f0; } /*&nbsp;File:&nbsp;typography.css&nbsp;*/ h1&nbsp;{ &nbsp;&nbsp;font-size:&nbsp;36px; &nbsp;&nbsp;font-weight:&nbsp;bold; &nbsp;&nbsp;line-height:&nbsp;1.2; } /*&nbsp;File:&nbsp;layout.css&nbsp;*/ .container&nbsp;{ &nbsp;&nbsp;max-width:&nbsp;960px; &nbsp;&nbsp;margin:&nbsp;0&nbsp;auto; } /*&nbsp;File:&nbsp;theme.css&nbsp;*/ @import&nbsp;url(&#39;styles.css&#39;); @import&nbsp;url(&#39;typography.css&#39;); @import&nbsp;url(&#39;layout.css&#39;); /*&nbsp;Additional&nbsp;styles&nbsp;for&nbsp;theme&nbsp;*/</pre><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">在上述代码中,theme.css 文件包含了 styles.css、typography.css 和 layout.css 三个文件的样式,可以在 theme.css 文件中添加额外的样式。这样,在页面中引用 theme.css 文件即可使用所有包含的样式。</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;">需要注意的是,在 Magento 2 中,所有的样式文件都应该使用 .css 后缀,并位于主题文件夹的 web/css 文件夹下。可以在 Magento 2 的布局文件中使用 &lt;head&gt; 区块来引用样式文件,例如:</p><p class="p2" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; min-height: 17px; white-space: normal;"><br/></p><p class="p1" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;Helvetica Neue&quot;; white-space: normal;"><br/></p><pre class="brush:as3;toolbar:false">&lt;!--&nbsp;File:&nbsp;Magento_Theme/layout/default.xml&nbsp;--&gt; &lt;head&gt; &nbsp;&nbsp;&lt;css&nbsp;src=&quot;css/theme.css&quot;&nbsp;/&gt; &lt;/head&gt;</pre><p class="p3" style="margin-top: 0px; margin-bottom: 0px; font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; font-size: 15px; line-height: normal; font-family: &quot;PingFang SC&quot;; white-space: normal;">在上述代码中,<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;">&lt;css&gt; </span>标签用于引用<span class="s1" style="font-variant-numeric: normal; font-variant-east-asian: normal; font-variant-alternates: normal; font-kerning: auto; font-optical-sizing: auto; font-feature-settings: normal; font-variation-settings: normal; font-stretch: normal; line-height: normal; font-family: &quot;Helvetica Neue&quot;;"> theme.css </span>文件。可以在不同的布局文件中引用不同的样式文件,例如针对特定页面或区域定义的样式。</p><p><br/></p>