简体   繁体   English

使网站适合屏幕尺寸

[英]Fitting Website To Screen Size

so I have been working on a website. 所以我一直在一个网站上工作。 Now I am working on making it fit all screen sizes, and I have a problem. 现在,我正在努力使其适合所有屏幕尺寸,但是我遇到了问题。 Here is a example: 这是一个例子:

@media only screen and (min-width : 1366px) {
    #wrapper {
        width: 1400px;
        height: 750px;
        position: absolute;
    }
}

Now the above will display the screen size as 1400 pixels if the screen size is at least 1366. Yet if the screen size is 1400 pixels, and your screen only happens to be 1366 pixels you are going to have to scroll side to side. 现在,如果屏幕大小至少为1366,则上面的命令将屏幕大小显示为1400像素。但是,如果屏幕大小为1400像素,并且屏幕恰好是1366像素,则您将不得不左右滚动。 That is my issue, but I have a idea on how to solve this. 那是我的问题,但是我对如何解决这个问题有一个想法。 I would like to center the 1366 pixels of content in the middle, and then if the screen size is bigger the left over pixels will be equally divided on the sides. 我想将1366像素的内容居中居中,然后,如果屏幕尺寸更大,则剩余的像素将在侧面平均分配。 Example: 例:

My screen size is 1500 pixels. 我的屏幕尺寸是1500像素。 Minimum screen size is 1200 pixel. 最小屏幕尺寸为1200像素。 Then 150 pixels will be added to each side of the 1200 pixel content (sense there are 300 pixels left over). 然后,将150个像素添加到1200个像素内容的每一侧(感觉剩下300个像素)。

Now on the sides where there are no content (because the pixels are bigger) I want there to be a background color. 现在在没有内容的侧面(因为像素较大),我希望有背景色。 So simply how would I achieve this? 那么,我将如何实现这一目标呢? If this does not make sense please just post a comment and I will try to make this more clear. 如果这没有意义,请发表评论,我将尽力使其更加清楚。 Thank You :) 谢谢 :)

EDIT: People are asking, why can you not just do width:100%. 编辑:人们在问,为什么你不能只做width:100%。 That would not work because if you shrink the screen size it messes up everything. 那是行不通的,因为如果您缩小屏幕尺寸会弄乱所有内容。

Welcome to SO. 欢迎来到SO。 You're right, your question is over complicated for the question you're asking. 没错,对于您要提出的问题,您的问题过于复杂。 What you're essentially asking is 'how to center a div'. 您实际上要问的是“如何使div居中”。

#wrapper {
   margin: 0 auto;
}

For the background, you can do: 对于背景,您可以执行以下操作:

body {
   background-color:#000;
}

Edit for your first issue: #wrapper {overflow:hidden} to remove the scroll bars, although this would only really be applicable for images, otherwise you could go for 100% on the width. 编辑第一个问题: #wrapper {overflow:hidden}删除滚动条,尽管这实际上仅适用于图像,否则可以使用100%的宽度。 Depending on your design you might benefit from reading up on responsive frameworks. 根据您的设计,您可能会受益于阅读响应式框架。

Try this: 尝试这个:

CSS CSS

@media only screen and (min-width : 1366px) {
     #wrapper {
          width: 100%;
          height: 750px;
          position: absolute;
     }
}

I am not sure what you are asking for. 我不确定您要什么。 I think this is it: 我觉得这就是:

@media only screen and (min-width : 1366px) {
    #wrapper {
        width: 1400px;
        height: 750px;
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
    }
}

I think @SajadLfc answered your question but still not sure because he got downvote :/ 我认为@SajadLfc回答了您的问题,但仍然不确定,因为他投票了:/

To get screen width and height 获取屏幕的宽度和高度

use this, 用这个,

width = screen.width;
height = screen.height;

and

width = window.innerwidth;
height = window.innerheight;

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM