问题描述:

I'm making a project on ASP.NET and I've the resolution 1280x800. I'm using the width of the web page 1245px which is running very fine on my system but when I'm using the site on the resolution 1024x768 then the alignment goes out of the browser. I'm using <div> width 1245px which is looking nice on the resolution of the 1280x800.

I tried this code but it is giving me error.

protected void Page_Load(object sender, EventArgs e)

{

div1.Style.Add(HtmlTextWriterStyle.Width, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width.ToString());

}

It is giving error that

The type or namespace name 'Windows' does not exist in the namespace

'System' (are you missing an assembly reference?)`

So is there any simple way to fix it.

网友答案:

The issue with screen resolution is that unless you try to shoot for the holy grail and use liquid layouts (pain in the a**) then you really can't realistically support different screen resolutions natively. I suppose you could use a lot of fancy javascript but ... OUCH ... and maintenance nightmare .. plus it would bog your pages down.

You should just setup your web site to only display content in an assumed window of 1024 x 768 since that is the most common screen size to date.

A general way to accomplish this is to center a div that is 1024x768 using auto margins so that the rest is just extra white space.

<div id="MainContainer">
My content here
</div>

  #MainContainer
  {
      width: 1024px;
      height: 768px;
      margin: auto;
  }

This won't solve the issue of vertically centering your content. One approach I prefer is to use absolute positioning so that centering isn't a factor. I can just setup the page to stretch to fit the size of the window by using this css instead:

  #MainContainer
  {
      position: absolute;
      top: 0;
      bottom: 0;

      width: 1024px;
      height: 768px;
      margin: auto;
  }

This will cause the div to strech from the top of the window to the bottom. I like this solution cause it is support by most browsers and their various versions and even handles pretty well with zooming.

相关阅读:
Top