【问题标题】:HTML/CSS center for all resolutions without % (percentage)没有 %(百分比)的所有分辨率的 HTML/CSS 中心
【发布时间】:2017-01-27 00:41:31
【问题描述】:

我想知道,是否可以在不使用 % 的情况下使这个中心框位于页面的“最”中心?

我正在使用 VPS,并且我有一些 Python 正在运行,所以我不知道如何使用 % sizes 来使其正确。但这就是我想出多远才听说这不是以 1280x1024 为中心的:

https://s18.postimg.org/vta0qfd5l/2809583a0f43627d0fc1b428863b3faa.jpg

这是我使用的代码:

h2 {
  margin: 0;
}
body {
  background-color: #121212;
  background-image: url("https://app.box.com/shared/static/vgfs65li424fk8h4n5e23pm7070yrewq.jpg");
  background-size: contain;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-position: center;
  background-height: auto;
  max width: 1200px;
}
table {
  display: inline-block;
  position: absolute;
  max-width: 350px;
  max-height: 265px;
  border: 5px ridge yellow;
  top: 0;
  bottom: 0;
  margin: auto;
  left: 0;
  right: 0;
  padding-left: 90px;
  padding-right: 90px;
  background: rgba(255, 255, 255, 0.8);
}
div {
  text-align: center;
  align: center;
}
<div>
  <table>

  </table>
</div>

那么,知道我可以改进什么来“钉它”吗?我注意到,如果我向最大宽度添加更多,完整框会向左移动。也许如果我能解决这个问题,我也会解决第一个问题?

这个问题很独特,因为我不能使用百分比来正确对齐表格,所以我只想知道这是否可能。

谢谢!

【问题讨论】:

  • 你说的是哪个盒子?
  • 提供 fiddle 或 codepen 以便更好地理解。!
  • 在 IE 版本中你必须走多远?
  • IE 根本不重要。 @Bálint 中心的盒子,里面有注册表
  • 它在我看来相当集中,我错过了什么?

标签: python html css center


【解决方案1】:

如果您的页面是作为 HTML 网页提供的,那么您应该能够使用 JS、HTML 和 CSS,而不会让您的后端搞砸。如果将以下样式应用于 any 元素,它将居中:

   position: absolute;
   left: 50%;
   top: 50%;
   transform: translate(-50%, -50%);

还有一些错误会破坏您可能预期的其余样式:

background-height 

不存在。

min width:

应该是:

> min-width:

align: center;

也不存在。

在此代码段中,我更正了前面提到的错误并进行了大量重构。您有一个 div 框架表格,因此将 div 居中并确保表格保持在 div 内更有意义。让我知道我是否接近预期结果。

片段 1

h2 {
  margin: 0;
}
body {
  background-color: #121212;
  background-image: url("https://app.box.com/shared/static/vgfs65li424fk8h4n5e23pm7070yrewq.jpg");
  background-size: contain;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-position: center;
  height: auto;
  max-width: 1200px;
}
table {
  position: absolute;
  top: 0;
  left: 0;
  margin: 0 auto;
  background: rgba(255, 255, 255, 0.8);
  width: 100%;
  height: 100%;
}
div {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 100%;
  height: 100%;
  max-width: 350px;
  max-height: 265px;
  border: 5px ridge yellow;
}
<div>
  <table>

  </table>
</div>

此解决方案应该可行,但如果您出于我无法理解的原因坚持不使用百分比,请考虑视口测量单位。

100vw = 100% of viewport width
100vh = 100% of viewport height

百分比测量单位和视口测量单位之间的最大区别在于,百分比长度由包含被测量的主题元素的任何内容决定。而视口长度取决于屏幕可视区域的尺寸。看下面的 Snippet 看看有什么区别,用整页模式查看,调整大小来测试不同维度的演示。有关vwvh 的更多信息,请参阅此article

片段 2

