Tips for aligning my icon in the middle when I collapse my sidebar

Seeking assistance on centering the hamburger icon on my sidebar. Currently, when I adjust the width of the sidebar, the icon is not centered at all. Does anyone have a solution for achieving this? I attempted using justify-content: center in the main class but it did not work.

let btnSidebar = document.querySelector(".bx-menu")
let sidebar = document.querySelector(".nk-sidebar")
btnSidebar.addEventListener("click", ()=> {
    sidebar.classList.toggle("close")
})
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500&display=swap');


* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: 'Poppins', sans-serif;
}

.nk-sidebar {
    position: fixed;
    top: 0;
    height: 100%;
    width: 270px;

    display: flex;
    flex-direction: column;
    border-right: 1px solid #e6e5e5;
    padding: 10px;
    transition: 0.3s ease all;
}

.nk-sidebar.close {
    width: 73px;
}

.nk-sidebar .nk-sb-hrd {
    display: flex;
    align-items: center;
    /* flex: 0 0 50px; */
    /* padding: 10px; */
    width: 100%;
    gap: 10px;
    background: black;
}
.nk-sidebar .nk-sb-hrd .nk-sb-logo {
    font-size: 16px;
    color: #fff;
    font-weight: 600;
}

.nk-sidebar .nk-sb-nav {
    flex: 1;
}

.nk-sidebar .nk-sb-footer {
    display: flex;
    flex-direction: column;

}

.nk-sidebar .nk-sb-hrd .nk-sb-logo {
    min-width: 50px;
    height: 50px;
    width: 50px;
    padding: 5px;
    border-radius: 10px;
}

.nk-sb-hr-ham {
    height: 40px;
    min-width: 40px;
    border: 1px solid #e6e5e5;
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    align-items: center;
    justify-content: center;
    cursor: pointer;
        background: red;
}

.ham {
    width: 20px;
    height: 2px;
    background: #0057d9;
}
.nk-sb-company {
    font-weight: 600;
    color: #0057d9;
    font-size: 14px;
    white-space: nowrap;
}

.nk-section {
    position: relative;
    left: 270px;
    width: calc(100% - 270px);
    padding: 10px;
    height: 100vh;
    background: yellow;
    transition: 0.3s ease all;
}

.nk-sidebar.close .nk-sb-company {
    opacity: 0;
    pointer-events: none;
}

.nk-sidebar.close ~ .nk-section {
    left: 73px;
    width: calc(100% - 73px);
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../materials/fonts/new/fonts.css">
    <link rel="stylesheet" href="../materials/css/dashboard.css">
    <script src="../materials/plugins/jquery-3.6.0.js"></script>
    <link href='https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eb8984938288848598abd9c5dac5d9">[email protected]</a>/css/boxicons.min.css' rel='stylesheet'>
    
</head>
<body>
<div class="nk-sidebar">
    <div class="nk-sb-hrd">
        <div class="nk-sb-hr-ham">
            <div class="ham top"></div>
            <div class="ham mid"></div>
            <div class="ham bot"></div>
        </div>
        <div class="nk-sb-company">Zybm Business Applications</div>
    </div>

    <div class="nk-sb-nav">

    </div>

    <div class="nk-sb-footer">
asdasd
    </div>
</div>

<div class="nk-section">
    <i class="bx bx-menu"></i>
</div>
<script type="text/javascript" src="../materials/js/dashboard.js"></script>
</body>
</html>

Answer №1

You can utilize it in this manner

<div style="
    display: flex;
    justify-content: space-around;
">...CONTENT...</div>

I have made some modifications to your code.

let btnSidebar = document.querySelector(".bx-menu")
let sidebar = document.querySelector(".nk-sidebar")
btnSidebar.addEventListener("click", ()=> {
    sidebar.classList.toggle("close")
})
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500&display=swap');


* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    font-family: 'Poppins', sans-serif;
}

.nk-sidebar {
    position: fixed;
    top: 0;
    height: 100%;
    width: 270px;

    display: flex;
    flex-direction: column;
    border-right: 1px solid #e6e5e5;
    padding: 10px;
    transition: 0.3s ease all;
}

