简体   繁体   English

HTML <span>和svg元素不是内联的</span>

[英]HTML <span> and svg element are not inline

My current situation is a fieldset that is inline with another one on my page. 我当前的情况是一个字段集,该字段集与我页面上的另一个字段内联。 The fieldset this post is about contains a svg circle and some text. 这篇文章所涉及的字段集包含一个svg圆圈和一些文本。

My goal is to have both of these inline to each other, with the fieldset adapting in size because i want to change the given <span> / <p> text and the color of the circle later on based on a function that returns either true or false. 我的目标是使这两个行彼此内联,并且使字段集的大小适应,因为我想稍后基于返回真值的函数更改给定的<span> / <p>文本和圆圈的颜色或错误。

My current code is this: 我当前的代码是这样的:

 <div> <fieldset class="fieldset-auto-width"> <legend>some legend</legend> <div> <svg height="32" width="32"><circle id="circle" cx="16" cy="16" r="15" stroke="#333" stroke-width="1" fill="#ffbf00"/></svg> </div> <span>some text</span> </fieldset> </div> 

JS-Fiddle to my code with CSS JS-用我的CSS拨弄我的代码

At this point i am not even sure if the second div that wraps the svg element is needed, because im trying to get it right for about half an hour now. 在这一点上,我什至不确定是否需要包裹svg元素的第二个div,因为我现在试图将其正确放置约半小时。 The nested dif was there because i tried to force a maximum size to the svg element because of the unwanted space it took, even with style="display: block" 嵌套的dif在那里,是因为我试图将最大尺寸施加到svg元素上,因为它占用了不必要的空间,即使使用style="display: block"

Now i want my svg circle and my <span> inline. 现在我想要我的svg圈子和我的<span>内联。

The reason why i am currently using <span> and not <p> is because there is too much unwanted space below the svg, which is not as critical when using <span> instead of <p> . 我当前正在使用<span>而不是<p>的原因是因为svg下方有太多不需要的空间,当使用<span>而不是<p>时,这不是那么关键。

Edit: wrong jsfiddle link 编辑:错误的jsfiddle链接

Here you go. 干得好。 div is a block element. div是一个块元素。 So, you have to make it inline-block. 因此,您必须使其内联。 vertical-align: middle sets the vertical alignment of an element vertical-align: middle设置元素的垂直对齐

 <div> <fieldset class="fieldset-auto-width" > <legend>some legend</legend> <div style="display: inline-block; vertical-align: middle"> <svg height="32" width="32"> <circle id="circle" cx="16" cy="16" r="15" stroke="#333" stroke-width="1" fill="#ffbf00"/> </svg> </div> <span>some text</span> </fieldset> </div> 

A <div> is display: block by default. 显示<div> display: block默认情况下为display: block

If you want the content of the div and the element following the div to be inline with each other … don't put a div there. 如果您希望div的内容和div后面的元素彼此内联…请不要在其中放置div。

 <div> <fieldset class="fieldset-auto-width" > <legend>some legend</legend> <svg height="32" width="32"> <circle id="circle" cx="16" cy="16" r="15" stroke="#333" stroke-width="1" fill="#ffbf00"/> </svg> <span>some text</span> </fieldset> </div> 

Not sure if I understand your question entirely but your span element is not in the same container as the SVG. 不知道我是否完全理解您的问题,但是您的span元素与SVG不在同一个容器中。 Since you've set your SVG element to display inline-block, this has no effect to your span. 由于您已将SVG元素设置为显示嵌入式块,因此这对您的跨度没有影响。

To align these two vertically, you can use vertical-align: middle on both your elements (svg and span) and set your SVG to display:inline-block 要垂直对齐这两个元素,可以在两个元素(SVG和跨度)上使用vertical-align:middle并将SVG设置为display:inline-block

Updated fiddle : https://jsfiddle.net/L70oupcp/5/ 更新的小提琴: https : //jsfiddle.net/L70oupcp/5/

      <div>
        <svg height="32" width="32">
           <circle id="circle" cx="16" cy="16" r="15" stroke="#333" stroke-width="1" fill="#ffbf00"/>
        </svg>
        <span>some text</span>
      </div>    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM