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

如何居中Bootstrap DateTimePicker日历组件?已添加组件但无法整体居中

解决Bootstrap DateTimePicker日历内容无法居中的问题

嘿,我之前也踩过这个坑!Bootstrap DateTimePicker的默认样式确实没把日历里的内容做居中处理,不过咱们用几行自定义CSS就能轻松搞定。

具体解决方案

你只需要添加下面这段CSS代码,覆盖插件的默认样式即可:

/* 让整个日历容器内的内容居中对齐 */
.bootstrap-datetimepicker-widget {
  text-align: center;
}

/* 确保日历表格的单元格和表头文字居中 */
.bootstrap-datetimepicker-widget table td,
.bootstrap-datetimepicker-widget table th {
  text-align: center;
}

/* 针对时间选择器部分,让按钮和输入框也居中排列 */
.bootstrap-datetimepicker-widget .timepicker-picker {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
}

注意事项

  • 把这段CSS放在你的项目样式文件里,或者页面的<style>标签中,一定要确保它在Bootstrap和DateTimePicker的CSS文件之后加载,这样才能优先覆盖默认样式。
  • 如果你的页面有其他自定义样式干扰了居中效果,可以尝试在样式属性后加上!important(尽量少用,万不得已再用):
    .bootstrap-datetimepicker-widget {
      text-align: center !important;
    }
    

这样设置之后,日历里的日期、表头以及时间选择区域的所有内容就都会居中显示啦!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:11:59