【问题标题】:Reposition html elements when window window is resized调整窗口大小时重新定位 html 元素
【发布时间】:2018-12-14 12:57:52
【问题描述】:

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

感谢您的帮助!

图片显示了预期的结果。

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】:

    我能够找到几个解决方案。当屏幕宽度缩小到某个宽度以下时,@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;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-13
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      • 1970-01-01
      • 1970-01-01
      • 2021-10-28
      • 1970-01-01
      • 2014-03-18
      相关资源
      最近更新 更多