如何保持一个SVG线相同的高度父DIV,在未指定的高度?

问题描述 投票:1回答:3

所以我想放置在两列之间的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的最大高度。

谢谢您的帮助!

html css css3 svg flexbox
3个回答
1
投票

height: 100%元素删除svg并添加align-self财产拉伸,以便实现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>

1
投票

您可以使用填充黑客:

你包在这种情况下,一个容器.svgDiv您的SVG。该.svgDivheight: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;to 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>

我希望这有帮助。


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%;
}
© www.soinside.com 2019 - 2024. All rights reserved.