• 0

CSS: Image fixed at top?


Question

I'm trying to modify a WordPress theme (The Morning After) and I have an image that is set to [repeat-x top] and I want it to stay fixed so that no other images will appear over top of it or replace it. Ideally I'd like to have any images I upload for the background show up below the bottom of image I want to have fixed.

Any help is greatly appreciated. My current CSS is below:

body { background: #f8f8f8 url(images/headers/top-bg.png) repeat-x top; }

Example below:

unledty.jpg

Link to comment
https://www.neowin.net/forum/topic/997584-css-image-fixed-at-top/
Share on other sites

16 answers to this question

Recommended Posts

  • 0

Glen is right, you're going to need to fiddle with the z-index of both the body and the banner you're trying to keep fixed. The best thing to do is encase the banner in a div and create a background image inside that div.

The following should work:

body {
   background: url(whatever.jpg);
   z-index: 0;
}

#repeated-banner {
   background: url(repeated-banner-image.jpg) repeat-x;
   position: fixed;
   z-index: 1;
   height: 20px; /* Or however high you want it */
   width: 100%;
}

  • 0

Make a new Div, what you want to do isn't exactly possible, as the new background image you specify for the body will have to have some fake padding at the top. It won't automatically start below your banner.

#banner{
background: #f8f8f8 url(images/headers/top-bg.png) repeat-x top;
width : 100%;
height : 100px;
z-index : 10;
position : fixed;
top : 0px;
left : 0px;
}
body{
background: #f8f8f8 url(images/headers/top-NEWBACKGROUND.png) repeat-x 0px 100px;/*same PX as above*/
}

I haven't test this, I just wrote it out in the post editor. So check syntax if it goes wrong.

  • 0

Do you want the second image to move when the user scrolls, or do you want it to be a fixed background?

Which of these is nearest? (ignore the file names)

http://lmky.co.uk/test/bottom-align-text-2.htm

http://lmky.co.uk/test/bottom-align-text.htm

http://lmky.co.uk/test/bottom-align-text-2.htm

That's essentially what I'm looking for. I want the top-bg image to span 100% width on the left and right of the header. Then I want a full bg image to start just below the to-bg, fixed. I'm just having a hard time getting both images to show up, let alone in the right places...

Again, i really appreciate the help guys.

  • 0

Thanks for the input, CSS is definitely not my fort?. I've attached an image to the original post in hopes that it will help show just exactly what I'm trying to do. So far I've tried JAGFin1's code, no success yet though.

Right, I think I know what to do... I've just done a quick mock-up using background colours and Lorem Ipsum to test the scrolling and the position of the banner. This is the code I used:

<html>
   <head>
      <style type="text/css">
         body {
	    background-color: blue;
	    z-index: 0; /* Lowest index for the body and anything in it unless stated otherwise*/ 
	    margin: 0;
	 }

	 #scroll-header {
	    background-color: red;
	    position: fixed;
	    height: 200px;
	    width: 100%;
	    z-index: 1; /* Put it on top of the body */
	    top: 0; /* Make sure it is ALWAYS at the top */
	 }

	 #content {
	    margin-top: 200px; /* Content placed in this wrapper, with a top margin matching the height of the banner */
	}
      </style>
   </head>

   <body>
      <div id="scroll-header">
      </div>

      <div id="content">
         LOREM IPSUM WENT HERE.
      </div>
   </body>
</html>

The idea is to wrap all of the content on the page in a containing div as it gives you control over its position. The body is a BIG thing and you generally don't put raw content in it (without initially placing it inside another div). If you wrap all of your content in the #content div, you can make sure that it begins below the banner. When it scrolls, anything within the content div (with an index of 0) will scroll under it (because it has a lower index than the banner).

Give that a go and give me a shout if you have any problems, detailing what exactly went wrong. I understand that you're messing about with WP so it's a bit more difficult, but I've outlined the theory behind what you're trying to do.

  • 0

Whoa, that's a lot of code. So far I've been dabbling in the style.css, do I need to put the code above elsewhere? Like the header or something? If so, I'll need to be careful seeing as how this is a WP theme and messing something up in there could break the entire theme.

  • 0

I just added the code above to keep it together. I just looked at your example and can see what you're up to.

