8

サンプルはhttp://jsfiddle.net/GGYtM/にあります。要求されたインライン コードは次のとおりです。

<html>
<style type='text/css>
.flex{
  /* old syntax */
  display: -webkit-box;
  display: -moz-box;

  /* new syntax */
  display: -webkit-flex; 
  display: -moz-flex; 
  display: -o-flex; 
  display: -ms-flex; 
  display: flex; 
}

.flex-direction-horizontal{
  /* old syntax */
  -webkit-box-orient: horizontal;
  -moz-box-orient: horizontal;

  /* new syntax */
  -webkit-flex-direction:raw;
  -moz-flex-direction:raw; 
  -o-flex-direction:raw; 
  -ms-flex-direction:raw; 
  flex-direction: raw;
}
.flex-cross-align-stretch{
  /* old syntax */
  -webkit-box-align:stretch;
  -moz-box-align:stretch;

  /* new syntax */
  -webkit-align-items:stretch;
  -moz-align-items:stretch;
  -o-align-items:stretch;
  -ms-align-items:stretch;
  align-items:stretch;
}  
.container{
  border: 1px solid gray;
  padding:5px;
  background:#ecd953;
  -moz-border-radius: 5px;
  border-radius: 5px;
}
.button{
  width:70px;
  height:50px;
  /*margin:5px;*/
  background: #1b486f;
  color : white;
  position:relative;
  text-align:center;
  padding-top:5px;
}

.wrap{
  margin:5px;
}
​
</style>
<body>
    <div class="flex flex-direction-horizontal flex-cross-align-stretch container" id='root'>
    <div class="wrap">
        <div id="elem2" class="button">
      <span id="txt">2</span>
    </div>
     </div>
    </div>
</body>
</html>

Firefox では、「ルート」の div 要素は親要素の幅に合わせて大きくなりませんが、コンテンツに合わせて必要なスペースを占有します - これは完璧です。ただし、Chrome と Safari では、「ルート」の div 要素が大きくなり、親コンテナーの幅全体を占有します。この違いの理由は何ですか?理想的には FF の動作を実現したいのですが、完璧です。

4

1 に答える 1

2

あなたが使う

display: flex; 

ただし、親要素の幅に合わせて大きくしたくないが、コンテンツに合わせて必要なスペースを占有したい場合は、使用する必要があります

display: inline-flex;

古いブラウザの場合、必要になる場合があります

display: -moz-box;
display: -ms-inline-flexbox;
display: -webkit-inline-flex;
display: inline-flex;

.flex {
  /* old syntax */
  display: -moz-box;
  display: -ms-inline-flexbox;
  /* new syntax */
  display: -webkit-inline-flex;
  display: inline-flex;
}
.container {
  border: 1px solid gray;
  padding: 5px;
  background: #ecd953;
  -moz-border-radius: 5px;
  border-radius: 5px;
}
.button {
  width: 70px;
  height: 50px;
  background: #1b486f;
  color: white;
  position: relative;
  text-align: center;
  padding-top: 5px;
}
.wrap {
  margin: 5px;
}
<div class="flex container">
  <div class="wrap">
    <div id="elem2" class="button">
      <span id="txt">2</span>
    </div>
  </div>
</div>

于 2015-01-02T18:24:00.300 に答える