body {
  background: black;
  color: white;
}
#A {
  border: 3px dotted red;
  width: 500px;
  height: 250px;
  background: rgba(255, 0, 0, .4);
}
#B {
  border: 2px dashed blue;
  width: 50%;
  height: 50%;
  background: rgba(0, 0, 255, .4);
}
#C {
  border: 2px dashed yellow;
  width: 50vw;
  height: 50vh;
  background: rgba(0, 255, 255, .4);
}
<p>Viewable area of browser is viewport. View this in Full page mode and resize the browser at various sizes. Div B (blue) doesn't change. Div C (yellow) does.</p>


<div id='A'>
  A width: 500px height: 250px
  <div id='B'>B width: 50% height: 50%</div>
  <div id='C'>C width: 50vw height: 50vh</div>
</div>

【讨论】:

  • 感谢您非常有建设性的帖子,我肯定会从中学到一些很好的东西,但是 - #1 - 我使用 Python 来运行服务器 - 所以,使用 % 会破坏页面并使它无法使用,我还没有找到如何使用 % 进行大小调整的方法。 #2 我使用了最大宽度,而不是“最小宽度” #3 我使用了 align: center ,因为看到 div 使用它,所以当我将它转移到表格时我可能犯了一个错误。现在检查,无论有没有对齐,代码的行为都是一样的 - 感谢您注意到这一点。
  • 为了尊重你,因为你注意到了我的问题——我不使用 % 的原因是因为我得到了这种类型的错误:image.prntscr.com/image/60b5ec26994b4dbe9aba656fc3648d28.png 我只是放了宽度:20%;结果得到了这个。在打开这个问题之前我已经用谷歌搜索过了,Python 和 % 符号 之间肯定存在问题
【解决方案2】:

您可以使用 CSS 中的绝对位置和 px 中的测量值来定位它。如果您使用的是 Jquery,那么您可以使用

获取文档大小
$(document).height();
$(document).width();

或 $(窗口).height(); // 用于视口。

一些快速的 JavaScript 计算应该允许您动态地将某些东西定位在您想要的任何位置。 不要忘记绑定 .resize() 事件以适应用户调整浏览器大小。 这是一篇关于获取窗口大小等的好文章。

https://andylangton.co.uk/blog/development/get-viewportwindow-size-width-and-height-javascript

【讨论】:

  • 我目前正在使用 VPS(Linux,Debian 7),所以我不太确定是否可以上传任何其他内容 - 例如,我知道我必须将背景图片放在外部为了让它加载,所以我想看看还有什么我可以用 CSS 做的吗?或者,更好的问题——当我在这个服务器上使用 Python 时,有没有办法使用 top: 50%;左:50%?我已经尝试使用 %% 和 %' 但没有任何反应,也许我遗漏了什么?
【解决方案3】:

感谢您的帮助,但同时我与一位朋友交谈,我们提出了令人满意的解决方案,所以这是最终代码和我们所做的

  1. 我把桌子从身上和其他不必要的东西拿走了(请原谅我,因为我是新手)
  2. 我们使用高度和宽度而不是最大高度和宽度 - 我们必须微调高度,以便所有浏览器都能正确显示内容,我已经在手机、iPad、几台显示器上进行了测试,一切都以我的方式显示愿意。
  3. IE8 除外……但老实说,没关系:)

<style>h2 {
  margin: 0;
}
body {
  background-color: #121212;
  background-image: url("https://app.box.com/shared/static/vgfs65li424fk8h4n5e23pm7070yrewq.jpg");
  background-size: contain;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-position: center;
}
.FormBody {
  display: inline-block;
  position: absolute;
  width: 380px;
  height: 305px;
  border: 5px ridge gold;
  top: 0;
  bottom: 0;
  margin: auto;
  left: 0;
  right: 0;
  padding-left: 90px;
  padding-right: 90px;
  text-align: center;
  background: rgba(255, 255, 255, 0.8);
}
</style>
<html>
<meta charset="utf-8">

<head>
  <title>CTLServer REGISTRACIJA</title>
