Fixed header similar to Twitter's design

Looking to create a fixed header similar to Twitter/Facebook that sticks to the top of the page?

I tried implementing this solution, but encountered design issues when resizing my browser.


  <div id="head">
   <div id="logo">
    <a href="#"></a>
   </div>
   <div style="display:inline;">
    <input style="margin-top:3px;" class="searchbox" type="text"/>
   </div>
 <ul>
  <li>
   <a href="#" title="Home">Home</a> 
  <li>
  <li>
   <a href="#" title="Profile">Profile</a> 
  </li>
  <li>
   <a href="#" title="Profile">Actions</a> 
  </li>
  <li>
   <a href="#" title="Profile">Invite Friends</a> 
  </li>
 </ul>


</div>
<div id="content">
      </div>

Custom Styles:

     
     html {
         height:100%; /* ensure 100% height for IE */
         max-height:100%; /* set maximum height for other browsers */
         padding:0; /* remove all padding */
         margin:0; /* remove all margins */

         background:#fff; /* set background color (IE only) */
         overflow:hidden; /* hide overflow in IE */
}
body {
         height:100%;
         max-height:100%;
         min-width:960px;
         overflow:hidden;
         padding:0;
         margin:0;
         font: 13px/1.5 Helvetica Neue,Arial,Helvetica,'Liberation Sans',FreeSans,sans-serif;
}
#content {
         display:block;
         height:100%;
         max-height:100%;
         overflow:auto;
         position:relative;
         z-index:3;
         word-wrap:break-word;
         top:45px;
}
#head {
         position:absolute;
         margin:0;
         top:0;
         display:block;
         width:100%;
         height:40px;
            -moz-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
         background:#333333;
            background: -moz-linear-gradient(center top , #736F6E, #111111) repeat scroll 0 0 transparent;

}
#logo a {
            background: url("2.gif") no-repeat scroll 6px 2px transparent;
            color: #FFFFFF;
            display: block;
            height: 100%;
            margin-right: 5px;
            outline: medium none;
            text-indent: -9999px;
            width: 140px;
           float:left;
}
.searchbox{
            -moz-border-radius: 4px 4px 4px 4px;
            -moz-box-shadow: 0 1px 0 #444444;
            background: none repeat scroll 0 0 #666666;
            border: 1px solid black;
            color: #CCCCCC;
            font: 13px Arial,sans-serif;
            padding: 6px 5px 4px 26px;
            width: 215px;
         float:right;
}

.searchbox:focus {
            background: none repeat scroll 0 0 #eeeeee;
            border: 1px solid #999999;
        }
#head ul {
          margin:0;
          padding:0;
          background:transparent;
          height:100%;
          margin-left:60px;
          padding-left:660px;
          padding-top:10px;
}
#head ul li {  display:inline;}
#head ul li a { padding-left:10px;  color:#BABABA; text-decoration:none;}
#head ul li a:hover { color:#FFFFFF;  }

Edit Sample http://jsfiddle.net/zerotoinfinite2006/tTmSH/embedded/result/

Answer №1

To ensure your #head element floats at the top, you should apply fixed positioning to it. This will keep it anchored at the top of the page consistently. Here is a sample CSS code you can use:

#head {
     position: fixed;
     margin: 0px;
     top: 0px;
     left: 0px;
     display: block;
     width: 100%;
     height: 40px;
     -moz-box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
     background: #333333;
     background: -moz-linear-gradient(center top , #736F6E, #111111) repeat scroll 0 0 transparent;
}

Answer №2

Check out the fantastic features of Twitter's Bootstrap:

You could save time by simply incorporating it into your project.

Answer №3

Check out the following HTML code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>WebSpeaks.in</title>
...

Here's a snippet of the corresponding CSS code:

