How to align inner text in div - html

I am trying to align the text inside a div in center of the page and also text start from left side. But getting a problem as role1, role2 text is showing in center of Role(r) text i want that all the text should right aligned but in center of the page.
.home-role-access-text {
text-align: center;
height: 20px;
width: 250px;
position: absolute;
top: 60%;
left: 50%;
margin-left: -125px;
font-size: 20px;
color: #026890;
}
<div class="home-role-access-text" data-ng-show="alarmsCounter <= 0">
<div>
<div data-ng-hide="assignedRoles.length<=0">
<div style="padding-bottom: 10px;"><b>Role(r)</div>
<div style="font-size:11px;font-weight: 100;display: inline-block; text-align: left" data-ng-repeat="role in assignedRoles">Role1 </div>
<div style="font-size:11px;font-weight: 100;display: inline-block; text-align: left" data-ng-repeat="role in assignedRoles">Role3 </div>
</div>
</div>
</div>

Change display in the child divs of <div class="roles"> to block instead of inline-block.
I have included a jsfiddle as well. I had to change the CSS for .home-role-access-textto get all text to start on the left and stay within it's container. I suggest using display: flex because it's better at centering a block of content. https://jsfiddle.net/Luxz8eff/
.home-role-access-text {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
font-size: 20px;
color: #026890;
}
<div class="home-role-access-text" data-ng-show="alarmsCounter <= 0">
<div>
<div data-ng-hide="assignedRoles.length<=0">
<div style="padding-bottom: 10px;"><b>Role(r)</b></div>
<div style="font-size:11px;font-weight: 100;display: block; text-align: left" data-ng-repeat="role in assignedRoles">Role1 </div>
<div style="font-size:11px;font-weight: 100;display: block; text-align: left" data-ng-repeat="role in assignedRoles">Role3 </div>
</div>

Going by your image it seems you want all text left aligned inside a div that is right aligned of the center.
Based on that, you can float the container right with all text aligned left.
.home-role-access-text {
border: 1px solid black;
text-align: left;
height: 20px;
width: 250px;
position: absolute;
top: 60%;
left: 50%;
margin-left: -125px;
font-size: 20px;
color: #026890;
}
.roles {
float:right
}
<div class="home-role-access-text" data-ng-show="alarmsCounter <= 0">
<div>
<div data-ng-hide="assignedRoles.length<=0" class="roles">
<div style="padding-bottom: 10px;"><b>Role(r)</b></div>
<div style="font-size:11px;font-weight: 100;display: block; text-align: left;" data-ng-repeat="role in assignedRoles">Role1 </div>
<div style="font-size:11px;font-weight: 100;display: block; text-align: left;" data-ng-repeat="role in assignedRoles">Role3 </div>
</div>
</div>
</div>
The black border in the example is only added to show the div is still centered with right aligned text - You can off course, remove it :)

Related

