容器内的Flexbox忽略了对齐内容

时间:2015-08-21 16:54:05

标签: css flexbox

我希望灵活容器中的两个div占据60%的空间,30%分别占据空间,但是它们没有空间而被碾压。

html
<!doctype html>
<html lang="en">
<head>
<title>gameplan.org.uk | A site for enthusiasts by enthusiasts.        
</title>

<link href="test.css" rel="stylesheet">

</head>

<header>Header</header>
<div id='main'>
<article>Player Page
<p>
<div class='playercontainer'>
<div class='playerbox1'>Player Summary</div>
<div class='playerbox2'>Player Details</div>
</div>
</p>
</article>
<nav>Menu</nav>
<aside>Stuff</aside>
</div>
<footer>Footer</footer>
</body>

和css: -

body {
font: 24px Helvetica;
background: #999999;
color:rgba(0,0,0,.25);
}

#main {
min-height: 800px;
margin: 0px;
padding: 0px;
display: -webkit-flex;
display:                 flex;
-webkit-flex-flow: row;
flex-flow: row;        
}

#main > article {
margin: 4px;
padding: 5px;
border: 1px solid #cccc33;
border-radius: 7pt;
background: #dddd88;
-webkit-flex: 3 1 60%;
flex: 3 1 60%;
-webkit-order: 2;
order: 2;
}

#main > nav {
margin: 4px;
padding: 5px;
border: 1px solid #8888bb;
border-radius: 7pt;
background: #ccccff;
-webkit-flex: 1 6 20%;
flex: 1 6 20%;
-webkit-order: 1;
order: 1;
}

#main > aside {
margin: 4px;
padding: 5px;
border: 1px solid #8888bb;
border-radius: 7pt;
background: #ccccff;
-webkit-flex: 1 6 20%;
flex: 1 6 20%;
-webkit-order: 3;
order: 3;
}

header, footer {
display: block;
margin: 4px;
padding: 5px;
min-height: 100px;
border: 1px solid #eebb55;
border-radius: 7pt;
background: #ffeebb;
}

/* Too narrow to support three columns */
@media all and (max-width: 640px) {

#main, #page {
-webkit-flex-flow: column;
flex-flow: column;
}

#main > article, #main > nav, #main > aside {
/* Return them to document order */
-webkit-order: 0;
order: 0;
}

#main > nav, #main > aside, header, footer {
min-height: 50px;
max-height: 50px;
}
}


/*On the flex container*/
.playercontainer {
webkit-inline-flex; /* Safari */
display: inline-flex;     
-webkit-flex-flow: row;
flex-flow: row;
-webkit-flex-wrap: wrap; /* Safari */
flex-wrap: wrap;
-webkit-justify-content: space-around; /* Safari */
justify-content: space-around;
-webkit-flex: 3 1 100%;
flex: 3 1 0%;
}

.playerbox1 {
border-style: solid;
border-color: #0000ff;
-webkit-order: 1; /* Safari */
order: 1;
-webkit-flex: 3 1 60%;
flex: 3 1 60%;
}


.playerbox2 {
border-style: solid;
border-color: #ff0000;
-webkit-flex: 1 1 30%; /* Safari */
flex: 1 1 30%;
-webkit-order: 1; /* Safari */
order: 1;
}

我确定有一些简单的东西我不知道但是我无法解决问题。您可以在此处查看代码1

2 个答案:

答案 0 :(得分:1)

您需要在flexbox这里检查两件事

  1. display: inline-flex更改为display: flex,因为内联不会 取父容器宽度但内容宽度。

  2. flex-grow起已将flex-shrinkflex-basis设为0 负责扩展(宽度覆盖)。

  3. 修改后的CSS

    .playercontainer {
      display: flex;     
    }
    
    .playerbox1 {
      flex-basis: 60%;
    }
    
    .playerbox2 {
      flex-basis: 30%;
    }
    

    <强>输出:

    body {
        font: 24px Helvetica;
        background: #999999;
        color: rgba(0, 0, 0, .25);
      }
      #main {
        min-height: 800px;
        margin: 0px;
        padding: 0px;
        display: -webkit-flex;
        display: flex;
        -webkit-flex-flow: row;
        flex-flow: row;
      }
      #main > article {
        margin: 4px;
        padding: 5px;
        border: 1px solid #cccc33;
        border-radius: 7pt;
        background: #dddd88;
        -webkit-flex: 3 1 60%;
        flex: 3 1 60%;
        -webkit-order: 2;
        order: 2;
      }
      #main > nav {
        margin: 4px;
        padding: 5px;
        border: 1px solid #8888bb;
        border-radius: 7pt;
        background: #ccccff;
        -webkit-flex: 1 6 20%;
        flex: 1 6 20%;
        -webkit-order: 1;
        order: 1;
      }
      #main > aside {
        margin: 4px;
        padding: 5px;
        border: 1px solid #8888bb;
        border-radius: 7pt;
        background: #ccccff;
        -webkit-flex: 1 6 20%;
        flex: 1 6 20%;
        -webkit-order: 3;
        order: 3;
      }
      header,
      footer {
        display: block;
        margin: 4px;
        padding: 5px;
        min-height: 100px;
        border: 1px solid #eebb55;
        border-radius: 7pt;
        background: #ffeebb;
      }
      /* Too narrow to support three columns */
      @media all and (max-width: 640px) {
        #main,
        #page {
          -webkit-flex-flow: column;
          flex-flow: column;
        }
        #main > article,
        #main > nav,
        #main > aside {/* Return them to document order */
          -webkit-order: 0;
          order: 0;
        }
        #main > nav,
        #main > aside,
        header,
        footer {
          min-height: 50px;
          max-height: 50px;
        }
      }
    /*On the flex container*/
      .playercontainer {
        
       
        display: flex;
        -webkit-flex-flow: row;
        flex-flow: row;
        -webkit-flex-wrap: wrap;/* Safari */
        flex-wrap: wrap;
        -webkit-justify-content: center;/* Safari */
        justify-content: center;
        -webkit-justify-content: space-around;/* Safari */
        justify-content: space-around;
      }
      .playerbox1 {
        border-style: solid;
        border-color: #0000ff;
        -webkit-order: 1;/* Safari */
        order: 1;
        -webkit-flex: 0 0 60%;
        flex: 0 0 60%;
      }
      .playerbox2 {
        border-style: solid;
        border-color: #ff0000;
        -webkit-flex: 1 1 30%;
        /* Safari */
        flex: 0 0 30%;
        -webkit-order: 1;
        /* Safari */
        order: 1;
      }
    <!doctype html>
    <html lang="en">
    
    <head>
      <title>gameplan.org.uk | A site for enthusiasts by enthusiasts.
      </title>
    
      <link href="test.css" rel="stylesheet">
    
    </head>
    
    <header>Header</header>
    <div id='main'>
      <article>Player Page
        <p>
          <div class='playercontainer'>
            <div class='playerbox1'>Player Summary</div>
            <div class='playerbox2'>Player Details</div>
          </div>
        </p>
      </article>
      <nav>Menu</nav>
      <aside>Stuff</aside>
    </div>
    <footer>Footer</footer>
    </body>

答案 1 :(得分:1)

对.playerContainer进行一些小改动。 删除2行for justify-content并添加宽度:100%

 .playercontainer {
    webkit-inline-flex; /* Safari */
    display: inline-flex;     
    -webkit-flex-flow: row;
    flex-flow: row;
    -webkit-flex-wrap: wrap; /* Safari */
    flex-wrap: wrap;
    -webkit-flex: 3 1 100%;
    flex: 3 1 0%;
    width:100%;
}

enter image description here