Search code examples
htmlcsscenteringtext-align

How to center sidebar text and icons in HTML/CSS?


I'm trying to center both the text and icons within my website's sidebar, but my existing CSS text-align: center; isn't working. Here's the relevant code and screenshots:

enter image description here

How to convert it to like this one example below:

enter image description here

Specific Questions:

  • Can you identify why text-align: center; isn't working in this context?
  • What's the best CSS approach to achieve the centering of both text and icons within the sidebar?

Code:

CSS

/* sidebar starts here*/
.sidebar {
    position: fixed;
    top: 60px;
    width: 260px;
    height: calc(100% - 60px);
    background: #df7f27;
    overflow-x: hidden;
}

.sidebar ul {
    margin-top: 20px;
}
.sidebar ul li {
    width: 100%;
    list-style: none;
}
.sidebar ul li a {
    width: 100%;
    text-decoration: none;
    color: rgb(255, 255, 255);
    height: 60px;
    display: flex;
    align-items: center;
}

.sidebar ul li a i {
    min-width: 60px;
    font-size: 24px;
    text-align: center;
}

HTML

<body>
    <div class="container">
        <div class="topbar">
            <div class="logo">
                <h2>Pomodone</h2>
            </div>
            <div class="search">
                <input type="text" id="search" placeholder="search here">
                <label for="search"><i class="fas fa-search"></i></label>
            </div>
            <i class="fas fa-bell"></i>
            <div class="user">
                <img src="img/user.jpg" alt="">
            </div>
        </div>
        <div class="sidebar">
            <ul>
                <li>
                    <a href="#">
                        <i class="fas fa-th-large"></i>
                        <div>Dashboard</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i class="fas fa-user-graduate"></i>
                        <div>Students</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i class="fas fa-chalkboard-teacher"></i>
                        <div>Teachers</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i class="fas fa-users"></i>
                        <div>Employees</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i class="fas fa-chart-bar"></i>
                        <div>Analytics</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i class="fas fa-hand-holding-usd"></i>
                        <div>Earnings</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i class="fas fa-cog"></i>
                        <div>Settings</div>
                    </a>
                </li>
                <li>
                    <a href="#">
                        <i class="fas fa-question"></i>
                        <div>Help</div>
                    </a>
                </li>
            </ul>
        </div>
        <div class="main"></div>
    </div>
</body>

Thank you in advance, I appreciate your help


Solution

  • Before anything first understand how the icons www.fontawesome.com works.

    There's only one simple thing to understand that is:

    That the < i > tag you used here is getting commented out and a svg is been loaded to your code above the tag
    <i class="sidebar-icon fas fa-user-graduate"></i>
    

    Solution

    Just add these Properties to your html

    • Padding-left: 30px or as per your requirement;
    • Justify-content: flex-start;
    .sidebar ul li a {
      width: 100%;
        text-decoration: none;
        color: rgb(255, 255, 255);
        height: 60px;
        display: flex;
        padding-left: 30px;
        justify-content: flex-start;
      align-items: center
    }
    
    .sidebar ul li a i {
        min-width: 60px;
        font-size: 24px;
        text-align: center;
    }
    

    New Class to be Added for svg

    • Added a fixed width for svg.
    • Margin-right to align the test in the same line
    .sidebar ul li a svg{
      margin-right: 15px;
      width: 25px !important;
    }
    

    Run this Code

    *{
        padding: 0;
        margin: 0;
        box-sizing: border-box;
        font-family: 'poppins', sans-serif;
    }
    .topbar{
        position: fixed;
        background: #fff;
        box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.08);
        width: 100%;
        height: 60px;
        padding: 0 20px;
        display: grid;
        grid-template-columns: 2fr 10fr 0.4fr 1fr;
        align-items: center;
        z-index: 1;
    }
    
    .logo h2{
        color: #df7f27;
    }
    
    .search{
        position: relative;
        width: 60%;
        justify-self: center;
    }
    
    .search input {
        width: 100%;
        height: 40px;
        padding: 0 40px;
        font-size: 16px;
        outline: none;
        border: none;
        border-radius: 10px;
        background: #f5f5f5;
        
    }
    .search label {
        position: absolute;
        right: 15px;
        top: 50%;
        transform: translateY(-50%);
    }
    .search i {
        position: absolute;
        right: 15px;
        top: 15px; 
        cursor: pointer;
    }
    
    .user{
        position: relative;
        width: 50px;
        height: 50px;
    }
    
    .user img{
        position: absolute;
        top: 0;
        left: 0;
        height: 100%;
        width: 100%;
        object-fit: cover; 
    }
    
    /* sidebar starts here*/
    .sidebar {
        position: fixed;
        top: 60px;
        width: 260px;
        height: calc(100% - 60px);
        background: #df7f27;
        overflow-x: hidden;
    }
    
    .sidebar ul {
        margin-top: 20px;
    }
    .sidebar ul li {
        width: 100%;
        list-style: none;
        display:  flex;
      justify-content: center;
    }
    .sidebar ul li a {
      width: 100%;
        text-decoration: none;
        color: rgb(255, 255, 255);
        height: 60px;
        display: flex;
        padding-left: 30px;
        justify-content: flex-start;
      align-items: center
    }
    
    .sidebar ul li a i {
        min-width: 60px;
        font-size: 24px;
        text-align: center;
        align-self: flex-start;
    }
    .sidebar ul li a svg{
      margin-right: 15px;
      width: 25px !important;
    }
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" href="styles.css">
        <script defer src="https://use.fontawesome.com/releases/v5.15.4/js/all.js" integrity="sha384-rOA1PnstxnOBLzCLMcre8ybwbTmemjzdNlILg8O7z1lUkLXozs4DHonlDtnE7fpc" crossorigin="anonymous"></script>
    <body>
        <div class="container">
            <div class="topbar">
                <div class="logo">
                    <h2>Pomodone</h2>
                </div>
                <div class="search">
                    <input type="text" id="search" placeholder="search here">
                    <label for="search"><i class="fas fa-search"></i></label>
                </div>
                <i class="fas fa-bell"></i>
                <div class="user">
                    <img src="img/user.jpg" alt="">
                </div>
            </div>
            <div class="sidebar">
                <ul>
                    <li>
                        <a href="#">
                            <i class="fas fa-th-large"></i>
                            <div>Dashboard</div>
                        </a>
                    </li>
                    <li>
                        <a href="#">
                            <i class="fas fa-user-graduate"></i>
                            <div>Students</div>
                        </a>
                    </li>
                    <li>
                        <a href="#">
                            <i class="fas fa-chalkboard-teacher"></i>
                            <div>Teachers</div>
                        </a>
                    </li>
                    <li>
                        <a href="#">
                            <i class="fas fa-users"></i>
                            <div>Employees</div>
                        </a>
                    </li>
                    <li>
                        <a href="#">
                            <i class="fas fa-chart-bar"></i>
                            <div>Analytics</div>
                        </a>
                    </li>
                    <li>
                        <a href="#">
                            <i class="fas fa-hand-holding-usd"></i>
                            <div>Earnings</div>
                        </a>
                    </li>
                    <li>
                        <a href="#">
                            <i class="fas fa-cog"></i>
                            <div>Settings</div>
                        </a>
                    </li>
                    <li>
                        <a href="#">
                            <i class="fas fa-question"></i>
                            <div>Help</div>
                        </a>
                    </li>
                </ul>
            </div>
            <div class="main"></div>
        </div>
    </body>
    </html>