.nk-sidebar.close {
    width: 73px;
}

.nk-sidebar .nk-sb-hrd {
    display: flex;
    align-items: center;
    /* flex: 0 0 50px; */
    /* padding: 10px; */
    width: 100%;
    gap: 10px;
    background: black;
}
.nk-sidebar .nk-sb-hrd .nk-sb-logo {
    font-size: 16px;
    color: #fff;
    font-weight: 600;
}

.nk-sidebar .nk-sb-nav {
    flex: 1;
}

.nk-sidebar .nk-sb-footer {
    display: flex;
    flex-direction: column;

}

.nk-sidebar .nk-sb-hrd .nk-sb-logo {
    min-width: 50px;
    height: 50px;
    width: 50px;
    padding: 5px;
    border-radius: 10px;
}

.nk-sb-hr-ham {
    height: 40px;
    min-width: 40px;
    border: 1px solid #e6e5e5;
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    align-items: center;
    justify-content: center;
    cursor: pointer;
        background: red;
}

.ham {
    width: 20px;
    height: 2px;
    background: #0057d9;
}
.nk-sb-company {
    font-weight: 600;
    color: #0057d9;
    font-size: 14px;
    white-space: nowrap;
}

.nk-section {
    position: relative;
    left: 270px;
    width: calc(100% - 270px);
    padding: 10px;
    height: 100vh;
    background: yellow;
    transition: 0.3s ease all;
}

.nk-sidebar.close .nk-sb-company {
    opacity: 0;
    pointer-events: none;
}

