Flexbox-行和列并排

时间:2018-07-11 14:35:14

标签: html css css3 flexbox

我要实现的目标是:

mockup

但是,我在执行最后一个块/矩形(红色)时遇到了麻烦。 因为它不属于我为其他块创建的行。 我试图向右浮动但没有成功。

如何实现最后一个红色块/矩形?

我正在使用Flexbox作为容器。

当前代码:

.container {
  display: flex;
  flex-wrap: wrap;
}

#wrapper {
  margin-right: auto;
  margin-left: auto;
  width: 1520px;
}

.row {
  flex-direction: row;
}

.column {
  flex-direction: column;
}

#dateAndTime {
  width: 450px;
  height: 270px;
  background-color: #0dab76;
  margin-right: 40px;
}

.bottom {
  margin-top: 40px;
}

#forecast {
  width: 450px;
  height: 270px;
  background-color: #143642;
}

#news {
  background-color: #a8201a;
  width: 230px;
  height: 100px;
  margin-left: 40px;
}

#title {
  background-color: #ec9a29;
  width: 650px;
  height: 270px;
  margin-left: 40px;
}

#none {
  background-color: #0f8b8d;
  width: 250px;
  height: 270px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>Dashboard</title>
</head>

<body>
  <div id="wrapper">
    <div class="container row">
      <div id="dateAndTime"></div>
      <div id="forecast"></div>
      <div></div>
    </div>
    <div class="container row bottom">
      <div id="none"></div>
      <div id="title"></div>
    </div>
  </div>
</body>

</html>

2 个答案:

答案 0 :(得分:1)

通常,您需要两列...结构很重要。

根据需要进行调整。

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

::before,
::after {
  box-sizing: inherit;
}

.container {
  display: flex;
  flex-wrap: wrap;
}

#wrapper {
  width: 416px;
  display: flex;
}

.left {
  flex: 2;
  display: flex;
  justify-content: space-between;
  margin-right: 4px;
}

.right {
  flex: 1;
  background: red;
}

#dateAndTime {
  width: 135px;
  height: 81px;
  background-color: #0dab76;
  margin-bottom: 4px;
}

#forecast {
  width: 135px;
  height: 81px;
  background-color: #143642;
}

#news {
  background-color: #a8201a;
  width: 69px;
  height: 30px;
}

#title {
  background-color: #ec9a29;
  width: 195px;
  height: 81px;
}

#none {
  background-color: #0f8b8d;
  width: 75px;
  height: 81px;
}
<div id="wrapper">
  <div class="container left">
    <div id="dateAndTime"></div>
    <div id="forecast"></div>
    <div id="none"></div>
    <div id="title"></div>
  </div>
  <div class="right"></div>
</div>

答案 1 :(得分:0)

您可以再添加一个包装器,并创建两列,如下所示:

#main-wrapper {
  display: flex;
}

#red {
  width: 200px;
  height: calc(270px * 2 + 40px);
  background-color: red;
}

/* http://meyerweb.com/eric/tools/css/reset/
   v2.0 | 20110126
   License: none (public domain)
*/

html,
body,
div,
span,
applet,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
a,
abbr,
acronym,
address,
big,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
s,
samp,
small,
strike,
strong,
sub,
sup,
tt,
var,
b,
u,
i,
center,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
canvas,
details,
embed,
figure,
figcaption,
footer,
header,
hgroup,
menu,
nav,
output,
ruby,
section,
summary,
time,
mark,
audio,
video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}


/* HTML5 display-role reset for older browsers */

article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
  display: block;
}

body {
  line-height: 1;
}

ol,
ul {
  list-style: none;
}

blockquote,
q {
  quotes: none;
}

blockquote:before,
blockquote:after,
q:before,
q:after {
  content: '';
  content: none;
}

table {
  border-collapse: collapse;
  border-spacing: 0;
}

body {
  padding-top: 130px;
}

.container {
  display: flex;
  flex-wrap: wrap;
}

#wrapper {
  margin-right: auto;
  margin-left: auto;
  width: 940px;
}

.row {
  flex-direction: row;
}

.column {
  flex-direction: column;
}

#dateAndTime {
  width: 450px;
  height: 270px;
  background-color: #0dab76;
  margin-right: 40px;
}

.bottom {
  margin-top: 40px;
}

#forecast {
  width: 450px;
  height: 270px;
  background-color: #143642;
}

#news {
  background-color: #a8201a;
  width: 230px;
  height: 100px;
  margin-left: 40px;
}

#title {
  background-color: #ec9a29;
  width: 650px;
  height: 270px;
  margin-left: 40px;
}

#none {
  background-color: #0f8b8d;
  width: 250px;
  height: 270px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>Dashboard</title>
</head>

<body>
  <div id="main-wrapper">
    <div id="wrapper">
      <div class="container row">
        <div id="dateAndTime"></div>
        <div id="forecast"></div>
        <div></div>
      </div>
      <div class="container row bottom">
        <div id="none"></div>
        <div id="title"></div>
      </div>
    </div>
    <div id="red"></div>
  </div>
</body>

</html>

但是在这种情况下使用CSS-Grid确实更好。您可以在css-tricks.com

上找到有关CSS-Grid的更多信息