search

Home  >  Q&A  >  body text

How to load CSS in a specific part of a Shopify theme?

<p>I'm trying to create another section and apply CSS from the assets folder. But when I put <code>{{ 'style-section-block.css' | asset url | stylesheet_tag }}</code> to the page where I want to apply the CSS, it also affects the other sections, even though I only Put in the parts I want to change. </p> <p>I want to be able to edit part of a page</p>
P粉752812853P粉752812853548 days ago555

reply all(1)I'll reply

  • P粉083785014

    P粉0837850142023-08-31 00:00:03

    If you want to apply a specific section of CSS, first add a unique class or ID to the section you want to style. After using this unique class or ID in your CSS file, you can style the section. Example is

    <div class="my-section">
      <!-- section content -->
    </div>

    "my-section" is the unique class name of the section. You can use the class name to add styles to a section.

    .my-section {
          font-size: 24px;      
        }

    Based on Shopify themes. You can conditionally use CSS files based on the current page handle or page template. From my idea, you can do this "template" or "page.handle" variable in your theme's "layout" file. I mean the "theme.liquid" file. You can add the following code to the "head" section of your "layout" file.

    {% if template contains 'your-specific-template' or page.handle == 'your-specific-page-handle' %}
      {{ 'style-section-block.css' | asset_url | stylesheet_tag }}
    {% endif %}

    Replace "your specific template" with your template name, or "your specific page handle" with your page handle name. And remove CSS links from other sections.

    reply
    0
  • Cancelreply