如何使用CSS实现这种布局?

时间:2019-01-23 03:42:15

标签: html css flexbox

我有一些Javascript从各种电影的数据库中获取信息,但是页面的布局令人恐惧,我想要实现类似于以下屏幕快照中的布局:

desired layout

但这是现在的样子:

current

我将如何实现与顶部图像类似的布局?

这是我的代码(经过调整,因此您可以看到内容,尽管通常没有数据库的div实际上是空的)

@import url(https://fonts.googleapis.com/css?family=Raleway);

body {
  margin: 0;
  font-family: 'Raleway', georgia, arial;
  background-color: #e0e0e0;
  text-align: center;
}

h1 {
  color: #aaaaaa;
  text-align: left;
}

.sortFilms {
  text-align: left;
  display: inline-block;
  background-color: #ff6699;
  width: 80%;
  padding: 20px;
}

header {
  text-align: center;
  display: inline-block;
  border-bottom: 5px;
  border-top: 0;
  border-left: 0;
  border-right: 0;
  border-style: solid;
  border-color: #aaaaaa;
  width: 80%;
  padding: 20px;
  background-color: #e0e0e0;
}

.newFilm {
  text-align: left;
  display: inline-block;
  background-color: #ff6699;
  width: 80%;
  padding: 20px;
}

label {
  font-size: 1em;
  padding: 6px;
  color: #fff;
  font-weight: 700;
  display: block;
  text-align: left;
}

.form {
  margin: auto;
  display: flex;
  text-align: center;
  flex-direction: column;
}

h2 {
  font-weight: 700;
  font-size: 2em;
  width: 50%;
  color: #B2365F;
}

#formTitle {
  margin-top: 0;
  margin-bottom: 0;
}

.row {
  margin-left: -20px;
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.col {
  padding: 20px;
}

input,
textarea, select {
  width: 100%;
  display: block;
  border-radius: 25px;
  background-color: #e0e0e0;
  padding: 10px;
  border: none;
  box-sizing:border-box; }
}


.tagline {
  margin: 0;
  color: #333333;
  font-size: 1em;
  font-weight: 700;
}

input::placeholder {
  color: #000;
}

textarea::placeholder {
  color: #000;
}

#modifyFilmButton {
  float: right;
}

@media only screen and (max-width: 700px) {
  .row  {
    grid-template-columns: 1fr;
  }
}

@media screen and (max-width:800px) {
  table {
    border: 0;
  }

  table caption {
    font-size: 1.3em;
  }

  table thead {
    border: none;
    clip: rect(0 0 0 0);
    height: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    width: 1px;
  }

  table tr {
    border: 2px solid #e0e0e0;
    background-color: #e0e0e0;
    display: block;
    margin-bottom: .625em;
    border-radius: 20px;
  }

  table td {
    display: block;
    font-weight: bold;
    font-size: 1.2em;
    text-align: left;
    padding: 15px;
  }

  table td::before {
    /*
    * aria-label has no advantage, it won't be read inside a table
    content: attr(aria-label);
    */
    content: attr(data-label);
    float: left;
    font-weight: bold;
    text-transform: uppercase;
  }

  table td:last-child {
    border-bottom: 0;
  }
}

.oldFilm {
    border-bottom-left-radius: 20px;
    border-bottom-right-radius: 20px;
    text-align: left;
    display: inline-block;
    background-color: #AAAAAA;
    width: 80%;
    padding: 20px;
}

#oldTitle {
  margin-top: 0;
  margin-bottom: 0;
  color: #ff6699;
  padding-bottom: 20px;
}

td {
  padding: 5px;
  font-weight: bold;
}

table {
  border-collapse: collapse;
  text-align: center;
  width: 100%;
}
thead {
  background: #ff6699;
}

.reviewImage {
  width: 200px;
  border-radius: 20px;
}
.filmRow img {
  width: 300px;
  height: 420px;
  margin: 10px;
  border-radius: 20px;
}

.filmRow {
 -webkit-flex-flow: row wrap;
  justify-content: space-around;
}

#filmContainer {
  width: 100%;
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-around;
}

#date {
  padding: 5px;
  text-align: left;
  width: 30%;
}

#date input {
  width: auto;
}

#date label {
  display: -webkit-inline-box;
}

#oldTitle2 {
  margin-top: 0;
  margin-bottom: 0;
  color: #ff6699;
}

