英文:
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 -->
<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>
<!-- end snippet -->
答案1
得分: 1
-
您必须定义一个高度,否则它将随内容增长,可能会出现内容超过垂直可用空间的情况。您将不得不使用
overflow: hidden
来隐藏溢出。 -
您并没有真正居中窗口。如果您想这样做,我建议您改变焦点。使一个元素占据整个可用窗口,使用
display: flex
并使用align-items
和justify-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;
}
- 您已经有一个专为此任务设计的交互标签:
<dialog>
。
英文:
You have several problems.
-
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. -
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 withalign-items
andjustify-content
.
An example.
<section class="modal">
<div class="modal__content">
<!-- Div that will be centered on the screen and will contain all your content. -->
</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;
}
- You already have an interactive tag that is designed for the task:
<dialog>
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论