Arranging icons using CSS and HTML

I've experimented with various CSS methods to align the icons with the text, but I'm facing an issue where the icons seem slightly shifted upward from the text baseline.

Here is how I target the icons:

/* Styling for the full-height sidebar */
.sidebar {
    height: 100%; /* Occupies the entire screen height */
    width: 180px; /* Sidebar width */
    position: absolute; /* Positions the sidebar relative to the browser window */
    z-index: 1; /* Places it on top of other elements */
    top: 0px; /* Top margin set to 0px */
    left: -140px; /* Left margin moves the sidebar off-screen, leaving 20px visible */
    padding: 10px 5px 5px 10px; /* Padding settings */
    background-color: #111; /* Background color */
    overflow-x: hidden;
}

.sidebar a {
    text-decoration: none; /* Removes underline from link elements */
    font-size: 20px; /* Font size for sidebar links */
    color:#818181; /* Font color for sidebar links */
    display: block;
    padding: 10px; /* Same as sidebar */
}

.sidebar a:hover {
    text-decoration: none; /* Removes underline from link elements */
    color: #f1f1f1; /* Changes font color of sidebar links on hover */
}

.sidebar:hover {
    left: 0;    /* Shows the sidebar on-screen when hovered over */
}
i {
    float: right;
}
<html>
<head>
      <title>Master Tracker</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <!-- Load a font library -->
      <link href="https://fonts.googleapis.com/css?family=Yantramanav:100,300,400,500,700,900" rel="stylesheet">
      <!-- Load an icon library -->
      <script src="https://kit.fontawesome.com/ae6badd0ee.js" crossorigin="anonymous"></script>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  </head>
<body>
<div class="sidebar">
            <a href="#home"><i class="fa fa-fw fa-home"></i> Home </a>
            <a href="#archive"><i class="fas fa-archive"></i></i> Archive </a>
            <a href="#newfile"><i class="fas fa-folder-open"></i> New File </a>
            <a href="#analytics"><i class="fas fa-chart-pie"></i> Analytics </a>
            <a href="#contacts"><i class="fas fa-address-book"></i> Contacts </a>
            <a href="#putoutfire"><i class="fas fa-dumpster-fire"></i> Hero </a>
            <a href="#search"><i class="fas fa-search"></i> Search </a>
            <a href="#help"><i class="fas fa-life-ring"></i> Help </a>
          </div>
</body>
</html>

The alignment looks fine in the post editor but appears off when viewed in the browser through VS Code. Any assistance on achieving proper icon alignment would be greatly appreciated.

Answer №1

Do you seek something along these lines?

I have implemented text-align: right for each icon. - Note that certain icons may differ in size, so adjust them individually until you are satisfied with the outcome.

/* Styling for the full-height sidebar */
.sidebar {
    height: 100%; /* Occupies entire screen height */
    width: 180px; /* Sidebar width */
    position: absolute; /* Positioned relative to the browser window */
    z-index: 1; /* Shows above other elements */
    top: 0px; /* Top margin set to 0px */
    left: -140px; /* Left margin shifts sidebar off-screen, with 20px visible */
    padding: 10px 5px 5px 10px; /* Padding of 15px */
    background-color: #111; /* Background color */
    overflow-x: hidden;
}

.sidebar a {
    text-decoration: none; /* Removes underline from link elements */
    font-size: 20px; /* Font size for sidebar links */
    color:#818181; /* Font color for sidebar links */
    display: block;
    padding: 10px; /* Same as sidebar */
}

.sidebar a:hover {
    text-decoration: none; /* Removes underline from link elements on hover */
    color: #f1f1f1; /* Changes font color of sidebar links on mouse over */
}

.sidebar:hover {
    left: 0;    /* Moves sidebar into view on hover */
}
i {
    float: right;
}
<html>
<head>
      <title>Master Tracker</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <!-- Include a font library -->
      <link href="https://fonts.googleapis.com/css?family=Yantramanav:100,300,400,500,700,900" rel="stylesheet">
      <!-- Include an icon library -->
      <script src="https://kit.fontawesome.com/ae6badd0ee.js" crossorigin="anonymous"></script>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  </head>