.genre {
  padding: 5px;
  text-align: left;
  width: 60%;
}

.genre input {
  width:auto;
}

.genre label {
  display: -webkit-inline-box;
}

/* third page */

#filmImage {
  width: 300px;
  height: 420px;
  margin: 10px;
  border-radius: 20px;
}

#filmTitle {
    font-weight: 700;
    margin: 0;
    font-size: 3em;
    width: 100%;
    text-align: center;
    color: #B2365F;

}
    <header>
      <img src="img/rv-logo.png">
      <p class="tagline">Want to know whether or not it's worth paying to watch a certain film or not? See what we think first!</p>
    </header>


  <div class="sortFilms">
<div id="filmContainer">
    <img id='filmImage'src="https://images-na.ssl-images-amazon.com/images/I/51Gh9OaWVcL.jpg">
    <p id='filmTitle'>Deadpool</p>
    <label>Year:</label><p id='filmYear'>2016</p>
    <label>Genre:</label><p id='filmGenre'>Comedy</p>
    <label>Rating:</label><p id='filmRating'>9</p>
    <label>Synopsis:</label><p id='filmSynopsis'>Just Ryan Reynolds, doing Ryan Reynolds things in a tight suit.</p>
    <a href='viewFilms.html'>Back to list</a>
</div>
</div>

(忽略理想布局中文本的字体,我只使用了在Photoshop中使用的最后一个字体)

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

尝试在不同的div中分离图像和文本。 制作两列,然后使用CSS相应地调整宽度。

<div id="filmContainer">
    <div id="leftcolumn">
       <img id='filmImage'src="https://images-na.ssl-images-amazon.com/images/I/51Gh9OaWVcL.jpg">
    </div>

    <div id="rightcolumn">
            <p id='filmTitle'>Deadpool</p>
            <label>Year:</label><p id='filmYear'>2016</p>
            <label>Genre:</label><p id='filmGenre'>Comedy</p>
            <label>Rating:</label><p id='filmRating'>9</p>
            <label>Synopsis:</label><p id='filmSynopsis'>Just Ryan Reynolds, doing Ryan Reynolds things in a tight suit.</p>
            <a href='viewFilms.html'>Back to list</a>

    </div>
</div>

答案 1 :(得分:0)

首先,您应该将大多数(不是全部)ID转换为CLASS标签。您无需将它们全部设为ID,然后限制您在一页上放置多张电影卡。

我在这里为您创建了一个JSfiddle,对html进行了一些更新,大部分是CSS的更新。

HTML:

<header>
      <img src="img/rv-logo.png">
      <p class="tagline">Want to know whether or not it's worth paying to watch a certain film or not? See what we think first!</p>
    </header>


  <div class="sortFilms">
<div id="filmContainer">
  <div class="poster">
    <img id='filmImage'src="https://images-na.ssl-images-amazon.com/images/I/51Gh9OaWVcL.jpg">
  </div>
  <div class="info">
  <p id='filmTitle'>Deadpool</p>
    <label>Year:</label><p id='filmYear'>2016</p>
    <label>Genre:</label><p id='filmGenre'>Comedy</p>
    <label>Rating:</label><p id='filmRating'>9</p>
    <label>Synopsis:</label><p id='filmSynopsis'>Just Ryan Reynolds, doing Ryan Reynolds things in a tight suit.</p>
    <a href='viewFilms.html'>Back to list</a>
  </div>
</div>
</div>

CSS(所有原始CSS也在其中,可以清理):

@import url(https://fonts.googleapis.com/css?family=Raleway);

body {
  margin: 0;
  font-family: 'Raleway', georgia, arial;
  background-color: #e0e0e0;
  text-align: center;
}

h1 {
  color: #aaaaaa;
  text-align: left;
}

.sortFilms {
  text-align: left;
  display: inline-block;
  background-color: #ff6699;
  width: 80%;
  padding: 20px;
}

header {
  text-align: center;
  display: inline-block;
  border-bottom: 5px;
  border-top: 0;
  border-left: 0;
  border-right: 0;
  border-style: solid;
  border-color: #aaaaaa;
  width: 80%;
  padding: 20px;
  background-color: #e0e0e0;
}

.newFilm {
  text-align: left;
  display: inline-block;
  background-color: #ff6699;
  width: 80%;
  padding: 20px;
}

label {
  font-size: 1em;
  padding: 6px;
  color: #fff;
  font-weight: 700;
  display: block;
  text-align: left;
}

.form {
  margin: auto;
  display: flex;
  text-align: center;
  flex-direction: column;
}

h2 {
  font-weight: 700;
  font-size: 2em;
  width: 50%;
  color: #B2365F;
}

#formTitle {
  margin-top: 0;
  margin-bottom: 0;
}

