• react 集成 tailwindcss


    已创建的项目集成 tailwindcss

    1.安装依赖

    npm install tailwindcss

    2. 根目录下创建文件tailwind.config.js  初始化文件

    1. // https://unpkg.com/browse/tailwindcss@3.1.6/stubs/defaultConfig.stub.js
    2. /** @type {import('tailwindcss').Config} */
    3. export default {
    4. purge: ['./src/**/*.{js,ts,tsx,jsx}'],
    5. corePlugins: {
    6. preflight: false,
    7. container: false
    8. },
    9. theme: {
    10. spacing: () => {
    11. let baseObj = {
    12. 120: '120px',
    13. 130: '130px',
    14. 140: '140px',
    15. 160: '160px',
    16. 180: '180px',
    17. 200: '200px',
    18. '1/2': '50%',
    19. '1/3': '33.333333%',
    20. '2/3': '66.666667%',
    21. '1/4': '25%',
    22. '3/4': '75%',
    23. '1/5': '20%',
    24. '2/5': '40%',
    25. '3/5': '60%',
    26. '4/5': '80%',
    27. '1/6': '16.666667%',
    28. '5/6': '83.333333%',
    29. '1/12': '8.333333%',
    30. '5/12': '41.666667%',
    31. '7/12': '58.333333%',
    32. '11/12': '91.666667%',
    33. full: '100%'
    34. // auto: 'auto',
    35. }
    36. // 0-100 px
    37. let obj = {}
    38. for (let i = 0; i <= 100; i++) {
    39. obj[i] = `${i}px`
    40. }
    41. return {
    42. ...obj,
    43. ...baseObj
    44. }
    45. },
    46. fontSize: theme => theme('spacing'),
    47. borderRadius: theme => theme('spacing'),
    48. borderWidth: theme => theme('spacing'),
    49. // lineHeight: (theme) => theme('spacing'),
    50. width: theme => ({
    51. ...theme('spacing'),
    52. 'max-content': 'max-content',
    53. screen: '100vw'
    54. }),
    55. maxWidth: theme => theme('width'),
    56. minWidth: theme => theme('width'),
    57. height: theme => ({
    58. ...theme('width'),
    59. screen: '100vh'
    60. }),
    61. maxHeight: theme => theme('height'),
    62. minHeight: theme => theme('height')
    63. }
    64. }

    3. 在全局css  index.css 文件中 导入Tailwindcss

    1. @import './_preflight'; // 这是做样式覆盖的 ,可不写
    2. @tailwind base;
    3. @tailwind components;
    4. @tailwind utilities;

    4. _preflight.css

    1. /* https://unpkg.com/browse/tailwindcss@3.0.23/src/css/preflight.css */
    2. /*
    3. 1. Prevent padding and border from affecting element width. (https://github.com/mozdevs/cssremedy/issues/4)
    4. 2. Allow adding a border to an element by just adding a border-width. (https://github.com/tailwindcss/tailwindcss/pull/116)
    5. */
    6. *,
    7. ::before,
    8. ::after {
    9. box-sizing: border-box;
    10. /* 1 */
    11. border-width: 0;
    12. /* 2 */
    13. border-style: solid;
    14. /* 2 */
    15. border-color: theme('borderColor.DEFAULT', currentColor);
    16. /* 2 */
    17. }
    18. ::before,
    19. ::after {
    20. --tw-content: '';
    21. }
    22. /*
    23. 1. Use a consistent sensible line-height in all browsers.
    24. 2. Prevent adjustments of font size after orientation changes in iOS.
    25. 3. Use a more readable tab size.
    26. 4. Use the user's configured `sans` font-family by default.
    27. */
    28. html {
    29. line-height: 1.5;
    30. /* 1 */
    31. -webkit-text-size-adjust: 100%;
    32. /* 2 */
    33. -moz-tab-size: 4;
    34. /* 3 */
    35. tab-size: 4;
    36. /* 3 */
    37. font-family: theme(
    38. 'fontFamily.sans',
    39. ui-sans-serif,
    40. system-ui,
    41. -apple-system,
    42. BlinkMacSystemFont,
    43. 'Segoe UI',
    44. Roboto,
    45. 'Helvetica Neue',
    46. Arial,
    47. 'Noto Sans',
    48. sans-serif,
    49. 'Apple Color Emoji',
    50. 'Segoe UI Emoji',
    51. 'Segoe UI Symbol',
    52. 'Noto Color Emoji'
    53. );
    54. /* 4 */
    55. }
    56. /*
    57. 1. Remove the margin in all browsers.
    58. 2. Inherit line-height from `html` so users can set them as a class directly on the `html` element.
    59. */
    60. body {
    61. margin: 0;
    62. /* 1 */
    63. line-height: inherit;
    64. /* 2 */
    65. }
    66. /*
    67. 1. Add the correct height in Firefox.
    68. 2. Correct the inheritance of border color in Firefox. (https://bugzilla.mozilla.org/show_bug.cgi?id=190655)
    69. 3. Ensure horizontal rules are visible by default.
    70. */
    71. hr {
    72. height: 0;
    73. /* 1 */
    74. color: inherit;
    75. /* 2 */
    76. border-top-width: 1px;
    77. /* 3 */
    78. }
    79. /*
    80. Add the correct text decoration in Chrome, Edge, and Safari.
    81. */
    82. abbr:where([title]) {
    83. text-decoration: underline dotted;
    84. }
    85. /*
    86. Remove the default font size and weight for headings.
    87. */
    88. h1,
    89. h2,
    90. h3,
    91. h4,
    92. h5,
    93. h6 {
    94. font-size: inherit;
    95. font-weight: inherit;
    96. }
    97. /*
    98. Reset links to optimize for opt-in styling instead of opt-out.
    99. */
    100. a {
    101. color: inherit;
    102. text-decoration: inherit;
    103. }
    104. /*
    105. Add the correct font weight in Edge and Safari.
    106. */
    107. b,
    108. strong {
    109. font-weight: bolder;
    110. }
    111. /*
    112. 1. Use the user's configured `mono` font family by default.
    113. 2. Correct the odd `em` font sizing in all browsers.
    114. */
    115. code,
    116. kbd,
    117. samp,
    118. pre {
    119. font-family: theme(
    120. 'fontFamily.mono',
    121. ui-monospace,
    122. SFMono-Regular,
    123. Menlo,
    124. Monaco,
    125. Consolas,
    126. 'Liberation Mono',
    127. 'Courier New',
    128. monospace
    129. );
    130. /* 1 */
    131. font-size: 1em;
    132. /* 2 */
    133. }
    134. /*
    135. Add the correct font size in all browsers.
    136. */
    137. small {
    138. font-size: 80%;
    139. }
    140. /*
    141. Prevent `sub` and `sup` elements from affecting the line height in all browsers.
    142. */
    143. sub,
    144. sup {
    145. font-size: 75%;
    146. line-height: 0;
    147. position: relative;
    148. vertical-align: baseline;
    149. }
    150. sub {
    151. bottom: -0.25em;
    152. }
    153. sup {
    154. top: -0.5em;
    155. }
    156. /*
    157. 1. Remove text indentation from table contents in Chrome and Safari. (https://bugs.chromium.org/p/chromium/issues/detail?id=999088, https://bugs.webkit.org/show_bug.cgi?id=201297)
    158. 2. Correct table border color inheritance in all Chrome and Safari. (https://bugs.chromium.org/p/chromium/issues/detail?id=935729, https://bugs.webkit.org/show_bug.cgi?id=195016)
    159. 3. Remove gaps between table borders by default.
    160. */
    161. table {
    162. text-indent: 0;
    163. /* 1 */
    164. border-color: inherit;
    165. /* 2 */
    166. border-collapse: collapse;
    167. /* 3 */
    168. }
    169. /*
    170. 1. Change the font styles in all browsers.
    171. 2. Remove the margin in Firefox and Safari.
    172. 3. Remove default padding in all browsers.
    173. */
    174. button,
    175. input,
    176. optgroup,
    177. select,
    178. textarea {
    179. font-family: inherit;
    180. /* 1 */
    181. font-size: 100%;
    182. /* 1 */
    183. line-height: inherit;
    184. /* 1 */
    185. color: inherit;
    186. /* 1 */
    187. margin: 0;
    188. /* 2 */
    189. padding: 0;
    190. /* 3 */
    191. }
    192. /*
    193. Remove the inheritance of text transform in Edge and Firefox.
    194. */
    195. button,
    196. select {
    197. text-transform: none;
    198. }
    199. /*
    200. 1. Correct the inability to style clickable types in iOS and Safari.
    201. 2. Remove default button styles.
    202. */
    203. /* FIXED: 解决 preflight 与 ant-design 样式冲突问题 */
    204. /* button,
    205. [type='button'],
    206. [type='reset'],
    207. [type='submit'] {
    208. -webkit-appearance: button;
    209. background-color: transparent;
    210. background-image: none;
    211. } */
    212. /*
    213. Use the modern Firefox focus style for all focusable elements.
    214. */
    215. :-moz-focusring {
    216. outline: auto;
    217. }
    218. /*
    219. Remove the additional `:invalid` styles in Firefox. (https://github.com/mozilla/gecko-dev/blob/2f9eacd9d3d995c937b4251a5557d95d494c9be1/layout/style/res/forms.css#L728-L737)
    220. */
    221. :-moz-ui-invalid {
    222. box-shadow: none;
    223. }
    224. /*
    225. Add the correct vertical alignment in Chrome and Firefox.
    226. */
    227. progress {
    228. vertical-align: baseline;
    229. }
    230. /*
    231. Correct the cursor style of increment and decrement buttons in Safari.
    232. */
    233. ::-webkit-inner-spin-button,
    234. ::-webkit-outer-spin-button {
    235. height: auto;
    236. }
    237. /*
    238. 1. Correct the odd appearance in Chrome and Safari.
    239. 2. Correct the outline style in Safari.
    240. */
    241. [type='search'] {
    242. -webkit-appearance: textfield;
    243. /* 1 */
    244. outline-offset: -2px;
    245. /* 2 */
    246. }
    247. /*
    248. Remove the inner padding in Chrome and Safari on macOS.
    249. */
    250. ::-webkit-search-decoration {
    251. -webkit-appearance: none;
    252. }
    253. /*
    254. 1. Correct the inability to style clickable types in iOS and Safari.
    255. 2. Change font properties to `inherit` in Safari.
    256. */
    257. ::-webkit-file-upload-button {
    258. -webkit-appearance: button;
    259. /* 1 */
    260. font: inherit;
    261. /* 2 */
    262. }
    263. /*
    264. Add the correct display in Chrome and Safari.
    265. */
    266. summary {
    267. display: list-item;
    268. }
    269. /*
    270. Removes the default spacing and border for appropriate elements.
    271. */
    272. blockquote,
    273. dl,
    274. dd,
    275. h1,
    276. h2,
    277. h3,
    278. h4,
    279. h5,
    280. h6,
    281. hr,
    282. figure,
    283. p,
    284. pre {
    285. margin: 0;
    286. }
    287. fieldset {
    288. margin: 0;
    289. padding: 0;
    290. }
    291. legend {
    292. padding: 0;
    293. }
    294. ol,
    295. ul,
    296. menu {
    297. list-style: none;
    298. margin: 0;
    299. padding: 0;
    300. }
    301. /*
    302. Prevent resizing textareas horizontally by default.
    303. */
    304. textarea {
    305. resize: vertical;
    306. }
    307. /*
    308. 1. Reset the default placeholder opacity in Firefox. (https://github.com/tailwindlabs/tailwindcss/issues/3300)
    309. 2. Set the default placeholder color to the user's configured gray 400 color.
    310. */
    311. input::placeholder,
    312. textarea::placeholder {
    313. opacity: 1;
    314. /* 1 */
    315. color: theme('colors.gray.400', #9ca3af);
    316. /* 2 */
    317. }
    318. /*
    319. Set the default cursor for buttons.
    320. */
    321. button,
    322. [role='button'] {
    323. cursor: pointer;
    324. }
    325. /*
    326. Make sure disabled buttons don't get the pointer cursor.
    327. */
    328. :disabled {
    329. cursor: default;
    330. }
    331. /*
    332. 1. Make replaced elements `display: block` by default. (https://github.com/mozdevs/cssremedy/issues/14)
    333. 2. Add `vertical-align: middle` to align replaced elements more sensibly by default. (https://github.com/jensimmons/cssremedy/issues/14#issuecomment-634934210)
    334. This can trigger a poorly considered lint error in some tools but is included by design.
    335. */
    336. img,
    337. svg,
    338. video,
    339. canvas,
    340. audio,
    341. iframe,
    342. embed,
    343. object {
    344. display: block;
    345. /* 1 */
    346. vertical-align: middle;
    347. /* 2 */
    348. }
    349. /*
    350. Constrain images and videos to the parent width and preserve their intrinsic aspect ratio. (https://github.com/mozdevs/cssremedy/issues/14)
    351. */
    352. img,
    353. video {
    354. max-width: 100%;
    355. height: auto;
    356. }
    357. /*
    358. Ensure the default browser behavior of the `hidden` attribute.
    359. */
    360. [hidden] {
    361. display: none;
    362. }

    5.  vscode  插件 可以直接搜索 Tailwindcss 下载敲代码 样式提示插件  

    6. 可以直接在 Tailwindcss 官网直接搜索样式  ,非常方便

    7 . 常见写法样式举例 h-30 为 height:30  也支持自定义 h-[30px] w-[30px]  text-[30px]   ,一般在官网搜索 后面会给出值自定义例子  

  • 相关阅读:
    自动驾驶创业方向有变化?如何突破技术瓶颈?
    校园网免认证服务器大增加
    如何处理前端文件上传?
    C语言速成笔记 —— 考点详解 & 知识点图解
    Java:jackson实现json缩进美化输出
    小小Python编程故事-小小的成绩单(1)
    redis中value/list
    HDLbits exercises 1 (开头到vector5节选题)
    springboot vue3 elementui plus点餐外卖系统源码
    Spring AOP
  • 原文地址:https://blog.csdn.net/qq_30071431/article/details/138202601