使用flexbox定位错误

时间:2017-08-30 10:29:38

标签: html css css3 flexbox

所以我正在做这个项目,并且我已经开始遇到显示属性和元素问题的问题,而不像我希望的那样。这是我到目前为止所拥有的:https://codepen.io/benasl/pen/zdMbKQ



* {
    box-sizing: border-box;
}

html, body {
    margin:0;
    background: #282828;
    font-family: 'Open Sans', sans-serif;
}
p {
    font-size: 12px;
    text-align: justify;
    color: #aec7d5;
}
#container {
    min-width: 320px;
    max-width: 782px;
    height: auto;
    background: #333333;
    padding: 5px;
    height: 300px;
    display: block;
    margin:0 auto;
    margin-top: 10%;
}
.wrapper {
    padding: 15px 10px;
    display: inline-block;
    width: 100%;
}

.left {
    padding-right: 10px;
    width: 50%;
    border-right: 1px solid #797977;
    display: flex;
    float: left;
}
.below {
    display: flex;
    clear: both;
    width: 50%;
    padding-right: 10px;
}
.rating {
    display: flex;
    float: left;
    margin-right: 10px;
    border-right: 1px dotted #c3c3c2;
}
.about {
    display: inline-block;
    float: left;
}
.rate {
    font-size: 20px;
    display: inline-block;
}
.star {
    display: inline-block;
    height: 30px;
    width: 30px;
}
.right {
    padding-left: 20px;
    width: 50%;
    display: flex;
}
aside {
    width: 40%;
    height: 95px;
    overflow: hidden;
    border: 1.5px solid #bbbbbb;
    display: inline-block;
    float: left;
    margin-right: 15px;
}
section {
    display: inline-block;
    float: left;
    width: 60%;
}
aside img {
    height: 100%;
    width: auto;
    position: relative;
    left: -20px;
}
.height {
    height: auto;
    top: -50px;
}

h1 {
    font-family: arial;
    font-size:bold;
    color: white;
    font-size: 18px;
}

.movieTitle {
    margin: 0;
    text-transform: capitalize;
    min-height: 45px;
}
.genre {
    text-transform: uppercase;
    color: #aec7d5;
    font-size: 10px;
    font-weight: 300;
    margin: 0;
    margin-bottom: 10px;
}
.btn {
    background:#868684;
    padding: 6px 20px 6px 10px;
    border-radius: 5px;
    border:none;
    color:#c3c3c2;
    cursor: pointer;
    transition:all 0.15s;
}
.btn:hover {
    background: #767676;
}
.btn .arrow {
    margin-right: 5px;
}

<div id="container">
          
        <div class="wrapper">
            <div class="left">
                <aside><img src="orh82o67aDQra74Tlp4-o.jpg"></aside>
                <section>
                    <h1 class="movieTitle">A Bug's life</h1>
                    <h2 class="genre">Animation, Adventure, Comedy</h2>
                    <button class="btn"><img class="arrow" src="Layer%207%20copy.png">more</button>
                </section>
            
            </div>
            <div class="below">           
                <div class="rating">
                    <img class="star" src="star.png">
                    <h1 class="rate">8.1</h1>
                </div>
                <div class="about"><p>A misfit ant, looking for "warriors" to save his colony from greedy grasshoppers, recruits a group of bugs that turn out to be an inept circus troupe.</p></div>
            </div>
            <div class="right">
                <aside><img class="height" src="MV5BNTM5OTg2NDY1NF5BMl5BanBnXkFtZTcwNTQ4MTMwNw@@._V1_UX182_CR0,0,182,268_AL_.jpg"></aside>
                <section>
                    <h1 class="movieTitle">All Quiet on
the Western Front</h1>
                    <h2 class="genre">Drama, War</h2>
                    <button class="btn"><img class="arrow" src="Layer%207%20copy.png">more</button>
                </section>
            </div>  
            <div class="below">           
                <div class="rating">
                    <img class="star" src="star.png">
                    <h1 class="rate">8.1</h1>
                </div>
                <div class="about"><p>A misfit ant, looking for "warriors" to save his colony from greedy grasshoppers, recruits a group of bugs that turn out to be an inept circus troupe.</p></div>
            </div>
            </div>
        </div>
      </div>
&#13;
&#13;
&#13;

