ASP.NET + CSS - 如何使元素在网页中间垂直和水平居中?

发布于 2024-10-03 12:50:25 字数 100 浏览 0 评论 0原文

我的网络表单非常简单。它必须具有三行文本/ASP.NET 元素。母版页有页眉和页脚。我需要将这三行垂直和水平居中于页面中间,尤其是在底部发生变化的情况下。我如何使用 CSS 做到这一点?

My web form is pretty simple. It has to have three lines of text/ASP.NET elements. The Master page has a header and a footer. I need to center those three lines in the middle of the page vertically and horizontally, especially if the bottom changes. How do I do this with CSS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

杀手六號 2024-10-10 12:50:25

在您的母版页中,您将需要一个容器并将其居中,然后您将需要一个用于主要内容的 div。原始代码将类似于:

<div id="center">
   <div id="main">
      <p>This text is perfectly vertically and horizontally centered.</p>
   </div><!-- end #main -->
</div><!-- end #center -->

<style>
   #center { position: absolute; top: 50%; width: 100%; height: 1px; overflow: visible }
   #main { position: absolute; left: 50%; width: 720px; margin-left: -360px; height: 540px; top: -270px } 
</style>

这使用与 Damien 提供的相同方法。它往往是单独使用 CSS 来完成任务的唯一方法。使用 JavaScript/jQuery 或许可以更好地解决这个问题。

In your master page you will want a container and center that, then you will want a div for the main content. the raw code would be something like:

<div id="center">
   <div id="main">
      <p>This text is perfectly vertically and horizontally centered.</p>
   </div><!-- end #main -->
</div><!-- end #center -->

<style>
   #center { position: absolute; top: 50%; width: 100%; height: 1px; overflow: visible }
   #main { position: absolute; left: 50%; width: 720px; margin-left: -360px; height: 540px; top: -270px } 
</style>

This is using the same approach as offered by Damien. It tends to be the only way to accomplish with CSS alone. You can probably better solve this with the use of JavaScript/jQuery.

把回忆走一遍 2024-10-10 12:50:25

水平对齐东西很简单。如果元素有固定的,则给它以下 css:

.element
{
      margin: 0 auto;
}

这告诉元素有 0 顶部和底部 marin 并自动计算左侧和右侧的边距。

有一种方法可以使元素垂直居中,称为死点。它使用距页面顶部 50% 的偏移量和负边距将内容置于中心(垂直)。缺点是您的元素需要具有固定的尺寸(高度和宽度)。

Horizontally aligning things is simple. If the element has a fixed with give it the following css:

.element
{
      margin: 0 auto;
}

This tells the element to have 0 top and bottom marin and auto-calculate the margin to the left and right.

There is a way to vertically center elements called dead center. It uses a 50% off-set from the top of the page and a negative margin to bring the content to the center (vertically). Drawback is that your element needs have a fixed size (height and width).

狼亦尘 2024-10-10 12:50:25

这对我有用:

<style type="text/css">
  .auto-style1 {text-align:center;}
</style>

This is what worked for me:

<style type="text/css">
  .auto-style1 {text-align:center;}
</style>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文