{"id":1436,"date":"2012-06-30T06:27:04","date_gmt":"2012-06-30T06:27:04","guid":{"rendered":"http:\/\/cssreset.com\/?p=1436"},"modified":"2012-06-30T06:27:04","modified_gmt":"2012-06-30T06:27:04","slug":"creating-fixed-headers-with-css","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/creating-fixed-headers-with-css\/","title":{"rendered":"Creating Fixed Headers with CSS"},"content":{"rendered":"<p>This well thought out, easy to understand CSS3 video tutorial will show you how to create a fixed header with CSS.<!--more--><\/p>\n<p>The goal here is to make a header that stays fixed to the top of the screen, whether you are scrolling or not. It&#8217;s a great option to have in your coding arsenal for clients who want to have a website that features fast and easy access to navigation, or perhaps just want to draw attention to a promotion with an eye-catching banner ad. Either way, this lesson will show you how to set up both a full-width header and one that matches up with custom width pages.<\/p>\n<p>He begins with a basic web page that has a set width and a small header that matches the width set up at the top of the page. so we just need to make that header follow us as we scroll down the page. Here is what the code looks like in the beginning.<\/p>\n<pre class=\"prettyprint linenums\">&lt;head&gt;\r\n&lt;style type=\"text\/css\"&gt;\r\nbody{\r\n    margin:0px;\r\n    background:#000;\r\n}\r\n.header {\r\n    height:50px;\r\n    background:#F0F0F0;\r\n    border:1px solid #CCC;\r\n    width:960px;\r\n    margin:0px auto;\r\n}\r\n.content {\r\n    width:960px;\r\n    background: #F0F0F0;\r\n    border: 1px solid #CCC;\r\n    height: 2000px;\r\n    margin: 20px auto;\r\n}\r\n&lt;\/style&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\n    &lt;div&gt;&lt;\/div&gt;\r\n\r\n    &lt;div&gt;&lt;\/div&gt;<\/pre>\n<p>Start by changing the header position to <code>fixed;<\/code> and the width to <code>100%;<\/code>. After this the content margin is set to <code>70px auto;<\/code>, which adds a total of 50pixels. This results in the header remaining at the to of the scree even when you scroll down, but it is not positioned at the top of the screen like many people would like. So adding the property <code>top:0px;<\/code> to the header will push that header to the top of the browser window.<\/p>\n<p>Now if you prefer to have a header that matches the width of the content, then there is still a little work to get done here. Changing the header width to <code>960px;<\/code> will create the proper size, but it still needs to be positioned correctly. To do this, we add a property of <code>margin: 0px auto;<\/code> to the header and then create a new class <code>.header-cont { width:100%; position:fixed; top:0px; }<\/code>. This then wraps the header division to apply the two classes to it. You can also now remove the <code>top:<\/code> and <code>position:<\/code> properties from the header.<\/p>\n<pre class=\"prettyprint linenums\">&lt;style type=\"text\/css\"&gt;\r\nbody{\r\n    margin:0px;\r\n    background:#000;\r\n}\r\n.header-cont {\r\n    width:100%;\r\n    position:fixed;\r\n    top:0px;\r\n}\r\n.header {\r\n    height:50px;\r\n    background:#F0F0F0;\r\n    border:1px solid #CCC;\r\n    width:960px;\r\n    margin:0px auto;\r\n}\r\n.content {\r\n    width:960px;\r\n    background: #F0F0F0;\r\n    border: 1px solid #CCC;\r\n    height: 2000px;\r\n    margin: 70px auto;\r\n}\r\n&lt;\/style&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\n&lt;div class=\"header-cont\"&gt;\r\n    &lt;div&gt;&lt;\/div&gt;\r\n&lt;\/div&gt;\r\n\r\n&lt;div&gt;&lt;\/div&gt;<\/pre>\n<p>We hope you have enjoyed the tutorial and encourage you to send in requests for future tutorial topics. And before you hurry off, you may find this recent post on <a title=\"Order on Demand: How to Use Divisions, IDs and Classes\" href=\"https:\/\/cssdeck.com\/blog\/\/order-on-demand-how-to-use-divisions-ids-and-classes\/\">how to use Division, IDs and Classes<\/a> to further organize your projects. Thanks for stopping by!<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>This well thought out, easy to understand CSS3 video tutorial will show you how to create a fixed header with CSS.<\/p>\n<p>Share and Enjoy !0Shares0<\/p>\n<p>0<\/p>\n<p> [&#8230;]<\/p>\n<p><a class=\"more-link video\" href=\"https:\/\/cssdeck.com\/blog\/creating-fixed-headers-with-css\/\" title=\"Click to watch 'Creating Fixed Headers with CSS'\">Watch Video<\/a><\/p>\n","protected":false},"author":18,"featured_media":1448,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,13],"tags":[79,107,106,73,102],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/1436"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=1436"}],"version-history":[{"count":5,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/1436\/revisions"}],"predecessor-version":[{"id":1475,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/1436\/revisions\/1475"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media\/1448"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=1436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=1436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=1436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}