css - 带有 svg 的响应式 flex 列

标签 css svg flexbox

我正在尝试在我的页面右侧设置一个面板。它将有一些内联元素和中间的 svg 图像。

我希望面板最大为宽度的 50% 和高度的 100%。 SVG 图像应该在保持宽高比的同时增长以填充可用高度。因此容器会变宽。当高度被填满或容器宽度达到 50% 时,它应该停止增长。

这是我想出的:

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

body {
  background: #333;
}

#viewport {
  background: #FFF;
  transition: all 200ms;
  padding: 5px;
  position: relative;
  animation: sizing 8s infinite;
}

.column {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  padding: 10px 15px;
  border: 1px dotted #000;
  max-height: 100%;
  max-width: 50%;
}

.svgContainer {
  flex: 1;
}

.svgContainer svg {
  max-width: 100%;
  max-height: 100%;
}

@keyframes sizing {
  0% {
    width: 300px;
    height: 200px;
  }
  25% {
    width: 500px;
    height: 200px;
  }
  50% {
    width: 500px;
    height: 400px;
  }
  75% {
    width: 300px;
    height: 400px;
  }
  100% {
    width: 300px;
    height: 200px;
  }
}
<div id="viewport">
  <div class="column">
    <h4>Some header</h4>
    <div class="svgContainer">
      <svg viewBox="0 0 300 214" width="300" height="214">
        <rect x="0" y="0"
          width="300" height="214" 
          stroke-width="5"
          stroke="#F00"
          rx="15" ry="15"
          fill="none"/>
         <circle cx="150" cy="107" r="80" stroke="#F00" stroke-width="5" fill="none"/>
    </svg>
    </div>
    <button>some button</button>
  </div>
</div>

我在视口(viewport)大小上添加了一个动画来说明几个问题:

  1. 当视口(viewport)变窄时,面板内容会溢出。我希望 svg 缩小。
  2. 当面板太高时,svg 和按钮之间有空间,我想将这个空间移到按钮下方。

我是用 f​​lexbox (flex-direction: column + flex:1) 做的,但我好像漏掉了什么

最佳答案

当面板太高时,svg 和按钮之间有空间,我想在底部移动这个空间。:从 svg 元素中删除高度。

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

body {
  background: #333;
}

#viewport {
  background: #FFF;
  transition: all 200ms;
  padding: 5px;
  position: relative;
  animation: sizing 8s infinite;
}

.column {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  padding: 10px 15px;
  border: 1px dotted #000;
  max-height: 100%;
  max-width: 50%;
}

.svgContainer {
  flex: 1;
}

.svgContainer svg {
  max-width: 100%;
  max-height: 100%;
}


/* DEBUG */

#stopButton {
  position: fixed;
  right: 0;
  bottom: 0;
  font-size: 2em;
}

@keyframes sizing {
  0% {
    width: 300px;
    height: 200px;
  }
  25% {
    width: 500px;
    height: 200px;
  }
  50% {
    width: 500px;
    height: 400px;
  }
  75% {
    width: 300px;
    height: 400px;
  }
  100% {
    width: 300px;
    height: 200px;
  }
}
<div id="viewport">
  <div class="column">
    <h4>Some header</h4>
    <div class="svgContainer">
      <svg viewBox="0 0 300 214" width="300">
        <rect x="0" y="0"
          width="300" height="214" 
          stroke-width="5"
          stroke="#F00"
          rx="15" ry="15"
          fill="none"/>
         <circle cx="150" cy="107" r="80" stroke="#F00" stroke-width="5" fill="none"/>
    </svg>
    </div>
    <button style="flex-shrink: 0;">some button</button>
  </div>
</div>

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

body {
  background: #333;
}

#viewport {
  background: #FFF;
  transition: all 200ms;
  padding: 5px;
  position: relative;
  overflow: auto;
  animation: sizing 8s infinite;
}

.column {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  padding: 10px 15px;
  border: 1px dotted #000;
  max-height: 100%;
  max-width: 50%;
  text-align: center;
}

.svgContainer {
  flex: 1;
}

.svgContainer svg {
  max-width: 100%;
  max-height: 100%;
}


/* DEBUG */

#stopButton {
  position: fixed;
  right: 0;
  bottom: 0;
  font-size: 2em;
}

@keyframes sizing {
  0% {
    width: 300px;
    height: 200px;
  }
  25% {
    width: 500px;
    height: 200px;
  }
  50% {
    width: 500px;
    height: 400px;
  }
  75% {
    width: 300px;
    height: 400px;
  }
  100% {
    width: 300px;
    height: 200px;
  }
}
<div id="viewport">
  <div class="column">
    <h4>Some header</h4>
    <div class="svgContainer">
      <svg viewBox="0 0 300 214" width="300">
        <rect x="0" y="0"
          width="300" height="214" 
          stroke-width="5"
          stroke="#F00"
          rx="15" ry="15"
          fill="none"/>
         <circle cx="150" cy="107" r="80" stroke="#F00" stroke-width="5" fill="none"/>
    </svg>
    </div>
    <button>some button</button>
  </div>
</div>

关于css - 带有 svg 的响应式 flex 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51286502/

相关文章:

javascript - 获取标题文本的值(HTML,CSS)

javascript - 有没有更好的方法来动态更改 css 属性

css - 我可以在一个文件中包含多个 SVG 图像吗?

html - 在列中显示网格元素

html - FlexBox 对齐元素

HTML选择标签更改箭头使用雪碧

html - CSS Filter Multiple URL - 同时获得模糊和灰度

javascript - Angular2 中的 SVG 循环

css - 如何将路径添加到剪辑路径

html - 如何使顶部溢出以使反向滚动工作?