Skip to main content

Tab Bars

Tabs are automatically created by the docking system (when in ‘docking’ branch). Use this to create tab bars/tabs yourself.

BeginTabBar

bool ImGui::BeginTabBar(const char* str_id, ImGuiTabBarFlags flags = 0)
Create and append into a TabBar. Only call EndTabBar() if this returns true.
str_id
const char*
Unique identifier for the tab bar
flags
ImGuiTabBarFlags
default:"0"
Tab bar flags (see ImGuiTabBarFlags_)
return
bool
True if tab bar is visible
// Example
if (ImGui::BeginTabBar("MyTabBar")) {
    if (ImGui::BeginTabItem("Tab1")) {
        ImGui::Text("This is tab 1!");
        ImGui::EndTabItem();
    }
    if (ImGui::BeginTabItem("Tab2")) {
        ImGui::Text("This is tab 2!");
        ImGui::EndTabItem();
    }
    ImGui::EndTabBar();
}

EndTabBar

void ImGui::EndTabBar()
Close tab bar. Only call EndTabBar() if BeginTabBar() returns true.

Tab Items

BeginTabItem

bool ImGui::BeginTabItem(const char* label, bool* p_open = NULL, ImGuiTabItemFlags flags = 0)
Create a Tab. Returns true if the Tab is selected.
label
const char*
Tab label
p_open
bool*
default:"NULL"
Optional pointer to bool to enable close button (set to false when clicked)
flags
ImGuiTabItemFlags
default:"0"
Tab item flags (see ImGuiTabItemFlags_)
return
bool
True when tab is selected
// Example
if (ImGui::BeginTabBar("Tabs")) {
    if (ImGui::BeginTabItem("Settings")) {
        ImGui::Text("Settings content");
        ImGui::EndTabItem();
    }
    if (ImGui::BeginTabItem("Tools")) {
        ImGui::Text("Tools content");
        ImGui::EndTabItem();
    }
    ImGui::EndTabBar();
}

// With close button
static bool tab_open = true;
if (ImGui::BeginTabBar("ClosableTabs")) {
    if (tab_open && ImGui::BeginTabItem("Closable", &tab_open)) {
        ImGui::Text("This tab can be closed");
        ImGui::EndTabItem();
    }
    ImGui::EndTabBar();
}
if (!tab_open) {
    ImGui::Text("Tab was closed!");
}

EndTabItem

void ImGui::EndTabItem()
Close tab item. Only call EndTabItem() if BeginTabItem() returns true.

TabItemButton

bool ImGui::TabItemButton(const char* label, ImGuiTabItemFlags flags = 0)
Create a Tab behaving like a button. Return true when clicked. Cannot be selected in the tab bar.
label
const char*
Button label
flags
ImGuiTabItemFlags
default:"0"
Tab item flags
return
bool
True when clicked
// Example
if (ImGui::BeginTabBar("TabBar")) {
    if (ImGui::TabItemButton("+", ImGuiTabItemFlags_Trailing)) {
        // Add new tab
    }
    
    if (ImGui::BeginTabItem("Tab1")) {
        ImGui::EndTabItem();
    }
    ImGui::EndTabBar();
}

SetTabItemClosed

void ImGui::SetTabItemClosed(const char* tab_or_docked_window_label)
Notify TabBar or Docking system of a closed tab/window ahead (useful to reduce visual flicker on reorderable tab bars). For tab-bar: call after BeginTabBar() and before Tab submissions. Otherwise call with a window name.
tab_or_docked_window_label
const char*
Tab or window label

Tab Bar Flags

ImGuiTabBarFlags_

