css to center a popup on the screen, works on computer and tablet, but not phone

huangapple go评论45阅读模式
英文:

css to center a popup on the screen, works on computer and tablet, but not phone

问题

试图在屏幕中央显示一个帮助弹出窗口,使用css。在我的计算机和iPad上,它运行正常,但在iPhone上,弹出窗口垂直居中于整个可滚动的垂直空间,这意味着我有时必须上下滚动才能看到它。我希望在调用时它在当前可见的屏幕内居中。(请注意,页面高度不是静态的,即根据用户活动而增长和缩小)。

这是相关的CSS:

.helpcontent {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  transition: 200ms ease-in-out;
  border: 1px solid black;
  border-radius: 10px;
  z-index: 10;
  background-color: khaki;
  width: 500px;
  max-width: 80%;
  padding: 10px 15px;
}
<div class="helpcontent">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
英文:

Trying to display a help popup in the center of the screen, using css. On my computer and ipad, it works fine, but on iPhone the popup is vertically centered within the entire scrollable vertical space, which means I sometimes have to scroll up or down to see it. I want it centered within the currently visible screen at the time it is invoked. (Note the page height is not static, i.e. grows and shrinks based on user activity).

Here is the relvant css:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

  .helpcontent {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    transition: 200ms ease-in-out;
    border: 1px solid black;
    border-radius: 10px;
    z-index: 10;
    background-color: khaki;
    width: 500px;
    max-width: 80%;
    padding: 10px 15px;
  }

<!-- language: lang-html -->

&lt;div class=&quot;helpcontent&quot;&gt;
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

  1. 您必须定义一个高度,否则它将随内容增长,可能会出现内容超过垂直可用空间的情况。您将不得不使用 overflow: hidden 来隐藏溢出。

  2. 您并没有真正居中窗口。如果您想这样做,我建议您改变焦点。使一个元素占据整个可用窗口,使用 display: flex 并使用 align-itemsjustify-content 居中。

示例。

<section class="modal">
    <div class="modal__content">
        <!-- 将在屏幕上居中并包含所有内容的 div。 -->
    </div>
</section>
.modal {
    display: flex;
    justify-content: center;
    align-items: center;
    position: fixed;
    inset: 0;
}

.modal__content {
    width: 10rem;
    height: 10rem;
    border: 1px solid black;
}
  1. 您已经有一个专为此任务设计的交互标签:<dialog>
英文:

You have several problems.

  1. You must define a height, otherwise it will grow with the content, and it may happen that you have more content than space available vertically. You will have to use overflow: hidden to hide the overflow.

  2. You are not really centering the window. If you want to do it I recommend you to change the focus. Make an element occupy all the available window, give it a display: flex and center it with align-items and justify-content.

An example.

&lt;section class=&quot;modal&quot;&gt;
    &lt;div class=&quot;modal__content&quot;&gt;
        &lt;!-- Div that will be centered on the screen and will contain all your content. --&gt;
    &lt;/div&gt;
&lt;/section&gt;
.modal {
    display: flex;
    justify-content: center;
    align-items: center;
    position: fixed;
    inset: 0;
}

.modal__content {
    width: 10rem;
    height: 10rem;
    border: 1px solid black;
}
  1. You already have an interactive tag that is designed for the task: &lt;dialog&gt;.

huangapple
  • 本文由 发表于 2023年2月9日 01:34:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75389666.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定