You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React中如何为组件Prop里的item.specTitle添加类名或内联样式?

How to Add Class Name or Inline Style to item.specTitle in Ant Design List

Hey there! As you're new to React and Ant Design, let's walk through how to style item.specTitle in your List component. The key here is to split specTitle and specDescription into separate elements (instead of concatenating them as plain strings) — this way you can target specTitle specifically for styling.

Option 1: Inline Styles

You can use React's inline style prop to directly apply styles to the specTitle element. Wrap item.specTitle in a <span> and pass a style object (remember to use camelCase for CSS properties like fontWeight instead of font-weight):

const spec1 = [
  {specTitle: "Size", specDescription: "Large suitcase."},
  {specTitle: "Color", specDescription: "Red car."},
  {specTitle: "Other", specDescription: "Other here."},
];

// ...

<List dataSource={spec1} renderItem={item => (
  <List.Item>
    <List.Item.Meta 
      description={
        <>
          <span style={{ fontWeight: 'bold', color: '#2563eb', marginRight: '8px' }}>
            {item.specTitle}:
          </span>
          {item.specDescription}
        </>
      } 
    />
  </List.Item>
)} />

Option 2: Custom Class Name

For reusable styles (a better practice if you'll use this style elsewhere), define a CSS class and apply it to the specTitle span:

First, add this to your CSS file:

.spec-title {
  font-weight: bold;
  color: #2563eb;
  margin-right: 8px;
}

Then import the CSS file into your React component and use the className prop:

// Import your CSS file (adjust the path to match your project structure)
import './component-styles.css';

const spec1 = [
  {specTitle: "Size", specDescription: "Large suitcase."},
  {specTitle: "Color", specDescription: "Red car."},
  {specTitle: "Other", specDescription: "Other here."},
];

// ...

<List dataSource={spec1} renderItem={item => (
  <List.Item>
    <List.Item.Meta 
      description={
        <>
          <span className="spec-title">{item.specTitle}:</span>
          {item.specDescription}
        </>
      } 
    />
  </List.Item>
)} />

Both approaches work well — inline styles are perfect for one-off tweaks, while class names are ideal for maintaining consistent, reusable styles across your app.

内容的提问来源于stack exchange,提问作者changshou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:09:43