Reputation: 21
I have this HTML:
<div>
<span>ooisaj dsaoijdiosa djoisaj doais </span>
</div>
And this 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.
Upvotes: 2
Views: 1386
Reputation: 353
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;
}
Upvotes: 0
Reputation: 141
You can use 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>
Upvotes: 1
Reputation: 14371
Using vertical-align
and display:table-cell
you can align multiple items in a <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
. This supports IE8+, basically every browser used (97%)
left-aligned
you can remove text-align:center
and add a little padding. This causes a problem where the size (of the div) increases. To counter this, use box-sizing: border-box
shape-inside
might be what you want but that is barely supported, even in the latest browsers
Learn about it here
Upvotes: 3
Reputation: 3712
Here's another way of centering, that doesn't depend on the knowing the span's height and doesn't use display:table-cell
. Keep in mind that it works in IE9 and above.
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%);
}
Upvotes: 0
Reputation:
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;
}
Upvotes: 0
Reputation: 7246
You can rely on display:table here, which has a very good browser support: http://caniuse.com/#feat=css-table
Apply display:table-cell, vertical-align: middle, text-align:center to your span.
Then remember to also apply 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/
Upvotes: 1
Reputation: 7122
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>
Upvotes: 1