简体   繁体   English

%基于固定和绝对定位的嵌套元素?

[英]% based, fixed and absolute positioned, nested elements?

Image: 图片: 在此处输入图片说明 I have a container div (yellow) which I'd like to keep at 50% width of the window. 我有一个容器div(黄色) ,我想保持窗口宽度的50%。 A child of that container is an image div (purple) that stretches to 100% of the parent container's width. 该容器的子级是图像div(紫色) ,其图像延伸到父容器宽度的100%。 and there's a sticky label (pink) on top of the image div (position: absolute so it can be offset relatively to the image). 并且在图像div的顶部有一个粘性标签(粉红色) (位置:绝对,因此可以相对于图像进行偏移)。 I'd like to keep that entire half of the screen fixed positioning so it stays sticky as I scroll. 我想将整个屏幕的一半保持固定的位置,以便在滚动时保持粘性。

There's also a title under the image, and that title needs to be visible no matter if someone shrinks the window vertically. 图像下方还有一个标题,无论有人垂直缩小窗口如何,该标题都必须可见。 So in that scenario the image div should shrink vertically, if needed, in order for that title to be shown. 因此,在这种情况下,如果需要, 图像div应该垂直缩小,以便显示该标题。

Basically I'm trying to have the image div always be 100% width of the parent container div . 基本上,我试图使图像div始终是父容器div的 100% 宽度 With the image div having a max % height so it can shrink vertically. 图像div的最大高度%,因此可以垂直缩小。 Or have it keep a fixed aspect ratio (3:4 or whatever) when it shrinks vertically. 或者在垂直收缩时保持固定的纵横比(3:4或其他比例)。

I'm trying to avoid using fixed pixels, or ems, in the entirety of my CSS. 我试图在整个CSS中避免使用固定像素或ems。 since the website needs to be stretchy/'fluid' vertically, because that title under the image has to show. 因为网站必须在垂直方向上具有弹性/“流畅”,因为图片下方的标题必须显示。

HTML looks roughly like: HTML大致如下所示:

<wrapper>
    <left-column>
        <normal text and scrollable stuff>
    <right-column-yellow>
        <image sticky label-pink>
        <image div-purple>
        <image title>

Sorry if this is damn confusing my brain is fried! 抱歉,这真令人困惑,我的大脑被炸了! Can anyone pls help me? 有人可以帮助我吗?

You can divide your left and right panel by using position fixed. 您可以使用固定位置划分左右面板。

If I'm not wrong with your description, this is the answer. 如果我对您的描述没有错,那就是答案。

<div class="wrapper">
  <div class="left">
   <p><!--Some very long text--></p>
  </div>
  <div class="right">
    <div class="image">
    <div class="label">Label</div>
    <div class="title">Title</div>
  </div>
</div>

Some CSS 一些CSS

.left,.right{
    position: fixed;
    width: 50%;
    height: 100%;
    display: inline-block;
  }

  .left{
    left:0;
    top: 0;
    overflow: auto;
  }

  .right{
    right: 0;
    top:0;
    background-color: yellow;
  }

  .right .image{
    position: relative;
    width: 100%;
    height: 50%;
    top: 50%;
    left: 0;
    background-color: #fff;
    transform: translateY(-50%);
  }

  .right .image .label{
    position: absolute;
    left: 0;
    right: 0;
    top: -10px;
    text-align: center;
    width: 200px;
    background-color: pink;
    margin: auto;
  }

  .right .image .title{
    position: absolute;
    left: 0;
    right: 0;
    bottom: -40px;
    text-align: center;
    width: 200px;
    background-color: #000;
    margin: auto;
    color: #fff;
    font-size: 30px;
  }

You can refer to my codeine as well. 您也可以参考我的可待因。 https://codepen.io/masonwongcs/pen/WMJGZb https://codepen.io/masonwongcs/pen/WMJGZb

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

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