Using the footer tag to create a footer on the page. DIY nailed footer footer tag usage

Hello dear readers of the blog site. We continue the topic of block layout, which was started and continued in the three previous articles. In principle, we have already managed to create both a two- and three-column website layout, and we even managed to consider the nuances of creating a rubber layout.

In addition, in the first articles devoted to site layout (), some basic concepts of webmastering were considered, without understanding which it would be rather difficult to understand the nuances.

What are the problems with our website layout

Today we will try to solve one small problem that may arise with the layout we created earlier. Most often, this situation occurs when viewing it on large monitors (with high resolution) and when displaying a page with a small amount of information.

In this case, it may turn out that the footer will not be pressed to the bottom of the screen, but will be located almost in its middle in height, which in most cases will look ugly and not aesthetically pleasing.

Nevertheless, in my opinion, it is necessary to press footer to the very bottom of the site layout, and this will be especially true in the case when the page height turns out to be less than the user's screen height. It can be schematically represented as follows:

Those. the correct footer behavior for the case of a small amount of information on the page and a large user screen would be as follows:

To implement this, you need to perform a number of manipulations with the code of our layout. Moreover, we will make changes not only to the style CSS file Style.css, but also to Index.html, which contains the Html code and forms the Div blocks. But first things first.

As an example, we will use the three-column site layout we created earlier. In this case, Index.html will look like this:

Title

Page content Page content Page content Page content


And the following CSS properties were specified in the Style.css file:

Body, html (margin: 0px; padding: 0px;) #maket (width: 800px; margin: 0 auto;) #header (background-color: # C0C000;) #left (background-color: # 00C0C0; width: 200px ; float: left;) #right (width: 200px; background-color: # FFFF00; float: right;) #content (background-color: # 8080FF; margin-left: 202px; margin-right: 202px;) #footer (background-color: # FFC0FF; clear: both;)

Well, the layout itself looked something like this:

As you can see, the footer is not pressed to the bottom and, therefore, does not meet our requirements (it is always located under the lowest column), so you will have to make adjustments to the code. All the same can be done for the two-column and rubber models too. The method is universal.

How to push a footer to the bottom of a website layout

So, we need to move the div container with the footer to the bottom of the screen. To do this, you first need to set the height of the entire page to one hundred percent (it will occupy the entire screen). This will be needed in order to then resize the main block with the layout to 100% too.

The entire content of the site page is placed in the opening and closing Body tags, and therefore we need to add another CSS property to the Body tag in Style.css that sets the height to 100%:

Body, html (margin: 0px; padding: 0px; height: 100%;)

This will not affect the appearance in any way, but now the main blog can be stretched to the full height of the screen. Those. it was a kind of preparatory stage.

Basic CSS properties, if you wish, you can look at. Now let's set the div container, which contains our entire layout, to a minimum height of 100%:

I also want to highlight it (div with id \u003d "maket"). To do this, I will give it a border using the corresponding Border () property:

The border: solid 3px black property sets the container to a solid 3px black border. This will allow you to visually see that the container with the layout has stretched to the full height of the screen, even with a small amount of information on the page:

Now we will need to move the footer block out of the general container and place it below, immediately after the general one. What will it give? And the fact that, finally, will deign to go down the footer in the layout, and will not, as before, cling to its longest column. In this case, Index.html will look like this:

Title

Left column Menu Menu Menu Menu
Page content Page content Content


Please note that the block with the footer is no longer inside the general container (maket), and therefore its width is no longer controlled by the CSS properties set for the maket in the Style.css file. The footer will stretch in width to fill the entire screen, but it will still be located at the bottom of the screen, right below the main block:

But again, a problem arises, because in order to see the footer, now you have to scroll the screen in the browser (you can see the scroll bar in the picture above).

It turns out that the main container (maket) occupies the entire screen size in height (this is determined by the min-height: 100% property), and the footer is located immediately behind it and you will have to use scrolling to view it, which is not very convenient and functional ...

A solution to this problem is to set a negative margin for the div container with a footer so that it is displaced upward by a distance equal to its height. In this case, the footer container will run over the main one and fit into the height of the browser screen (i.e., it will not need to use scrolling to view it).

But in order to set a negative margin from the top, you need to know this very height of footer, but we do not know it yet.

Therefore, we first set the container containing the footer to a height by setting the appropriate property in Style.css:

#footer (background-color: # FFC0FF; clear: both; height: 50px;)

