skip to Main Content

I am trying to render Tabs using react-tabs. The tab contents are rendered one below another (across different tabs) as follows

Adding my code Snippet

<Tabs className="tabs">
    <TabList className="tab-header-list">
        <Tab selectedClassName="tab-header-list-items"><b>A</b></Tab>
        <Tab selectedClassName="tab-header-list-items"><b>B</b></Tab>
        <Tab selectedClassName="tab-header-list-items"><b>C</b></Tab>
        <Tab selectedClassName="tab-header-list-items"><b>D</b></Tab>
    </TabList>
    <TabPanel key="1" className="tab-panel">
        <BarChart chartData={userData} />
    </TabPanel>
    <TabPanel key="3" className="tab-panel">
         <BarChart chartData={userData} />
    </TabPanel>
    <TabPanel key="2" className="tab-panel">
         <BarChart chartData={userData} />
    </TabPanel>
</Tabs>

CSS used

.tabs {
    
}

.tab-header-list{
    color: grey;
}

.tab-header-list-items {
    display: inline-block;
    background-color: black;
    width: 25%;
    color: black;
}

.tab-panel {
    width: 900px;
    height: 300px;
}

Actual Rendering
Expected

Tried all values for flex property, display property, but nothing seems to work.
Please suggest.

3

Answers


  1. I don’t really know waht you mean with "Tried all values for flex property, display property, but nothing seems to work", but try using a CSS grid layout.

    <Tabs className="tabs">
      <TabList className="tab-header-list">
        <Tab selectedClassName="tab-header-list-items"><b>A</b></Tab>
        <Tab selectedClassName="tab-header-list-items"><b>B</b></Tab>
        <Tab selectedClassName="tab-header-list-items"><b>C</b></Tab>
        <Tab selectedClassName="tab-header-list-items"><b>D</b></Tab>
      </TabList>
      <TabPanel key="1" className="tab-panel">
        <BarChart chartData={userData} />
      </TabPanel>
      <TabPanel key="3" className="tab-panel">
        <BarChart chartData={userData} />
      </TabPanel>
      <TabPanel key="2" className="tab-panel">
        <BarChart chartData={userData} />
      </TabPanel>
    </Tabs>
    
    
    .tabs {
      display: flex;
    }
    
    .tab-panel {
      flex: 1;
    }
    

    If you already tried this, please let me know.

    Login or Signup to reply.
  2. This rule is breaking it

    .tab-panel {
        width: 900px;
        height: 300px;
    }
    

    Specifying height and width like that can break libraries. I would apply these rules to the content within the .tab-panel, but not the .tab-panel itself

    Login or Signup to reply.
  3. Remove the height property from .tab-panel

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search