简体   繁体   English

跨度必须出现在圆div内

[英]Span must appear inside circle div

I have this HTML: 我有这个HTML:

<div>
    <span>ooisaj dsaoijdiosa djoisaj doais </span>
</div>

And this CSS: 而这个CSS:

div
{
    margin-left:50px;
    border-radius:50%;
    height:150px;
    width:150px;
    background-color:green;
}

Here is a fiddle with the code. 这是代码的摆弄

I want to make the span appear inside the circle. 我想使跨度显示在圆圈内。 But, I can't achieve it and can't find any question in SO. 但是,我无法实现它,因此无法在SO中找到任何问题。

Using vertical-align and display:table-cell you can align multiple items in a <div> . 使用vertical-aligndisplay:table-cell可以对齐<div>多个项目。 This is automatic and allows multiple lines to be positioned. 这是自动的,可以放置多条线。 tables by default vertically center text, so we can take this property and apply it to our div . 表格默认垂直居中放置文本,因此我们可以使用此属性并将其应用于div This supports IE8+, basically every browser used (97%) 它支持IE8 +,基本上每个使用的浏览器(97%)

Fiddle 小提琴


If you wish for the text to be left-aligned you can remove text-align:center and add a little padding. 如果您希望文本left-aligned ,则可以删除text-align:center并添加一些填充。 This causes a problem where the size (of the div) increases. 这会导致(div的)大小增加的问题。 To counter this, use box-sizing: border-box 要解决此问题,请使用box-sizing: border-box

Fiddle 小提琴


shape-inside might be what you want but that is barely supported, even in the latest browsers shape-inside可能是您想要的,但即使在最新的浏览器中也几乎不受支持

Learn about it here 在这里了解


CSS3 has pleanty of cool share features. CSS3具有纯净的共享功能。 Check this link out 检查此链接

you can try like this. 您可以尝试这样。

 div { margin-left:50px; border-radius:50%; height:150px; width:150px; background-color:green; text-align:center; vertical-align:middle; display:table-cell; } div span { display:inline-block; vertical-align:middle; } 
 <div> <span>ooisaj dsaoijdiosa djoisaj doais </span> </div> 

You can rely on display:table here, which has a very good browser support: http://caniuse.com/#feat=css-table 您可以在此处依赖display:table ,它对浏览器有很好的支持: http : //caniuse.com/#feat=css-table

Apply display:table-cell, vertical-align: middle, text-align:center to your span. display:table-cell,vertical-align:middle,text-align:center应用于您的跨度。

Then remember to also apply display:table ; 然后记住还要应用display:table ; to your parent container. 到您的父容器。

div {
    margin-left:50px;
    border-radius:50%;
    height:150px;
    width:150px;
    background-color:green;
    display:table;
}

span{
    display:table-cell;
    vertical-align:middle;
    text-align:center;
}

Jsfiddle: https://jsfiddle.net/a_incarnati/5nuLkr4q/3/ Jsfiddle: https ://jsfiddle.net/a_incarnati/5nuLkr4q/3/

You can use position:absolute 您可以使用position:absolute

div {
    margin-left:50px;
    border-radius:50%;
    height:150px;
    width:150px;
    background-color:green;
    position:relative;
}
span{
    position:absolute;
    top:30%;
    left:20%;
}

you should try different percentages 你应该尝试不同的百分比

 div { margin-left:50px; border-radius:50%; height:150px; width:150px; background-color:green; position:relative; } span{ position:absolute; top:30%; left:20%; } 
 <div> <span>ooisaj dsaoijdiosa djoisaj doais </span> </div> 

div {
    margin-left:50px;
    border-radius:50%;
    height:150px;
    width:150px;
    background-color:green;
    text-align:center;
    vertical-align:middle;
    display:table-cell;
}
div span {
    display:inline-block;
    vertical-align:middle;
}

You can try to change display but stay as lose as can to the original "display" defaults. 您可以尝试更改显示,但尽量避免丢失原始的“显示”默认设置。

Assign: 分配:

div{
    display: table;
}
span{
    display: table-cell;
    vertical-align: middle;
}

Fiddle 小提琴

Here's another way of centering, that doesn't depend on the knowing the span's height and doesn't use display:table-cell . 这是另一种居中方法,它不依赖于知道跨度的高度,也不使用display:table-cell Keep in mind that it works in IE9 and above. 请记住,它可以在IE9及更高版本中使用。

https://jsfiddle.net/5nuLkr4q/7/ https://jsfiddle.net/5nuLkr4q/7/

div {
    margin-left:50px;
    border-radius:50%;
    height:150px;
    width:150px;
    background-color:green;
    position:relative;
}

span{
    position:absolute;
    top: 50%;
    left: 0;
    width: 100%;
    text-align:center;
    transform: translateY(-50%);
}

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

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