【问题标题】:CSS Flexbox Center Items with Positioning [duplicate]具有定位的 CSS Flexbox 中心项目 [重复]
【发布时间】:2018-07-30 17:30:18
【问题描述】:

我正在尝试使用 flexbox 将 3 个元素定位在特定位置,同时水平和垂直居中。

像这样:

这是我到目前为止所拥有的,我在这里做错了什么?

.container {
  width: 100%;
  max-width: 800px;
  height: auto;
  margin: 0 auto;
  background: pink;
  display: flex;
  flex-wrap: wrap;
  flex-direction: column;

  .first 	{ 
    background: green; 
    height: 50px;
    align-self: stretch;
  }

  .second { 
    background: blue; 
    height: 100px;
  }

  .third 	{ 
    background: crimson; 
    height: 100px;
  }
}
<div class="container">
  <div class="first"></div>
  <div class="second"></div>
  <div class="third"></div>
</div>

    

看看这个小提琴:https://jsfiddle.net/u21uqs7q/

【问题讨论】:

    标签: html css flexbox flexboxgrid


    【解决方案1】:

    只需使用以下 sn-p 更新您的代码..

    .container {
    	width: 100%;
    	max-width: 800px;
    	height: 100vh;
    	margin: 0 auto;
    	background: pink;
    	display: flex;
      align-items: center;
      justify-content: center;
    	
    }
    	.left  .first	{ 
    		background: green; 
    		height: 50px;
    	}
    	
    	.right .second { 
    		background: blue; 
    		height: 100px;
    	}
    	
    	.right .third 	{ 
    		background: crimson; 
    		height: 100px;
    	}
    <div class="container">
    	<div class="left">
      <div class="first">111</div>
      </div>
      
    	<div class="right">
      <div class="second">222</div>
    	<div class="third">333</div>
      </div> 
    </div>

    【讨论】:

      猜你喜欢
      • 2018-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 1970-01-01
      • 2019-05-18
      • 2015-12-17
      相关资源
      最近更新 更多