Hello, today we will talk with you about how to create your own navigation bar for the site. It is used on almost every Internet resource, so every developer should be able to create this element.

Explanations for the article:

  1. "Sidebar / sidebar" - site navigation bar.
  2. "Position" is a property in CSS.
  3. "Top", "left", "right", "bottom" are CSS movement properties.
  4. WordPress is a website content management system/CMS.
  5. Webix - UI library. Allows you to create table elements.
  6. View is a JavaScript function.

This article will show various ways creation of navigation panels, both manually and with the help of special site-constructors. Our example uses WordPress.

How to make a sidebar manually? CSS & HTML

Now we will show the traditional way of development, namely writing code in text editor. To do this, you need to open the HTML and CSS documents in a code editor.

Creating a sidebar on the right side. HTML & CSS

Most often, the navigation bar is located either in the header of the site, or on its right side. In the second case, a 2-column layout is usually used.

It’s good if you have a ready-made layout in advance, since you know the width navigation block. If the layout is missing, then this will not interfere with development.

Attention! When creating sidebars and other site elements, prepare a ready-made layout in advance. Focusing on it, you will simplify the development process.

The navigation bar can be created using lists and regular blocks. If you are using lists, disable the "text-decoration" property for them.

In our example, a div block construction is used.

To begin with, we create a common div block in which the columns will be located. Let's create some class for it, in our example the layout class is used, but this does not particularly affect the development process.

The main block will be used to create positioning as well as sizing.

Let's start with positioning. For the general container, we set the relative positioning - property (position: relative). For columns, the property (position: absolute) is set.

When using this type, it will be easier for you to position columns inside the block. To do this, we will use the displacement properties: , , and .

In our example, the code looks like this:


HTML

test page

Column 2

Copy


css

layout (

position: relative;

background: rgba(119, 115, 115, 0.58);

}

Sidebar , .content ( position: absolute ; )

