最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - How can the listing of many repetitive identical height and width class names be shortened? - Stack Overflow

programmeradmin6浏览0评论
<Image
  src={"/insta.png"}
  alt="Instagram"
  width={24} height={24}
  className="
    cursor-pointer
    2xl:h-8 2xl:w-8  md:h-6 md:w-6  sm:h-4 sm:w-4
  "
/>

Instead of repeatedly mentioning 2xl:h-8 and 2xl:w-8, you can combine both classes into one using group or a custom utility class in Tailwind CSS.

I want to know if there is any other shortened way to give responsiveness rather than this. This is taking too much time and config.js is not implementing here as well. Kindly developers, help me here and suggest some pros and cons as well.

<Image
  src={"/insta.png"}
  alt="Instagram"
  width={24} height={24}
  className="
    cursor-pointer
    2xl:h-8 2xl:w-8  md:h-6 md:w-6  sm:h-4 sm:w-4
  "
/>

Instead of repeatedly mentioning 2xl:h-8 and 2xl:w-8, you can combine both classes into one using group or a custom utility class in Tailwind CSS.

I want to know if there is any other shortened way to give responsiveness rather than this. This is taking too much time and config.js is not implementing here as well. Kindly developers, help me here and suggest some pros and cons as well.

Share Improve this question edited Mar 20 at 20:38 rozsazoltan 11.1k6 gold badges20 silver badges58 bronze badges asked Mar 20 at 20:16 Moosa HaroonMoosa Haroon 14 bronze badges 2
  • so you want this class: 2xl:h-8 2xl:w-8 md:h-6 md:w-6 sm:h-4 sm:w-4 to be applied over another element? – jexroid Commented Mar 20 at 20:19
  • yes like i have to set it again and again for every element – Moosa Haroon Commented Mar 23 at 2:03
Add a comment  | 

2 Answers 2

Reset to default 0

If the width and height have the same value, use the size class, which declares both at once.

2xl:h-8 2xl:w-8 md:h-6 md:w-6 sm:h-4 sm:w-4

Merging w-8 h-8 can be shortened to size-8, etc.:

2xl:size-8 md:size-6 sm:size-4
  • Sizing: width - TailwindCSS v4 Docs
Class Styles
size-<number> width: calc(var(--spacing) * <number>);
height: calc(var(--spacing) * <number>);
size-<fraction> width: calc(<fraction> * 100%);
height: calc(<fraction> * 100%);

the most simplest way to avoid repeating 2xl:h-8 2xl:w-8 md:h-6 md:w-6 sm:h-4 sm:w-4 is applying CSS layer There are two layers to apply styles, depending on your needs.

1. Components layer

in this approach, you are essentially making a new class and applying the styles to that element. i think this is a better choice for your use case

in your css file, you can use:

@layer components {
  .my-custom-container {
    @apply 2xl:h-8 2xl:w-8 md:h-6 md:w-6 sm:h-4 sm:w-4;
  }
}

2. Base layer

@layer base {
 div {
   @apply 2xl:h-8 2xl:w-8 md:h-6 md:w-6 sm:h-4 sm:w-4;
 }
}

Using the @apply directive, you can apply tailwind styles, not raw CSS. You are rewriting an HTML element and making it the default behavior, according to tailwinds documents

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论