Skip to content

Global CSS included with component CSS import #4331

Description

@benthehenten

Thanks for this great library!

I tried to use babel-plugin-import to do modular CSS imports, but along with the css for an individual component, there is a lot of global css being imported that interferes with my existing styles. Is it possible to import the CSS for an individual component without global CSS being included?

Thanks!

Activity

  1. benjycui commented on Dec 22, 2016

    @benjycui
    Contributor

    Do you mean CSS rules like this?

    html,
    body {
      ...
    }
  2. benthehenten commented on Dec 22, 2016

    @benthehenten
    Author

    Yes.

  3. benjycui commented on Dec 22, 2016

    @benjycui
    Contributor

    It's impossible not to import those CSS rules now.

  4. afc163 commented on Dec 22, 2016

    @afc163
    Member

    #1966 (comment)

    You have to override these global styles now.


    Or you drop the style option of babel-plugin-import, import the less files indenpently.

    @import "~antd/lib/style/themes/default.less";
    @import "~antd/lib/button/style/index.less";
    
  5. DogAndHerDude commented on Apr 21, 2017

    @DogAndHerDude

    That's terrible design though.

  6. peitalin commented on Apr 25, 2017

    @peitalin

    This issue makes this library very difficult to work with. Especially having global properties on body etc. I think this issue should be made much more clear unless you have plans to change this.

  7. benjycui commented on Apr 26, 2017

    @benjycui
    Contributor

    @forestDev @peitalin it's an known issue, but we don't have enough time to solve this issue.

    But we can keep discussing here.

  8. benjycui commented on Apr 26, 2017

    @benjycui
    Contributor

    If we can import breaking changes, we can solve this issue. Such as, move styles in reset to components' styles:

    Move:

    * {
      box-sizing: border-box;
      -webkit-tap-highlight-color: rgba(0, 0, 0, 0); //  remove tap highlight color for mobile safari
    }

    To:

    .ant-btn,
    .ant-btn * {
      box-sizing: border-box;
      -webkit-tap-highlight-color: rgba(0, 0, 0, 0); //  remove tap highlight color for mobile safari
    }

    And for backward compatibility, we can provide a standalone reset stylesheet.

    import 'antd/styles/reset.less'
  9. bsheikh commented on May 5, 2017

    @bsheikh
    Contributor

    is there any update on this?

  10. benjycui commented on May 8, 2017

    @benjycui
    Contributor

    We can discuss, but we have no time to apply any solution(if we can find) now.

  11. bchenSyd commented on May 12, 2017

    @bchenSyd
    Contributor

    I think this is a critical issue as not many people can afford importing a global css and having their existing styles overridden

  12. 72 remaining items

  13. axhamre commented on Oct 3, 2018

    @axhamre

    I don't know if this helps anyone, but the following helped me import the CSS for only the component in use:

    import Modal from 'antd/lib/modal'
    import 'antd/lib/modal/style/css'

  14. scleikas commented on Oct 3, 2018

    @scleikas

    I don't know if this helps anyone, but the following helped me import the CSS for only the component in use:

    import Modal from 'antd/lib/modal'
    import 'antd/lib/modal/style/css'

    Yeah, this seems to work ok for many components. Your milage may vary, though -- at least Select needs a little more than just the component's own styles. The "remove" icon in selections won't render otherwise; I didn't have time to track down the issue completely, but i's probably something minor. So you may need to mimic some styles from the main styles in some of the components.

    While this option is not quite perfect, it could work well for many components. The custom styles are in risk of breaking with updates, but you can obviously control when you want to do your updates.

  15. kclonts commented on Oct 5, 2018

    @kclonts

    The best solution I found for our organization is to include the entire CSS sheet from antd, with any styles that affect non antd named styles stripped out. Mileage might vary, and there is obviously the downside of every style now being imported. But it works well and I haven't seen any issues after doing so for about two months now.

  16. renatoruk commented on Dec 12, 2018

    @renatoruk

    @jaleikas
    Have you found a workaround on this? As far as I can tell, the issue with less-loader@4.x is still not resolved and therefore the suggested fix with NormalModuleReplacementPlugin does not work. That solution actually seems like the best one since things can be manually overridden and scoped.
    I tried the same thing as you, downgrading the less-loader, but then webpack resolve does not work.

    Using antd@3.10.2 here.

  17. pedroas93 commented on Dec 19, 2018

    @pedroas93

    Las personas que sufren de esto pueden querer probar mi fork agregando el paquete directamente desde el repositorio

    yarn add github:serguzest/ant-design#3.4.1-NoCssPollution

    o

    npm install github:serguzest/ant-design#3.4.1-NoCssPollution

    Solo descarté la base y la construí sin ninguna otra modificación. Funciona en mi caso, no uso muchos componentes de él. Probablemente se verá afectado por algunos componentes, especialmente si aún no tiene una hoja de estilo de restablecimiento razonable en su proyecto. # 4546

    this works for me

  18. Trontor commented on Dec 31, 2018

    @Trontor

    People who are suffering from this might wanna try my fork by adding the package directly from the repository

    yarn add github:serguzest/ant-design#3.4.1-NoCssPollution

    or

    npm install github:serguzest/ant-design#3.4.1-NoCssPollution

    I only ruled out base.less and built it without any other modifications. It works in my case, I don't use many components from it. It will probably break look of some components especially if you don't already have sane reset stylesheet in your project. #4546

    Thank you for this solution!

  19. scleikas commented on Dec 31, 2018

    @scleikas

    Have you found a workaround on this? As far as I can tell, the issue with less-loader@4.x is still not resolved and therefore the suggested fix with NormalModuleReplacementPlugin does not work. That solution actually seems like the best one since things can be manually overridden and scoped.

    I chose to stop using antd, and decided to pick some unstyled components (like rc-switch). The problem is that most of the "unstyled" components are either horribly/not documented, and some of them also seem to behave differently, so for the most part, I gave up and looked elsewhere...

  20. dfeinzeig commented on Feb 19, 2019

    @dfeinzeig

    hi, it should be possible to try match all elems, starting with .ant, and assign them styles

    *[class^='ant-']{
    box-sizing: border-box;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0); // remove tap highlight color for mobile safari
    }

    It should have crossbrowser compatibility and shouldn't take long to implement.

    @tylik1 , thank you! i just needed to fix the font-family to match the rest of my electron app. this totally saved me.

  21. lin-123 commented on Dec 28, 2019

    @lin-123

    I don't know if this helps anyone, but the following helped me import the CSS for only the component in use:

    import Modal from 'antd/lib/modal'
    import 'antd/lib/modal/style/css'

    it work for me by the following.

    import Icon from 'antd/lib/icon';
    import 'antd/lib/icon/style/index.css';
    
  22. marijoo commented on Jan 9, 2020

    @marijoo

    Unfortunately this is a show stopper. I will also not implement Antd even though the date picker is the best I could find so far.

  23. ctrl-f5 commented on Apr 8, 2020

    @ctrl-f5

    We are currently searching for a react component library for our company and this is also a blocking issue for us, and we probably an not use ant because of it.

    We are also looking at react MUI, which has great theming options, maybe you can have a look at how they do it and implement something similar.

  24. antonkatz commented on Apr 18, 2020

    @antonkatz

    I used https://www.npmjs.com/package/patch-package to comment out

    // import 'normalize.css/normalize.css';
    

    from antd-mobile/es/style/index.js.
    My purpose was to get next.js to build, so in my case I just included a CND link in my header instead.

  25. cuteshaun commented on Apr 24, 2020

    @cuteshaun

    It was 2020, humanity died out, coronavirus, ant-design adds global styles to the project...

  26. yoyo837 commented on Apr 24, 2020

    @yoyo837
    Contributor

    Tracking in #9363

  27. locked as too heated and limited conversation to collaborators on Apr 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions