Solved

Getting margin-left to cooperate between IE 9 and Firefox

Posted on 2011-09-28
6
334 Views
Last Modified: 2012-05-12
I cannot seem to get this simple css and layout to work on both firefox and IE9.  This is a very simplified version of my normal css but it demonstrates the issue.  In IE the textbox appears in the div (this is where I want it).  In Firefox the textbox appears offset right from the div.  I do not doubt that IE is the culprit here, but what can I do to get this to render the same in both browsers (without taking the textbox out of the div)?

Thanks for any suggestions!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<style>
		input[type=text]
		{
			margin-left: 118px;
			width: 380px;
			background-color: Red;
		}
		.userNameDiv
		{
			position: fixed;
			top: 50%;
			left: 50%;
			margin-left: -118px;
			width: 380px;
			background-color: Green;
		}
	</style>
</head>
<body>
	<div class="userNameDiv">
		<input type="text" />
	</div>
</body>
</html>

Open in new window

0
Comment
Question by:nicolees
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 16

Expert Comment

by:SSupreme
ID: 36718142
It looks the same in FF6 and IE9. Could you please provide FF version you have used, and confirm that you used IE9.
0
 

Author Comment

by:nicolees
ID: 36718176
Firefox 6, and you are right about IE9.  It looks like I had my browser compatibility mode set to IE7.  So Firefox 6 and IE 9 behave the same.  IE7 and IE8 are different.

Fundamentally I want the textbox to sit inside the div on all 4 browsers (IE7, IE8, IE9, Firefox 6).  
0
 
LVL 16

Expert Comment

by:SSupreme
ID: 36718753
Try to remove margin-left: -118px; from .userNameDiv.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:nicolees
ID: 36718868
Removing margin-left: -118px from .userNameDiv makes all browsers behave the same, but leave the div in the incorrect location.  I need it offset left 118 pixels from center

This gives me what I want in IE8, IE9, Firefox
		input[type=text]
		{
			margin-left: 0px;
			width: 380px;
			background-color: Red;
		}
		.userNameDiv
		{
			position: fixed;
			top: 50%;
			left: 50%;
			margin-left: -118px;
			width: 380px;
			background-color: Green;
		}

Open in new window


This gives me what I want on IE7
		input[type=text]
		{
			margin-left: 118px;
			width: 380px;
			background-color: Red;
		}
		.userNameDiv
		{
			position: fixed;
			top: 50%;
			left: 50%;
			margin-left: -118px;
			width: 380px;
			background-color: Green;
		}

Open in new window


I would rather not conditionally load stylesheets based on browser type.  This seems like a simple issue, I guess I was hoping that, with a single stylesheet, I could render a textbox aligned and inside of a div at 118px left of center.
0
 
LVL 16

Accepted Solution

by:
SSupreme earned 500 total points
ID: 36719640
I cannot get what you want as it isn't in the content. I mean is there any better possibilities to place div in center?
But you can do this: <div class="userNameDiv"><div><input type="text" /></div></div>
or add rule into CSS file specially for IE7, after original rule: *+html input[type=text] {margin-left: 118px;}
0
 

Author Comment

by:nicolees
ID: 36719815
Thanks, that is what I needed.  This css/html resolves my issue

<head>
	<style>
		input[type=text]
		{
			margin-left: 0px;
			width: 380px;
			background-color: Red;
		}
		.userNameDiv
		{
			position: fixed;
			top: 50%;
			left: 50%;
			margin-left: -118px;
			width: 380px;
			background-color: Green;
		}
		*+html input[type=text]
		{
			margin-left: 118px;
		}
	</style>
</head>
<body>
	<div class="userNameDiv">
		<input type="text" />
	</div>

</body>
</html>

Open in new window

0

Featured Post

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

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…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

695 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