简体   繁体   English

如何使浮动页面div响应

[英]How to make a floating page div responsive

Hi I'm still new to web development. 嗨,我还是Web开发的新手。 So I have a register page that floats as a div above the main page but I was wondering how do I ensure that the div gets centered in a responsive manner? 所以我有一个注册页面,它作为div浮动在主页上方,但我想知道如何确保div以响应方式居中?

The pages are separated and included at the header. 页面被分开并包含在标题中。

<?php 
include ('includes/login.php');
include ('includes/register.php');
?>

my register's css 我的寄存器的CSS

#regScreen {
    padding: 5 5 40px 5px;
    margin: 0 auto;
    position: fixed;
    top: 5%;
    left: 33%;
    z-index: 10;
    display: none;
    background: #ebebeb;
}

#regScreen:target, #regScreen:target+#cover {
    display: block;
    opacity: 2;
}

#reghead {
    background-color: #e2e1e1;
    text-align: center;
    display: block;
    padding: 0px 0px 10px 0px;
}

I tried to use media query on my #regscreen: 我试图在#regscreen上使用媒体查询:

@media (max-width: 300px) {
  #regScreen {width: 100%;
  left:0%;
}
}

But using media queries doesn't seems to recognize the page as responsive as it is already small. 但是使用媒体查询似乎无法识别该页面,因为它已经很小了。 From my understanding, please correct me if I'm wrong. 据我了解,如果我错了,请纠正我。

i already see one error just by looking 我已经看过一个错误

@media and (max-width: 300px) {
#regScreen {
 width: 100%;
 left:0%;
}
}

you for got 'and' before '(max-width: 300px)' 您在“(最大宽度:300像素)”之前获得“和”

I'm not sure about what is the element using those selectors, but I tried to make a sample html & css reference for solving your issue. 我不确定使用这些选择器的元素是什么,但是我尝试制作一个示例html&css参考来解决您的问题。 Here is the link jsfiddle.net/3Le34w8p/ 这是链接jsfiddle.net/3Le34w8p/

It's difficult to provide an exact answer without more infomation ( it would be great if you added more of the HTML markup ), however... 没有更多信息很难提供确切的答案( 如果您添加了更多的HTML标记那就太好了 ),但是...

If the issue is that the floating div does not resize to fit various screen sizes (and since you're new to web development...welcome aboard!), there are a couple of suggestions I can make: 如果问题是浮动div不能调整大小以适合各种屏幕尺寸(并且由于您是Web开发的新手...欢迎上来!),我可以提出一些建议:

1) You may be overcomplicating it by trying to apply the @media (max-width:300px) media query. 1)您可能会尝试应用@media (max-width:300px)媒体查询,从而使其过于复杂 By simply adding the following styles, the registration form should resize accurately: 通过简单地添加以下样式,注册表单应准确调整大小:

#regScreen {

  /* The rest of your styles go here */

  width:90%;
  max-width:600px; /* em or rem value would be better than px... e.g. 37.5 em */

}

This would ensure that the width of the form is always either 90% of the screen width OR 600px, whichever is smaller . 这样可以确保表单的宽度始终为屏幕宽度的90%或600px(以较小者为准)

2) If you think there may be an issue with the media query not trigerring, an easy way to test it is to make something really obvious happen at that breakpoint...for example: 2)如果您认为媒体查询可能没有触发问题,一种简单的测试方法是使该断点处确实发生某些事情……例如:

@media (max-width: 300px) {

  /* Test Style */
  /* Turn background red when below 300px */
  body{
    background-color:red !important;
  }

  /* Your original styles */
  #regScreen {
    width: 100%;
    left:0%;
  }

}

By doing this, it should allow you to start troubleshooting whether it's your media query syntax or something else that is the issue; 这样,它应该允许您开始进行故障排除,无论是您的媒体查询语法还是其他问题。 maybe the media query styles are being correctly applied (so your media query syntax is ok) but the new styles are being overwritten later in the CSS (or due to the specificity of certain rules). 可能是正确应用了媒体查询样式(因此,您的媒体查询语法是可以的),但是新样式后来在CSS中被覆盖(或由于某些规则的特殊性)。

If you add more info to your question, let me know and I'll take another look but until then, this should hopefully help get you on the right track. 如果您在问题中添加了更多信息,请告诉我,我会再看一遍,但是在那之前,这有望帮助您走上正确的道路。

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

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