Solved

Getting margin-left to cooperate between IE 9 and Firefox

Posted on 2011-09-28
6
331 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
  • 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

856 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