【问题标题】:Fixed element resize-able by inner elements固定元素可通过内部元素调整大小
【发布时间】:2012-10-08 19:39:05
【问题描述】:

我在 css 中有这段代码:

div#show{
background-color:black;
position: fixed;
width:100px;
height:100px;
left: 0;
right: 0;
top: 0;
bottom: 0;

margin: auto;
}

它在我的页面上居中显示一个黑框。但是我想让它成为非常量的大小。

div#show{
background-color:black;
position: fixed;
left: 0;
right: 0;
top: 0;
bottom: 0;

margin: auto;
}

但在这种情况下,它会覆盖整个页面。是否有解决方案使其尽可能大以覆盖内部元素?

【问题讨论】:

    标签: css html fixed


    【解决方案1】:

    使用position: fixed; 使元素忽略其父元素并适合窗口。

    您可以只使用left: 20%; right: 20%; top: 20%; bottom: 20%; 或类似名称,它会拉伸以填满屏幕,在外面留下 20% 的空间……

    如果您将父级设置为position:relative,然后

    div#show{
      background-color:black;
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
    
      margin: auto;
    }
    

    它只会拉伸以覆盖父元素

    【讨论】:

    • 我说的是拉伸到子元素。
    • @oneat 父级的大小将由子级决定,因此您的#show div 将拉伸到子级组合的大小。
    • 但事实并非如此。它从边到边覆盖。
    • 你想覆盖什么?每个子元素单独?还是一个块的所有子元素?
    • 我在这个固定(位置固定)元素中有图像,我希望它居中。
    猜你喜欢
    • 1970-01-01
    • 2017-05-09
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多