如何修改antd里Select的样式?????

本文详细介绍了如何修改Ant Design中的Select组件样式,包括Select选择器、下拉菜单、选项及焦点状态的样式调整方法,提供了具体CSS代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何修改antd里Select的样式?????

antd的Select

附一个官网链接 antd Select
在这里插入图片描述

官方给出的框架用起来是很方便,但是美观程度差强人意,给出的API里虽有className属性,但是这是进行修改Option的样式
在这里插入图片描述

那如何修改Select的样式呢?

废话不多说,直接上代码

 :global{
	.ant-select-selection{    // antd 版本3.*
		// 你想要的样式
	}
	.ant-select-selector{   // antd 版本 4.*
		// 你想要的样式
	}
}

【修改样式 antd】
方法很简单,有两点需要注意:

  • 引入的 antd 组件类名没有被 CSS Modules 转化,所以被覆盖的类名 .ant-select-selection 必须放到 :global 中。
  • 因为上一条的关系,覆盖是全局性的。为了防止对其他 Select 组件造成影响,所以需要包裹额外的 className 限制样式的生效范围。

下拉框的样式也并不好看,改如何进行修改呢?

效果图:
在这里插入图片描述

css代码:

:global{
  .ant-select-dropdown{
    border-radius: 0 0 10px 10px;   // 圆角
    overflow: hidden; 
      .ant-select-dropdown-menu,.ant-select-dropdown-menu-root,.ant-select-dropdown-menu-vertical{
        li:hover{// 鼠标 hover 效果
          background-color: rgba(132, 63, 255,0.4);
        }
        background-color: #fff; // 背景色
      }
      .ant-select-dropdown-menu-item-active{ 
        background-color: rgba(132, 63, 255,0.4);  // 展开时。默认选中option的背景色
      }
  }
  // 聚焦时 边线颜色为背景色   失焦时蓝色高亮颜色替换成紫色
 .ant-select-focused .ant-select-selection, .ant-select-selection:focus, .ant-select-selection:active{
    border-color: transparent; 
    box-shadow: 0 0 2px rgba(132,63,255,1);
  } 
}


加更 没有废话,直接上图+代码



在这里插入图片描述
这种样式的select组件,该如何修改成自己显示想要的样式呢,如下图:

在这里插入图片描述

:global{
	.ant-radio-group{ // 日、周、月模式切换 
	    .ant-radio-button-wrapper{ // 每一项的宽、高、文字居中方式
	      width: 75px;
	      height: 42px;
	      line-height: 42px;
	      text-align: center;
	    }
	    .ant-radio-button-wrapper-checked{  // 选中项的 文字颜色、边线颜色
	      color: #843FFF;
	      border-color: #843FFF !important;
	    }
	    .ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled){  
	    // 第二项以及第三项的左侧线条的颜色  如不明白,看下 图一
	      box-shadow:-1px 0 0 0 #843FFF
	    }
	    .ant-radio-button-wrapper:hover{ // 鼠标经过每一项的 字体的颜色设置
	      color: #3B4042!important;
	    }  
	    .ant-radio-button-wrapper-checked:not(.ant-radio-button-wrapper-disabled):focus-within{
	     // 鼠标选中某项时,去掉默认的外发光
	      outline: none;
	    }
	    label:first-child{ // 单独设置第一项的圆角效果
	      border-radius:9px 0px 0px 9px;
	    }
	    label:last-child { // 单独设置最后一项的圆角效果
	      border-radius:0px 9px 9px 0px;
	    }
  }
}

图一 如下所示 红色线条即为所描述线
在这里插入图片描述

`antd`(Ant Design)是一个流行的React UI框架,它提供了大量的预制组件来帮助开发者快速构建美观、一致的用户界面。`Select`组件是`antd`中的一个下拉选择组件,它允许用户从一组选项中选择一个或多个值。 要修改`Select`组件的样式,你可以通过以下几个步骤进行: 1. **使用自定义类名**: 为`Select`组件指定一个`className`属性,这样你就可以通过CSS直接修改这个组件的样式。 ```jsx <Select className="custom-select" /> ``` 2. **覆盖默认样式**: 通过CSS选择器定位到`Select`组件,并使用CSS规则来覆盖默认样式。 ```css .custom-select { width: 200px; } .custom-select .ant-select-selector { /* 修改选择器的样式 */ height: 40px; line-height: 40px; background-color: #fff; border: 1px solid #d9d9d9; } .custom-select .ant-select-selection { /* 修改选中项的样式 */ color: #1890ff; } ``` 3. **使用Less变量**: 如果你使用的是`antd`的Less版本,可以通过覆盖内置的Less变量来改变样式。在项目中创建一个`.less`文件,并配置相应的Less变量。 ```less @select_height: 40px; @select_item_height: 40px; @select_item_selected_color: #1890ff; ``` 4. **使用内联样式**: 直接在JSX中使用内联样式属性来修改样式。 ```jsx <Select style={{ width: '200px' }} className="custom-select" placeholder="Select a person" optionFilterProp="children" filterOption={(input, option) => option.props.children .toLowerCase() .includes(input.toLowerCase()) } /> ``` 5. **使用`@ant-design/compatible`**: 如果你希望兼容`antd`旧版本的样式,可以使用`@ant-design/compatible`包,并在其中进行样式覆盖。 ```jsx <Compatible component={Select} className="custom-select" style={{ width: '200px' }} // 其他属性 > {/* 子组件 */} </Compatible> ``` 通过上述方法,你可以根据需要自定义`antd`的`Select`组件样式。但请记住,修改样式时要确保遵循CSS的优先级规则,以确保你自定义的样式能够正确应用。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值