Flags for BeginTabBar().
FlagDescription
ImGuiTabBarFlags_NoneDefault
ImGuiTabBarFlags_ReorderableAllow manually dragging tabs to re-order them
ImGuiTabBarFlags_AutoSelectNewTabsAutomatically select new tabs when they appear
ImGuiTabBarFlags_TabListPopupButtonEnable popup button for tab list
ImGuiTabBarFlags_NoCloseWithMiddleMouseButtonDisable closing tabs with middle mouse button
ImGuiTabBarFlags_NoTabListScrollingButtonsDisable scrolling buttons (when fitting policy is scroll)
ImGuiTabBarFlags_NoTooltipDisable tooltips when hovering a tab
ImGuiTabBarFlags_DrawSelectedOverlineDraw selected overline markers over selected tab
ImGuiTabBarFlags_FittingPolicyMixedShrink tabs when they don’t fit, then enable scrolling (default)
ImGuiTabBarFlags_FittingPolicyShrinkShrink tabs when they don’t fit
ImGuiTabBarFlags_FittingPolicyScrollEnable scrolling buttons when tabs don’t fit
// Reorderable tabs
if (ImGui::BeginTabBar("Reorderable", ImGuiTabBarFlags_Reorderable)) {
    if (ImGui::BeginTabItem("Tab1")) {
        ImGui::EndTabItem();
    }
    if (ImGui::BeginTabItem("Tab2")) {
        ImGui::EndTabItem();
    }
    ImGui::EndTabBar();
}

// Auto-select new tabs
if (ImGui::BeginTabBar("AutoSelect", ImGuiTabBarFlags_AutoSelectNewTabs)) {
    // ...
    ImGui::EndTabBar();
}

Tab Item Flags

ImGuiTabItemFlags_

Flags for BeginTabItem().
FlagDescription
ImGuiTabItemFlags_NoneDefault
ImGuiTabItemFlags_UnsavedDocumentDisplay a dot next to the title + set NoAssumedClosure
ImGuiTabItemFlags_SetSelectedTrigger flag to programmatically make the tab selected
ImGuiTabItemFlags_NoCloseWithMiddleMouseButtonDisable closing with middle mouse button
ImGuiTabItemFlags_NoPushIdDon’t call PushID()/PopID() on BeginTabItem()/EndTabItem()
ImGuiTabItemFlags_NoTooltipDisable tooltip for the given tab
ImGuiTabItemFlags_NoReorderDisable reordering this tab or having another tab cross over
ImGuiTabItemFlags_LeadingEnforce the tab position to the left of the tab bar
ImGuiTabItemFlags_TrailingEnforce the tab position to the right of the tab bar
ImGuiTabItemFlags_NoAssumedClosureTab is selected when trying to close + closure is not immediately assumed
// Set tab as selected
if (ImGui::BeginTabItem("Selected", NULL, ImGuiTabItemFlags_SetSelected)) {
    ImGui::EndTabItem();
}

// Unsaved document indicator
if (ImGui::BeginTabItem("Document*", NULL, ImGuiTabItemFlags_UnsavedDocument)) {
    ImGui::Text("Unsaved changes");
    ImGui::EndTabItem();
}

// Leading tab (stays on left)
if (ImGui::BeginTabItem("Settings", NULL, ImGuiTabItemFlags_Leading)) {
    ImGui::EndTabItem();
}

// Trailing tab (stays on right)
if (ImGui::BeginTabItem("Help", NULL, ImGuiTabItemFlags_Trailing)) {
    ImGui::EndTabItem();
}

Complete Example

void ShowTabBarExample() {
    static int tab_count = 3;
    static bool tab_open[10] = { true, true, true, false, false, false, false, false, false, false };
    
    if (ImGui::BeginTabBar("MyTabs", ImGuiTabBarFlags_Reorderable | ImGuiTabBarFlags_AutoSelectNewTabs)) {
        // Add button
        if (ImGui::TabItemButton("+", ImGuiTabItemFlags_Trailing | ImGuiTabItemFlags_NoTooltip)) {
            if (tab_count < 10) {
                tab_open[tab_count] = true;
                tab_count++;
            }
        }
        
        // Tab items
        for (int n = 0; n < tab_count; n++) {
            if (!tab_open[n])
                continue;
                
            char buf[16];
            snprintf(buf, sizeof(buf), "Tab %d", n + 1);
            
            if (ImGui::BeginTabItem(buf, &tab_open[n])) {
                ImGui::Text("This is the content of %s", buf);
                ImGui::Text("Close me by clicking the X button.");
                
                if (ImGui::Button("Close This Tab")) {
                    tab_open[n] = false;
                }
                
                ImGui::EndTabItem();
            }
        }
        
        ImGui::EndTabBar();
    }
}

Build docs developers (and LLMs) love