Basically, if you want to push the content of your page down so that it starts below the banner, you need to edit the CSS for the main container of content that WP uses. I'm sorry but I don't know what class that is, so you'll have to find it (or wrap all content in your own).

The basic principle relies on two things: the index of the banner, and the top-margin location of the content. If you look at my code, you'll see that #content has a top-margin: 200px. That matches the height of the banner, meaning that the content starts when the banner finishes. It's also important to make sure the banner has a top: 0; so that it explicitly starts at the top of the page.

The only thing you really need to be paying attention to is this:

#scroll-header {
   background: url(banner.jpg) repeat-x;
   position: fixed;
   height: 200px;
   width: 100%;
   z-index: 1; /* Put it on top of the body */
   top: 0; /* Make sure it is ALWAYS at the top */
}

#content {
   margin-top: 200px; /* Content placed in this wrapper, with a top margin matching the height of the banner */
}

It's no use relying on the body for moving content down as the banner is also part of the body, which encases everything on a web page. Like lunamonkey stated, you need to use divs in order to fix the positions of the conflicting objects.

Edit: I've just realised that lunamonkey has used a slightly different method to me. Swings and roundabouts I suppose.

  • 0

See, the problem is the ONLY thing I want to push below the top-bg is the background image. With what you posted above it sounds like I'll be pushing EVERYTHING content-wise below the top-bg which will throw everything that is currently in the header completely off...right? Is there anyway to use two images for the bg within the CSS, instead of hacking WP? I want to make sure upgrading WP for security fixes is as painless as possible.

  • 0

So, you're saying that you want the background image of the body to appear at a lower top-margin to the actual content of the body? Erm...

You'll probably want something like:

body {
   background: url(img.jpg) 0px 200px repeat-x;
}

That will repeat the background across the x-axis, and place it 200px from the top of the container.

You'll have to excuse the ambiguity, I'm quite tired!

  • 0

So, you're saying that you want the background image of the body to appear at a lower top-margin to the actual content of the body? Erm...

You'll probably want something like:

body {
   background: url(img.jpg) 0px 200px repeat-x;
}

That will repeat the background across the x-axis, and place it 200px from the top of the container.

You'll have to excuse the ambiguity, I'm quite tired!

No excuse needed, I'm extremely appreciative that people are taking the time to try and help a newb :)

  • 0

Is there anyway to use two images for the bg within the CSS, instead of hacking WP? I want to make sure upgrading WP for security fixes is as painless as possible.

wordpress upgrades don't touch the themes. You can hack the themes all you like. As long as wordpress don't change the engine completely, but then that would break all themes.

  • 0

I was under the impression I'd have to hack a few of the WP files. So far no luck, granted I'm in no way a coder. Right now I'm trying to figure out how to make the background image work along side the top-bg image that spans the top of the page...all in CSS.