sidebar(

background: #C6DD7D;

Content(

background: #F4ECCE;

Copy

Attention! Be sure to specify a width for each individual navigation item. The point is that they have absolute positioning, which means their width will be equal to the width of the inner content.

We also want to note that in , the second column is placed first, and then the first. Using this method it doesn't matter, as the columns can be easily swapped.

You can also use the float property for that. This method is much simpler as it relies on element wrapping. Thanks to him, our sidebat will be located on the right side.

Creating a navigation bar on the right side. HTML & CSS


To create such a panel, you can use the same markup as in the first example. The CSS code also does not need to be changed much.

You need to make small adjustments to your CSS code. This is necessary so that the menu is located on the right side. We don't change the width and position properties!


How to create a sidebar in WordPress?

Now let's look at a method that is very different from the previous one. The difference is that you can create better navigation with little to no CSS or HTML code.

You will not need to write all the code manually, all the work will take place in the constructor itself.

At using WordPress you will need to register the navbar and add some widgets to it.

In order to register a sidebar in WordPress, you will need to write a few functions in PHP files. Basically you will need arrays of data that will refer to the widgets and their div tags.


In our example, the right sidebar is considered, as well as the sidebar of the footer.

A few words about webix

There is another special framework that will allow you to create this sidebar. Its main advantage is that it will allow you to make a good dashboard with just a few lines of code.

First you need to download this framework. After that, provide links to its styles and scripts in your HTML document.

After that, you can start creating the sidebar. To place it in the framework, there is a special function - view. Which will place for the placement of elements.

The downside of this framework is its limitations. When using it, you will not be able to create a truly unique creation, since the panel is not limited by your imagination, but only by a couple of functions.

Conclusion

In this article, we have covered a few basic ways to create a navigation bar, but there are many more. You can even come up with your own way based on our experience.

In these examples, a simple navigation bar has been shown. If you work a little source code, then you can get a real masterpiece.

Tags:

What are the layout requirements?

  • Rubber two-column layout
  • sidebar fixed width 300px
  • content is stretched to the rest of the width.
  • footer pressed to the bottom (in this tutorial I will not show how this is done).

What Css problems arise when laying out such a layout:

1 way.

If you float blocks, you must give them a fixed width(you can't set 1 sidebar to 300px and content to 100%). In this case, either the content slides down or a 300px horizontal scroll to the left appears. Well, the main thing to understand is that this method does not suit us.


2 way.

Many typesetters offer this method, what are they thinking about?! The essence of the method is that you can set the sidebar to float with a width of 300, and not float the content and set margin-left: 300px to it. Good way and everything seems to work out so well. To be honest, I thought this was the best way, but this method has its own pitfalls. The reasons for abandoning this method are that if we suddenly make up in the content, for example, a menu with floated li or any other float blocks and then we want to clear them with clear:both, then the bottom border of this block slides down to the level of the bottom border of the sidebar (What and it's not strange since foat is cleared, you can avoid this if set a floated block to a fixed height, but it's not at all the case to set a fixed height).


3 way.

Can be used for sidebar position absolute but only if you we are sure that the content will be larger than the sidebar in height otherwise, the entire contents of the sidebar will fit on the footer (after all, absolute positioning pulls out of the general flow).

But as for me, this is also not a very good way!


4 way.

"Great way, if it has any flaws please comment. But I think this is the best way."

  • Advantages of this method: firstly, the DOM content will go before the sidebar, which is good for search engines.
  • nothing will fit on the footer
  • any blocks can be cleared and nothing will slide to the lower border (So we overcame the problems of the second method).

In general, how it works: look at the code first comes content followed by sidebar. we set float to these two blocks (of course the sidebar slides down). after that we set the sidebar property margin-left:-100%. great, it's back in place, and indent the content with margin-lerft:300px.


html

css

.sidebar(
width:300px;
display:block;
float: left;
margin: 0 0 0 -100%;
}
.content(
min-width:723px;
display:block;
float: left;
margin: 0 0 0 220px;
}

The simplest example of a fixed floating sidebar in HTML+CSS+JS.

What is its peculiarity - when scrolling, the sidebar sticks to the top of the window, but at the same time, when the sidebar scrolls to the footer, then it comes off the top of the screen and sticks to its own bottom to the footer, thereby not overlapping the footer (the footer must be high). If you know what I mean.

The example was taken from some site and it is very primitive (and morally outdated), suitable only for this layout. More universal code can be found in this article and you can already write your own code for your project.

HTML markup for a simple page

HEADER
CONTENT

CSS styles for content blocks

.header ( width: 100%; background: purple; height: 80px; ) .content ( width: 80%; background: blue; height: 800px; float: left; ) .sidebar ( width: 20%; background: green; height: 100px; float: right; ) .sidebar.fixed ( position: fixed; right: 50%; margin-right: -50%; ) .footer ( width: 100%; background: gray; height: 500px; clear: both; )

JS script for fixed sidebar on page scroll

Don't forget to include jQuery

$(function() ( var $window = $(window); var $sidebar = $(".sidebar"); var $sidebarTop = $sidebar.position().top; var $sidebarHeight = $sidebar.height() ; var $footer = $(".footer"); var $footerTop = $footer.position().top; $window.scroll(function(event) ( $sidebar.addClass("fixed"); var $scrollTop = $window.scrollTop(); var $topPosition = Math.max(0, $sidebarTop - $scrollTop); if ($scrollTop + $sidebarHeight > $footerTop) ( var $topPosition = Math.min($topPosition, $footerTop - $scrollTop - $sidebarHeight); ) $sidebar.css("top", $topPosition); )); ));

A floating block (or as it is also called moving, fixed, stuck) is needed on the site so that the user sees one fixed element when scrolling the page, in which advertising is most often placed (teasers, banners or context).

Alas, the rules of adsense forbid us this. However, many site owners ignore the rules at their own risk. Maybe they are not even punished for this, but I would not advise taking risks.

I place YAN, my teasers / banners in such blocks, and sometimes, instead of advertising, I display similar posts or some useful information for the visitor.

Let's tell you how you can make a floating block on your site.

A task: make the last block in the sidebar (sidebar) floating. Moreover, so that it sticks only at the moment when the user reaches it by scrolling, and not immediately when the page is opened. Also, the block should “stick off”, reaching the footer (i.e. not overlapping it).

The most working way

There are many implementations of the sticky block, but not all of them work correctly. I'll tell from personal experience: The same method of installing a block can work on one site, but jambs appear on another.

Below is an example of a floating block that has worked on almost every site I've installed it on. There were no goats. The engine is also not important (DLE, WordPress, LiveStreet, etc.).

Paste the following HTML code in the desired location of the sidebar:

$(window).scroll(function() (
var sb_m = 20 ; /* top and bottom padding */
var mb = 300 ; /* basement height with margin */
varst = $(window).scrollTop() ;
var sb = $(".sticky-block" ) ;
var sbi = $(".sticky-block .inner" ) ;
var sb_ot = sb.offset() .top;
var sbi_ot = sbi.offset() .top;
var sb_h = sb.height () ;

If(sb_h + $(document).scrollTop() + sb_m + mb< $(document) .height () ) {
if(st > sb_ot) (
var h = Math.round(st - sb_ot) + sb_m;
sb.css(("paddingTop" : h) ) ;
}
else(
sb.css(( "paddingTop" : 0 ) ) ;
}
}
} ) ;

In this code, you can set the top, bottom, and height of your footer, i.e. the height at which the block should stop.

Now we include JS. To do this, write in the HEAD section:

  • You can use the header.php file itself by enclosing this code between the opening and closing head tags and wrapping it in script tags, like so:
  • You can also write this code in script tags anywhere else. The main thing is that it loads on the right pages of the blog. For example, you can in footer.php before the closing body tag.
  • Now let's go directly to this code. It turns out that after 10 pixels from the top, the relative positioning is replaced by a fixed one (see the article at the link above). If necessary, in the line with else, you can choose a value other than zero for top, and then the menu fixed at the top will indent from the top edge of the viewport by this pixel value (in my opinion, this is superfluous).

    Unlike the original code, I also had to add width: "100%", because otherwise the size of the menu in width would decrease, which spoiled the whole picture.

    Look, for clarity, I will give the Html code that forms the top menu in my WordPress blog template (it lives in my header.php file from ):

    In your template, most likely, the output of menu items will be set using, for example, such a construction (function), but this is not the point.

    The wp_list_pages function is, of course, good (it allows you to configure sorting, set exceptions, etc.), but it's better to do everything manually through plain Html, as shown above. IMHO.

    It is important to grasp here that the whole matter is concluded into container divs, and the topmost one has an id="navi" attribute. Here we will cling to it. You see in the above JS code it occurs twice #navi? You will need to put down your ID there instead of #navi (or a class, which, as you remember, is written not through a hash, but through a dot, for example, like this: .menu).

    After that, your menu should be fixed in the top position when scrolling the page by a certain number of pixels. However, I had a problem with the fact that this very menu did not always appear on top of the page elements over which it floated.

    This, you see, is not good. Therefore, I had to get a bit into the CSS code and add with a value of 1000 for the id of the #navi selector:

    #navi(background:#03658e url(https://website/wp-content/themes/Organic/images/spriteme2.png) repeat-x;background-position:0px -10px;height:31px;z-index:1000 )

    The fact is that when you set one of the three types of positioning using Position, this element ceases to interact with the usual elements of the Html code. But with other similarly positioned ones, it will compete for the “above or below” position. z-index:1000 allows us to place our menu obviously above all other blocks. Read the article above for details.

    How to make a floating sidebar in WordPress without plugins

    Using the method described below, you can make the whole sidebar, or part of it, float (or in other words, fix in a certain place in the viewport). If this area is quite small and fits on one screen (even a portable device), then you can make it all float.

    But most often this will not be the case, and it will be possible to force the lower part to float. If the sidebar, as in my case, is an inseparable monolith, then you can create a floating block yourself using the principles laid down in the design theme you use and place it below the main one.

    I did all this quickly and without really bothering myself with frills and finding the optimal solution, because from my rich experience of experiments I learned the pattern that the more you place hopes on some idea, the less likely it is to “shoot”. Well, vice versa. In general, there was no point in bothering yet, because the likelihood that all this will take root is not so great.

    So I just took to create the bottom block I moved the upper part of my main sidebar (in the sidebar.php template), then transferred it from the top to the bottom block “I use it for making money”, and at the end I stuck what was the end in the main block. It turned out something like this:

    Well, now I have the “I use for earnings” block moved from the top block of the sidebar to the bottom one. It turned out not a fountain, but for a "temporary" it will do. It remains only to arrange all this in a JS file with code and the lower block will begin to float. JS code looks like this:

    $(document).ready(function()( var br = $.browser; $(window).scroll(function() ( var top = $(document).scrollTop(); if (top< 2561) { $(".sidebar123").css({top: "0", position: "relative", marginLeft: "25px"}); } else if ((!br.msie) || ((br.msie) && (br.version >7))) ( $(".sidebar123").css((top: "52px", position: "fixed", marginLeft: "760px")); ) else if ((br.msie) && (br.version<= 7)) { $(".sidebar123").css({top: "52px", position: "fixed", marginLeft: "25px"}); } }); });

    Note that instead of .sidebar123 , you need to substitute your class or the ID of the outer container in which your sidebar bottom block lives.

    I don’t understand this code very well (I don’t know JS), but everything works. At least partially. When you set fixed positioning, the report is from the top left point. Therefore, with marginLeft: "760px" I place this block exactly at the level of the sidebar (the number was obtained through "trial and error").

    The value top: "52px" sets the padding of the already floating sidebar block from the top border. The value of top However, I have there was a problem in the event that the total height of the main sidebar was less than the height of the content area. Such a thing got out, for example, when viewing the archive of headings:

    From a CSS point of view, I understand why this happens, but I was too lazy to think about a fix. I just had to refuse to show this floating sidebar block on such pages (it is not needed there).

    ";} ?>

    Please note that if there are single quotes in the code enclosed in echo "" , then they will be needed shield, i.e. prepend each of them with a backslash (\") without parentheses, of course.

    In general, it turned out how it happened. How to specifically tie this to your topic, you will have to decide for yourself - when there is time, it’s even cool to “break your head”. Thank you.

    Good luck to you! See you soon on the blog pages site

    You may be interested

    WebPoint PRO is a responsive WordPress theme with wide functionality and competent technical search engine optimization
    Share42 - script for adding social network buttons and bookmarks to the site (there is a floating panel option)