如何保持SVG线与未指定高度的父div高度相同?

时间:2019-02-05 11:22:27

标签: html css css3 svg flexbox

因此,我试图在两列之间放置SVG线,但该线未占用父容器的高度。父容器没有指定高度,因为我希望它自动缩放到最高列。

示例: https://jsfiddle.net/Lye0z5wx/4/

代码

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

div.container {
  margin: 40px auto;
  width: 70%;
  display: flex;
  align-items: center;
}

.column {
  display: inline-block;
  width: 40%;
}

svg {
  display: block;
  height: 100%;
  width: 20%;
}
<body>
  <div class="container">
    <div class="column">
      <h3>Content</h3>
      <p>Easily change/switch/swap every placeholder inside every image on Sedna with the included Sketch files. You’ll have this template customised to suit your business in no time!</p>
      <p>Nam vehicula malesuada lectus, interdum fringilla nibh. Duis aliquam vitae metus a pharetra. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec fermentum augue quis augue ornare, eget faucibus felis pharetra. Proin condimentum et leo
        quis fringilla.</p>
    </div>
    <svg viewbox="0 0 100 100" preserveAspectRatio="none">
      <line x1="50%" x2="50%" y1="0" y2="100%" style="stroke:rgb(235,235,235);stroke-width:1" />
      Sorry, your browser does not support inline SVG.
    </svg>
    <div class="column">
      <h3>Content</h3>
      <p>Easily change/switch/swap every placeholder inside every image on Sedna with the included Sketch files. You’ll have this template customised to suit your business in no time!</p>
      <p>Nam vehicula malesuada lectus, interdum fringilla nibh. Duis aliquam vitae metus a pharetra. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec fermentum augue quis augue ornare, eget faucibus felis pharetra. Proin condimentum et leo
        quis fringilla.</p>
    </div>
  </div>
</body>

我一直在尝试使它起作用的几种方法,但是在调整浏览器大小时,SVG产品线会不断缩小。当我希望它包含父div的整个高度时。

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

您可以使用padding hack:

在这种情况下,您将svg包装在容器.svgDiv上。 .svgDiv具有height:0padding-top:100%,使其与容器一样高。然后,您可以给svg容器的宽度和高度。

html, body {
  width: 100%;
  height: 100%;
}
div.container {
  margin: 40px auto;
  width: 70%;
  display: flex;
  align-items: center;
  outline:1px solid;
}

.column {
  display: inline-block;
  width: 40%;

}

.column h3{
  color: #1c3653;
  font-size: 20px;
  margin: 0px;
}

.column p {
  color: #1c3653;
  font-size: 15px;
}

