Solved

Position <div> with CSS using margin-top with percentage

Posted on 2013-06-06
10
473 Views
Last Modified: 2013-06-06
Here is my test page:
http://secure.bbdesign.com/watersedgepa

See attached screenshot. I am getting inconsistent placement of one of my elements (I added a red border around it for easier identification).

The element is <div id="contentwrapper"> and is styled like this:

div#contentwrapper{
position:absolute;
top:0;
left:50%;
margin:30% 0 0 -30em;
padding-bottom:2.5em;
width:60em;
border:1px solid #c00;}

I'm sure the problem is the 30% margin-top.

The reason for the percentage is that we are trying to change the position of various elements over the background image, depending on the browser window's size and proportions. A percentage seemed to be the way to go.

It basically works the way we want, however with Safari is much higher up than in any other browser. All others appear to be about the same (including IE).

I'm not sure I understand 100% how a percentage in margin-top is supposed to work. Is there something I can modify in my CSS that will make this more consistent? Or is this an inherent browser inconsistency problem that I will need to find a workaround for?

Thank you!
1.jpg
0
Comment
Question by:bbdesign
  • 4
  • 4
  • 2
10 Comments
 
LVL 10

Expert Comment

by:Ishaan Rawat
ID: 39226108
try this...

div#contentwrapper{
margin-top:30%;
margin-left: -30em;
}

Open in new window

0
 

Author Comment

by:bbdesign
ID: 39226130
I tried that. It should be semantically identical, though, correct? It doesn't seem to have any effect.
0
 
LVL 10

Expert Comment

by:Ishaan Rawat
ID: 39226149
Try adding position: relative instead of position:absolute to div#contentwrapper
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:bbdesign
ID: 39226165
That moves things around a bit, but its still inconsistent from Safari to all other browsers, by the same amount.
0
 
LVL 10

Accepted Solution

by:
Ishaan Rawat earned 500 total points
ID: 39226173
Sorry about the guessing.. but I don't have safari...

so try this this might work 80% chances...

div#contentwrapper {
position: absolute;
top: 50%;
left: 50%;
margin-left: -30em;
padding-bottom: 2.5em;
width: 60em;
border: 1px solid #c00;
}
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39226196
I think you would be better of using media queries https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Media_queries
0
 

Author Comment

by:bbdesign
ID: 39226236
IshaanRawat - yes, it had occurred to me to try putting it in "top" instead of "margin-top", but still no effect.

Padas - do you mean I should try to detect the Safari browser and give it a different percentage? I normally only use Media Queries when doing responsive web design for browser widths.

It seems Safari calculates its percentages based on the parent element differently from other browsers?

Thanks!
0
 
LVL 10

Expert Comment

by:Ishaan Rawat
ID: 39226281
Yp for that try this...

if ($.browser.safari) {
    $("#contentwrapper").css("margin-top", "50%");
}

but make sure to use it with jQuery version < 1.9   ....
Because jQuery has disabled that since 1.9

See here... http://api.jquery.com/jQuery.browser/
0
 
LVL 52

Expert Comment

by:Scott Fell, EE MVE
ID: 39226331
That might be the easier route even though the site it's self is not responsive.
0
 

Author Comment

by:bbdesign
ID: 39226336
Actually, while playing around with this, I have realized that top:30% does seem to fix this. It didn't affect Safari, but it did actually "fix" the other browsers so they are now consistent. I don't know why I didn't notice this before. For now, I think we are good to go.

Thanks again!
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Host asp.net pages 5 35
Why is this panel heading creating extra space 2 50
is this a cms? 8 60
Slider Moving to right hand side CSS? 2 23
When applying CSS to your HTML, there are many different ways to select which element(s) the CSS applies to.  Some of these selectors are more commonly known and used than others - Here are the more common ones: #X - Matches an ID of X .X - Matche…
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 tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…

777 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