简体   繁体   English

设置 span 元素的百分比宽度

[英]Set percentage width for span element

A straight forward question.. is it possible to set the width in percentage for a span tag in CSS?一个直截了当的问题。是否可以在 CSS 中设置 span 标签的百分比宽度? for example:例如:

<span style="width: 50%">...</span> 

etc.. ETC..

In my project I'm currently using divs but ofcourse after each div tag a line break gets inserted (which I don't want).在我的项目中,我目前正在使用 div,但当然在每个 div 标记之后插入一个换行符(我不想要)。 So the most obvious solution to that is then to use span tags instead of div.因此,最明显的解决方案是使用 span 标签而不是 div。 But then I'm not able to define the width for the span tags.. Atleast not in a percentage kind of way.但是后来我无法定义 span 标签的宽度。至少不是以百分比的方式。

Thanks in advance for any help!在此先感谢您的帮助!

Define the element as an inline block and you can control the width and height like a block element while keeping it inline with surrounding content.将元素定义为内联块,您可以像块元素一样控制宽度和高度,同时使其与周围内容保持内联。

#element {
  display: inline-block;
  width: 50%;
}

inline elements cannot have dimensions.内联元素不能有维度。 do them to do so, and still remain inline, add:让他们这样做,并且仍然保持内联,添加:

display: inline-block

Add display: flex;添加display: flex; on parent div style .在父div style上。

<div style="display: flex;">
    <span style="width:50%">...</span>
    <span style="width:50%">...</span>
</div>

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

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