Step four
1. CSS Navigation Bar
Navigation Bars
Navigation bars are crucial for any website, providing a user-friendly way to navigate through the site.
With CSS, you can transform simple HTML menus into stylish navigation bars.
Navigation Bar = List of Links
A navigation bar is fundamentally a list of links, making the <ul>
and <li>
elements a logical choice.
Example
<ul>
<li><a href="default.asp">Home</a></li>
<li><a href="news.asp">News</a></li>
<li><a href="contact.asp">Contact</a></li>
<li><a href="about.asp">About</a></li>
</ul>
To style the list as a navigation bar, start by removing bullets, and default margins and paddings:
Example
Example explained: list-style-type: none; - This removes the bullets, making it more suitable for navigation bars. Set margin: 0; and padding: 0; to clear browser defaults, ensuring consistency across different browsers. This code forms the basis for both vertical and horizontal navigation bars.
Vertical Navigation Bar
For a vertical navigation bar, style the <a> elements within the list, in addition to the previous styles:
Example
Example explained:
display: block; - Makes the entire area of the link clickable, not just the text, and allows for setting dimensions like width.
width: 60px; - By default, block elements take up the full width available. Specifying width helps control the layout.
Setting the <ul>
width and removing the <a>
width lets them fill the available space, useful for a clean vertical bar look.
Example
li a {
display: block;
}
Vertical Navigation Bar Examples
Create a basic vertical navigation bar with a gray background and change the background color of links on hover:
Example
ul {
list-style-type: none;
margin: 0;
padding: 0;
width: 200px; /* Adjust width as needed */
background-color: #f1f1f1;
}
li a {
display: block;
color: #000;
padding: 8px 16px;
text-decoration: none;
}
/* Change the link color on hover */
li a:hover {
background-color: #555;
color: white;
}
2. Adding Images to Web Pages
Images can be embedded in a webpage via CSS, offering advantages such as responsive design adjustments. CSS can change an image's display properties based on the screen size, ensuring optimal layout across devices.
Image File Types
Each image file type has its unique characteristics. Understanding these can help you choose the most suitable format for your website's needs.
.jpg:
Small file sizes facilitate quicker loading times, ideal for photographs due to good color reproduction. However, quality may decrease when scaled up.
.png:
Known for supporting transparencies and maintaining color depth with high quality, but larger file sizes may slow down web loading times.
.gif:
Best suited for animations, offering small file sizes and sharp image quality. Though it supports transparency, it can result in a halo effect around images.
.svg
(Scalable Vector Graphics): Excellent for graphics that need to scale without loss of quality, such as logos and icons.
Pros:
.webp:
Offers superior compression techniques, achieving high quality with smaller file sizes compared to jpg and png. Well-supported in modern browsers.
.pdf:
While not traditionally used for web images, PDFs are becoming more common for certain types of web content.
Pros:
.tiff:
Once used in early web design, TIFF files are now impractical for web use due to their size.
Pros:
Steps for Implementing an Image
<img>
tag or CSS, depending on the context and requirements for responsiveness.
Steps for Getting an Image Ready for Web Use
Always store original, unedited image files separately for future editing needs.
To embed an image in your page: