简体   繁体   English

如何在HTML段落中垂直对齐范围和文本?

[英]How to vertically align a span and text in an HTML paragraph?

I want to create a clickable rounded rectangle that will change color when clicked, using bootstrap. 我想使用引导程序创建一个可单击的圆角矩形,该矩形将在单击时更改颜色。 The best way I found to do this is with a span. 我发现做到这一点的最佳方法是跨度。

Here is the example code I've written: 这是我编写的示例代码:

<p id="proyect_c_leasons" style="height:30px;vertical-align:middle;display:table-cell"> <span class="label label-default" style="background-color:#D80909;width:35px;height:25px;display:inline-block" onclick='console.log("hola")'>  </span> <b>Lecciones Aprendidas</b> </p>

This is what it shows: 它显示了以下内容:

在此处输入图片说明

What I want is the text to be align to the middle of the red square instead of the bottom of the red square. 我想要的是使文本与红色正方形的中间对齐,而不是与红色正方形的底部对齐。

This code: 这段代码:

<p id="proyect_c_leasons"> <span class="label label-default" style="background-color:#D80909;width:35px;height:25px;display:inline-block" onclick='console.log("hola")'>  </span> <b>Lecciones Aprendidas</b> </p>

Shows the exact same thing (basically p ignores everything I write into style) 显示完全相同的内容(基本上p忽略了我写成样式的所有内容)

Can anyone provide me with a way to fix this? 谁能为我提供解决方法? Or an alternative? 还是另类?

Set line-height for the div and then vertical-align the span within. 设置div的行高,然后在其中垂直对齐跨度。

 <p id="proyect_c_leasons" style="height:30px;line-height:30px;display:table-cell"> <span class="label label-default" style="background-color:#D80909;width:35px;height:30px;display:inline-block;vertical-align:middle;" onclick='console.log("hola")'> </span> <b>Lecciones Aprendidas</b> </p> 

Bootply 自举

Try giving the vertical-align: middle; 尝试给出vertical-align: middle; and line-height equal to the height to the <span> : line-height等于<span>height

 <p id="proyect_c_leasons"> <span class="label label-default" style="background-color:#D80909;width:35px;height:25px;line-height:25px;display:inline-block;vertical-align: middle;" onclick='console.log("hola")'></span> <b>Lecciones Aprendidas</b> </p> 

\n
\n
\n
    <p id="proyect_c_leasons" style="line-height: 25px;"> <span class="label label-default" style="background-color:#D80909;width:35px;height:25px;display:inline-block; float: left;" onclick='console.log("hola")'>  </span> <b>Lecciones Aprendidas</b> </p>
\n
\n
\n

It was somewhat unclear what you were asking here, so I'll answer both interpretations. 您在这里问的内容还不太清楚,因此我将回答两种解释。

a) You want the text in the middle of the square (contained within) a)您希望文本位于正方形的中间(包含在其中)

This is a relatively simple change. 这是一个相对简单的更改。 You just need to contain the text within the span. 您只需要在范围内包含文本即可。

b) You want the text to be vertically in the middle of the square (still outside of it) b)您希望文本垂直位于正方形的中间(仍在正方形外面)

For this, you'll want to look at text alignment. 为此,您需要查看文本对齐方式。

<p id="proyect_c_leasons" style="height:30px;vertical-align:middle;display:table-cell">
<span class="label label-default" style="background-color:#D80909;width:35px;height:25px;display:inline-block" onclick='console.log("hola")'>
</span>
<b style="vertical-align: 40%;">Lecciones Aprendidas</b>

In the example above I've literally only changed the style for the <b> tag. 在上面的示例中,我实际上仅更改了<b>标记的样式。 You can alter where it lies by changing the percentage. 您可以通过更改百分比来更改其位置。 There are a variety of different options available to you, which are listed on mdn: 在mdn上列出了多种可用的选项供您选择:

https://developer.mozilla.org/en/docs/Web/CSS/vertical-align https://developer.mozilla.org/en/docs/Web/CSS/vertical-align

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

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