Cannot center content in div [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Vertical Align Center in Bootstrap 4 [duplicate]
(20 answers)
Bootstrap Center Vertical and Horizontal Alignment
(17 answers)
How to center a "position: absolute" element
(31 answers)
How can I vertically align elements in a div?
(28 answers)
Closed 2 years ago.
This is my first post. Just as the title says, I am not able to center content inside of a div. Here is my code:
.coupon {
border-style: dashed;
border-color: black;
min-height: 350px;
}
.fineprint {
font-size: 11px;
position: absolute;
bottom: 25px;
display: table-cell;
vertical-align: bottom;
text-align: center;
}
.couponcontent {
position: absolute;
top: 40%;
bottom: 60%;
display: table-cell;
vertical-align: middle;
text-align: center;
}
.scissors {
font-size: 48px;
color: #ed1a24;
position: absolute;
top: 25px;
display: table-cell;
vertical-align: top;
text-align: center;
}
<!-- Coupon Boxes -->
<div class="col-md-4 col-sm-6">
<div class="feature boxed feature-s6">
<div class="fbox-content center coupon">
<span class="scissors"><i class="fa fa-scissors"></i></span>
<div class="couponcontent">
<h2>middle center</h2>
<p>middle center</p>
</div>
<p class="fineprint">bottom center</p>
</div>
</div>
</div>
<!-- End Coupon box -->
Can someone help me center this content inside of the div? It is currently aligned left. Thanks!
You need remove position: absolute and change display from table-cell to block.
.coupon {
border-style: dashed;
border-color: black;
min-height: 350px;
}
.fineprint {
font-size: 11px;
bottom: 25px;
display: block;
vertical-align: bottom;
text-align: center;
}
.couponcontent {
display: block;
vertical-align: middle;
text-align: center;
}
.scissors {
font-size: 48px;
color: #ed1a24;
top: 25px;
display: block;
vertical-align: top;
text-align: center;
}
<!-- Coupon Boxes -->
<div class="col-md-4 col-sm-6">
<div class="feature boxed feature-s6">
<div class="fbox-content center coupon">
<span class="scissors"><i class="fa fa-scissors"></i></span>
<div class="couponcontent">
<h2>middle center</h2>
<p>middle center</p>
</div>
<p class="fineprint">bottom center</p>
</div>
</div>
</div>
<!-- End Coupon box -->
try to simplify your CSS as much as possible otherwise, there will be clashes and things like text-align:center won't work. https://www.w3schools.com/ is a great source when it comes to CSS.
.main{text-align:center}
.coupon {
border-style: dashed;
border-color: black;
min-height: 350px;
}
.couponcontent {
position: absolute;
top: 40%;
bottom: 60%;
}
.scissors {
font-size: 48px;
color: #ed1a24;
position: absolute;
top: 25px;
display: table-cell;
vertical-align: top;
text-align: center;
}
<div class='main'>
<div class="coupon">
<span class="scissors"><i class="fa fa-scissors"></i></span>
<h2>middle center</h2>
<p>middle center</p>
</div>
<p >bottom center</p>
</div>

Vertically middle align floated contents

I'm following this SO Answer to vertically align a div's contents that has float: left styling:
.main {
height: 85px;
}
.child_1,
.child_2 {
float: left;
width: 8rem;
}
<div class="main">
<div style="display: inline-block;vertical-align: middle;">
<div class="child_1">
Some long text is put here
</div>
</div>
<div style="display: inline-block;vertical-align: middle;">
<div class="child_2">
22
</div>
</div>
</div>
But, it doesn't vertically align as per the height of the wrapper div main. What is wrong in the code?
If you add
display: flex;
align-items: center;
to .main class, it should work.
.main {
height: 85px;
background-color: #f00;
/*position: absolute;*/
/*top: 2rem;*/
display: flex;
align-items: center;
}
.child_1,
.child_2 {
float: left;
width: 8rem;
}
<div class="main">
<div style="display: inline-block;vertical-align: middle;">
<div class="child_1">
Some long text is put here
</div>
</div>
<div style="display: inline-block;vertical-align: middle;">
<div class="child_2">
22
</div>
</div>
</div>
In the linked question, you can see that the line box aligns itself, but not exactly with the main - try setting a height for the main and you'll see.
You've height set for the main here and that makes all the difference. For this you can use a psuedo element to center:
.main:after{
display: inline-block;
vertical-align: middle;
height: 100%;
content: '';
}
See demo below:
.main {
height: 85px;
border: 1px solid;
}
.child_1,
.child_2 {
float: left;
}
.main:after{
display: inline-block;
vertical-align: middle;
height: 100%;
content: '';
}
<div class="main">
<div style="display: inline-block;vertical-align: middle;">
<div class="child_1">
Some long text is put here
</div>
</div>
<div style="display: inline-block;vertical-align: middle;">
<div class="child_2">
22
</div>
</div>
</div>

Align two divs side-by-side and center them inside another

I'm trying to place two divs (.health-icon and .health-description) side-by-side, but horizontally center them inside .health-status. I was able to do this.
However, they're not properly vertically aligned. I need .health-description line-height to be the same size of the .ranking. But it seems that .health-description is a little bit lower than .health-icon.
.health-status {
text-align: center;
margin: 0 auto;
}
.health-icon {
display: inline-block;
}
.health-description {
display: inline-block;
line-height: 70px;
font-weight: bold;
}
.rating-circle {
height: 70px;
width: 70px;
border-radius: 250px;
font-size: 36px;
font-weight: 700;
color: white;
line-height: 65px;
text-align: center;
background: #2c3e50;
/*float: left;*/
display: inline-block;
/*margin-right: 20px;*/
padding-left: 2px;
padding-top: 2px;
}
<div class="panel-body">
<div class="health-status">
<div class="health-icon">
<div id="rating" class="rating-circle sq-orange" data-role="status-name">A</div>
</div>
<div class="health-description">UTI</div>
</div>
<div class="minidash">
<section class="row">
<div class="card col-md-6">
<div class="value">123.456</div>
<div class="name">Porte</div>
</div>
<div class="card col-md-6">
<div class="value">-435,0</div>
<div class="name">Variação de Curto Prazo</div>
</div>
</section>
</div>
</div>
http://jsfiddle.net/n1o8uvcv/1/
EDIT
This is different from How to horizontally center a <div> in another <div>?, where the focus is to horizontally center a div inside another. This is already solved here. What happens is that when I use the inline-block display, the two divs are not correctly vertically aligned.
This is an extremely specific question so i'll give an answer that is not usable to general cases.
Change your health-description class to this:
.health-description {
display: inline-block;
transform: translate(0, -30%);
}

CSS: Place text at beginning of centered headline

I want to place a short texte at the beginning of a centered headline.
My current solution is to have a container with display inline arround the headline
and placing the text with text right and display block.
This works pretty good until the I have a long headline.
The HTML:
<div class="article">
<div class="headline-container">
<span class="headline-companion">FooBar</span>
<h3>Sample Headline</h3>
</div>
</div>
THE SCSS:
.article{
display:block;
width: 700px;
margin: 50px auto;
padding: 20px;
text-align: center;
}
.headline-container{
display: inline-block;
margin: 0 auto;
text-align: center;
position: relative;
h3{
font-size: 40px;
margin: 0;
padding: 0;
display: inline;
}
.headline-companion{
margin: 0;
padding: 0;
display: block;
text-align: left;
}
}
http://codepen.io/anon/pen/PwNJGy
Are there any solutions where I don't have to position the text with absolute?
Clarification:
I want that the Short text beggins always at the first letter of the headline no matter how long the centered headline is.
Something like what (I think) you want to do can be accomplished if you wrap your <h3> tag in another element and set a max-width value, and by specifying word-wrap.
This allows you to change the width of the parent divs, or length of the header text without having very negative effects on the overall formatting:
[Edit]: In addition, another element needs to be added if you want to have the headline-companion and headline centered with respect to article but with left justified text... in this case sub-headline-container.
.article {
display: block;
width: 800px;
margin: 50px auto;
padding: 20px;
text-align: center;
}
.headline-container {
display: inline-block;
margin: 0 auto;
text-align: center;
position: relative;
}
.headline-wrap {
word-wrap: break-word;
max-width: 800px;
text-align: left;
}
.headline-companion {
margin: 0;
padding: 0;
display: block;
text-align: left;
}
<div class="article">
<div class="headline-container">
<div class="sub-headline-container">
<span class="headline-companion">FooBar</span>
<div class="headline-wrap">
<h3>
Sample Headline
</h3>
</div>
</div>
</div>
</div>
<div class="article">
<div class="headline-container">
<div class="sub-headline-container">
<span class="headline-companion">FooBar</span>
<div class="headline-wrap">
<h3>
SUUUUUUPPPPERRRRRRRR BIG HEADLINE!!! ------------------------------- --------------
</h3>
</div>
</div>
</div>
</div>

text align left and right around centre of div

imagine the star * is centred inside the div
textAlignRight * text AlignLeft
That's basically what I am trying to achieve
<center>
<span class="left_host">Right aligned</span>
*
<span class="right_host">Left aligned</span>
</center>
Any help to get this solved would be awesome
thank you
HTML:
<div class="wrap">
<div class="right">text aligned to the right</div>
<div class="star">★</div>
<div class="left">text aligned to the left</div>
</div>
CSS:
body
{
text-align: center;
}
.wrap
{
display: inline-block;
background: #eee;
}
.wrap > div
{
float: left;
padding: 10px 5px;
}
.right
{
text-align: right;
direction: rtl;
}
.left
{
text-align: left;
}
i've added buttons to the live example to let you see what happen when more content will append dynamically: http://jsfiddle.net/wqsb38jr/1/
This is how I would do it structurally:
div {
display: inline-block;
padding: 10px;
vertical-align: middle;
}
.left {
text-align: right;
}
.center {
text-align: center;
width: 110px;
}
.right {
text-align: left;
}
.left,
.right {
width: 200px;
}
.container {
text-align: center;
min-width: 580px;
}
<div class="container">
<div class="left">This text will be aligned right but appear on the left of stuff that you want to put in the center of all this madness.</div>
<div class="center">* This will be centered. *</div>
<div class="right">This text will be aligned left but appear on the right of stuff that you want to put in the center of all this madness.</div>
</div>
http://jsfiddle.net/zn2uafm3/