使用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>

在我添加 .below class的所有内容之前,一切都还好,我需要的是 .left在左侧,而 .right在右边。

我尝试了各种显示属性,它们似乎都没有用。

请注意,您的标记和CSS可以经常清理,尽管我选择不为您做。

如果将.below元素的元素移至每个.left/.right元素中,请将flex-wrap: wrap添加到.left/.right规则中,然后将calc()用于aside的宽度(因此,它将边框/边框计入),您将获得一个两者都可以看到Flexbox的工作原理并重组您的标记。

更新的codepen

堆栈片段

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

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>

最新更新