0


前端多人项目开发中,如何保证CSS样式不冲突?

在前端项目开发中,例如突然来了一个大项目,很可能就需要多人一起开发,领导说了,要快,要快,要快,你们给我快。然后下面大伙就一拥而上,干着干着发现,一更新代码,哎,我写的样式怎么没了?最后一排查发现,张三跟李四的CSS命名一样,有的级别高,有的级别低,然后就有的被覆盖掉了。那么,我们该如何做一些控制,保证CSS样式尽量少一些冲突呢?

1. 使用CSS Modules

CSS Modules是一种CSS文件的模块化方案,能够保证每个CSS类名的作用域只在当前模块中,从而避免了全局命名空间的污染。

假设我们有一个React项目,目录结构如下:

  1. /src
  2. /components
  3. /Button
  4. Button.js
  5. Button.module.css

**Step 1: 创建CSS Module文件 **

  1. Button.module.css

文件中编写样式:

  1. /* Button.module.css */
  2. .button {
  3. background-color: blue;
  4. color: white;
  5. padding: 10px 20px;
  6. border: none;
  7. border-radius: 5px;
  8. cursor: pointer;
  9. }
  10. .button:hover {
  11. background-color: darkblue;
  12. }

**Step 2: 在组件中使用CSS Module **

  1. Button.js

文件中引入和使用这个CSS Module:

  1. // Button.js
  2. import React from 'react';
  3. import styles from './Button.module.css';
  4. const Button = ({ children }) => {
  5. return (
  6. <button className={styles.button}>
  7. {children}
  8. </button>
  9. );
  10. };
  11. export default Button;

Step 3: 使用组件

现在可以在其他组件中使用这个Button组件:

  1. // App.js
  2. import React from 'react';
  3. import Button from './components/Button/Button';
  4. const App = () => {
  5. return (
  6. <div>
  7. <h1>Hello, World!</h1>
  8. <Button>Click Me</Button>
  9. </div>
  10. );
  11. };
  12. export default App;

2. 命名规范

比如组长可能发话啦,大家开发的时候注意点,CSS命名不要冲突。张三和李四一脸问号?组长,我们该怎么保证呢?组长说,张三,你的CSS开头写个zs,李四,你的CSS命名开头写个ls,这样:

  1. zs_button {
  2. width: 100px;
  3. height: 100px;
  4. border: 1px solid red;
  5. }
  6. ls_button {
  7. width: 100px;
  8. height: 100px;
  9. border: 1px solid red;
  10. }
  11. ww_button {
  12. width: 100px;
  13. height: 100px;
  14. border: 1px solid red;
  15. }

你们记住自己是谁,不就好啦。大家都觉得不错,果然没有冲突。

3. Scoped样式(例如在Vue中)

这个比较好理解吧,就是开发Vue项目的时候呢,大家喜欢把每个业务组件的样式写到文件底部,然后底部有个 style 部分,给style标签添加 scoped 属性即可。

  1. <style scoped lang="less">
  2. .box-404 {
  3. display: flex;
  4. flex-direction: column;
  5. align-items: center;
  6. justify-content: center;
  7. height: 100vh;

4. 使用预处理器(如Sass/LESS)嵌套

这个也比较好理解,less 这种预处理器支持嵌套真是一大创举,第一开发者也不知道是如何想到的,我们只要保证最外层的CSS命名不冲突,那么其内部即便和其他CSS命名相同,也不会冲突。

  1. .txt {
  2. position: relative;
  3. overflow: hidden;
  4. border: 1px solid red;
  5. width: 100px;
  6. height: 40px;
  7. padding-right: 20px;
  8. line-height: 20px;
  9. .btn {
  10. width: 100px;
  11. height: 100px;
  12. border: 1px solid red;
  13. }
  14. }
  15. .txt1 {
  16. position: relative;
  17. overflow: hidden;
  18. border: 1px solid red;
  19. width: 100px;
  20. height: 40px;
  21. padding-right: 20px;
  22. line-height: 20px;
  23. .btn {
  24. width: 100px;
  25. height: 100px;
  26. border: 1px solid red;
  27. }
  28. }

我们看代码中虽然都有 **btn **的CSS命名,但其外部的命名不同,就可以保证嵌套在内部的命名不会冲突。

5 使用CSS-in-JS库

有句话不知当不当讲,我觉得这个库啊,算了,不说了,我有啥资格说呢。后面我举个例子,表达一下我的看法,我可没说不好啊

  1. styled-components

允许你在JavaScript中编写CSS,创建的样式会自动生成唯一的类名,确保样式不冲突。

  1. // Button.js
  2. import React from 'react';
  3. import styled from 'styled-components';
  4. // 动态设置背景颜色
  5. const StyledButton = styled.button`
  6. background-color: ${props => props.primary ? 'blue' : 'gray'};
  7. color: white;
  8. padding: 10px 20px;
  9. border: none;
  10. border-radius: 5px;
  11. cursor: pointer;
  12. &:hover {
  13. background-color: ${props => props.primary ? 'darkblue' : 'darkgray'};
  14. }
  15. `;
  16. const Button = ({ children, primary }) => {
  17. return (
  18. <StyledButton primary={primary}>
  19. {children}
  20. </StyledButton>
  21. );
  22. };
  23. export default Button;

使用时可以传递

  1. primary

属性来控制按钮的样式:

  1. // App.js
  2. import React from 'react';
  3. import Button from './components/Button/Button';
  4. const App = () => {
  5. return (
  6. <div>
  7. <h1>Hello, World!</h1>
  8. <Button primary>Primary Button</Button>
  9. <Button>Default Button</Button>
  10. </div>
  11. );
  12. };
  13. export default App;

是不是看着挺简约的,看着是不是挺方便的,这不是标签名就是上面的样式名嘛,你看多高效,还能入参。哈哈,你自己品去吧,我们做的是一个项目,不是一个小demo,一旦项目大了,元素多了,慢慢玩去吧,很爽哒,哈哈。

标签: 前端 css 面试

本文转载自: https://blog.csdn.net/xingyu_qie/article/details/139506329
版权归原作者 经海路大白狗 所有, 如有侵权,请联系我们删除。

“前端多人项目开发中,如何保证CSS样式不冲突?”的评论:

还没有评论