Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Thin banner on top of page (like the orange one on this page)

Tags:

css

I need to put a thin banner on the top of the page kinda like the orange notification one on this page. I need it to be absolutely positioned though, because when you click on it it needs to drop down (over the current page, not pushing the current page down). The banner needs to stretch the entire width of window but the content needs to be within 800px in the middle. I have it already made but I am having trouble with the CSS positioning of it.

Thanks!!

like image 318
JD Isaacks Avatar asked Oct 15 '09 14:10

JD Isaacks


People also ask

How do I fix my banner in CSS?

Answer: Use CSS fixed positioning You can easily create sticky or fixed header and footer using the CSS fixed positioning. Simply apply the CSS position property with the value fixed in combination with the top and bottom property to place the element on the top or bottom of the viewport accordingly.

Why is there white space at the top of my website?

If you're using a layout that includes your theme's styling, it's possible that you see some space or a 'gap' between your header and where your content starts, or between your content and the footer of your page. This space is actually a part of your theme's layout.

How do I get rid of the white space at the top of a page in CSS?

Adjusting the Margin Size of an HTML Element With CSS You can remove this margin by setting the top and left margin to zero. Like the padding and border, the sizes of specific sides of the margin can be set using margin-left , margin-right , margin-top , and margin-bottom .

How do you make a top banner in HTML?

Declare the same custom class attribute from the HTML <div> element, in this case banner , then add the property background-image . Within the background-image , call the URL function. This function takes in relative URLs, data URIs, and absolute URLs. Finally, add the image URL to the function.


1 Answers

Below is an example. The main #banner element stretches the full screen and is pinned to the top of the viewport using position: absolute; top: 0; left: 0. The width: 100% makes it stretch the full width.

The #banner-content is where you put your content. This is centered and fixed at 800px in width. I've put a border around it so you can see.

Note: I've also 'reset' the margins and padding of all the elements to clear the default padding. You might want to use something like Eric Meyer's Reset CSS in your final application.

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>Test</title>
    <style type="text/css" media="screen">
     * {
       margin: 0; 
       padding: 0;
     }

     div#banner { 
       position: absolute; 
       top: 0; 
       left: 0; 
       background-color: #DDEEEE; 
       width: 100%; 
     }
     div#banner-content { 
       width: 800px; 
       margin: 0 auto; 
       padding: 10px; 
       border: 1px solid #000;
     }
     div#main-content { 
       padding-top: 70px;
    }
    </style>
</head>

<body>
  <div id="banner">
    <div id="banner-content">
    This is your banner text, centered and fixed at 800px in width
    </div>
  </div>
  <div id="main-content">
    <p>Main page content goes here</p>
  </div>
</body>
</html>
like image 65
Olly Avatar answered Sep 21 '22 03:09

Olly