Flex容器宽度适合内容宽度与flex-wrap:wrap?

时间:2017-06-29 12:11:26

标签: css css3 flexbox

以下是一个例子:



.wrap {
  display: flex;
}

.wrap2 {
  padding: 4px;
  margin: 20px;
  outline: 1px dashed;
  display: flex;
  flex-wrap: wrap;
  max-width: 310px;
}

.box {
  width: 40px;
  height: 40px;
  outline: 1px solid;
}

<div class="wrap">
  <div class="wrap2">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
  </div>
</div>
&#13;
&#13;
&#13;

这就是我需要的:

Img

我尝试用另一个flex包装flex容器,但它没有用。是否有任何CSS解决方案,或者我应该使用js来达到预期的结果?

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
 struct JSONStringArrayEncoding: ParameterEncoding {
    private let array: [[String : Any]]

    init(array: [[String : Any]]) {
        self.array = array
    }

    func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
        var urlRequest = try urlRequest.asURLRequest()

        let data = try JSONSerialization.data(withJSONObject: array, options: [])

        if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
            urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
        }

        urlRequest.httpBody = data

        return urlRequest
    }
}
&#13;
&#13;
&#13;