And then set a negative top margin for it to a height equal to its height:

This will allow the footer to rise up exactly to its own height and thus fit into the browser screen (now we can remove the border: solid 3px black CSS property from the maket rule so that the border thickness does not interfere with our entire layout along with the footer to fit into the screen in height) :

As you can see, now the scrollbar in the browser does not appear and our entire three-column layout based on the block layout fits into one screen (in the case of a small amount of information on the page) and has a footer located at the very bottom. What we, in fact, needed to do.

Insert the spacer and fight Internet Explorer

But there is a problem, which will appear only when there is more information on the layout page and the following situation may turn out:

It turns out that a situation may arise when information in one of the columns of the layout overlaps the footer, which will not look pretty. This happens because of the notorious negative padding that we set for it and which helped to raise our footer with a hit on the main container of the layout.

Those. it turns out that two blocks are located at the bottom of the screen, overlapping each other in the basement area.

The solution to this problem is to add a new empty Div container (the so-called spacers) into the main container of our layout (maket), to the place where the block with the footer was previously located.

By setting the height of this new container to the height of the basement, we can avoid collision of information from the main container with the block with the footer. Let's give this container an ID () called Rasporka and as a result, the Index.html of our three-column layout will look like this:

Title

Left column Menu Menu Menu Menu
Page content Page content Page page page page page page page