.nk-sidebar.close ~ .nk-section {
    left: 73px;
    width: calc(100% - 73px);
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../materials/fonts/new/fonts.css">
    <link rel="stylesheet" href="../materials/css/dashboard.css">
    <script src="../materials/plugins/jquery-3.6.0.js"></script>
    <link href='https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90f2ffe8f9f3fffee3d0a2bea1bea2">[email protected]</a>/css/boxicons.min.css' rel='stylesheet'>
    
</head>
<body>
<div class="nk-sidebar">
    <div class="nk-sb-hrd">
        <div class="nk-sb-hr-ham">
            <div class="ham top"></div>
            <div class="ham mid"></div>
            <div class="ham bot"></div>
        </div>
        <div class="nk-sb-company">Zybm Business Applications</div>
    </div>

    <div class="nk-sb-nav">

    </div>

    <div class="nk-sb-footer">
asdasd
    </div>
</div>

<div class="nk-section">
    <div style="display:flex; justify-content: space-around;"><i class="bx bx-menu"></i></div>
</div>
<script type="text/javascript" src="../materials/js/dashboard.js"></script>
</body>
</html>

UPDATE : https://codepen.io/agokselb/pen/eYrYBMP

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Placing an image at the bottom within a DIV container in HTML

My goal is to neatly align a group of images at the bottom of a fixed-height div. The images all have the same height and width, making the alignment easier. Here is the current structure of my code: <div id="randomContainer"> <div id="image ...

Why is a div nested inside an overflow: hidden; div not expanding to the full width of its content?

.container { white-space: nowrap; overflow: hidden; } .inner { width: 100%; } .list-item { display: 'inline-block', boxSizing: 'border-box', border: '3px solid black' } import React, { Component } from 're ...

Having trouble getting my AngularJS animations to work, hitting a roadblock

Here is the code I've put together for you to review, with no official purpose in mind. I just saved this code to use at a later time. <script> var apps = angular.module('myApp', ['ngAnimate']); //header apps.cont ...

Ways to create a self-contained video viewer

Is it possible to create a self-contained video player similar to jwplayer or the YouTube video player using just HTML, CSS, and JavaScript? I know that I can build a video player by utilizing the video tag along with some custom javascript and css, but ho ...

Customize the background color of a particular select element within an array of dynamically created select elements

I've been experimenting with jQuery to dynamically adjust the background color of a select element based on the chosen option. The select elements are being generated using the following code: while ($qrow = $qquery->fetch(PDO::FETCH_ASSOC)) { ...

displaying an item within a text field

I have an input box created using Angular reactive forms <input type="text" formControlName="OrgName" placeholder="Enter name" maxlength="60"> <p class="fieldRequired" *ngIf="showNameMSg" ...

What is the best way to eliminate the margin on the <v-textarea> component in Vuetify?

My textarea seems to have a top margin that I can't get rid of. Here is my code: <v-flex d-flex xs12> <v-textarea v-model="test" outline type="text" color="primary" v-valida ...

Create a circular status button that overlays on top of another element using CSS

Looking to enhance my CSS skills! Seeking advice on creating a status button that changes color based on chat availability and positioning it on top of another button. ...

Styling the background of the endAdornment in a material-ui textfield using React

I tried following the instructions from this source: Unfortunately, the example code provided doesn't seem to be functioning properly now. Is there a way to achieve the same result without having that right margin so that it aligns better with the r ...

Having trouble loading styles in Vue after publishing to npm

I'm currently using vue-cli3 for the npm publication of a Vue component. Below are my build scripts: "package-build": "eclint fix && vue-cli-service build --target lib --name @xchat-component/chat-component ./src/index.ts & ...

Issue with mix-blend-mode causing text to not show up on top of the gradient

I am struggling to find a way to make text easily readable over my gradient background. Despite trying to use MUI's Typography or a simple <p>, the contrast isn't great as the text appears in black. After some research, I discovered that I ...

Issues with jQuery causing responsive CSS triangles to fail implementations

Recently, I stumbled upon the interesting concept of CSS triangles created using element borders. I decided to incorporate this design into one of my custom Wordpress themes, but I encountered a problem. I wanted to add a triangle at the bottom of a div, l ...

Most effective approach to managing state in a React UI Component

I recently delved into the world of React. I've been using it to build a design system library for my work, which consists of standalone UI components tailored for use in React applications. I've developed a specific approach to managing the sta ...

How can I align the title of a cell to the left while centering the remaining content?

I have a dilemma with two table cells placed side by side that stack upon triggering a media query for an HTML newsletter. I wish to align the headlines "Be Ready" and "Stay Organized" to the left when the responsive code activates, but the use of "margin: ...

Strange Firefox behavior with absolutely positioned div and percentage height

One of my projects includes a CSS modal dialog that is structured as follows div { position:fixed; top:50%; left:50%; margin:-26% 0 0 -26%; width:46%; height:46%; padding:3%; } Interestingly, this div appears centered in webkit browsers. Ho ...

Is it possible to incorporate HTML content into the metadata within the head section of a Nuxt application?

Received HTML content from the backend that needs to be incorporated into the meta tag of the HTML head using nuxt. Encountered an error when attempting to display the received content View Error Outlined below is the code implementation: Snippet of Code ...

How to optimize form fields in Bootstrap by utilizing the size/maxlength attributes in HTML inputs

When I attempted to utilize html5's form size/maxlength with bootstrap, I encountered an intriguing issue. The .form-control class in bootstrap overrides the size, but if removed, the input loses its styling. Check out the code pen here: http://code ...

The overflow:hidden feature doesn't appear to be functioning as expected

I am struggling to hide the text details within a div containing text and image, organized in ul li structure. Despite applying overflow hidden to the .sbox class, the text details refuse to remain hidden and appear to be overflowing. Explore this issue o ...

Adjusting the border-right size based on scroll position

Apologies if this question seems trivial, but I am completely new to coding in HTML and JavaScript. I understand that I can make some changes based on scroll position, but right now I'm a little confused. I want to increase the height of a box as the ...

Using colored circles in ASP Dropdownlist ListItems (without jQuery): A step-by-step guide

Objective: To create a Dropdown list that displays a green circle if someone's Availability is True, and a red circle if someone's Availability is False. Note: The task needs to be accomplished without the use of jQuery, as it is restricted in t ...