Search code examples
cssreactjspaddingantd

How to remove padding in Ant Design menu?


I can't figure out how to make the text in my menu be flush left (need to do this to align with other text). In the picture below, I want "One", "Two" and "Three" to be all the way left.

enter image description here

I have tried setting padding and margin to 0 everywhere I can think of, to no avail:

import React, { Component } from "react";
import { Menu, Icon } from 'antd';
const SubMenu = Menu.SubMenu;
const MenuItemGroup = Menu.ItemGroup;

class CategoryNav extends React.Component<Props> {
  handleClick = (e: Event) => {
    console.log('click ', e);
  }
  render() {
    return (
      <Menu
        onClick={this.handleClick}
        style={{ width: 256, height: "100%", paddingLeft: 0, marginLeft: 0 }}
        defaultSelectedKeys={['1']}
        defaultOpenKeys={['sub1']}
        mode="inline"
      >
        <SubMenu style={{ paddingLeft: 0, marginLeft: 0 }} key="sub1" title={<span>One</span>}>
          <Menu.Item style={{ paddingLeft: 0, marginLeft: 0 }} key="1">Option 1</Menu.Item>
          <Menu.Item key="2">Option 2</Menu.Item>
          <Menu.Item key="3">Option 3</Menu.Item>
          <Menu.Item key="4">Option 4</Menu.Item>
        </SubMenu>

        <SubMenu key="sub2" title={<span>Two</span>}>
          <Menu.Item key="5">Option 5</Menu.Item>
          <Menu.Item key="6">Option 6</Menu.Item>
        </SubMenu>

        <SubMenu key="sub4" title={<span>Three</span>}>
          <Menu.Item key="9">Option 9</Menu.Item>
          <Menu.Item key="10">Option 10</Menu.Item>
          <Menu.Item key="11">Option 11</Menu.Item>
          <Menu.Item key="12">Option 12</Menu.Item>
        </SubMenu>
      </Menu>
    );
  }
}

export default CategoryNav;

The above code is a minimal example edited slightly from the Ant Design Docs.

EDIT: Here is a screenshot of Chrome's inspector: enter image description here

I added a custom class in my React code:

    <SubMenu className="categoryNav" key="sub1" title={<span>One</span>}>
      <Menu.Item key="1">Option 1</Menu.Item>

and added this CSS:

.categoryNav .ant-menu-submenu-title {
  padding: 0;
  margin: 0;
}

div.ant-menu-submenu-title {
  padding: 0;
  margin: 0;
}

which seems to have gotten rid of right padding and top/bottom margin, but for some reason I just can't drop the left padding.

Strangely, the inspector says that the left padding comes from inline styling in "element.styles". Does this mean it's impossible to overwrite?


Solution

  • <Menu inlineIndent={0} />