And in Style.css, for this we will write (, which sets the height of this spacer container equal to the height of the footer:

#rasporka (height: 50px;)

As a result, the footer will be pressed from the bottom not to the information contained in the main container (for example, the text in the tallest column), but to an area equal to the footer in height with a spacer container that does not contain any information.

This way we avoid collisions and distortions in our three-column layout. Everything will be clear and beautiful (decorous and noble):

As I already mentioned above, the footer width should now be set separately for us, because this container is no longer part of the main one. To do this, you need to add additional properties for the Footer to the CSS file, allowing you to set its width and align it horizontally in the middle of the screen.

It makes sense to set the width equal to the width of the entire layout using the Width property, and horizontal alignment can be done in the same way as we did for the entire layout on a block layout.

Thus, we will need to add additional properties for the ID Footer:

#footer (background-color: # FFC0FF; clear: both; height: 20px; margin-top: -20px; width: 800px; margin-left: auto; margin-right: auto;)

Using the width: 800px property sets the width to 800 pixels, and using the two properties margin-left: auto and margin-right: auto sets the padding to the left and right of the footer automatically, as a result of which these paddings will be equal and our hero will be aligned to middle:

Well, there seems to be nothing more to improve, but it was not there. As always, our favorite browser Internet Explorer 6 does not understand something from the CSS properties we use. In this browser (and, perhaps, in some other old ones too), despite all our efforts, the footer will not be pressed to the bottom, but will still stick to the highest column of the site layout.

All this is due to the fact that (does not understand the min-height: 100% property, which we used to set the minimum height of the main box to be equal to the screen height.

Therefore, to solve this problem, we will have to apply a so-called hack that allows us to explain (on fingers) old browsers what to do. Before the list of CSS properties for maket, you will need to insert the following combination:

* html #maket (height: 100%;)

This rule will be applied only for the Internet Explorer 6 browser, the others will not take it into account and enforce it.

So, the final look of Style.css with the footer pressed to the bottom of the screen will be as follows:

Body, html (margin: 0px; padding: 0px; height: 100%;) * html #maket (height: 100%;) #maket (width: 800px; margin: 0 auto; min-height: 100%;) # header (background-color: # C0C000;) #left (background-color: # 00C0C0; width: 200px; float: left;) #right (width: 200px; background-color: # FFFF00; float: right;) #content (background-color: # 8080FF; margin-left: 202px; margin-right: 202px;) #footer (background-color: # FFC0FF; clear: both; height: 50px; margin-top: -50px; width: 800px; margin-left: auto; margin-right: auto;) #rasporka (height: 50px;)

Well, the final view of Index.html was shown just above. That's it, this series of articles devoted to block layout of 2 and 3 column fixed and fluid layouts of the site can be considered complete.

You can also watch the video "Working with the Html div tag":

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

You might be interested

Block Layout - Create Two Column, Three Column, and Fluid Site Layouts
DiV layout - Create blocks for a two-column layout in HTML, determine their sizes and set their positioning in CSS

Anyone who is used to fully-fledged website pages prefers the look of "nailed" (sticky, sticky) to the bottom of the page footer. But there are two troubles on the Internet: input fields that do not grow down and footers that are not nailed (to the bottom of the window). For example, when we open short-height pages like habrahabr.ru/settings/social, it is immediately striking that the information designed to be at the bottom of the viewport sticks to the content and is somewhere in the middle, or even at the top of the window when below it is empty.

So, instead of.
This guide for novice layout designers will show you how to make a "nailed" footer in 45 minutes, correcting the flaws of even such a respected publication as Habr, and compete with it as a fulfillment of your promising project.

Let's look at the implementation of one kind of nailed footer taken from the network and try to figure out what is happening. css-tricks.com/snippets/css/sticky-footer
CSS:
* (margin: 0; padding: 0;) html, body, #wrap (height: 100%;) body\u003e #wrap (height: auto; min-height: 100%;) #main (padding-bottom: 150px; ) / * must be same height as the footer * / #footer (position: relative; margin-top: -150px; / * negative value of footer height * / height: 150px; clear: both;) / * CLEAR FIX * / .clearfix: after (content: "."; display: block; height: 0; clear: both; visibility: hidden;) .clearfix (display: inline-block;) / * Hides from IE-mac \\ * / * html .clearfix (height: 1%;) .clearfix (display: block;) / * End hide from IE-mac * /
HTML:

It is unlikely that everyone, even those who know CSS, looking at this code, will understand the principles and will confidently edit a complex project. Any step to the side will lead to side effects. The reasoning and footer construction below is intended to give you more understanding of the CSS rules.

Let's start with theory

The usual implementation of a nailed footer relies on the CSS2-unique property that elements are immediate descendants BODY - maintain percentage height ( height: 100% or another) relative to the window, if all their parents have the same percentage height starting from the tag Html... Previously, without doctypes, but now in Quirks Mode, percentage heights of elements are supported at any level, and in modern doctypes - only within percentage specified elements. Therefore, if we make a content block (let's call it #layout) that has 100% height, it will scroll as if it were a window. All (streaming) content is put into it, except for the footer and maybe the header.

A footer is placed next to this block and given 0 pixels of height. In general, you can follow #layout put as many blocks as you like, but all of them must be either from 0 pixels in height, or outside the flow of the document (not position: static). And there is another important trick that is usually used. It is not necessary to make the height equal to 0. You can make the height fixed, but subtract it from the main block using the property margin-bottom: - (height);.

In human terms, styles make an empty "pocket" at the bottom, into which the footer is inserted, and it always turns out to be either stuck to the bottom border of the window, or to the bottom border of the document if the document is higher in height than the window height. There are many footer implementations on the Internet and on Habré, with varying success in all browsers. Let's continue to build it on our own, using Habr's layout as a "workhorse".

Since the bottom of the block #layout - this is a pocket, it should be empty for the footer, not displaying the page objects. And here we come across another limitation - we cannot make an empty pocket at the expense of padding at #layout, because then it will become more than 100%. Will not save and margin - emptiness needs to be done due to the properties of nested elements. In addition to everything, it is necessary to ensure that floating elements do not fit under the border of the block, which is done, for example, by a block

where .clear (clear: both)... It is important that either this " height"was fixed, or in the same relative units, or we would calculate it in the process of changing the page. It is usually convenient to combine this alignment box with setting the required height for it.

Let's look at the structure of the pages of our experimental. The easiest way to do this is to open the Firebug window or similar window (Developer Tools (Ctrl-F12)) in Chrome.

... Upper ad block ...


Let's move on to a working example

What we see disadvantages of layout in terms of implementing the effect of a nailed footer? We see that
1) the footer on the site is inside a block with id \u003d layout, which has no percentage height. In theory, he, the parents, and the content-left block need to be set to 100% height. There are problems with the latter - it is not suitable for this. Consequently, one interlayer block is missing or the footer is on the wrong level. Besides,
2) the height of the footer is variable (depends on the number of elements in the list and on the font size, this can be seen not from HTML, but from CSS). AND
3) over #layout there is an ad unit with a fixed height of 90px;
4) there are no alignment boxes in the footer or (generally speaking) in the box #layout (yes, but above the block .rotated_posts; however, perhaps it should be attributed to the footer).

