【问题标题】:Positioning error with flexboxflexbox 定位错误
【发布时间】:2018-02-08 00:39:11
【问题描述】:

所以我正在做这个项目,我开始遇到显示属性和元素没有按我希望的那样定位的问题。这是我目前所拥有的: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>

在我添加 .below 类及其所有内容之前一切都很好,我需要的是 .left 位于左侧,.right 位于右侧..

我尝试了各种显示属性,但似乎都不起作用。

【问题讨论】:

  • 请注意,floatflex 的显示没有影响
  • 不,我试过了,float:right 可以工作
  • 我认为@PierreLeBot 说 float 对 flex items (flex 容器的子项)没有影响,例如参见 this answer。它仍然对 flex 容器本身有影响
  • 没错@FelipeAls,感谢您的澄清

标签: html css flexbox


【解决方案1】:

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>

【讨论】:

    【解决方案2】:

    请注意,您的标记和 CSS 可以清理很多,尽管我选择不为您这样做。

    如果将.below 元素移动到每个.left/.right 元素中,请将flex-wrap: wrap 添加到.left/.right 规则中,并使用calc() 作为aside 的宽度(所以它考虑了边框/边距),您将在了解 Flexbox 的工作原理和重构您的标记方面有一个良好的开端。

    Updated codepen

    堆栈sn-p

    * {
        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>

    【讨论】:

    • 非常感谢。我会明确地修复项目的结构和 css,但现在我只需要它作为草图或类似的东西。
    • @BenasLengvinas 不客气……这篇文章在开始学习 Flexbox 时非常有用:css-tricks.com/snippets/css/a-guide-to-flexbox
    猜你喜欢
    • 2019-05-18
    • 2017-07-25
    • 2017-03-09
    • 2017-08-20
    • 2021-04-18
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    相关资源
    最近更新 更多