<body>
<div class="sidebar">
            <a href="#home"><i style="text-align: right;" class="fa fa-fw fa-home"></i> Home </a>
            <a href="#archive"><i style="text-align: right;" class="fas fa-archive"></i></i> Archive </a>
            <a href="#newfile"><i style="text-align: right;" class="fas fa-folder-open"></i> New File </a>
            <a href="#analytics"><i style="text-align: right;" class="fas fa-chart-pie"></i> Analytics </a>
            <a href="#contacts"><i style="text-align: right;" class="fas fa-address-book"></i> Contacts </a>
            <a href="#putoutfire"><i style="text-align: right;" class="fas fa-dumpster-fire"></i> Hero </a>
            <a href="#search"><i style="text-align: right;" class="fas fa-search"></i> Search </a>
            <a href="#help"><i style="text-align: right;" class="fas fa-life-ring"></i> Help </a>
          </div>
</body>
</html>

Answer №2

This information will assist you.
I observed discrepancies in the image sizes and made adjustments to standardize them.

/* Styling for a full height sidebar */
.sidebar {
    height: 100%; /* Takes up the entire screen height */
    width: 180px; /* Sidebar width */
    position: absolute; /* Positioned relative to the browser window */
    z-index: 1; /* Appears above other elements */
    top: 0px; /* Top margin set to 0px */
    left: -140px; /* Left margin positions the sidebar off-screen with 20px visible */
    padding: 10px 5px 5px 10px; /* Padding of 15px */
    background-color: #111; /* Background color */
    overflow-x: hidden;
}

.sidebar a {
    text-decoration: none; /* Removes underline from link elements */
    font-size: 20px; /* Font size for sidebar links */
    color:#818181; /* Font color for sidebar links */
    display: block;
    padding: 10px; /* Same padding as the sidebar */
}

.sidebar a:hover {
    text-decoration: none; /* Stops underline on mouse hover */
    color: #f1f1f1; /* Changes font color on mouse hover */
}

.sidebar:hover {
    left: 0;    /* Sidebar appears on-screen on mouse hover */
}
i {
    float: right;
    width:20px;
    height:20px;
}
<html>
<head>
      <title>Master Tracker</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <!-- Load a font library -->
      <link href="https://fonts.googleapis.com/css?family=Yantramanav:100,300,400,500,700,900" rel="stylesheet">
      <!-- Load an icon library -->
      <script src="https://kit.fontawesome.com/ae6badd0ee.js" crossorigin="anonymous"></script>
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
  </head>
<body>
<div class="sidebar">
            <a href="#home"><i class="fas fa-home"></i> Home </a>
            <a href="#archive"><i class="fas fa-archive"></i></i> Archive </a>
            <a href="#newfile"><i class="fas fa-folder-open"></i> New File </a>
            <a href="#analytics"><i class="fas fa-chart-pie"></i> Analytics </a>
            <a href="#contacts"><i class="fas fa-address-book"></i> Contacts </a>
            <a href="#putoutfire"><i class="fas fa-dumpster-fire"></i> Hero </a>
            <a href="#search"><i class="fas fa-search"></i> Search </a>
            <a href="#help"><i class="fas fa-life-ring"></i> Help </a>
          </div>
</body>
</html>

I made modifications to the following code:

<a href="#home"><i class="fa fa-fw fa-home"></i> Home </a><!--removed fa-fw and fa-->
<a href="#home"><i class="fas fa-home"></i> Home </a><!--added fas-->

Additionally, I added this piece of code:

i {
float: right;
width:20px;//added
height:20px;//added
}

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

Fade out the div element when clicked

For my game project, I needed a way to make a div fade out after an onclick event. However, the issue I encountered was that after fading out, the div would reappear. Ideally, I wanted it to simply disappear without any sort of fade effect. Below is the co ...

Ways to calculate outcome

I'm fairly new to PHP and have run into an issue regarding displaying the number of results. For example, showing 'There are 200 results'. Thank you in advance. Below is the code I am working with: try { $bdd = new PDO("mysql:host=localho ...

What could be causing the malfunction in this positioning and display?

<article id="l1"> <img class="active_pic" id="a1" src="img/load.gif"/> </article> <article id="l2"> <img class="active_pic" id="a2" src="img/load.gif"/> </article> <article id="l3"> <img class="activ ...

Issue with Photoswipe pswp class Not Getting Properly Cleared Upon Closing Image