在我添加.below课程及其所有内容之前,一切都还可以,我需要的是.left位于左侧,.right位于右侧..

我尝试过各种各样的显示属性,但似乎都没有。

2 个答案:

答案 0 :(得分:1)

请注意,您的标记和CSS可以清理很多,但我选择不为您执行此操作。

如果您将.below元素移动到每个.left / .right元素中,请将flex-wrap: wrap添加到.left / .right规则中,使用calc()作为aside的宽度(因此需要考虑边框/边距),您将获得良好的开端,看看Flexbox如何工作以及重新构建标记。

Updated codepen

Stack snippet

* {
    box-sizing: border-box;
}

html, body {
    margin:0;
    background: #282828;
    font-family: 'Open Sans', sans-serif;
}
p {
    font-size: 12px;
    text-align: justify;
    color: #aec7d5;
}
#container {
    min-width: 320px;
    max-width: 782px;
    height: auto;
    background: #333333;
    padding: 5px;
    height: 300px;
    display: block;
    margin:0 auto;
    margin-top: 10%;
}
.wrapper {
    padding: 15px 10px;
    display: inline-block;
    width: 100%;
}

.left {
    padding-right: 10px;
    width: 50%;
    border-right: 1px solid #797977;
    display: flex;
    float: left;
  flex-wrap: wrap;
}
.below {
    display: flex;
    clear: both;
    width: 100%;
    padding-right: 10px;
}
.rating {
    display: flex;
    float: left;
    margin-right: 10px;
    border-right: 1px dotted #c3c3c2;
}
.about {
    display: inline-block;
    float: left;
}
.rate {
    font-size: 20px;
    display: inline-block;
}
.star {
    display: inline-block;
    height: 30px;
    width: 30px;
}
.right {
    padding-left: 20px;
    width: 50%;
    display: flex;
  flex-wrap: wrap;
}
aside {
    width: calc(40% - 18px);
    height: 95px;
    overflow: hidden;
    border: 1.5px solid #bbbbbb;
    margin-right: 15px;
}
section {
    width: 60%;
}
aside img {
    height: 100%;
    width: auto;
    position: relative;
    left: -20px;
}
.height {
    height: auto;
    top: -50px;
}

h1 {
    font-family: arial;
    font-size:bold;
    color: white;
    font-size: 18px;
}

.movieTitle {
    margin: 0;
    text-transform: capitalize;
    min-height: 45px;
}
.genre {
    text-transform: uppercase;
    color: #aec7d5;
    font-size: 10px;
    font-weight: 300;
    margin: 0;
    margin-bottom: 10px;
}
.btn {
    background:#868684;
    padding: 6px 20px 6px 10px;
    border-radius: 5px;
    border:none;
    color:#c3c3c2;
    cursor: pointer;
    transition:all 0.15s;
}
.btn:hover {
    background: #767676;
}
.btn .arrow {
    margin-right: 5px;
}
<div id="container">
    <div class="wrapper">
      <div class="left">
        <aside><img src="orh82o67aDQra74Tlp4-o.jpg"></aside>
        <section>
          <h1 class="movieTitle">A Bug's life</h1>
          <h2 class="genre">Animation, Adventure, Comedy</h2>
          <button class="btn"><img class="arrow" src="Layer%207%20copy.png">more</button>
        </section>
        <div class="below">
          <div class="rating">
            <img class="star" src="star.png">
            <h1 class="rate">8.1</h1>
          </div>
          <div class="about">
            <p>A misfit ant, looking for "warriors" to save his colony from greedy grasshoppers, recruits a group of bugs that turn out to be an inept circus troupe.</p>
          </div>
        </div>
      </div>

      <div class="right">
        <aside><img class="height" src="MV5BNTM5OTg2NDY1NF5BMl5BanBnXkFtZTcwNTQ4MTMwNw@@._V1_UX182_CR0,0,182,268_AL_.jpg"></aside>
        <section>
          <h1 class="movieTitle">All Quiet on
the Western Front</h1>
          <h2 class="genre">Drama, War</h2>
          <button class="btn"><img class="arrow" src="Layer%207%20copy.png">more</button>
        </section>
        <div class="below">
          <div class="rating">
            <img class="star" src="star.png">
            <h1 class="rate">8.1</h1>
          </div>
          <div class="about">
            <p>A misfit ant, looking for "warriors" to save his colony from greedy grasshoppers, recruits a group of bugs that turn out to be an inept circus troupe.</p>
          </div>
        </div>
      </div>
    </div>
  </div>

