.
Inside the base container we place the content we need.
) formed in css in the form of the very “hamburger button”, with the help of which we will establish a connection with the hidden checkbox . For this, it is mandatory, the name of the for attribute must match the id of the checkbox. An empty onclick attribute is used for iOS
website <
ul>
<
li><
a href=
"#1"
>One
a>
<
li><
a href=
"#2"
>Two
a>
<
li><
a href=
"#3"
>Three
a>
<
li><
a href=
"#4"
>Four
a>
<
li><
a href=
"#5"
>Five
a>
<
li><
a href=
"#6"
>Six
a>
<
li><
a href=
"#7"
>Seven
a>
ul>
One Two Three Four Five Six Seven This concludes our side menu layout. True, there is one more completely optional feature, this is the background of dimming the main content when the panel is on. If you need it, just write below the menu, or in any other place of the page body, an additional div-container with a certain class:
<
div class
=
"mask-content"
>
div>
In the demo, I excluded this feature from work by default, commenting out this block, if you suddenly need it, you can easily find it and turn it on just as easily))). In general, to make it easier to understand the html of the demo page, I wrote detailed comments for each element, so it will take a lot of effort to get lost.
So, all the necessary elements are in place, the most important and interesting thing remains, to form appearance , color, shape, and give movement to our menu. We will do all this exclusively with CSS. No javascript or additional images.
css I will not describe each rule and property, since I did it directly in the css code . The styles for the panels located on the left or right are almost the same, they differ only in a couple of values. In the source archive, both options are packed as separate files, so choose the one you need, connect it correctly to the document and that's it. Here I post the “minced css” for the menu that slides out from the left edge of the page:
/** * Switchable side navigation bar * slides out on left click */ . nav ( /* width is arbitrary, feel free to experiment */ width: 320px min-width: 320px; /* fix and set the height of the panel to the maximum */ height: 100%; position: fixed; top: 0 bottom: 0 margin: 0 /* shift (hide) the panel relative to the left edge of the page */ left: -320px; /* padding */ padding: 15px 20px; /* smooth transition panel offset */ - webkit-transition: left 0. 3s; - moz-transition: left 0. 3s; transition: left 0. 3s; /* define the background color of the panel */ background: #16a085; /* on top of other elements */ z-index: 2000 ; ) /** * Panel toggle button * tag
*/
. nav-toggle( /* absolutely position */ position: absolute; /* relative to the left edge of the panel */ left: 320px; /* padding from the top edge of the panel */ top: 1em; /* padding */ padding: 0.5em /* define the background color of the radio button * more often according to the background color of the panel */ background: inherit; /* text color */ color: #dadada; /* cursor type */ cursor: pointer; /* font size */ font-size: 1.2em; line-height: 1 ; /* always on top of other page elements */ z-index: 2001; /* animate text color on hover */ -webkit-transition: color . 25s ease-in-out; - moz-transition: color . 25s ease-in-out; transition: color . 25s ease-in-out; ) /* define button text * Unicode character (TRIGRAM FOR HEAVEN) */ . nav- toggle: after ( content: "\2630" ; text- decoration: none; ) /* hover text color */ . nav-toggle: hover ( color: #f4f4f4; ) /** * Hidden checkbox (checkbox) * invisible and inaccessible :) * selector name flag attribute */ [ id="nav-toggle" ] ( position: absolute; display: none; ) /** * change switch position * when viewed on mobile devices * when nav is expanded, put it inside the panel */ [ id= "nav-toggle" ] : checked ~ . nav > . nav- toggle ( left: auto; right: 2px; top: 1em; ) /** * When the checkbox is checked, the panel opens * use the :checked pseudo-class */ [ id= "nav-toggle" ] : checked ~ . nav ( left: 0 ; box-shadow: 4px 0px 20px 0px rgba(0 , 0 , 0 , 0.5 ) ; - moz- box-shadow: 4px 0px 20px 0px rgba(0 , 0 , 0 , 0.5 ) ; - webkit- box-shadow: 4px 0px 20px 0px rgba(0 , 0 , 0 , 0.5 ) ; overflow- y: auto; ) /* * page content offset * by the size of the panel width, * feature is optional, for an amateur */ [ id= "nav-toggle" ] : checked ~ main > article ( - webkit- transform: translateX(320px) ; - moz- transform: translateX(320px) ; transform: translateX(320px) ; ) /* * change the switch symbol, * the usual cross (MULTIPLICATION X), * you can use any other icon */ [ id= "nav-toggle" ] : checked ~ . nav > . nav-toggle: after ( content: "\2715" ; ) /** * fix the bug in Android<= 4.1.2
* см: http://timpietrusky.com/advanced-checkbox-hack
*/
body ( - webkit-animation: bugfix infinite 1s; ) @- webkit- keyframes bugfix ( to ( padding: 0 ; ) ) /** * take care of medium and small screens * of mobile devices */ @ media screen and (min- width: 320px) ( html, body ( margin: 0 ; overflow- x: hidden; ) ) @ media screen and (max- width: 320px) ( html, body ( margin: 0 ; overflow- x: hidden; ) .nav ( width: 100%; box-shadow: none ) ) /** * We form the style of the title (logo) of the panel */ . nav h2 ( width: 90%; padding: 0 ; margin: 10px 0 ; text- align: center; text- shadow: rgba(255 , 255 , 255 , .1 ) - 1px - 1px 1px, rgba(0 , 0 , 0 , .5 ) 1px 1px 1px; font-size: 1. 3em; line-height: 1. 3em; opacity: 0 ; transform: scale(0.1 , 0.1 ) ; -ms- transform: scale(0.1 , 0.1 ) ; - moz- transform: scale(0.1 , 0.1 ) ; - webkit- transform: scale(0.1 , 0.1 ) ; transform- origin: 0 % 0 %; - ms- transform- origin: 0 % 0 %; - moz- transform- origin: 0 % 0 % - webkit- transform- origin: 0 % 0 % - transition: opacity 0. 8s, transform 0. 8s - ms- transition: opacity 0. 8s, - ms- transform 0. 8s - moz-transition: opacity 0.8s, -moz-transform 0.8s;-webkit-transition: opacity 0.8s, -webkit-transform 0.8s; ) . nav h2 a ( color: #dadada; text- decoration: none; text- transform: uppercase; ) /*smooth appearance of the title (logo) when opening the panel */ [ id= "nav-toggle" ] : checked ~ . nav h2 ( opacity: 1 ; transform: scale(1 , 1 ) ; - ms- transform: scale(1 , 1 ) ; - moz- transform: scale(1 , 1 ) ; - webkit- transform: scale(1 , 1 ) ; ) /** * form the menu itself * use an unordered list for menu items * fasten transforms and smooth transitions */ . nav > ul ( display: block; margin: 0 ; padding: 0 ; list-style: none; ) . nav > ul > li ( line-height: 2.5 ; opacity: 0 ; - webkit- transform: translateX(- 50 % ) ; - moz- transform: translateX(- 50 % ) ; - ms- transform: translateX(- 50 % ) ; transform: translateX(- 50 % ) ; - webkit-transition: opacity. 5s. 1s, - webkit- transform. 5s. 1s; - moz- transition: opacity. 5s. 1s, - moz- transform. 5s. 1s ;- ms- transition: opacity .5s .1s, - ms- transform .5s .1s; transition: opacity .5s .1s, transform .5s .1s; ) [ id= "nav-toggle" ] : checked ~ . nav > ul > li ( opacity: 1 ; - webkit- transform: translateX(0 ) ; - moz- transform: translateX(0 ) ; - ms- transform: translateX(0 ) ; transform: translateX(0 ) ; ) /* determine the intervals for the appearance of menu items */ . nav > ul > li: nth- child(2 ) ( - webkit- transition: opacity . 5s . 2s, - webkit- transform . 5s . 2s; transition: opacity . 5s . 2s, transform . 5s . 2s; ) . nav > ul > li: nth- child(3 ) ( - webkit- transition: opacity . 5s . 3s, - webkit- transform . 5s . 3s; transition: opacity . 5s . 3s, transform . 5s . 3s; ) . nav > ul > li: nth- child(4 ) ( - webkit- transition: opacity . 5s . 4s, - webkit- transform . 5s . 4s; transition: opacity . 5s . 4s, transform . 5s . 4s; ) . nav > ul > li: nth- child(5 ) ( - webkit- transition: opacity . 5s . 5s, - webkit- transform . 5s . 5s; transition: opacity . 5s . 5s, transform . 5s . 5s; ) . nav > ul > li: nth- child(6 ) ( - webkit- transition: opacity . 5s . 6s, - webkit- transform . 5s . 6s; transition: opacity . 5s . 6s, transform . 5s . 6s; ) . nav > ul > li: nth- child(7 ) ( - webkit- transition: opacity . 5s . 7s, - webkit- transform . 5s . 7s; transition: opacity . 5s . 7s, transform . 5s . 7s; ) /** * style menu item links */ . nav > ul > li > a ( display: inline- block; position: relative; padding: 0 ; font- family: "Open Sans" , sans-serif; font-weight: 300 ; font-size: 1. 2em; color : #dadada;width: 100%;text-decoration: none; /* smooth transition */ -webkit-transition: color . 5s ease, padding . 5s ease; - moz-transition: color . 5s ease, padding . 5s ease; transition: color . 5s ease, padding . 5s ease; ) /** * state of menu links on hover */ . nav > ul > li > a:hover, . nav > ul > li > a: focus ( color: white; padding-left: 15px; ) /** * underline menu links */ . nav > ul > li >
a :
before {
content :
""
;
display :
block ;
position :
absolute ;
right :
0
;
bottom :
0
;
height :
1px ;
width :
100
%;
-
webkit -
transition :
width 0s ease ;
transition :
width 0s ease ;
}
.
nav >
ul >
li >
a :
after {
content :
""
;
display :
block ;
position :
absolute ;
left :
0
;
bottom :
0
;
height :
1px ;
width :
100
%;
background :
#3bc1a0; -
webkit -
transition :
width .
5s ease ;
transition :
width .
5s ease ;
}
/** * animate the underline * of links on hover */
.
nav >
ul >
li >
a :
hover :
before {
width :
0
%;
background :
#3bc1a0; -
webkit -
transition :
width .
5s ease ;
transition :
width .
5s ease ;
}
.
nav >
ul >
li >
a :
hover :
after {
width :
0
%;
background :
transparent ;
-
webkit -
transition :
width 0s ease ;
transition :
width 0s ease ;
}
/* dimming background on main content * while blocking elements * controversial feature, if you need it * just uncomment */
/* .mask-content ( display: block; position: fixed; top: 0; left: 0; z-index: 1000; width: 100%; height: 100%; background-color: rgba(0, 0, 0 , 0.4); visibility: hidden; opacity: 0; ) :checked ~ .mask-content ( visibility: visible; opacity: 1; -webkit-transition: opacity .5s, visibility .5s; transition: opacity .5s, visibility . 5s;) */
/** * Switchable side navigation bar * pops up on left click */ .nav ( /* width is arbitrary, feel free to experiment */ width: 320px; min-width: 320px; /* fix and set the height of the panel to the maximum */ height : 100%; position: fixed; top: 0; bottom: 0; margin: 0; /* shift (hide) the panel relative to the left edge of the page */ left: -320px; /* padding */ padding: 15px 20px; / * smooth panel offset transition */ -webkit-transition: left 0.3s; -moz-transition: left 0.3s; transition: left 0.3s; /* set the background color of the panel */ background: #16a085; /* on top of other elements * / z-index: 2000; ) /** * Panel toggle button * tag */ .nav-toggle ( /* absolutely position */ position: absolute; /* relative to the left edge of the panel */ left: 320px; /* padding from the top edge of the panel */ top: 1em; /* padding */ padding: 0.5em; /* define the radio button background color * more often according to the panel background color */ background: inherit; /* text color */ color: #dadada; /* cursor type */ cursor: pointer; /* font size * / font-size: 1.2em; line-height: 1; /* always on top of other page elements */ z-index: 2001; /* animate text color on hover */ -webkit-transition: color .25s ease-in- out; -moz-transition: color .25s ease-in-out; transition: color .25s ease-in-out; ) /* define button text * Unicode character (TRIGRAM FOR HEAVEN) */ .nav-toggle:after ( content: "\2630"; text-decoration: none; ) /* hover text color */ .nav-toggle:hover ( color: #f4f4f4; ) /** * Hidden checkbox (checkbox) * invisible and unavailable :) * selector name flag attribute */ ( positi on: absolute; display: none; ) /** * change toggle position * when viewed on mobile devices * when nav is expanded, position inside the panel */ :checked ~ .nav > .nav-toggle ( left: auto; right: 2px; top: 1em; ) /* * * When the box is checked, the panel opens * use the pseudo-class :checked */ :checked ~ .nav ( left: 0; box-shadow:4px 0px 20px 0px rgba(0,0,0, 0.5); -moz-box-shadow :4px 0px 20px 0px rgba(0,0,0, 0.5); -webkit-box-shadow:4px 0px 20px 0px rgba(0,0,0, 0.5); overflow-y: auto; ) /* * content offset page * by the width of the panel, * feature optional, for an amateur */ :checked ~ main > article ( -webkit-transform: translateX(320px); -moz-transform: translateX(320px); transform: translateX(320px); ) /* * change toggle symbol, * custom cross (MULTIPLICATION X), * you can use any other icon */ :checked ~ .nav > .nav-toggle:after ( content: "\2715"; ) /** * prefix bug in android<= 4.1.2
* см: http://timpietrusky.com/advanced-checkbox-hack
*/ body {
-webkit-animation: bugfix infinite 1s;
} @-webkit-keyframes bugfix {
to {
padding: 0;
}
} /**
* позаботьтимся о средних и маленьких экранах
* мобильных устройств
*/ @media screen and (min-width: 320px) {
html,
body {
margin: 0;
overflow-x: hidden;
}
} @media screen and (max-width: 320px) {
html,
body {
margin: 0;
overflow-x: hidden;
}
.nav {
width: 100%;
box-shadow: none
}
} /**
* Формируем стиль заголовка (логотип) панели
*/ .nav h2 {
width: 90%;
padding: 0;
margin: 10px 0;
text-align: center;
text-shadow: rgba(255, 255, 255, .1) -1px -1px 1px, rgba(0, 0, 0, .5) 1px 1px 1px;
font-size: 1.3em;
line-height: 1.3em;
opacity: 0;
transform: scale(0.1, 0.1);
-ms-transform: scale(0.1, 0.1);
-moz-transform: scale(0.1, 0.1);
-webkit-transform: scale(0.1, 0.1);
transform-origin: 0% 0%;
-ms-transform-origin: 0% 0%;
-moz-transform-origin: 0% 0%;
-webkit-transform-origin: 0% 0%;
transition: opacity 0.8s, transform 0.8s;
-ms-transition: opacity 0.8s, -ms-transform 0.8s;
-moz-transition: opacity 0.8s, -moz-transform 0.8s;
-webkit-transition: opacity 0.8s, -webkit-transform 0.8s;
} .nav h2 a {
color: #dadada;
text-decoration: none;
text-transform: uppercase;
} /*плавное появление заголовка (логотипа) при раскрытии панели */ :checked ~ .nav h2 {
opacity: 1;
transform: scale(1, 1);
-ms-transform: scale(1, 1);
-moz-transform: scale(1, 1);
-webkit-transform: scale(1, 1);
} /**
* формируем непосредственно само меню
* используем неупорядоченный список для пунктов меню
* прикрутим трансфомации и плавные переходы
*/ .nav > ul ( display: block; margin: 0; padding: 0; list-style: none; ) .nav > ul > li ( line-height: 2.5; opacity: 0; -webkit-transform: translateX(-50%); -moz-transform: translateX(-50%); -ms-transform: translateX(-50%); transform: translateX(-50%); -webkit-transition: opacity .5s .1s, -webkit-transform .5s .1s; -moz-transition: opacity .5s .1s, -moz-transform .5s .1s; -ms-transition: opacity .5s .1s, -ms-transform .5s .1s; transition: opacity .5s .1s , transform .5s .1s; ) :checked ~ .nav > ul > li ( opacity: 1; -webkit-transform: translateX(0); -moz-transform: translateX(0); -ms-transform: translateX(0 ); transform: translateX(0); ) /* define menu item intervals */ .nav > ul > li:nth-child(2) ( -webkit-transition: opacity .5s .2s, -webkit-transform .5s .2s; transition: opacity .5s .2s, transform .5s .2s; ) .nav > ul > li:nth-child(3) ( -webkit-transition: opacity .5s .3s, -webkit-transform .5s . 3s;transition: opacity .5s .3s, transform .5s .3s; ) .nav > ul > li:nth-child(4) ( -webkit-transition: opacity .5s .4s, -webkit-transform .5s .4s; transition: opacity .5s .4s, transform .5s .4s; ) .nav > ul > li:nth-child(5) ( -webkit-transition: opacity .5s .5s, -webkit-transform .5s .5s; transition: opacity .5s .5s, transform .5s .5s; ) .nav > ul > li:nth-child(6) ( -webkit-transition: opacity .5s .6s, -webkit-transform .5s .6s; transition: opacity .5s .6s, transform .5s .6s; ) . nav > ul > li:nth-child(7) ( -webkit-transition: opacity .5s .7s, -webkit-transform .5s .7s; transition: opacity .5s .7s, transform .5s .7s; ) /* * * menu item link styling */ .nav > ul > li > a ( display: inline-block; position: relative; padding: 0; font-family: "Open Sans", sans-serif; font-weight: 300; font-size: 1.2em; color: #dadada; width: 100%; text-decoration: none; /* smooth transition */ -webkit-transition: color .5s ease, padding .5s ease; -moz-transition: color .5s ease, padding .5s ease; transition: color .5s ease, padding .5s ease; ) /** * state of menu links on hover */ .nav > ul > li > a:hover, .nav > ul > li > a:focus ( color: white; padding-left : 15px; ) /** * menu link underline */ .nav > ul > li > a:before ( content: ""; display: block; position: absolute; right: 0; bottom: 0; height: 1px; width: 100 %; -webkit-transition: width 0s ease; transition: width 0s ease; ) .nav > ul > li > a:after ( content: ""; display: block; position: absolute; left: 0; bottom: 0; height: 1px; width: 100%; background: #3bc1a0; -webkit-transition: width . 5s ease; transition: width .5s ease; ) /** * animate the underline * links on hover */ .nav > ul > li > a:hover:before ( width: 0%; background: #3bc1a0; -webkit-transition: width .5s ease; transition: width .5s ease; ) .nav > ul > li > a:hover:after ( width: 0%; background: transparent; -webkit-transition: width 0s ease; transition: width 0s ease; ) /* fade background to main content * while blocking elements * controversial feature, if you need it * just uncomment */ /* .mask-content ( display: block; position: fixed; top: 0; left: 0; z-index: 1000; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.4); visibility: hidden; opacity: 0; ) :checked ~ .mask-content ( visibility: visible; opacity: 1; -webkit -transition: opacity .5s, visibility .5s; transition: opacity .5s, visibility .5s; ) */
As you understand, almost all property values are optional, i.e. you can easily change all its elements to your taste and color, the explanations, I hope, will help you with this. Well, if something doesn’t trample on, or some of my cant is found, write in the comments, we will definitely figure it out and fix it.
In conclusion, I want to remind you that not all browsers are equally good at handling CSS3 properties. This solution, of course, is very interesting, but still more experimental. Having adjusted it to your needs, before tightly screwing it to the working site, be sure to check its work in different browsers and on various mobile devices.
Look at the result again, download the source archive, experiment with different parameters and create, create, create...
In the near future I will try to tell and show how, based on this decision , you can easily implement retractable, side panels , with other equally important elements to interact with users on board.
Now users have the opportunity to familiarize themselves with the . All of them are presented in a separate category, which can be found on the TemplateMonster marketplace. There shouldn't be any problems with them. Just add your unique content and you're done - you can start a business and attract more and more readers. It is also very important to remember that the text for each template was written by hand.
With all respect, Andrew
Good day, dear readers. Today we will discuss the topic how to do horizontal menu using HTML and CSS ". The menu, as a rule, is located in the header of the site and is a list of links to the most important pages, it is also called main menu . Users will constantly click on these links. How you arrange them and what design you give the menu will affect user behavior, conversion, their overall experience of your site and, of course, .
HTML code for horizontal menu
Once upon a time, the main menu for the site was made on pictures, tables, flash, and possibly something else, but nowadays there is the most popular and correct method of creating a menu using “list” tags.
Tags are used to create menus.
An example of using html tags to create a menu in the code below:
home
Services
Prices
Contacts
Standard CSS Styles for Horizontal Menu ul ( list-style: none; /*remove list markers*/ margin: 0; /*remove padding*/ padding-left: 0; /*remove padding*/ ) a ( text-decoration: none; /*remove underlining link text*/ ) li ( float:left; /*Place the list horizontally to implement the menu*/ margin-right:5px; /*Indent the menu items*/ ) We get a ready-made top menu in the header, without any special styles and bells and whistles, this can be called the frame of your future beautiful menu . If you copy and paste this html and css it will look like this.
An example of a frame (template) for your future menu
Everything turned out quite simply, you, of course, want a beautiful main menu right away, but without understanding the basics, you simply won’t be able to create a good menu without errors in html and css.
There are also several other CSS methods used to make menus horizontal besides float:left; , for example display:inline-block; or display:flex; , but it is recommended to use the method described above.
Let's fill our menu template with various styles and make it beautiful .
Examples of a beautiful horizontal menu for a website
I will now present a few ready-made examples with a ready-made horizontal menu design.
You can create all the "pretty things" for your site yourself, and not look on the Internet. The easiest way to do this is based on one of the examples below.
Simple blue menu with separate items
CSS styles for the "top" menu
Below are the styles for this menu. The HTML remains the same as in the menu "skeleton".
Ul ( list-style: none; /*remove list markers*/ margin: 0; /*remove padding*/ padding-left: 0; /*remove padding*/ margin-top:25px; /*indent top*/ ) a ( text-decoration: none; /*remove link text underlining*/ background:#30A8E6; /*add background to menu item*/ color:#fff; /*change link color*/ padding:10px; /*add padding*/ font-family: arial; /*change font*/ border-radius:4px; /*add rounding*/ -moz-transition: all 0.3s 0.01s ease; /*make a smooth transition*/ -o-transition : all 0.3s 0.01s ease; -webkit-transition: all 0.3s 0.01s ease; ) a:hover ( background:#1C85BB;/*add hover effect*/ ) li ( float:left; /*lay list horizontally to implement the menu*/ margin-right:5px; /*Indent the menu items*/ )
Main menu located on a colored line with a red background
css style menu on color line ul ( list-style: none; /*remove list markers*/ margin: 0; /*remove padding*/ padding-left: 0; /*remove padding*/ margin-top:25px; /*indent top*/ background:#FF4444; /*add a background to the entire menu (replacing this parameter will change the color of the entire menu)*/ height: 50px; /*set the height*/ ) a ( text-decoration: none; /*remove the underlining of the link text* / background:#FF4444; /*add a background to the menu item (replacing this setting will change the color of all menu items)*/ color:#fff; /*change the link color*/ padding:0px 15px; /*add padding*/ font-family: arial; /*change the font*/ line-height:50px; /*align the menu vertically*/ display: block; border-right: 1px solid #F36262; /*add a border to the right*/ -moz-transition : all 0.3s 0.01s ease; /*make a smooth transition*/ -o-transition: all 0.3s 0.01s ease; -webkit-transition: all 0.3s 0.01s ease; ) a:hover ( background:#D43737;/ *add hover effect*/ ) li ( float:left; /*lay list horizontally for real menu*/ ) Dropdown menu in HTML+CSS
For implementation additional drop-down (drop-down) menu on the site for any menu item, we need to add an additional list of items to the HTML code for any link from the horizontal menu and change the CSS styles. In the styles, we will use a simple trick - changing the display of the drop-down menu on hover to the item we need in top menu . For example, let's take the item "services".
Example of creating a simple dropdown menu
HTML code of the dropdown menu
home
Services
Service 1
Long service 2
Service 3
Prices
Contacts
Dropdown CSS Styles ul ( list-style: none; /*remove list markers*/ margin: 0; /*remove padding*/ padding-left: 0; /*remove padding*/ margin-top:25px; /*indent top*/ background:#819A32; /*add a background to the entire menu*/ height: 50px; /*set the height*/ ) a ( text-decoration: none; /*remove the underlining of the link text*/ background:#819A32; /*add a background to menu item*/ color:#fff; /*change color of links*/ padding:0px 15px; /*add padding*/ font-family: arial; /*change font*/ line-height:50px; /*align menu to verticals*/ display: block; border-right: 1px solid #677B27; /*add a border to the right*/ -moz-transition: all 0.3s 0.01s ease; /*make a smooth transition*/ -o-transition: all 0.3s 0.01s ease; -webkit-transition: all 0.3s 0.01s ease; ) a:hover ( background:#D43737;/*Add a hover effect*/ ) li ( float:left; /*Place the list horizontally to implement the menu* / position:relative; /*set the position for positioning*/ ) /*Styles for the hidden dropdown menu* / li > ul ( position:absolute; top:25px; display:none; ) /*Make the hidden part visible*/ li:hover > ul ( display:block; width:250px; /*Set the width of the dropdown menu*/ ) li:hover > ul > li ( float:none; /*Remove horizontal positioning* / ) And in order to understand exactly what services and categories you should have, I recommend that you familiarize yourself with the material:.
I tried to tell you as briefly as possible about how to create a main horizontal menu, make a few templates, how to add simple styles to it and make it prettier, how to make a drop-down menu for your site. For convenience, I have collected all the menus presented above in one html file, which you can download below. It looks like in the screenshot:
Thank you for attention.
In this tutorial, we will make a classic horizontal menu on pure CSS . It has icons in lists. When hovering over an item, it smoothly changes the color of the button and text, and adds a shadow. Drop-down lists can be made multi-level, and most importantly, this is all quite simply implemented in pure CSS3.
In the lesson we will use:
display:flex;
use transition ;
we will position the elements using position .
HTML structure of the horizontal menu
First of all, let's write the markup for the horizontal menu. We open our development environment in my case it is PhpStorm , create an index.html file, write the html:5 framework, replace lang with ru .
I will delete all meta except the encoding, I will write my title " tom menu ».
Between the body we write the header tag, and in it is a block with the .dws-menu class in which our menu will be located. Further, the structure will be as follows, we will create lists in the amount of five pieces. Each list will have a link with the href="#" attribute. Then there will be an icon I with the class .fa .fa-
Click apply.
Let's write the name of the menu items ( Home, Products, Services, News, Contacts ).
Next, select and connect the icons. We go to the Font Awesome website, select icons for these menu items:
We download the archive from the site with icons, extract its contents to our computer, copy the fonts folder and the css folder to our development environment.
The fonts folder contains icon fonts, and the css folder contains their styles. Compressed styles can be removed by font-awesome.min , include uncompressed font-awesome.css .
In index.html we connect icons, and prescribe each item its own icon style ( home , shopping cart , cogs , th-list , envelope-open ).
We have made the main frame, we will form the submenu after describing the main style, and now we will create a file where we will describe the main styles of the horizontal menu style.css and connect it to index.html . To check that the styles are connected, I will create a folder img , in it I will place an arbitrary image on the background . Let's write the connection of the image using background .
Body( background-image: url("../img/ep_naturalwhite.png"); )
Describing CSS Styles for the Horizontal Menu
First of all, let's reset all the indents that different browsers can set by default:
Dws-menu *( margin: 0; padding: 0; )
Let's set the header to 200 spades. and assign the Cuprum font, which can be downloaded and separately connected to your site, just in case, we will write additional fonts.
Header( margin: 200px; font-family: Cuprum, Arial, Helvetica, sans-serif; )
Let's hide the markers from the lists:
dws-menu ul, .dws-menu ol( list-style: none; )
Let's display the lists horizontally with display: flax , and make it centered:
Dws-menu > ul( display: flex; justify-content: center; )
In the header, let's indent only from the top, write margin-top .
Header( margin-top: 200px; font-family: Cuprum, Arial, Helvetica, sans-serif; )
Let's design our menu, set the color of the buttons, font, etc.
Dws-menu > ul li a( display: block; background: #ececed; padding: 15px 30px 15px 40px; font-size: 14px; color: #454547; text-decoration: none; text-transform:uppercase; )
Then we position the icons, assign the lists position: relative; to further center the icons:
dws-menu > ul li( position: relative; )
dws-menu > ul li > a i.fa( position: absolute; top: 15px; left: 12px; font-size: 18px; )
Let's assign a border separator to the lists, select the first LI element, and set the border. We select the last element LI and assign it a similar border.
Dws-menu > ul li:first-child( border-left: 1px solid #b2b3b5; ) .dws-menu > ul li:last-child( border-right: 1px solid #babbbd; )
Making separators for LI lists:
.dws-menu > ul li( position: relative;
border-right: 1px solid #c7c8ca;
}
The menu has acquired the appearance, then you can start describing styles on hover.
Animate the horizontal menu on hover
Dws-menu li a:hover( background: #454547; color: #ffffff; box-shadow: 1px 5px 10px -5px black; transition: all 0.3s ease; )
And to make this effect disappear smoothly, add this style to the link at rest:
.dws-menu > ul li a( display: block; background: #ececed; padding: 15px 30px 15px 40px; font-size: 14px; color: #454547; text-decoration: none; text-transform:uppercase; transition: all 0.3s ease; ) The main menu is finished and you can start describing the submenus and their submenus.
Describing a CSS/HTML Dropdown Menu
We open index.html and add, for example, an additional menu to the products. Between the LI lists we insert UL , we will place five lists in it, in which there will be links with the attribute herf=”#” .
ul>li*5>a
Click apply, write the name of the items ( Clothing, Electronics, Food, Tools, Life. chemistry ).
clothing
Electronics
Food
Tools
Gen. chemistry
Then we open style.css and describe the styles of the submenu.
Select the second list and give it position: absolute; , set the minimum width to 150 pixels.
/*sub menu*/ .dws-menu li ul( position: absolute; min-width: 150px; )
Let's write the border of 1 peak to the lists.
Dws-menu li > ul li( border: 1px solid #c7c8ca; )
For links in the submenu, set the padding to 10 pixels, remove the text transformation and make the background a couple of tones darker background: #e4e4e5; .
Dws-menu li > ul li a( padding: 10px; text-transform: none; background: #e4e4e5; )
Then we will create another nested menu. Let's go to the markup file and, for example, in "Electronics" we form the menu by analogy, as we did before. Describe paragraph headings ( Cameras, Computers, Phones ) and save.
Electronics
They are displayed, but hidden under the main menu, now position: absolute; nested UL and shift it by 150 peaks. to the side:
Dws-menu li > ul li ul( position: absolute; right: -150px; top: 0; )
/*sub menu*/ .dws-menu li ul( position: absolute; min-width: 150px; display: none; ) And for their appearance, we will select the lists on hover and display them using display: block; .
dws-menu li:hover > ul( display: block; )
Now you can add layered menus just by simply copying the UL block, changing its clauses.
home
Products
clothing
Electronics
Food
Tools
Gen. chemistry
Services
Service 1
Service 2
Service 3
News
Contacts
Then let's decorate the buttons with the ingredient for the final steps. I use a CSS generator, I have several Presets created, you can create your own, in my case I just copy given code and place in place the background that I wrote before.
.dws-menu > ul li a( display: block;
/* Permalink - use to edit and share this gradient: http://colorzilla.com/gradient-editor/#c9c9c9+0,f6f6f6+2,c4c5c7+98,757577+100;Custom+3 */ background: #c9c9c9 ; /* Old browsers */ background: -moz-linear-gradient(top, #c9c9c9 0%, #f6f6f6 2%, #c4c5c7 98%, #757577 100%); /* FF3.6-15 */ background: -webkit-linear-gradient(top, #c9c9c9 0%,#f6f6f6 2%,#c4c5c7 98%,#757577 100%); /* Chrome10-25,Safari5.1-6 */ background: linear-gradient(to bottom, #c9c9c9 0%,#f6f6f6 2%,#c4c5c7 98%,#757577 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */ filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#c9c9c9", endColorstr="#757577",GradientType=0); /* IE6-9 */
padding: 15px 30px 15px 40px; font-size: 14px; color: #454547; text-decoration: none; text-transform:uppercase; transition: all 0.3s ease; ) .dws-menu li a:hover( /* Permalink - use to edit and share this gradient: http://colorzilla.com/gradient-editor/#e0e1e5+0.454547+2.454547+98,e0e1e5+100 */ background: #e0e1e5; /* Old browsers */ background: -moz-linear-gradient(top, #e0e1e5 0%, #454547 2%, #454547 98%, #e0e1e5 100%); /* FF3.6-15 */ background: -webkit-linear-gradient(top, #e0e1e5 0%,#454547 2%,#454547 98%,#e0e1e5 100%); /* Chrome10-25,Safari5.1-6 */ background: linear-gradient(to bottom, #e0e1e5 0%,#454547 2%,#454547 98%,#e0e1e5 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */ filter: progid:DXImageTransform.Microsoft.gradient(startColorstr="#e0e1e5", endColorstr="#e0e1e5",GradientType=0); /* IE6-9 */
color: #ffffff; box-shadow: 1px 5px 10px -5px black; transition: all 0.3s ease; )
If desired this menu you can arrange for the style that suits you on the site, it’s quite simple to generate a color and replace it in the code. The result is a simple and at the same time nice horizontal menu made with pure CSS.
In this post, we will make a fixed side info menu. To implement the icons, we will connect the font .
Step 1. Connect the font with icons, create an empty document
You can find detailed information about working with the FontAwesome font and a link to the project itself in my post -.
Here is the code of the page with connected files:
Fixed side menu
Step 2: Adding the Fixed Menu HTML Markup
Here is the markup code for our fixed menu:
As you noticed, I applied the tag - this is an html5 markup element, if you are using html4, you can replace this tag with a regular one or remove it altogether, and move the identifier to the list, like this: