Course Content
CSS Basic
0/1
CSS Selectors
0/1
CSS Comments
0/1
CSS Backgrounds
0/1
CSS Borders
0/1
CSS Outline
0/1
CSS Fonts
0/1
CSS Height and Width
0/1
CSS Margins and Paddings
0/2
CSS Icons
0/1
CSS Links
0/1
CSS Lists
0/1
CSS Tables
0/1
CSS Display Properties
0/1
CSS Max-Width Property
0/1
CSS Positioning Elements
0/1
CSS Z-Index Property
0/1
CSS Overflow
0/1
CSS Float
0/1
CSS Opacity
0/1
CSS Forms
0/1
CSS Dropdowns
0/1
CSS Buttons
0/1
CSS Media Queries
0/1
About Lesson

How to create Vertical Navbar using CSS?

Cards are versatile components that can be used for displaying various types of content, such as articles, products, or user profiles.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vertical Navbar</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="navbar">
        <a href="#" class="active">Home</a>
        <a href="#">About</a>
        <a href="#">Services</a>
        <a href="#">Contact</a>
    </div>
</body>
</html>
CSS
body {
    margin: 0;
    font-family: Arial, sans-serif;
}
  • margin: 0;: Resets the default margin of the <body> element to 0, removing any default spacing.
  • font-family: Arial, sans-serif;: Sets the default font family for the entire document to Arial, falling back to a generic sans-serif font if Arial is not available.
CSS
.navbar {
    height: 100%;
    width: 200px;
    background-color: #333;
    position: fixed;
    left: 0;
    top: 0;
    overflow-x: hidden;
    padding-top: 20px;
}
  • height: 100%;: Sets the height of the navbar to 100% of the viewport height, making it span the entire height of the browser window.
  • width: 200px;: Sets the width of the navbar to 200 pixels.
  • background-color: #333;: Sets the background color of the navbar to a dark gray color (#333).
  • position: fixed;: Fixes the position of the navbar, so it remains in place even when the user scrolls the page.
  • left: 0;: Positions the navbar flush against the left edge of the viewport.
  • top: 0;: Positions the navbar flush against the top edge of the viewport.
  • overflow-x: hidden;: Hides any content that overflows horizontally, preventing a horizontal scrollbar from appearing.
  • padding-top: 20px;: Adds 20 pixels of padding to the top of the navbar, creating space between the content and the top edge of the navbar.
CSS
.navbar a {
    display: block;
    color: white;
    padding: 10px 20px;
    text-decoration: none;
}
  • display: block;: Makes the links appear as blocks, causing them to take up the full width of their container and allowing for padding and margins to be applied.
  • color: white;: Sets the text color of the links to white.
  • padding: 10px 20px;: Adds 10 pixels of padding to the top and bottom, and 20 pixels of padding to the left and right of each link.
  • text-decoration: none;: Removes the underline decoration from the links.
CSS
.navbar a:hover {
    background-color: #555;
}
  • background-color: #555;: Changes the background color of the links to a darker shade of gray (#555) when hovered over.
CSS
.navbar a.active {
    background-color: #4CAF50;
}
  • background-color: #4CAF50;: Sets the background color of the active link to a green color (#4CAF50). This would typically be used to indicate the currently selected or active page in the navbar.