CSS can be used to customize links and navigation menus. In CSS links we learned to customize link properties. In this lesson we will learn to customize navigation menus, which are a a list of links.
At the bottom of this page there is a code editor pre-loaded with the lesson's code.If you are not familar with lists, visit HTML Lists and CSS Lists. If you are not familar with <a> elements, visit HTML Navigation and CSS links.
/*<ul>, class = "myMenu"*/
ul.myMenu {
list-style-type: none;
padding-top:20px;
padding-bottom: 20px;
}
/* <a> links in elements with "myMenu" class*/
.myMenu a {
padding: 2px;
font-size: 16px;
}
/* <a> links specifically in <ul class 'myMenu'>*/
ul.myMenu a {
display:block;
width: 40%;
border-style: solid;
border-width: 1px;
border-color: lightgrey;
background-color: black;
}
/* <li> items, class = "myMenu"*/
li.myMenu{
text-decoration: none;
padding-top: 2.5px;
padding-bottom: 2.5px;
padding-right: 5px;
margin: 5px;
}
/* <a> hover*/
.myMenu a:hover {
background-color: white;
color: black;
}
/* "myMenu" unvisited links*/
.myMenu a:link {
color: white;
}
/* "myMenu" visited links*/
myMenu a:visited {
color:pink;
}
/* "myMenu" visited links: on hover*/
.myMenu a:visited:hover {
background-color: white;
color: black;
}
<ul class = "myMenu">
<li><a href = "index.php">Home</a></li>
<li><a href = "blog.php">Blog</a></li>
<li><a href = "photos.php">Photo Gallery</a></li>
<li><a href = "https://www.facebook.com/codingcommanders/">Facebook</a></li>
</ul>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- YouTube-->
<a href = 'https://www.youtube.com/channel/UC-gCUMK_EGUqJ7P9VA7BJmQ' class="fa fa-youtube"></a>
<!-- Facebook-->
<a href="https://www.facebook.com/codingcommanders/" class="fa fa-facebook"></a>
<!-- Instagram-->
<a href="https://www.instagram.com/codingcommanders" class="fa fa-instagram"></a>
<!-- Twitter-->
<a href="https://twitter.com/codingCommander" class="fa fa-twitter"></a>
<!-- Google+-->
<a href="https://plus.google.com/102944579677628755090" class="fa fa-google"></a>
<!-- Github-->
<a href="https://github.com/codingCommanders/" class="fa fa-github"></a>
Follow the same pattern with any other social media platform you wish to use. Most of them are included in the icon librabry.
Below is all the code used to create the example horizontal menu. Remember to click the orange info buttons for descriptions and related lesson links.
ul.horizon {
overflow: hidden;
background-color: #006310;
border-radius: 9px;
list-style-type: none;
font-family: helvetica, arial, sans-serif;
}
➼ ul.horizon
- This code block is for "horizon" class unordered lists. - (CSS Lists,HTML Lists,CSS Classes)
➼ overflow: hidden;
- Any content larger than it's element will be cut off.
➼ background-color
- #006310 - (CSS Colors)
➼ border-radius
- 9px - (CSS Border-radius)
➼ list-style-type
- none - (CSS Lists)
➼ font-family
- helvetica, arial, sans-serif; - (CSS Fonts)
.horizon a {
float: left;
display: block;
color: #E7F0D8;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
width: 14%;
}
➼ .horizon a
- This code block is for <a> links within a "horizon" class element. - (CSS Links, HTML Links, CSS Classes)
➼ float
- left - (CSS Align & Position)
➼ display
- block - Displays like a block element (div, paragraph ect) - (HTML divs)
➼ text color
- #E7F0D8 - (CSS Colors)
➼ text-align
- center - (CSS Align & Position)
➼ padding
- 14px 16px - (CSS Padding)
➼ text-decoration
- none - (CSS Links)
➼ font-size
- 17px - (CSS Fonts)
➼ width
- 14% - each link will take up about 14% of the parent element's space.
width = 14%;
?
.horizon a:hover {
background-color: black;
color: #B5B5B5;
}
➼ .horizon a:hover
- hover selector - This code applies to <a> links that are inside a "horizon" class
element, when mouse hover is true. (aka when the user hovers over a link located inside a "horizon" element) (CSS Links,HTML Links)
➼ colors
- (CSS Colors)
<!DOCTYPE html>
<html lang="en-us">
<head>
<!-- Meta Viewport-->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Stylesheet with our CSS code-->
<link rel="stylesheet" type="text/css" href="customMenus.css">
<!-- Stylesheet URL to include social media icons-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- Page Title-->
<title>Coding Commanders: Menus</title>
</head>
<body>
<!-- Horizontal Menu (class = "horizon")-->
<ul class = "horizon">
<li><a href = "index.php">Home</a></li>
<li><a href = "blog.php">Blog</a></li>
<li><a href = "photos.php">Photos</a></li>
<li><a href = 'https://www.facebook.com/codingcommanders/' class ='fa fa-facebook'></a></li>
<li><a href = 'https://www.instagram.com/codingcommanders' class ='fa fa-instagram'></a></li>
<li><a href = 'https://github.com/codingCommanders/' class ='fa fa-github'></a></li>
<li><a href = 'https://www.youtube.com/channel/UC-gCUMK_EGUqJ7P9VA7BJmQ' class='fa fa-youtube'></a></li>
</ul>
</body>
</html>
Continue working on the Ad Project:
Part I: Explained on the bottom of CSS Color Lesson.
AND
Part II: Explained on the bottom of CSS Links Lesson.