Magento – How to override default CSS properties in the theme

cssmagento2theme

I've already created a custom.css file which include some properties that I use in my custom theme (inherits from Blank theme).
I can apply successfully those custom properties, however some properties dont apply since they interfere with the default CSS properties in Blank theme.

For example:

.font { red; } - styles.css (default CSS magento file)

.font { blue; } - custom.css (my own CSS file)

The font appears red instead of blue. What should I do to override property values from the default CSS magento files?

Best Answer

In order to do add custom css which will override parent theme, you must do the following:

Add these folders to your custom theme

 app / design / frontend / [vendor] / [theme] / Magento_Theme / layout
 app / design / frontend / [vendor] / [theme] / web / css

Create the following files:

app / design / frontend / [vendor] / [theme] / Magento_Theme / layout / default_head_blocks.xml
app / design / frontend / [vendor] / [theme] / web / css / local-m.css
app / design / frontend / [vendor] / [theme] / web / css / local-l.css

place this code within default_head_blocks.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../vendor/magento/framework/Module/etc/module.xsd">
  <head>
    <css src="css/local-m.css" />
    <css src="css/local-l.css" media="screen and (min-width: 768px)"/>
  </head>
</page>

Apply your theme: http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/themes/theme-apply.html

Deploy static resources (SSH to magento root):

php bin/magento setup:static-content:deploy