</head>
<script src="/md5.js" type="text/javascript"></script>
<script>
  function makeHash() {
    a = document.getElementById('serial').value;
    a = a.replace(/^\s+/, '').replace(/\s+$/, '').replace(/-/g, '').toUpperCase();
    if (!a.match(/^[A-Z0-9]{20}$/)) {
      alert('Neispravan serijski broj.');
      return false;
    }
    document.getElementById('serial').value = a;
    while (a.length < 36) {
      a += '\0';
    }
    u = document.getElementById('username').value;
    u = u.replace(/^\s+/, '').replace(/\s+$/, '');
    if (u.length < 3 || !u.match(/^[0-9a-zA-Z]+$/)) {
      alert('Zabranjeno ime za korisnika - dozvoljeno 3-36 alfa-numerička znaka (0-9 | a-z | A-Z).');
      return false;
    }
    p = document.getElementById('password').value;
    if (p.length < 3) {
      alert('Lozinka je kratka - minimum 3 alfa-numerička znaka (0-9 | a-z | A-Z)');
      return false;
    }
    document.getElementById('hash').value = hex_md5(a + u + '-' + p);
    return true;
  }
</script>
<div class="FormBody">
  <br />
  <h2><u>• CTLServer REGISTRACIJA •</h2>
  </u>
  <strong><a href="https://app.box.com/shared/static/krtchlikkjw48u21aywx">LINK za HOST</a><br /></strong>
  <br />
  <form name="registration" action="/register" method="POST">
    <input type="hidden" name="nonce" id="nonce" value="%(nonce)s" />
    <input type="hidden" name="hash" id="hash" size="32" value="" />
    <strong>Serijski Broj:</strong>
    <br />
    <input type="text" name="serial" id="serial" size="40" value="%(serial)s" onkeydown="if (event.keyCode == 13) {document.getElementById('btnGen').click(); return false;}" />
    <br />
    <br />
    <strong>Korisničko Ime:</strong>
    <br />
    <input type="text" name="user" id="username" size="40" value="%(username)s" onkeydown="if (event.keyCode == 13) {document.getElementById('btnGen').click(); return false;}" />
    <br />
  </form>
  <strong>Lozinka:</strong>
  <br />
  <input type="password" id="password" size="40" value="" onkeydown="if (event.keyCode == 13) {document.getElementById('btnGen').click(); return false;}" />
  <br />
  <p>
    <input class="button" type="button" id="btnGen" value="Registruj se" onclick="if (makeHash()) {document.forms['registration'].submit();}" />
  </p>
</div>

</html>

因此,再次感谢您的所有帮助,尤其是 @zer00ne 我肯定会简要阅读您的帖子几次,因为我认为我可以从那里理解很多内容。事实上,我想我也会尝试其他方法,以防万一我在某个时候需要它们。

【讨论】:

  • 你知道你有哪些用于 HTML 解析的库吗?是ElementTree、Beautiful Soup、lxml、twisted等吗?我不是 Python 程序员,但据我所知,如果您从 Python 提供 HTML 服务,您可以使用正则表达式或库来解析 HTML。由于分号;,您发布的图片显示错误消息,因此如果之前是%,现在看起来好像是分号;。如果你坚持用正则表达式做老派,尝试转义 Python 拒绝的符号。一般来说,你要做的就是在有问题的符号前面放一个 ``。
  • 我在您的帖子中添加了 Python 标签,您会得到更好的结果。顺便说一句,我完全同意,IE8 不是问题。
  • 我认为它是扭曲的。我在安装服务器时看到了它。所以只是回顾一下,如果我想让 Python 覆盖 % 符号,我需要把它放在这种格式中 width: 50'% ?
  • 试试这个article 它会指导你制作服务器和客户端。在 RegEx 中,如果您想使用 width: 50%,请尝试 width: 50\%,这称为转义。通过在实体之前放置反斜杠,您将指定该特定符号/实体/字符作为其文字(例如,在 url 哈希中,如果您像这样转义百分比符号,则空格键是 %20:\%20,解释器将看到 %20 而不是空格键击键。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-12-03
  • 2011-04-17
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 2012-08-31
相关资源
最近更新 更多