简体   繁体   English

如何将元素居中放置在视口中间?

[英]How can I center an element in the middle of the viewport?

I want to center an element (div, modal/dialog box) in the middle of the viewport, regardless of the page size.无论页面大小如何,我都想将元素(div、模态/对话框)居中放置在视口的中间。

The idea is that the dialog box always appears in the middle of the current view port, so it can be seen by a user regardless of the page size.这个想法是对话框总是出现在当前视口的中间,因此无论页面大小如何,用户都可以看到它。 In my case, I have a very long page with lots of scrolling and if the user is at the bottom of the page and the dialog box appears in the middle of the page it cannot be seen by the user because it is not in his viewport.在我的情况下,我有一个很长的页面,有很多滚动,如果用户在页面底部并且对话​​框出现在页面中间,用户将无法看到它,因为它不在他的视口中.

My HTML site is run in an iframe, which makes the task even more difficult.我的 HTML 站点在 iframe 中运行,这使得任务更加困难。

How can I do that with CSS only?我怎么能只用 CSS 做到这一点?

Apply following CSS应用以下 CSS

.center{
 position: fixed;
 left: 50%;
 top: 50%;
 transform: translate(-50%,-50%);
}

Position fixed would make sure it's relative to the viewport.位置固定将确保它相对于视口。 left and top 50% would move the element's top and left to the center. left 和 top 50% 会将元素的顶部和左侧移动到中心。 You'd want it to be in the absolute center, it can be done by transform: translate(-50%,-50%);您希望它位于绝对中心,可以通过转换完成: translate(-50%,-50%);

Try this snippet.试试这个片段。

 .modal{ width: 200px; height: 150px; border: 1px solid black; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
 <div class="modal"> </div>

Well, after you edited the question and mentioned, that the page is in an iframe , I can at least say, that it is not possible to place an element inside of an iframe to the current viewport of the browser.好吧,在您编辑问题并提到页面位于iframe ,我至少可以说,不可能将iframe内的元素放置到浏览器的当前视口。 The most global context of the element is the iframe itself, and it has no information about in whatever it is embedded.元素最全局的上下文是iframe本身,它没有关于它嵌入的任何信息。

如何自动使底部居中<div>在视口上?</div><div id="text_translate"><p> 我正在创建一种仅基于纯文本、没有图像或其他任何内容的网站/实验,它仅包含一个&lt;div&gt;包含几个&lt;span&gt;和&lt;a&gt;元素,这些元素在使用 JS 点击时打开,显示堆叠的文本在&lt;div&gt;的底部。</p><p> 当屏幕上覆盖的文本过多时,问题就出现了,并且用户不断点击——因此,显示更多文本——这些新行一直堆叠在底部,不会自动可见:用户必须开始滚动到继续阅读。</p><p> 有没有办法可以自动将&lt;div&gt;的底部放在屏幕<em>的</em>中心,所以它不需要滚动? 当文本填满整个屏幕时,您是否有更好的解决方法来避免手动滚动?</p><p> 谢谢!</p><p> 编辑:我添加了一段代码,以便更好地理解我所做的。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;div id="container"&gt; &lt;p class="fade-in"&gt; &lt;a data-opens="1" href="#"&gt;Hello&lt;/a&gt;.&lt;span data-openedby="1" class="fade-in"&gt; How are &lt;a data-opens="2" href="#"&gt;you&lt;/a&gt;? &lt;/span&gt;&lt;span class="fade-in" data-openedby="2"&gt;&lt;a data-opens="3" href="#"&gt;Welcome&lt;/a&gt; to my website.&lt;/span&gt; &lt;/p&gt; &lt;/div&gt;</pre></div></div><p></p><p> 基本上,这段代码,但不是只有三个&lt;a&gt; ,到目前为止,它有 300 个带有各自的&lt;span&gt; 。</p></div> - How can I automatically center the bottom of a <div> on the viewport?

暂无
暂无

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

相关问题 如何将 svg 居中在视口中间的 css 中? - How can i center my svg in css in the middle of viewport? 我怎样才能 position 视口中心的模态元素? - How can I position a modal element in the center of the viewport? 如何在页面中间放置此中心? - How can I make this center in the middle of the page? 如何在跨度中间将这些项目居中? - How can I center these items in the middle of span? 如何将菜单置于中间位置? - How can I center my menu in the middle? 如何在视口中间放置图像元素? - How to position an image element in the middle of the viewport? 如何将div中的文本居中与视口单元? - How can I center text in a div with viewport units? 如何制作视口以查看页面中心? - How can I make viewport to view center of the page? 如何自动使底部居中<div>在视口上?</div><div id="text_translate"><p> 我正在创建一种仅基于纯文本、没有图像或其他任何内容的网站/实验,它仅包含一个&lt;div&gt;包含几个&lt;span&gt;和&lt;a&gt;元素,这些元素在使用 JS 点击时打开,显示堆叠的文本在&lt;div&gt;的底部。</p><p> 当屏幕上覆盖的文本过多时,问题就出现了,并且用户不断点击——因此,显示更多文本——这些新行一直堆叠在底部,不会自动可见:用户必须开始滚动到继续阅读。</p><p> 有没有办法可以自动将&lt;div&gt;的底部放在屏幕<em>的</em>中心,所以它不需要滚动? 当文本填满整个屏幕时,您是否有更好的解决方法来避免手动滚动?</p><p> 谢谢!</p><p> 编辑:我添加了一段代码,以便更好地理解我所做的。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-html lang-html prettyprint-override"> &lt;div id="container"&gt; &lt;p class="fade-in"&gt; &lt;a data-opens="1" href="#"&gt;Hello&lt;/a&gt;.&lt;span data-openedby="1" class="fade-in"&gt; How are &lt;a data-opens="2" href="#"&gt;you&lt;/a&gt;? &lt;/span&gt;&lt;span class="fade-in" data-openedby="2"&gt;&lt;a data-opens="3" href="#"&gt;Welcome&lt;/a&gt; to my website.&lt;/span&gt; &lt;/p&gt; &lt;/div&gt;</pre></div></div><p></p><p> 基本上,这段代码,但不是只有三个&lt;a&gt; ,到目前为止,它有 300 个带有各自的&lt;span&gt; 。</p></div> - How can I automatically center the bottom of a <div> on the viewport? 如何将div居中于Google等浏览器视口的中间 - how to center div in middle of browser viewport like google
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM