简体   繁体   English

即使在容器溢出时也要保持元素居中

[英]Keep element centered even when overflowing its container

I recreated my problem in the snippet below: If you make it Full page and drag to decrease the horizontal size of the window the h1 element doesn't stay centered when it exceeds the bounds of it's container. 我在下面的代码片段中重新创建了我的问题:如果你将它设为全页并拖动以减小窗口的水平尺寸,当h1元素超出其容器的边界时,它不会保持居中。

Here is a visual: 这是一个视觉:

在此输入图像描述

Notice after it exceeds its containers bounds it just remains left aligned. 请注意,超过其容器边界后,它仍然保持左对齐。 I would like it to stay centered. 我希望它保持中心。 Is there a simple CSS fix? 有一个简单的CSS修复?

 @import url( 'https://necolas.github.io/normalize.css/latest/normalize.css' ); * { outline: 1px solid red; } html, body { overflow: hidden; height: 100%; text-align: center; } h1 { margin: 0; color: #f3f3f3; } .v-cntr { position: relative; top: 50%; transform: translateY( -50% ); } .hdg-wrap { font-size: 5.5rem; position: absolute; top: 48%; right: 0; bottom: 0; left: 0; transform: translateY( -100% ); z-index: -1; } 
 <header class="v-cntr"> <!-- << v-cntr = vertically center --> <!-- ----------------------- HEADING WRAPPER ------------------------ --> <div class="hdg-wrap"> <h1>RIDE</h1> </div> <!-- ------------------------ IMAGE WRAPPER ------------------------- --> <div class="img-wrap"> <img src="http://svgshare.com/i/xL.svg" alt="Logo"> </div> </header> 

I'm trying not to change the structure of the HTML 我试图不改变HTML的结构


EDIT: I'm getting answers attempting to center the image instead of the text behind it. 编辑:我得到的答案是试图使图像居中而不是背后的文字。 I can see the confusion as both get off center. 我可以看到混乱,因为两个偏离中心。 I will reiterate: My main concern is to keep the h1 ( 'ride' text ) element centered in the window as it shrinks. 我将重申:我主要担心的是在收缩时将h1 ('ride'文本)元素保持在窗口中心。

You can reset the text-alignment for .img-wrap and center the image inside it using this CSS. 您可以重置.img-wrap的文本对齐,并使用此CSS将图像置于其中。

.img-wrap {
  text-align: initial;
  position: relative;
}
.img-wrap img {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

ADDITION after comment: And you can apply the same priciple to the h1 . 评论后的补充:您可以将相同的原则应用于h1 Since its parent is already absolutely positioned, I didn't change this and just added 由于它的父母已经绝对定位,我没有改变它,只是添加了

h1 {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

Here's the complete snippet: 这是完整的片段:

 @import url('https://necolas.github.io/normalize.css/latest/normalize.css'); * { outline: 1px solid red; } html, body { overflow: hidden; height: 100%; text-align: center; } h1 { margin: 0; padding: 0; color: #eee; position: absolute; left: 50%; transform: translateX(-50%); } .v-cntr { position: relative; top: 50%; transform: translateY( -50%); } .hdg-wrap { color: #fff; opacity: 0.5; font-size: 5.5rem; position: absolute; top: 48%; right: 0; bottom: 0; left: 0; transform: translateY( -100%); z-index: -1; } .img-wrap { text-align: initial; position: relative; } .img-wrap img { position: absolute; left: 50%; transform: translateX(-50%); } 
 <header class="v-cntr"> <!-- << v-cntr = vertically center --> <!-- -- - - - - - - - - - - - - - HEADING - - - - - - - - - - - - - - --> <div class="hdg-wrap"> <!-- << hdg-wrap = heading wrapper --> <h1>RIDE</h1> </div> <!-- -- - - - - - - - - - - - IMAGE WRAPPER - - - - - - - - - - - - - --> <div class="img-wrap"> <img src="http://svgshare.com/i/xL.svg" alt="Logo"> </div> </header> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM