调整窗口窗口大小时重新定位html元素

问题描述 投票:0回答:1

我连续有3个元素。当窗口调整为更窄时,我希望中心元素移动到顶部,右边元素移动到底部。然后左边的元素应该在中间。理想情况下,它们应该同时移动,所以如果最右边的元素向下移动,中心元素也应该向上移动。

谢谢你的帮助!

图片显示了所需的结果。

Wide Narrow

HTML

<div class="container">
    <span class="left">This should go in between</span>
    <span class="center">This should go ABOVE</span>
    <span class="right">This should go BELOW</span>
</div>

CSS

body {
  margin: 0px;
  padding: 0px;
}

.container {
  width: 100%;
}

.container span {
  min-width: 200px;
  width: calc(100% /3);
  width: 33.33%;
}

.left {
  float: left;
  background-color: red;
}

.center {
  text-align: center;
  float: left;
  background-color: green;
}

.right {
  float: right;
  text-align: right;
  background-color: blue;
}

@media (max-width: 600px) {
  .right {
    float: left;
    text-align: left;
  }
  .center {
    text-align: left;
  }
}

JSFiddle

css mobile dynamic position
1个回答
0
投票

我能够找到一些解决方案。 @media css规则处理屏幕宽度缩小到某个宽度以下的时间。这是一个例子:Solution 1

我最终使用的解决方案是使用flexbox和“order”属性来设置元素的顺序。然后@media规则在屏幕足够薄时更改顺序#。

这是解决方案1(我没有写这个):

<div class="container">
    <div class="inner-w">        
        <div class="block center">Center</div>  
        <div class="block left">Left</div>
        <div class="block right">Right</div>
    </div>
</div> <!-- .container -->

* {
    box-sizing: border-box;
}

.container {
    width: 100%;
    float: left;
    overflow: hidden; /* should be clearfix instead */
}

.container .inner-w {
    position: relative;
    max-width: 50em;
    margin-right: auto;
    margin-left: auto;
    overflow: hidden; /* should be clearfix instead */
}

.block {
    width: 100%;
    float: left;
    min-height: 10em; /* just for show */
}

@media (min-width: 50em) {  

    .center {
        width: 50%;
        position: relative;
        left: 25%;
    }

    .left {
        position: absolute;
        top: 0;
        left: 0;
        width: 25%;
    }

    .right {
        float: right;
        width: 25%;    
    }
} /* end break-point */















/* just for show */
.container {
    border: 1px solid blue;
}

.container .inner-w {
    border: 1px solid orange;
}

.block {
    border: 1px solid red;
    min-height: 10em; /* just for show */
    padding: 1em;
}
© www.soinside.com 2019 - 2024. All rights reserved.