Now, one question I do have is the 'Background' option under Appearance in WP...where would I edit that file? Surely that is within the WP files themselves and not the theme, making me question if I could set some attributes there to make the background image I upload show up below any other images from the theme(s)...

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • XnConvert 1.112 by Razvan Serea  XnConvert is a cross-platform batch image-converter and resizer with a powerful and ease of use experience. All common picture and graphics formats are supported (i.e. JPG, PNG, TIFF, GIF, Camera RAW, JPEG2000, WebP, OpenEXR) as well as supporting over 500 other image formats. Also available within the batch operations include rotating, adding of watermarks, adding of text along with many image-adjustment features such as brightness, shadows and more. Among the features included are: Batch adding of files and folders Support for drag and drop of files Batch rotating, cropping, resizing and more Adding of photo masks Preserving or removing image metadata in conversions Multipage image file support (i.e animated GIF, APNG, TIFF) Command line integration via NConvert Filters - such as 'Blur', 'Gaussian Blur', 'Emboss', "Sharpen' and much more Effects - such as 'Old camera' and much more Download: XnConvert 64-bit | Standalone | ~30.0 MB (Freeware) Download: XnConvert 32-bit | Standalone Links: XnConvert Website | Screenshot | Release Announcement Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Microsoft updates Visual Studio Code with chat cost tracking and multi-agent chats by Paul Hill Microsoft has just launched Visual Studio Code 1.126, its latest weekly release. This time, the company has focused on letting you see the total cost of chat sessions to spot expensive conversations; enabling multiple chats per session that run side-by-side in one agent host Copilot session; and letting you browse new folders safely in restricted mode. We have now reached the stage where free AI in IDEs is coming to an end. To help you keep track of your costs, VS Code now lets you see the entire cost of a chat session, rather than just individual turns. This should give you more transparency about which sessions consume the most credits, so you can better manage your usage over time and spend less. For those of you using the Agents window, you know it is possible to run and manage multiple agent sessions at once. In this update, a Copilot session started from an agent host can hold several chats at once. Explaining how this feature works, Microsoft writes: Finally, from this update forward, Microsoft will remove the pop-up when opening an untrusted folder. When you open a new folder now, it will automatically open in Restricted Mode. You will see a banner that lets you manage the trust level of the folder. Microsoft has made this change so that it’s easier to start inspecting code without giving it trust right away. If you have VS Code, you can check for updates within the app now to get this new version. Otherwise, you can download it from the Visual Studio Code website.
    • Anthropic accuses Alibaba of using 25,000 fake accounts to copy Claude's capabilities by Karthik Mudaliar Anthropic has accused Alibaba of using nearly 25,000 fraudulent accounts to extract capabilities from Claude on a huge scale. According to a report from Reuters, Anthropic told US lawmakers that operators linked to Alibaba and the company’s Qwen AI team generated 28.8 million exchanges with Claude between April 22 and June 5, 2026. That is a lot of Claude conversations, but Anthropic says this was not ordinary chatbot use. The company believes the accounts were part of a coordinated effort to collect answers that could help train or improve rival AI systems. The alleged campaign reportedly focused on some of Claude’s most valuable skills, including software development, multi-step reasoning, and agentic tasks. In practical terms, that means getting an AI model to plan and complete work across several stages rather than simply answering a single question. This is called 'distillation,' where AI companies use outputs from a larger model to train a smaller and cheaper one. The smaller model learns to imitate useful parts of the more capable system without needing the same amount of computing power. The distillation process isn't automatically suspicious, but the problem comes when one company gathers another provider's outputs without permission and at an industrial scale. Also, this does not mean Alibaba obtained Claude’s source code, model weights, or original training data. Instead, Anthropic claims the accounts repeatedly asked Claude carefully designed questions and collected the answers. Those answers could then be used as training material for another model. Anthropic has made similar accusations against DeepSeek, Moonshot AI, and MiniMax earlier this year. As Neowin previously reported, Anthropic said those three companies collectively generated more than 16 million Claude exchanges through roughly 24,000 accounts. Anthropic says the new campaign produced almost twice as many exchanges in a matter of weeks. Anthropic reportedly told lawmakers that the campaign could help Chinese AI developers approach the capabilities of its Mythos Preview model. Mythos is focused on advanced cybersecurity work, including finding and exploiting complex software vulnerabilities. via Reuters | Photo via DepositPhotos.com
    • An Indian manufacturer that assembles roughly one-third of Apple's iPhones and supplies semiconductor components to Tesla confirmed Monday that attackers had stolen and publicly published a 630-gigabyte cache of confidential files — including engineering blueprints stamped "TRADE SECRET," a 52-page quality inspection document for iPhone circuit board components, and cryptographic certificates that security experts say could be weaponized in follow-on attacks. https://www.techtimes.com/articles/319019/20260624/apple-tesla-supplier-tata-electronics-confirms-630-gb-data-theft-iphone-specs-dark-web.htm
  • Recent Achievements

    • Rookie
      krychek57 went up a rank
      Rookie
    • Grand Master
      Jaybonaut went up a rank
      Grand Master
    • One Year In
      Philsl earned a badge
      One Year In
    • Dedicated
      Scoobystu earned a badge
      Dedicated
    • First Post
      Tom Schmidt earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      441
    2. 2
      +Edouard
      176
    3. 3
      PsYcHoKiLLa
      133
    4. 4
      Michael Scrip
      79
    5. 5
      Xenon
      77
  • Tell a friend

    Love Neowin? Tell a friend!