简体   繁体   English

大小未知的垂直和水平居中元素,具有固定div中的块同级

[英]Vertically and horizontally centering element of unknown size with block sibling in fixed div

I have a sidebar fixed to the left of the screen. 我的侧边栏固定在屏幕的左侧。

I have the current time in a paragraph tag at the top of the sidebar. 我在侧边栏顶部的段落标签中有当前时间。 The width of this element is what sets the width of the sidebar. 该元素的宽度是设置边栏宽度的因素。

What I am struggling with is then adding another paragraph to the sidebar which is centered horizontally and vertically in the sidebar. 然后,我正在努力的是在侧边栏中添加另一个段落,该段落在侧边栏中水平和垂直居中。

I've looked at many other examples of centering elements but can't seem to get it. 我看过许多其他居中元素的示例,但似乎无法理解。 Help is much appreciated. 非常感谢您的帮助。

Basic structure: 基本结构:

<div id="left">
  <p id="time">
    12:03<span>45</span>
  </p>
  <p id="currentWeather">
    <!-- I've replaced this <i> with an <img> to avoid loading the font in the fiddle -->
    <i class="wi wi-day-sunny"></i>
    <img src="http://findicons.com/files/icons/2603/weezle/256/weezle_sun.png">
    <span>32</span>
  </p>
</div>

Fiddle: https://jsfiddle.net/w5m7qd6u/1/ 小提琴: https : //jsfiddle.net/w5m7qd6u/1/

Perhaps this is what you're looking for... 也许这就是您要寻找的...

A combination of height: 100vh on a parent element, setting the top element to be position: absolute and thereby not positioned vertically, and adding styles to the <p> which you will vertically center: height: 100vh元素的组合height: 100vh父元素上的height: 100vh ,将顶部元素设置为position: absolute ,因此未垂直放置,并为<p>添加了样式,您将在这些元素上垂直居中:

#currentWeather {
  top: calc(50%);
  -webkit-transform: translateY(-50%);
  transform: translateY(-50%);
}

Full code and snippet below: (view full page for clarity) 以下是完整的代码和摘要:(为清楚起见,请查看整页)

I commented out the font sizes to make this easier on me with a smaller window, but that shouldn't affect it. 我注释掉了字体大小,以便使用较小的窗口使此操作更容易,但这不应该影响它。

 html, body { height: 100%; width: 100%; margin: 0; padding: 0; font-family: "Roboto", Helvetica, sans-serif; } #left { position: fixed; z-index: 1; top: 0; left: 0; height: 100vh; box-sizing: border-box; padding: 24px; background-color: rgba(0, 0, 0, 0.7); color: white; text-align: center; } #time { position: absolute; width: 100%; left: 0; padding: 0; margin: 0; /* font-size: 90pt; */ } #time > span { margin-left: 6px; /* font-size: 24pt; */ } #currentWeather { position: relative; left: 0; /* font-size: 84pt; */ margin: auto; top: calc(50%); -webkit-transform: translateY(-50%); transform: translateY(-50%); } #currentWeather > span { display: block; /* font-size: 40pt; */ } /* this is just a placeholder. real image is dynamic and won't have set height */ img { width: 124px; height: auto; } 
 <div id="left"> <p id="time"> 12:03<span>45</span> </p> <p id="currentWeather"> <!-- <i class="wi wi-{{currentConditions}}"></i> --> <!-- have used image for placeholder to avoid uploading font --> <img src="http://findicons.com/files/icons/2603/weezle/256/weezle_sun.png"><span>32</span> </p> </div> 

html HTML

<div id="left">
  <p id="time">
    12:03<span>45</span>
  </p>
  <div id="hvcenterme">
     <p>
       Another Paragraph centered both ways.
     </p>
  </div>
  <p id="currentWeather">
    <!-- <i class="wi wi-{{currentConditions}}"></i> -->
    <!-- have used image for placeholder to avoid uploading font -->
    <img src="http://findicons.com/files/icons/2603/weezle/256/weezle_sun.png"><span>32</span>
  </p>
</div>

css CSS

html,
body {
  height: 100%;
  width: 100%;
  margin: 0;
  padding: 0;
  font-family: "Roboto", Helvetica, sans-serif;
}

#left {
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  height: 100vh;
  display:flex;
  flex-direction:column;
  padding: 24px;
  background-color: rgba(0, 0, 0, 0.7);
  color: white;
  text-align: center;
}

#time {
  flex:0 0 auto;
  margin:0;
  font-size: 90pt;
}

#time > span {
  margin-left: 6px;
  font-size: 24pt;
}

#hvcenterme {
   flex:1 0 auto;
   margin:0;
   width:100%;
   display:flex;
   background:Green;
}

#hvcenterme p {
  flex:1 0 100%;
  line-height:1.5em;
  margin:0;
  text-align:center;
  align-self:center;
  background:Blue;
}

#currentWeather {
  flex:0 0 auto;
  font-size: 84pt;
  margin:0 auto;
}

#currentWeather > span {
  display: block;
  font-size: 40pt;
}

/* this is just a placeholder. real image is dynamic and won't have set height */
img {
  width: 124px;
  height: 112px;
}

JSFiddle link JSFiddle链接

https://jsfiddle.net/w5m7qd6u/7/ https://jsfiddle.net/w5m7qd6u/7/

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

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