Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Absolute position div not staying at right-side of page

Posted on 2012-03-23
5
Medium Priority
?
306 Views
Last Modified: 2012-08-13
I can't seem to figure this out. I want the right-side DIV to always be on the right-side of the page (not affecting the main content). It works for the most part, except if you resize the browser window less than the main content width, then the horizontal scroll-bar appears and if you then move the horizontal scrollbar the right-side DIV is no longer at the far right of the page!

It's the same for all browsers. How do I force it to be always on the right?

Float: left works, except that I can't use float as it will interfere with main body content.

Here is the minimum code to reproduce it:
<div style="position: absolute; right:0; top:0; width:100px; height:100px; background-color:red;">RIGHT-SIDE DIV</div>
<div style="width:800px; height:400px; background-color: #DCC;">
MAIN CONTENT
</div>

Open in new window


Thanks!
0
Comment
Question by:intoxicated_curveball
  • 2
  • 2
5 Comments
 
LVL 15

Expert Comment

by:William Fulks
ID: 37759323
Go here and look at step 4. I think that's what you are wanting to do.

http://www.barelyfitz.com/screencast/html-training/css/positioning/

Basically, you need to make that floating right side positioned relative to the rest of the content, otherwise the browser just knocks it around.
0
 

Author Comment

by:intoxicated_curveball
ID: 37759392
No it didn't work, I took all their code. And the same problem occurs once I add an element with a fixed width that creates the horizontal scrollbar when resizing the browser window. Here is the new code, can somebody update the code to make it work?
<html>
<head>
<title>Untitled Document</title>

<style type="text/css" media="screen">

#div-1 {width:100%; background-color:#000;color:#fff;position:relative;}
#div-1a {background-color:#d33; color:#fff;position:absolute;top:0;right:0;width:200px;}
#div-1b {background-color:#3d3; color:#fff;}
#div-z { background-color: purple; width: 800px; }

</style>

</head>
<body>

<div id="div-1">
<p>id = div-1</p>
    
  <div id="div-1a">
<p>id = div-1a</p>
  </div>
    
  <div id="div-1b">
    <p>id = div-1b</p>

  </div>
</div>

<div id="div-z">
<p>Hello world!</p>
</div>

</body>
</html>

Open in new window

0
 
LVL 16

Accepted Solution

by:
s8web earned 1000 total points
ID: 37760443
In your original example, change position:absolute to position:fixed.

<!DOCTYPE html>
<html lang="en">
	<head>
		<title>Test</title>
	</head>
	<body>
		<div style="position: fixed; right:0; top:0; width:100px; height:100px; background-color:red;">RIGHT-SIDE DIV</div>
		<div style="width:800px; height:400px; background-color: #DCC;">
			MAIN CONTENT
		</div>
	</body>
</html>

Open in new window

0
 

Author Closing Comment

by:intoxicated_curveball
ID: 37765639
Clearly I need to brush up on my CSS, this was a simple solution!
0
 
LVL 16

Expert Comment

by:s8web
ID: 37765777
Sometimes a second set of eyes catches the simple stuff. I've spent hours troubleshooting before just to have a friend look over my shoulder and tell me that a comma's missing :)
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
In this Micro Tutorial users will learn how to embed custom fonts into websites using @font-face in CSS Select a font: Ensure the EULA allows you to use @font-face: Download the font: Get the browser-compatible files you need: Edit your CSS       - Name …
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question