.row {
  margin-left: -20px;
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.col {
  padding: 20px;
}

input,
textarea, select {
  width: 100%;
  display: block;
  border-radius: 25px;
  background-color: #e0e0e0;
  padding: 10px;
  border: none;
  box-sizing:border-box; }
}


.tagline {
  margin: 0;
  color: #333333;
  font-size: 1em;
  font-weight: 700;
}

input::placeholder {
  color: #000;
}

textarea::placeholder {
  color: #000;
}

#modifyFilmButton {
  float: right;
}

@media only screen and (max-width: 700px) {
  .row  {
    grid-template-columns: 1fr;
  }
}

@media screen and (max-width:800px) {
  table {
    border: 0;
  }

  table caption {
    font-size: 1.3em;
  }

  table thead {
    border: none;
    clip: rect(0 0 0 0);
    height: 1px;
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    width: 1px;
  }

  table tr {
    border: 2px solid #e0e0e0;
    background-color: #e0e0e0;
    display: block;
    margin-bottom: .625em;
    border-radius: 20px;
  }

  table td {
    display: block;
    font-weight: bold;
    font-size: 1.2em;
    text-align: left;
    padding: 15px;
  }

  table td::before {
    /*
    * aria-label has no advantage, it won't be read inside a table
    content: attr(aria-label);
    */
    content: attr(data-label);
    float: left;
    font-weight: bold;
    text-transform: uppercase;
  }

  table td:last-child {
    border-bottom: 0;
  }
}

.oldFilm {
    border-bottom-left-radius: 20px;
    border-bottom-right-radius: 20px;
    text-align: left;
    display: inline-block;
    background-color: #AAAAAA;
    width: 80%;
    padding: 20px;
}

#oldTitle {
  width:100%;
  margin-top: 0;
  margin-bottom: 0;
  color: #ff6699;
  padding-bottom: 20px;
}

td {
  padding: 5px;
  font-weight: bold;
}

table {
  border-collapse: collapse;
  text-align: center;
  width: 100%;
}
thead {
  background: #ff6699;
}

.reviewImage {
  width: 200px;
  border-radius: 20px;
}
.filmRow img {
  width: 300px;
  height: 420px;
  margin: 10px;
  border-radius: 20px;
}

.filmRow {
 -webkit-flex-flow: row wrap;
  justify-content: space-around;
}

#filmContainer {
  width: 100%;
  margin-top: 10px;
  display: block;
  position: relative;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-around;
}
#filmContainer .poster {
  width:45%;
  float:left;
}
#filmContainer .info {
  width:50%;
  float: right;
}
#filmContainer .info label {
  float:left;
  width: 25%;
  padding:0;
  margin: 5px 0;
}
#filmContainer .info p {
  float:left;
  width: 75%;
  margin: 5px 0;
}
#filmContainer .info a {
  display:block;
  padding: 25px 0;
  clear:both;
}
#filmContainer .info p#filmTitle {
  width:100%;
  clear:both;
  margin-top: 0;
  margin-bottom: 0;
  padding-bottom: 20px;
  float: unset;
  text-align:left;
}

#date {
  padding: 5px;
  text-align: left;
  width: 30%;
}

#date input {
  width: auto;
}

#date label {
  display: -webkit-inline-box;
}

#oldTitle2 {
  margin-top: 0;
  margin-bottom: 0;
  color: #ff6699;
}

.genre {
  padding: 5px;
  text-align: left;
  width: 60%;
}

.genre input {
  width:auto;
}

.genre label {
  display: -webkit-inline-box;
}

/* third page */

#filmImage {
  width: 95%;
  height: auto;
  margin: 10px;
  border-radius: 20px;
}

#filmTitle {
    font-weight: 700;
    margin: 0;
    font-size: 3em;
    width: 100%;
    text-align: center;
    color: #B2365F;

}

http://jsfiddle.net/sstracy/vaq60zyp/