html {
 height:100%; /* fix height to 100% for IE */
 max-height:100%; /* fixing height for other browsers */
 padding:0; /* removing padding */
 ...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to make a bootstrap component (container) stretch to the full width and height of the window when it first loads, and which

I am looking to achieve something similar to the design on a certain website: The goal is to have a container with a background and a form inside that resizes proportionally based on the size of the window it is opened in. The resizing should be smooth an ...

Instant Pay Now Option for Your WordPress Website with PayFast Integration

I have encountered an interesting challenge that I would like some guidance on. My goal is to integrate a PayFast "Pay Now" button into a Wordpress.com blog, specifically within a sidebar text widget. The tricky part is that I need the customer to input th ...

Is the 3D cube spinning with a slight perspective glitch?

I've successfully created a spinning cube using html and css. However, I'm facing an issue where pressing the up and down arrow keys causes the cube to rotate in a larger space rather than around its center. I've attempted using margin: 0 a ...

The function view() is not displaying the CSS and JS files properly

Having trouble with loading the css and js on my view: This is how I set up the controller: return view('home.news') ->with("news", $news); And here's how the route is defined: Route::get('/news/{id}&apos ...

How to align icon and text perfectly in a Bootstrap 5 Button

I am looking to align icons and text within a modal body: <div class="modal-body"> <div class="d-grid gap-2" id="someButtons"> </div> </div> This code snippet demonstrates how I insert buttons ...

Presenting a data table in Angular

I'm new to using Angular and need help creating a table to organize my data, which is being fetched from a JSON file on the server. Here's the content of data.component.html: <div class="container"> <h1>Search history</h1> ...

Elements are failing to respond to the padding and margin adjustments in my CSS styling

I've been experimenting with creating a visually appealing div that serves as the centerpiece of a website. Imagine it resembling a standard "news" link. The width set at 80%, an image aligned to the left with a border, a headline detailing the lates ...

How can a Bootstrap card be designed with the image positioned at the top left, but adjusting based on different

When it comes to Bootstrap 4 cards with images, most have the image positioned at the top using .card-img-top. But what if you want the image to be at the left or at the top depending on the width? Currently, I have two types of cards - one with the image ...

Tips on toggling between slides with Bootstrap Carousel

I am in the process of designing a portfolio website and have encountered a challenge. I want to divide it into two sections - a brief introduction and a carousel for showcasing projects. While I have managed to set up both sections, I'm facing an iss ...

Ensuring that the carousel images maintain a consistent size when switching between images

Issue with Carousel I have been struggling with a carousel feature on my website. Despite following the code provided on the Bootstrap website, I am facing an issue where the height and width of the entire div change when loading the next image. I have tr ...

Retrieve information from an HTML form, make updates to the database, and exhibit the data in a table on the current page with the help

I'm in need of some assistance. I have a small application that utilizes a Java Servlet and an HTML form. The user enters data into the HTML form, and upon clicking the submit button, the Java servlet retrieves this data in its post method and stores ...

I'm trying to create a horizontal list using ng-repeat but something isn't quite right. Can anyone help me figure out

Feeling a bit lost after staring at this code for what seems like an eternity. I'm trying to create a horizontal list of 2 image thumbnails within a modal using Angular's ng-repeat. Here's the HTML snippet: <div class="modal-body"> ...

Creating a seamless transition from 2 CSS grid columns to 1 on mobile devices without the need for a media query

Is utilizing a media query the sole solution for keeping itemX and itemY in the same cell on each device? Are there any native CSS grid methods that could achieve the same result? Feel free to check out the demonstration on the following fiddle: https://j ...

Adjusting the width of the final card in the deck grid

Currently, I am utilizing angular-deckgrid for image display purposes. Within a container, there are two columns in which images are displayed with the column-1-2 class according to the documentation. However, in certain scenarios where only one image is p ...

Opening an HTML file in Eclipse is a simple process that can be

Q1) Is there a way to open this file in a browser for testing? Q2) Can I quickly open this in Chrome from Eclipse and test it? click here for image ...

Display a div with a link button when hovering over an image

Currently, I'm attempting to display a link button within a div that will redirect the user to a specified link upon clicking. Unfortunately, my current implementation is not functioning as expected. I have provided the code below for reference - plea ...

What is the best way to show an image on the screen once a submit button is clicked?

I have a hidden loader-bar gif that I want to display when the user submits a form. Here is the code: <p class="loadingImg" style="display:none;"><img src="/design/styles/_shared/classic-loader.gif" alt="" /></p> Is there a way to ...

Default behavior in Fullcalendar 6 allows for rendering links on both days of the month and weekdays

Is there a way to customize the rendering of days and weekdays in Fullcalendar React? Currently, they are displayed as links by default (<a>{dayContent}</a>), but I'm looking to have them rendered as <div> or <span>. Any sugges ...

Using different time intervals, setTimeout can be implemented within a for loop

I have an array consisting of different objects, each containing a time property with various values. My goal is to iterate through this array and use a setTimeout function inside to print out the name of each object after a specific amount of time based ...

How can I make the hover effect only apply to the text and not the entire box?

I'm wondering how I can make the :hover property only affect individual letters in my navigation bar, instead of hovering over the entire box. * { margin: 0px; padding: 0px; } .navigation-bar { height: 3.2em; background: gray; text-align: ...