答案 1 :(得分:0)

display: flex添加到包装器中。

在新的包装器中包含左上角内容(在本例中为.left-top),以将其与below分开。

分隔leftright并将flex-direction: column添加到堆栈left-topbelow

*避免混合使用flexfloat

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  background: #282828;
  font-family: 'Open Sans', sans-serif;
}

p {
  font-size: 12px;
  text-align: justify;
  color: #aec7d5;
}

#container {
  min-width: 320px;
  max-width: 782px;
  height: auto;
  background: #333333;
  padding: 5px;
  height: 300px;
  display: block;
  margin: 0 auto;
  margin-top: 10%;
}

.wrapper {
  padding: 15px 10px;
  display: flex;
  width: 100%;
}

.left {
  padding-right: 10px;
  border-right: 1px solid #797977;
  display: flex;
}

.below {
  display: flex;
  padding-right: 10px;
}

.rating {
  display: flex;
  float: left;
  margin-right: 10px;
  border-right: 1px dotted #c3c3c2;
}

.about {
  display: inline-block;
  float: left;
}

.rate {
  font-size: 20px;
  display: inline-block;
}

.star {
  display: inline-block;
  height: 30px;
  width: 30px;
}

.right {
  padding-left: 20px;
  display: flex;
}

aside {
  width: 40%;
  height: 95px;
  overflow: hidden;
  border: 1.5px solid #bbbbbb;
  display: inline-block;
  float: left;
  margin-right: 15px;
}

section {
  display: inline-block;
  float: left;
  width: 60%;
}

aside img {
  height: 100%;
  width: auto;
  position: relative;
  left: -20px;
}

.height {
  height: auto;
  top: -50px;
}

h1 {
  font-family: arial;
  font-size: bold;
  color: white;
  font-size: 18px;
}

.movieTitle {
  margin: 0;
  text-transform: capitalize;
  min-height: 45px;
}

.genre {
  text-transform: uppercase;
  color: #aec7d5;
  font-size: 10px;
  font-weight: 300;
  margin: 0;
  margin-bottom: 10px;
}

.btn {
  background: #868684;
  padding: 6px 20px 6px 10px;
  border-radius: 5px;
  border: none;
  color: #c3c3c2;
  cursor: pointer;
  transition: all 0.15s;
}

.btn:hover {
  background: #767676;
}

.btn .arrow {
  margin-right: 5px;
}

.left,
.right {
  flex-direction: column;
}
<div id="container">

  <div class="wrapper">
    <div class="left">
      <div class="left-top">
        <aside><img src="orh82o67aDQra74Tlp4-o.jpg"></aside>
        <section>
          <h1 class="movieTitle">A Bug's life</h1>
          <h2 class="genre">Animation, Adventure, Comedy</h2>
          <button class="btn"><img class="arrow" src="Layer%207%20copy.png">more</button>
        </section>
      </div>

      <div class="below">
        <div class="rating">
          <img class="star" src="star.png">
          <h1 class="rate">8.1</h1>
        </div>
        <div class="about">
          <p>A misfit ant, looking for "warriors" to save his colony from greedy grasshoppers, recruits a group of bugs that turn out to be an inept circus troupe.</p>
        </div>
      </div>
    </div>
    <div class="right">
      <div class="right-top">
        <aside><img class="height" src="MV5BNTM5OTg2NDY1NF5BMl5BanBnXkFtZTcwNTQ4MTMwNw@@._V1_UX182_CR0,0,182,268_AL_.jpg"></aside>
        <section>
          <h1 class="movieTitle">All Quiet on the Western Front</h1>
          <h2 class="genre">Drama, War</h2>
          <button class="btn"><img class="arrow" src="Layer%207%20copy.png">more</button>
        </section>
      </div>

      <div class="below">
        <div class="rating">
          <img class="star" src="star.png">
          <h1 class="rate">8.1</h1>
        </div>
        <div class="about">
          <p>A misfit ant, looking for "warriors" to save his colony from greedy grasshoppers, recruits a group of bugs that turn out to be an inept circus troupe.</p>
        </div>
      </div>
    </div>
  </div>
</div>