Point 4 - you will have to draw with a script.
It would seem simple to figure out the third point by adding #layout (margin-top: -90px;) But remember that this block may not exist - it is suppressed by a banner cutter, or advertisers will suddenly decide not to show it. There are a number of site pages where it is not. Therefore dependence margin-top from an ad unit is a bad idea. Much better is to place it inside #layout - then he will not interfere with anything.

The first point is that for the nailed footer to work at all, you need to place the footer block under #layout... However, with the help of javascript, you can implement other schemes of the nailed footer, but in any case, you need JS or the initially correct layout to do without it.

Since we cannot be stronger than the very last site designer, who "stuck" the footer inside the content, we will postpone the idea of \u200b\u200bthe correct placement of the footer on our future site (which, therefore, will be "cooler" Habr!), And dissect Habr with javascript (user script) to the correct states. (Let's say right away that it is not the layout designer or the switchman who is to blame, but the type of the site, of course, determines the strategic decision of the project management.) This way we will not reach the ideal, because in the first second or two during the loading process, the page will be with the wrong layout. But the concept and the ability to surpass the most popular site in the IT world are important for us.

Therefore, in the right place in the script (early, at the end of the page load), we will write the transfers of the DOM ad and footer blocks to the required places. (Let's get ready for the fact that the solution will be more complicated than a clean one due to user scripts.)
var dQ \u003d function (q) (return document.querySelector (q);) // for shorthand var topL \u003d dQ ("# topline"), lay \u003d dQ ("# layout"), foot \u003d dQ ("# footer" ); if (topL && lay) // banner - inside the content block lay.insertBefore (topL, lay.firstChild); if (lay && foot && lay.nextSibling) // wrapping the footer lay.parentNode.insertBefore (footer, lay.nextSibling);
We put the blocks in their places - now it remains to assign the necessary properties to the elements. The height of the footer will have to be set exactly, simply because we already know it by the time the user script acts (end of page loading). Due to the trigger point of the user script, as mentioned above, a jump in the display of the footer on the page is inevitable. Can you try to make a "good face" but with a "bad game"? What for? The “bad game” of the site allows you to make a concept without super-efforts, which will be enough to assess the quality and will not be needed if you “play correctly” on your project.
if (foot) (// block aligner

in the footer h.apnd_el ((clss: "clear", appendTo: footer)); var footH \u003d foot.offsetHeight; // ... and measure the height of the footer) if (topL && lay && footer && lay.nextSibling) (// aligning block of the desired height in the content ("layout") h.apnd_el ((clss: "clear", css :( height: (footH || 0) + "px"), appendTo: lay)); lay.style.minHeight \u003d "100%"; h.addRules ("# layout (margin-bottom: -" + footH + "px ! important) html, body (height: 100%) ");)
Here we allowed ourselves to use a self-written function h.apnd_elwhich does approximately the same as in jQuery -
$("
") .css ((height: footH || 0)). appendTo ($ (footer))
And then there is another typical CSS rule injection function - h.addRules... Here you cannot do without it, because you need to declare a rule with " ! important"- just because of the peculiarities of the style priorities from the user script.

With these pieces of code, we will be able to see the nailed footer in the user script (after jumping it down) and fully understand how to build the page layout. It is frustrating to use the bouncing design on a daily basis, so it is recommended to do it solely for demonstration and testing. In the HabrAjax user script, I installed a similar script, closing it with the "underFooter" setting (check the box in the list of settings before the "nailed footer"), starting from version 0.883_2012-09-12.

Does the nailed footer affect the need to update the ZenComment styles if installed? Yes, it does. Due to the complex chain of style priorities, in which the styles inserted by the user script have the lowest priority, I had to slightly adjust the user styles for capabilities work with a nailed footer. If you don't update the userstyles (up to 2.66_2012-09-12 +), the footer will not work accurately.

Block rotated_post (three popular posts from the past) looks more logical in the footer, so in a real script it is also moved to the footer.

The second point (from the list of imperfections in the layout) is the reasoning purely for Habr (they do not apply to the user script and partially repeat the previous ones).

Pages have a problem preventing them from rendering a nailed footer in pure CSS - an undefined footer height, depending on the browser's default font sizes. To implement a footer in CSS, you need to choose the relative heights of the fonts, but they may not work if the provided fonts are not available on the user's computer. Therefore, the solution should include a javascript that can fit the approximate position of the footer with transitions to the exact one. Or, having looked at the acceptability of the solution made in the user script on different platforms, make a calculated installation of the nailed footer - the first observations show that the solution is practical.

Conclusion: it is possible to fully arrange the layout on Habré, but for this you need a layout designer who clearly understands the behavior of the layout and places the blocks in the correct order. (Now the footer and the top banner are "in the wrong place" and not so that just styles to get a nailed footer.) You can do without JS if you set the footer height in relative units, taking some margin for font uncertainty.

Implementation

If you enable HabrAjax 0.883+, we will see the work of the "nailed footer". It adapts in height using scripts. It allows you to evaluate how much better a page with a nailed footer looks compared to normal pages. ZenComment userstyles are compatible with scripts, but for the nailed footer to work properly, you need to install ZenComment 2.66_2012-09-12 + version with them.

Implementation Behavior Facts

Shamanism with footer, styles and scripts is shamanism (only to be supported by theory). The behavior is slightly different in different browsers, but in some places it is unexpected. Without user scripts and block rearrangements, the results will be different. This is what the experiments with the implementation in the user script gave.

1) Firefox - unexpected lack of footer jumping. It's strange that they are not there - rendering occurs after placing the footer at the bottom.

2) Chrome - it surprised me with a "wandering scroll" - empty spaces at the bottom are added to the page with a period of once a second - something wrong happens with the calculation of heights. It is treated by writing html, body (height: 100%) in the userstyle, but without guarantees that it will always work. It is safer to check if the document does not exceed the height of the window, and if not, then move the footer, otherwise nothing. With jumping - everything is in order, it is.

3) Opera - no jumping (v. 12.02) on first page load, but a hasty reload may show a footer jump. Otherwise, it leads no less correctly than Fx.

Well, you will have to specially teach Chrome to behave correctly (with a script) and in this form roll out the version for review. Therefore, the site in the user script is a little more complicated than the one given in the article.

It should be recalled that this is not a full-fledged implementation - it does not take into account, for example, cases of window resizing by the user. You can also find rare (in practice) combinations of changing footer heights before and after moving, where the logic will start to malfunction without leading to inconvenience. The disadvantages were deliberately left, because the balance of the complexity of the revision and the timeliness of the solution is observed.

As a result, it turned out to be quite workable scheme of work, at least for fast stationary computers. If incorrect footer behavior is found, the "underFooter" setting should be disabled.

What pages is this useful for?

On a standard site, without user styles, even short Q&A pages turn out to be longer than 1500px, which in most cases is invisible with horizontal monitors. But even with ordinary monitors, users' personal pages with a height of about 1300 pixels often come across, where an unbroken footer appears in all its glory. A number of pages in the user settings are not very long either.

If you use ZenComment user styles, they greatly reduce the required page height, and the HabrAjax user script may not show some or all of the sidebars in the sidebar. Therefore, with scripts and styles, the effect of an unset footer is noticeably more often observed. Therefore, it is logical that a footer fix appeared for the first time in HabrAjax. But even a normal site has a number of pages where a nailed footer would be useful.

Will there be support?

The behavior of the site over the past year shows that the developers (and therefore the management) have begun to implement features that previously existed only in user scripts and user styles. For example, at the beginning of the year I wrote where I collected many small wishes. Six months later, I returned to it and noted with satisfaction (right in the text; you can see the "UPD" and dates) that a number of features described as wishes had already been implemented into the site.

Next, let's look at the "arrows" instead of the boxes for evaluating comments. They appeared in the usersily almalexa ("Prettifier") 3 years ago and were taken over in ZenComment 2 years ago. About 2-3 months ago, they appeared on the site. Begins to believe that after a while the arrows will spread some distance, as it is done in ZenComment (one arrow to the left of the number, the second to the right), in order to miss less.

Therefore, perhaps, the "nailed footer" on Habré is not such a fantasy as it might seem about 3 years ago.

Other features in the HabrAjax script introducedfor the last 3 months (can be disabled in the settings):
* Authors of input fields (in the Opera can slow down on large texts);
* days of the week for dates other than "today" and "yesterday";
* events in the Feed, collapsible to 1 line and 2 characters;
* reduction of the words "habr *" to "χ ·" and "χα";
* hints of dates by article numbers - it is reported what month and year the article was before it was loaded, by the number in the URL;
* collapsed "Related posts" to 2 words. Screenshot of "similar posts" pop-up (showing 12 links, not 4).

HTML5 introduces several new tags for code structure:

,