文本正在调整flexbox子元素的大小,而不是自动换行(使用flex-grow)

时间:2019-02-07 18:04:05

标签: html css flexbox

我正在使用flex-grow和media查询来尝试创建一个简单且响应迅速的网站。但是,文本似乎没有换行,而是调整了其所在的div的大小,并抵消了其他所有内容。有人知道为什么会这样吗?

我认为这种行为与我正在使用flex-grow的事实有关。我尝试设置最小/最大宽度,还尝试了各种不同的自动换行属性,但没有运气。

这是我的代码:

jsFiddle

<div id="container">
  <div id="title">
    <h1>Title</h1>
  </div>
  <div class="work" id="motion">
    <h2>Sub</h2>
  </div>
  <div class="work" id="print">
    <h2>Sub</h2>
  </div>
  <div class="work" id="interaction">
    <h2>Sub</h2>
  </div>
</div>
* {
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
  width: 100%;
}

h1,
h2,
h3,
h4,
h5,
h6,
p {
  font-family: sans-serif;
  font-weight: normal;
  max-width: 100%;
}

#container h1,
h2 {
  padding: 1rem;
}

#container {
  height: 100%;
  display: flex;
  flex-direction: row;
}

#title {
  background-color: red;
  flex-grow: 2;
  margin: 1em;
  min-height: 15em;
}

.work {
  margin: 1em;
  flex-grow: 1;
  background-color: gray;
  min-width: 4em;
}

#motion,
#print,
#title {
  margin-right: 0;
  margin-bottom: 1em;
}

#interaction {}

@media only screen and (max-width: 1020px) {
  body {
    background-color: blue;
  }
  #container {
    flex-direction: column;
    flex-wrap: wrap;
    height: 100%;
  }
  #motion,
  #print {
    margin-right: 1em;
    margin-bottom: 0;
  }
  .work {
    min-height: 10em;
  }
  #title {
    height: 100%;
  }
}

@media only screen and (max-width: 600px) {
  body {
    background-color: yellow;
  }
  #motion,
  #print,
  #title {
    margin-right: 1em;
    margin-bottom: 0;
  }
  #container {
    flex-wrap: nowrap;
  }
}

1 个答案:

答案 0 :(得分:1)

flexbox子级没有设置任何宽度,因此,当您具有更多文本内容时,这些div将会增加。

jsFiddle

您可以添加以下内容:

# Class Hierarchy:
# AA inherits from A inherits from letter (= base class)

# Constructors ---------------------------------------------
letter <- function()
{
  x <- numeric(0)  # this shall be an class attribute
  class(x) <- append("letter", class(x))
  return(x)
}

a <- function()   # class "a" inherits from class "letter"
{
  obj <- letter()
  class(obj) <- append("a", class(obj))  # attach the specialized class first!
  return(obj)
}


aa <- function()    # class "aa" inherits from class "a"
{
  obj <- a()
  class(obj) <- append("aa", class(obj))
  return(obj)
}

# Class methods -------------------------------------------

# This is a function in the base class "letter" that is inherited in the sub classes
# so that every sub class can provide its own implementation (polymorphism).
# To register such a generic function (= function call dispatching to class-specific functions/methods)
# "UseMethod" is called
met <- function(obj, x)  # met = method?!
{
  UseMethod("met", obj)    # "dispatch": Call the first existing function of pattern "met.<class>"
  # Code after this line will never be executed due to calling "UseMethod"
  print("met")
}

met.aa <- function(obj, x)
{
  print("met.aa - starting")
  x = x + 1
  NextMethod("met", obj)  # as last code in the function: Returns the return value of this function!
  # Do not add code after "NextMethod" or you will get the output of this code as return value
  # instead of the return value of NextMethod!
  # print("met.aa - leaving")
}

met.a <- function(obj, x)
{
  print("met.a - starting")
  x <- c(4, x, 1)
  res <- NextMethod("met", obj) # , c(4, x, 1))
  print("met.a - leaving")  #
  return(res)
}

met.letter<- function(obj, x)   # x may be a vector!
{
  print("met.letter starting")
  # "append" looses the attributes (class!) so we reassign it
  # a() should better return a list with one vector element as "class attribute"
  # so that the attributes keep untouched if changing the "class attribute"
  old.classes <- class(obj)
  obj <- append(obj, x)
  class(obj) <- old.classes
  # no NextMethod() call - it is the base class (= root!)
  return(obj)
}

met.default <- function(obj, x) {
  warning("met.default: not implemented")
}

aaobj <- aa()
aaobj
# numeric(0)
# attr(,"class")
# [1] "aa"      "a"       "letter"  "numeric"

aaobj <- met(aaobj, 6)
aaobj
# [1] 4 7 1

    #container > div {
      width: calc(25% - 2em); /* Minus 2 em for the 1em margin on left and right */
    }

@media only screen and (max-width: 1020px) {
     #container>div {
        width: calc(33% - 2em);
      }
}

@media only screen and (max-width: 600px) {
     #container>div {
        width: calc(100% - 2em);
      }
}
* {
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
  width: 100%;
}

h1,
h2,
h3,
h4,
h5,
h6,
p {
  font-family: sans-serif;
  font-weight: normal;
  max-width: 100%;
}

#container h1,
h2 {
  padding: 1rem;
}

#container {
  height: 100%;
  display: flex;
  flex-direction: row;
}

#container>div {
  width: calc(25% - 2em);
}

#title {
  background-color: red;
  flex-grow: 2;
  margin: 1em;
  min-height: 15em;
}

.work {
  margin: 1em;
  flex-grow: 1;
  background-color: gray;
  min-width: 4em;
}

#motion,
#print,
#title {
  margin-right: 0;
  margin-bottom: 1em;
}

#interaction {}

@media only screen and (max-width: 1020px) {
  body {
    background-color: blue;
  }
  #container {
    flex-direction: column;
    flex-wrap: wrap;
    height: 100%;
  }
  #container>div {
    width: calc(33% - 2em);
  }
  #motion,
  #print {
    margin-right: 1em;
    margin-bottom: 0;
  }
  .work {
    min-height: 10em;
  }
  #title {
    height: 100%;
  }
}

@media only screen and (max-width: 600px) {
  body {
    background-color: yellow;
  }
  #motion,
  #print,
  #title {
    margin-right: 1em;
    margin-bottom: 0;
  }
  #container {
    flex-wrap: nowrap;
  }
  #container>div {
    width: calc(100% - 2em);
  }
}