centering text inside a div - html

I am trying to center text(horizontal and vertical) and I am having a little difficulty with this.
This is what I have tried but is only centering it horizontally, not vertically.
How can I get this text in the center?
<div class="playerDetailsPage">
<p>
<h4>The text will be in the center</h4>
</p>
</div>
css
.playerDetailsPage {
background-color: #edd4d4;
width: 630px;
height: 390px;
text-align: center;
vertical-align: middle;
line-height: 390px;
}

Don't use tables for this kind of things. It isn't something you can solve by using a table.
For what I can see, it is working perfect. Here is a working example: http://jsfiddle.net/T7V58/1/
Your code is correct, what is the problem? Just remove the p tag and you are there.
<div class="playerDetailsPage">
<h4>The text will be in the center</h4>
</div>
CSS:
.playerDetailsPage {
background-color: #edd4d4;
width: 630px;
height: 390px;
text-align: center;
vertical-align: middle;
line-height: 390px;
}

Vertical align middle will do nothing on a div (do nothing on a display:block element exactly). vertical aligning works with table cells. The example posted on the comment aligns the h4 becouse the line height is exactly the same as the height of the parent div. But what happens when you have two (or more) lines text? That will break.
The solution:
<div class="parent">
<div class="aligner">
<h4>The text will be in the center which can be several lines long, and will not break the verical centering. Lorem ipsum more text.</h4>
</div>
</div>
.parent {
background-color: #edd4d4;
width: 630px;
height: 390px;
text-align: center;
}
.parent .aligner {
display: table-cell;
height: inherit;
width: inherit;
vertical-align: middle;
}
You should use an internal aligner div and display it as a table cell (so vertcal align will work. You must specify the height and width of the internal element but fortunately inheritane comes to the rescue (so you don't have to redundantly specify it again).
Hope it helps!

Related

Do I need to use another property other than "text-align" to center my DIV within a DIV?

I want to center a DIV within a parent DIV. I have tried using the recommende dsolution on SO -- How to horizontally center a <div> in another <div>?, but its not centering it. The basic layout is this
#revealScoreMobile {
padding: 10px;
display: block;
text-align: center;
width: 100%;
}
.stats {
text-align: center;
width: 100%;
background-color: red;
margin: 0 auto;
}
<div id="revealScoreMobile">
...
<div class="stats" style="">
<div class="score">5.0</div>
(<span class="votesCast">1</span> votes cast)
</div>
</div>
and yet as you can see from the Fiddle -- https://jsfiddle.net/5Lgu0uw3/2/, the child DIV is not centering within the parent, despite the fact I have
text-align:center;
in there. What gives? What else do I need to do to center that DIV within its parent?
I am not completely sure what you want, but if you want the inner DIV NOT have the full width, but only as much as its text contents require, make it an inline-block and erase the widthsetting (or give it a widthsetting less than 100%). inline-blocks are affected by text-align: center
(note that I erased some superfluous settings, but put the ... content into its own DIV, since it otherwise would be on one line with the subsequent inline-block.
#revealScoreMobile {
padding: 10px;
text-align: center;
}
.stats {
display: inline-block;
text-align: center;
background-color: red;
}
<div id="revealScoreMobile">
<div> ... </div>
<div class="stats" style="">
<div class="score">5.0</div>
(<span class="votesCast">1</span> votes cast)
</div>
</div>
As others have suggested in the comments, text-align: center; only applies to text content, not the inner div.
Your CSS applies width: 100%; to .stats which is forcing it to take up the full width of it's parent container #revealScoreMobile, which is also width: 100%;. Secondly it needs display: inline-block; to override the previous display: table-cell; as present in your jsfiddle example.
Replace in your CSS:
.stats {
display: inline-block;
text-align: center;
background-color: red;
margin: 0 auto;
}

Text positing weird on canvas

Novice question; trying to learn web design and i've come along way. However could somebody try and explain this to me.
This is how one particular section in my website looks:
I am trying to split the image 40% of the screen and the text in the remaining 60%. It's gone well, however I cannot figure out why the text appears at the bottom and not the top? I am guessing it is very simple, or have I just messed it up?
Here is my HTML:
<section>
<div class="about">
<div class="about_img"></div>
<div class="about_text">
<h1>Some Text</h1>
<p>Some supporting text</p>
</div>
</section>
And my CSS for the section:
.about{
width: 100%;
height: 300px;
background-color: #F0F0F0;
min-height: auto;
display: inline-block;
}
.about_img{
width: 40%;
height: 100%;
background-image: url("/assets/alex-image.JPG");
background-size: cover;
display: inline-block;
}
.about_text{
width: 20%;
display:inline-block;
position: relative;
margin-left: 50px;
}
Please let me know if you need any additional information. I have also uploaded my site to http://www.alexwiley.co.uk
Thanks,
ANSWER, thanks to CBroe.
Use vertical-align: top; to align the element to the top of the block.
You may add vertical-align: top to the <div class="about_text"> to align it vertically on top.
Possible values
According to this page, other possible values you have are:
vertical-align: middle
The element is placed in the middle of the parent element
vertical-align: bottom
The bottom of the element is aligned with the lowest element on the line
vertical-align: {length}
Raises or lower an element by the specified length. Negative values are allowed
vertical-align: {X}%
Raises or lower an element in a percent of the "line-height" property. Negative values are allowed
vertical-align: sub
Aligns the element as if it was subscript
vertical-align: super
Aligns the element as if it was superscript
vertical-align: text-top
The top of the element is aligned with the top of the parent element's font
vertical-align: text-bottom
The bottom of the element is aligned with the bottom of the parent element's font
Default
the default value is:
vertical-align: baseline
Align the baseline of the element with the baseline of the parent element.
Answer
.about{
width: 100%;
height: 300px;
background-color: #F0F0F0;
min-height: auto;
display: inline-block;
}
.about_img{
width: 40%;
height: 100%;
background-image: url("http://www.alexwiley.co.uk/assets/alex-image.JPG");
background-size: cover;
display: inline-block;
}
.about_text{
width: 20%;
display:inline-block;
vertical-align: top;
position: relative;
margin-left: 50px;
}
<div class="about">
<div class="about_img"></div>
<div class="about_text">
<h1>Some Text</h1>
<p>Some supporting text</p>
</div>
</div>
You should add this css to the text to have it in the top :
vertical-align: top

Centering text and image side by side

I am stuck with a CSS problem and I am calling out for your expertise to help me!
I am trying to align text and image. Text and image should be vertically centered, left aligned and fit right next to each other. Both elements should be contained inside the wrapper div that can have varying width.
Below is code that I have so far:
<style>
.cell {
width: 150px;
height: 200px;
box-shadow: 0 0 0.5em #999;
white-space: nowrap;
}
.text_element {
height: 100%;
display: inline-block;
white-space: normal;
float: left;
}
.tooltip_element {
height: 100%;
display: inline-block;
}
</style>
<div class="cell">
<div class="text_element"> This is some random, random text.</div>
<div class="tooltip_element">
<img src="http://www.sainsburysbank.co.uk/library/default/images/life-insurance/icon-tooltip.png"/>
</div>
</div>
Above code produces next image when the wrapper content is 500px:
In the above image elements fit correctly next to each other, but aren't vertically aligned.
For width 200px, we get another problem however:
Here we get empty space between text and image, which shouldn't be there, as image should fit right next to the text. Furthermore image element is now outside the div.
Note that:
wrapper content can have varying width
solution should work in all browsers (no flex solutions)
no JS, only CSS can be used
Thank you very much for your help!
EDIT:
Text should be left aligned!
Ok, here it is:
.cell {
width: 210px;
height: 200px;
box-shadow: 0 0 0.5em #999;
white-space: nowrap;
padding-right: 30px;
}
.text_element {
display: inline-block;
white-space: normal;
float: left;
text-align: justify;
position: relative;
}
.tooltip_element {
position: absolute;
left: 100%;
top: 50%;
transform: translateY(-50%);
}
<div class="cell">
<div class="text_element">This is some random, random text. Moar random text. And moar, moar random text...
<div class="tooltip_element">
<img src="http://www.sainsburysbank.co.uk/library/default/images/life-insurance/icon-tooltip.png" />
</div>
</div>
</div>
Please note I included the tooltip element inside the text element. So I could vertically align them. If they must be siblings, I'd need to wrap them both in a container for vertical centering without flex-box.
If you prefer jsFiddle, to play around with cell width, here it is.
You can use css table https://jsfiddle.net/2Lzo9vfc/247/
CSS
.cell {
width: 100%;
height: 200px;
box-shadow: 0 0 0.5em #999;
word-break:break-all;
}
.text_element {
display: table-cell;
vertical-align: middle;
}
.tooltip_element {
display: table-cell;
vertical-align: middle;
}

Vertically center multi-lined text in div with a tag

I am working on a menu system which consists of div's (width: 275px, height: 75px, border: 1px solid) among each other. I want the whole div to be clickable, which I do with an a tag and display:block property (see code below). Some of the menu entries are multi-lined text, but I can't align them vertically. The basic code is:
.div {
width: 275px;
height: 75px;
border: 1px solid #000;
text-align: center;
float: right;
}
<div class="div">
<a style="display: block; width: 100%; height: 100%" href="#">link..</a>
</div>
I have tried the following:
line-height: 75px: that doesn't work with multi-lined text
display: table and vertical-align: middle does not work with the 100% width and height of the -tag.
I have really tried a lot other code with wrapper div's, tables, ... but with no success.
Otherwise, I do not want to make use of javascript (onclick="location.href").
Thanks!
You can do it with what you've already tried: 'display:table-cell; vertical-align: middle;', you just have to set the height to 75px instead of 100%.
Use display:table-cell, to achieve vertically centred multi-lined text.
JSFiddle Demo
CSS:
div {
width: 300px;
height: 200px;
border: 1px solid #000;
text-align: center;
display: table-cell;
vertical-align: middle;
}

centering block made of an image next to a paragraph

it's my first question here, while i solved many problems in any area reading other's solutions, but this time i have no luck, i keep trying and googling.
I am working with something i'm not very skilled, CSS. I want to center a box made of an image with a paragraph of text next to it (two rows of text - no more), the text is aligned to the left of the image.
|------------------------------------ NOW ------------------------------------|
|img|BIG TITLE
|img|smaller subtext
|----------------------------- <-- CENTERED----> -----------------------------|
|img|BIG TITLE
|img|smaller subtext
I tried to put everything in a div, but since the width of the DIV depends on the length of the text, and since it changes depending on the page's title, i can't set a fixed width.
Here is my HTML:
<div class="container">
<div class="row">
<div class="header">
<p class="small">
<img class="ok" src="ok.png">
<span class="bigtitle">TITLE</span>
<br><span style="text-align:left">subtext</span></p>
</div>
</div>
and here is my CSS:
.row .header {
width: 100%;
}
img.ok {
top: 15px;
}
p.small {
color: #000000;
font: bold 12px arial,helvetica,sanserif;
display: block;
}
span.bigtitle {
color: #679819;
text-transform:uppercase;
font: bold 42px arial,helvetica,sanserif;
}
text-align: center;
Then i tried:
putting everything in a paragraph and text-align: center;
setting Margin 0px auto to the paragraph;
putting everything in a div and tried centering but i don't know its width
googled and searched for solutions here...
Nothing works, i can't center the block, please help :-)
Thanks in advance
You could float and relatively position the containers like this:
.container {
overflow: hidden;
}
.row {
position: relative;
left: 50%;
float: left;
}
.header {
position: relative;
left: -50%;
float: left;
}
If you have more content in the container div than just this centered bit, you might wrap the centered bit in another div and move the overflow: hidden rule out of .container and to that div instead.