Hi all,
Please help in middle aligning text in a div without using padding.
Technically you're not meant to have text in a div, it should be nested within another container, but the following would work...
<div id="example">
[INDENT]this is my text[/INDENT]
</div>
#example {
[INDENT]width: 200px;
text-align: center;[/INDENT]
}
However you would need a width value, otherwise the div would naturally assume the width of the text.
R.
But with this its only aligning text in horizontal. I want it to centered aligned in vertical also. Here is what I tried with your code.
<html>
<head>
<style>
#ta {
text-align: center;
border: 1px solid #000000;
width: 200px;
height: 100px;
}
</style>
</head>
<body>
<div name='banner' id='ta'>Hello</div>
</body>
</html>
I don't think
#ta {
[INDENT]height: 400px; width: 400px;
text-align: center; vertical-align: middle;[/INDENT]
}
will work on text.
You could try nested divs, giving them both widths and heights, then giving the internal div, margin: auto;
R.
I just wonder why on earth you can't use padding?
Technically you're not meant to have text in a div, it should be nested within another container, but the following would work...
R.
Could you please explain how you are supposed to properly use text in a div.
Thanks,
-Moe
A few points:
- You can center text vertically in a container, but NOT on the browser window. The web is not designed for centering objects on the screen vertically. Web pages start at the top, and grow down enough to display the content. Different browsers, screen resolutions, and window sizes make it impossible to center objects on the screen in a way that works on all computers.
- Don't use pixel counts to define sizes. Use points or percentages.
- Divs are box objects, not text containers. Use p tags for text.
Here is one that really works, even on IE!!!!!! From: css-tricks.com
to set in the exact center of the page(if you change it you can have it vertically or horizontally centered):
div
{
height:200;
width:100;
position: absolute;
top:50%;
left: 50%;
margin-top: -100px;
margin-left: -50px;
}
What css-tricks did was to set the top left corner on the exact center of the page, and then, move it backwards by half its height and width using negative margins.
I would have to disagree with MidiMagic, you can easily return the height of a screen resolution(with a script that can run from the browser) and then use percentages, like Mr.css-tricks.
Reading the screen resolution works only if you have scripting available. If scripting is turned off, it won't center because the script won't run.
Negative margins and other attributes are nonstandard, and do not work the same in different browsers.
Why can't people accept the fact that the web is not intended to have vertical centering on a screen? If it had been intended to have vertical centering, vertical centering would have been provided.
The real question is what happens to the rest of the content if it won't fit on the artificially centered page with a smaller resolution than the design calls for.
Why can't people accept the fact that the web is not intended to have vertical centering on a screen? If it had been intended to have vertical centering, vertical centering would have been provided..
I came up with this method for vertically aligning a div in body:
<html>
<head>
<title>Center Align</title>
<style>
.btable {
position: absolute;
width: 100%;
height: 100%;
border: 1px solid #ccc;
}
#center {
background-color: red;
width: 200px;
}
</style>
<script>
</script>
</head>
<body>
<table class='btable'>
<tbody>
<tr >
<td >
<div id="center">I am middle aligned</div>
</td>
</tr>
</tbody>
</table>
</body>
</html>
PEOPLE WITH SCRIPTS TURNED OFF DO NOT DESERVE TO UISE THE INTERNET!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
(in my humble opinion, of course)
That doesn't work on all browsers, because different browsers interpret the height of the body as a container differently. Some use the height of the content, others use the height of the window, and still others ignore a height attribute entirely when the container is the body.
The btable style won't even be the same in IE and FF. FF will render it 2 pixels larger in all directions than IE will.
PEOPLE WITH SCRIPTS TURNED OFF DO NOT DESERVE TO UISE THE INTERNET!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
(in my humble opinion, of course)
And what about people misusing script capabilities to spread havoc on others machines, what do they deserve?
Every coin has two faces...
Well, that is what you have protection software for; to discern what is a safe script and what isn't, that does NOT mean turning off scripts altogether!
All same here, it doesn't mean you have to produce scripts for every little thing. There are other technologies that can also handle this and often more efficient...
Well anyway the percent script is built in to almost all browsers, and you dont need to write it(even if someones scripting is off percents still work).
<div style=" width:200px; height:50px; display:table; background:#FF0000; text-align:center;">
<div style="display:table-cell; vertical-align:middle;">text middle</div>
I also have the same need. if we want to align <p> at the middle of a <div>, what must we do?
This is probably not important now as this is old. But this is for those who have come across this thread.
You use this CSS code to align text in the middle of the div.
___________________________________________________________________________
line-height: 30px;
___________________________________________________________________________
Change the pixel '30px' accordingly until the text is placed in the middle of the div.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.