svg {
    display: block;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

.svgDiv{ 
   width: 10%;
   height:0;
   padding-top:100%;
   position: relative;
}
  
<div class="container">
		<div class="column">
			<h3>Content</h3>
			<p>Easily change/switch/swap every placeholder inside every image on Sedna with the included Sketch files. You’ll have this template customised to suit your business in no time!</p>
			<p>Nam vehicula malesuada lectus, interdum fringilla nibh. Duis aliquam vitae metus a pharetra. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec fermentum augue quis augue ornare, eget faucibus felis pharetra. Proin condimentum et leo quis fringilla.</p>
		</div>
    
    <div class="svgDiv">
    <svg viewbox="0 0 100 100" preserveAspectRatio="none">
 
      <line x1="50%" x2="50%" y1="0" y2="100%" style="stroke:rgb(0,0,0);stroke-width:1" />
    </svg>
    </div>
    
		<div class="column">
			<h3>Content</h3>
			<p>Easily change/switch/swap every placeholder inside every image on Sedna with the included Sketch files. You’ll have this template customised to suit your business in no time!</p>
			<p>Nam vehicula malesuada lectus, interdum fringilla nibh. Duis aliquam vitae metus a pharetra. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec fermentum augue quis augue ornare, eget faucibus felis pharetra. Proin condimentum et leo quis fringilla.</p>
		</div>
  </div>

还有另一种方法

  • 在这种情况下,您将svg包装在.svgDiv容器上

  • 您将容器的align-items: center;更改为align-items: stretch;

  • 要将文本保留在中心,请为各列添加align-self:center;

  • 接下来,您将SVG的宽度设为.svgDiv的宽度和高度(100%)

html, body {
  width: 100%;
  height: 100%;
}
div.container {
  margin: 40px auto;
  width: 70%;
  display: flex;
  align-items: stretch;
  outline:1px solid;
}

.column {
  display: inline-block;
  width: 40%;
  align-self:center;
}

.column h3{
  color: #1c3653;
  font-size: 20px;
  margin: 0px;
}

.column p {
  color: #1c3653;
  font-size: 15px;
}

svg {
display: block;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;

}

.svgDiv{ 
width: 10%;
position:relative;
}
<div class="container">
		<div class="column">
			<h3>Content</h3>
			<p>Easily change/switch/swap every placeholder inside every image on Sedna with the included Sketch files. You’ll have this template customised to suit your business in no time!</p>
			<p>Nam vehicula malesuada lectus, interdum fringilla nibh. Duis aliquam vitae metus a pharetra. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec fermentum augue quis augue ornare, eget faucibus felis pharetra. Proin condimentum et leo quis fringilla.</p>
		</div>
    <div class="svgDiv">
    <svg viewbox="0 0 100 100" preserveAspectRatio="none">
 
      <line x1="50%" x2="50%" y1="0" y2="100%" style="stroke:rgb(0,0,0);stroke-width:1" />
    </svg>
     </div>
		<div class="column">
			<h3>Content</h3>
			<p>Easily change/switch/swap every placeholder inside every image on Sedna with the included Sketch files. You’ll have this template customised to suit your business in no time!</p>
			<p>Nam vehicula malesuada lectus, interdum fringilla nibh. Duis aliquam vitae metus a pharetra. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec fermentum augue quis augue ornare, eget faucibus felis pharetra. Proin condimentum et leo quis fringilla.</p>
      <p>Easily change/switch/swap every placeholder inside every image on Sedna with the included Sketch files. You’ll have this template customised to suit your business in no time!</p>
			<p>Nam vehicula malesuada lectus, interdum fringilla nibh. Duis aliquam vitae metus a pharetra. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec fermentum augue quis augue ornare, eget faucibus felis pharetra. Proin condimentum et leo quis fringilla.</p>
		</div>
  </div>

我希望这会有所帮助。

答案 1 :(得分:1)

height: 100%元素中删除svg并将align-self属性添加为 stretch ,以便 flexbox 自身将其延伸 flexbox 的完整高度-请参见下面的演示和updated fiddle

html, body {
  width: 100%;
  height: 100%;
}
div.container {
  margin: 40px auto;
  width: 70%;
  display: flex;
  align-items: center;
}

.column {
  display: inline-block;
  width: 40%;

}

.column h3{
  color: #1c3653;
  font-size: 20px;
  margin: 0px;
}

.column p {
  color: #1c3653;
  font-size: 15px;
}

svg {
  display: block;
  /*height: 100%;*/
  width: 20%;
  align-self: stretch; /* ADDED */
}
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<div class="container">
  <div class="column">
    <h3>Content</h3>
    <p>Easily change/switch/swap every placeholder inside every image on Sedna with the included Sketch files. You’ll have this template customised to suit your business in no time!</p>
    <p>Nam vehicula malesuada lectus, interdum fringilla nibh. Duis aliquam vitae metus a pharetra. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec fermentum augue quis augue ornare, eget faucibus felis pharetra. Proin condimentum et leo quis
      fringilla.</p>
  </div>
  <svg viewbox="0 0 100 100" preserveAspectRatio="none">
      <line x1="50%" x2="50%" y1="0" y2="100%" style="stroke:rgb(235,235,235);stroke-width:1" />
      Sorry, your browser does not support inline SVG.
    </svg>
  <div class="column">
    <h3>Content</h3>
    <p>Easily change/switch/swap every placeholder inside every image on Sedna with the included Sketch files. You’ll have this template customised to suit your business in no time!</p>
    <p>Nam vehicula malesuada lectus, interdum fringilla nibh. Duis aliquam vitae metus a pharetra. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec fermentum augue quis augue ornare, eget faucibus felis pharetra. Proin condimentum et leo quis
      fringilla.</p>
  </div>
</div>

答案 2 :(得分:0)

您只需要为.container.column fiddle here

添加高度
div.container {
  margin: 40px auto;
  width: 70%;
  display: flex;
  align-items: center;
  height: 350px;
}

.column {
  display: inline-block;
  width: 40%;
  height: 100%;
}