My website has a Photoswipe image gallery from . The issue I'm facing is that the CSS class does not reset or clear after closing the gallery for the second time. For example, when a user opens item 1, the images are loaded into the picture div via A ...

Prevent side-to-side scrolling using the translate property

With Bootstrap 4, I successfully implemented a functionality where clicking a button hides/shows the sidebar on desktop, allowing the main content area to occupy the remaining space: HTML: <a class="sidebar-toggle" href="#"><i class="fa fa-bars" ...

Retrieving the chosen option in Vue.js when the @change event occurs

I have a dropdown menu and I want to perform different actions depending on the selected option. I am using a separate vue.html and TypeScript file. Here is my code snippet: <select name="LeaveType" @change="onChange()" class="f ...

CSS regression testing through version control systems and continuous integration

Is there a tool that integrates with CI systems and VCS like Git to automatically assign regression test results to individual commits? Ideally, this tool would provide a concise summary on a main page showing passed/failed numbers for each commit, with th ...

Is there a way to verify the percentages and margins in this HTML code?

On the lower half of the page, I have three divs named (vd-grid-sub-box). I am attempting to equalize the gaps between each of these three divs. Currently, there is a larger gap between the 2nd and 3rd div compared to the 1st and 2nd. However, every time ...

Arrange pictures and div elements in a horizontal layout

I am facing an issue with my code that is displaying 5 'cards' in a messed up layout instead of a straight line. I have tried to align them vertically, but some are still higher and not in the right positions. For reference, I want the layout to ...

What is the best way to ensure my image and text are centered and responsive?

After resizing the window, here's the desired outcome I am aiming for: https://i.sstatic.net/FdQKg.jpg The issue with this result is the lack of centering. When I attempt to center them, it leads to a problematic layout as shown here: the problem. I ...

Implementing Decimal Input in Bootstrap-Vue for Google Chrome on Android

Issue is isolated to Google Chrome for Android (version 90.0.4430.210) and not present in other browsers. We have an input field that only allows numeric characters, structured like this: https://i.sstatic.net/zi4qn.png <b-form-input v-model="m ...

JavaScript does not function properly with dynamically loaded content

Trying to load the page content using the JQuery load() method. See below for the code snippet: $(window).ready(function() { $('#content').load('views/login.html'); }); .mdl-layout { align-items: center; justify-content: center ...

Dividing an AngularJS module across multiple iFrames on a single webpage

Currently, I am working on a web application that consists of 1 module, 5 pages, and 5 controllers. Each HTML page declares the same ng-app. These pages are loaded within widgets on a web portal, meaning each page is loaded within an iFrame in the portal. ...

Creating an HTML file using PUG in a local environment (using npm and gulp)

Is there a way to automatically generate an HTML file with the same name as my Pug file whenever I save using gulp? I've checked all the documentation on but it only explains how to return Pug content in console... ...

IE9 does not properly display SVGs used as background images

Utilizing SVG files as backgrounds for my HTML elements has been successful in most major browsers. However, an issue arises when trying to ensure compatibility with Internet Explorer 9. The problem lies within IE9's rendering of the SVG backgrounds, ...

A comprehensive guide on connecting an HTML Label to a sqldatareader

Trying to associate a label with a datareader has been challenging. When dealing with an input type="text", I can easily accomplish something like this: NameLabel.Value = reader["Name"].ToString(); - <label id="lblName" runat="server"></label&g ...

What is the best way to achieve a full-width navbar with the scrollbar positioned below it?

I have a similar code to what's shown here The menu is fixed, but the scrollbar seems to be appearing from the right of the navbar What I want is for the scrollbar to start below the menu instead see image example here ...

Prevent the resizing of my website on iOS devices using HTML and CSS

I'm encountering an issue with a website I developed that seems to be resizable on certain iOS devices, including the new iPhone X. I haven't been able to replicate this behavior on other standard websites. Perhaps there's a simple CSS solut ...

trouble with image tag not displaying correct source URL

Currently, my views.py in Django is rendering a list of objects to my html template successfully. Each object has an associated object ID, and I have named the images in the 'static/images/' directory accordingly. For example, image 1.jpg corres ...

Conceal the pseudo element when clicked

Running a WordPress site, I have a button on the home page that is set to load more posts when clicked. I wanted to add a touch of design by including an upside-down caret next to the text "load more posts" using pseudo-elements. While I successfully achie ...