css center div not working - html

I want to center the speaker div, which is within the review div. i cannot seem to get it working. :(
HTML:
<div class="review">
<div class="speaker">
<p>SPEAKER DIV</p>
</div>
</div>
CSS:
.speaker{
align:center;
}
This doesn't work :/

Give it a width and margin:0 auto;
<div class="review">
<div class="speaker">
<p>SPEAKER DIV</p>
</div>
</div>
div.speaker{
background-color:red;
width:100px;
margin:0 auto;
}
See it in action!

There’s no such CSS property as align.
When you say you want to “center” the speaker div, what exactly do you mean?
You can center-align its text like this:
.speaker {
text-align:center;
}
(See http://jsfiddle.net/pauldwaite/X7LN5/)
If, alternatively, you want the speaker div to only be as wide as its text, and be positioned in the center of the review div, then you’d need to use this:
.review {
text-align:center;
}
.speaker {
display:inline-block;
}
(See http://jsfiddle.net/wxha4/)

I'm about a year late to the party, but here goes:
In most browsers, this will work:
.speaker{
margin: 0 auto;
}
However, in IE8 and below, the margin:auto will not work (IE8 only if there is no !DOCTYPE declaration. See W3Schools Horizontal-Align Tutorial)
In that case, you can use a combination of text-align: center and width to get the desired effect:
.review{
text-align:center;
}
.speaker{
text-align:left;
width:200px;
margin:0 auto;
}
The downside to this method is that you have to declare a width, or it won't be centered.
Good luck!

It work perfectly.
.speaker{
margin: 0 auto;
text-align:center;
width:100%;
}
good luck

Give the parent a text-align: center;
Then you can move the child anywhere in the parent div
.speaker {
margin: 0 auto;

try
.speaker p{
text-align:center;
}
or
.speaker {
text-align:center;
}

There is no align attribute in CSS. Set the horizontal margins to auto to centre a block. See Centring using CSS for more details (including work arounds for Internet Explorer issues)

Thats because that syntax does not exist. You need to center the div via the margin attribute.
.speaker{
margin:0px auto 0px auto;
}
DEMO http://jsfiddle.net/t4kBj/

Related

Centering a form in css

I have a form that I cannot center. I have tried many things including :
form{
display:inline-block;
text-align:center;
margin:auto;
}
the dang thing won't center though. help please?
URL: http://s1527.mtchs.org/wordpress/contact/
form {
width: 50%; /* adjust as needed */
margin: 0 auto;
}
Block-level elements are centred by setting the left and right margins to the same value - in general the easiest way to do this is to set them both to auto. text-align on the other hand, applies to inline elements inside the element you apply the style to.
See here for examples.
Playing around with margins should work. Set a top/bottom margin for your element and the rest auto and it should center it automatically.
margin: 100px auto 0 auto;
if you wanna use display: inline-block and text-align combination, then you have to put text-align in the parent element
example
<div style="position: relative; display: block; text-align: center">
<div style="display: inline-block">
</div>
OR
you can simply put width in the from css
form{
display:block;
margin:auto;
width:600px;
}

I am having problems centering a scroll div

I'm trying to place a div that scrolls. I want it dead center on the page but it's not doing it with the code I provided below. Please help.
CSS
#content
{
text-align: center;
}
.scroll
{
background-color:#fff;
color:#000;
width:500px;
height:400px;
overflow:scroll;
}
HTML
<div id ="content">
<div class="scroll"> Stuff </div>
</div>
A div is a block level element and will not listen to text-algin. You will either need to use margin: 0 auto on the .scroll element or make the div an inline-block element. Though support for block level elements to be inline-block level elements is not totally supported so you would have to use a span for complete support. However the better option is if your div has a set width, use a left and right margin of auto on the element you want to center.
text-align only affects text. To position a <div> in the center, use
margin-left:auto;margin-right:auto.
try this
HTML
<div id ="content">
<div class="scroll"> Stuff </div>
</div>
CSS
#content
{
text-align: center;
margin-left:auto;
margin-right:auto;
width:300px;
height:200px;
overflow: scroll;
}
.scroll
{
background-color:#fff;
color:#000;
width:500px;
height:400px;
}
live fiddle here
You can add display:inline;margin:auto to your <div>.

HTML relative centre align

I'm trying to centre align an image using a div and a CSS class (the tag is wrapped in a div class="center" tag). What I'm using at the moment is working in Dreamweaver (when I go to design view) but not when I load the page up in Safari. Here is my code:
.center {
display:inline;
text-align:center;
-webkit-inline;
-webkit-center;
background-color:transparent;
}
Sorry for asking such a simple question, I'm completely new to HTML, my experience is in Objective-C.
text-align: center caused the content to be centered (within the container), and not the container itself being centered.
Since you use display: inline, the size of the container will be the same as its content, and the centering will not have the effect you're after.
Either, you use the margin: 0 auto to center the container (towards its parent container), OR you change display: inline to display: block.
Give text-align:center; to it's .center parent DIV. Write like this:
HTML
<div class="parent">
<div class="center"></div>
</div>
CSS
.parent{
text-align:center;
}
.center {
display:inline;
background-color:transparent;
}
You can use margin : 0 auto , to a vertical align , but if you want a vertical-horizontal align , you need a code like this:
.center{
width:200px;
height:200px;
margin-left:-100px;
margin-top:-200px;
position:absolute;
top :50%;
left:50%;
}
margin: 0 auto. Is what you're looking for. You'll need a width also.
div.center { margin:0 auto; width: 20%;background:orange;}

Centering a div within a div

I know it should be a fairly easy thing to do but for some reason I'm having issues with it and not sure why. I'm adding a controller under the gallery that you can use to scroll through the slides and need to it be centered.
This is what the code looks like ATM:
<div class="control_wrap" style="">
<div class="gallery_controls" style="">
<a target="_blank"></a> <a target="_blank"></a> <a target="_blank"></a>
</div>
</div>
CSS:
.control_wrap{
float:left;
width:100%;
}
.gallery_controls{
margin:0 auto;
float:left;
width:50%;
}
Let me know if you need anymore information.
Thanks.
You have to get rid of the float: left on .gallery_controls. margin: 0 auto won't work otherwise.
Try to remove float left from this class:
.gallery_controls{
margin:0 auto;
/*float:left;*/
width:50%;
}
Change your CSS to something like this:
.control_wrap {
float:left;
width:100%;
}
.gallery_controls {
margin:0 auto;
float: left;
text-align:center;
width:50%;
}​
Result http://jsfiddle.net/AmAMP/1/
I have updated see your CSS please check it:-
.gallery_controls{
margin:0 auto;
width:50%;
height:50px;
background:red;
}
or you can see the live example for easier understanding :-
http://jsfiddle.net/cPk53/5/
in order for margin:0 auto; to work, you have to specify a width on that div and omit the float.
If you also want to vertically center your div, you need either position:absolute on the div, or display:table-cellon the parent div with vertical-align:middle.
Something like this:
parent{
position:relative;
}
child{
position:absolute;
top:50%;
left:50%;
width: <your-width>;
height: <your-height>;
margin-left: <your-width> / 2;
margin-top: <your-height> / 2;
}
Example fiddle
Use Tables instead. With div the look and feel would vary from browser to browser. The moment you feel that you have done it, loading in another browser or an older version of the browser will break your illusion.

Center an <h1> tag inside a <div>

I have the following <div> inside a <body> tag:
<div id="AlertDiv"><h1>Yes</h1></div>
And these are their CSS classes:
#AlertDiv {
position:absolute;
height: 51px;
left: 365px;
top: 198px;
width: 62px;
background-color:black;
color:white;
}
#AlertDiv h1{
margin:auto;
vertical-align:middle;
}
How can I vertically and horizontally align an <h1> inside of a <div>?
AlertDiv will be bigger than <h1>.
You can add line-height:51px to #AlertDiv h1 if you know it's only ever going to be one line. Also add text-align:center to #AlertDiv.
#AlertDiv {
top:198px;
left:365px;
width:62px;
height:51px;
color:white;
position:absolute;
text-align:center;
background-color:black;
}
#AlertDiv h1 {
margin:auto;
line-height:51px;
vertical-align:middle;
}
The demo below also uses negative margins to keep the #AlertDiv centered on both axis, even when the window is resized.
Demo: jsfiddle.net/KaXY5
On the hX tag
width: 100px;
margin-left: auto;
margin-right: auto;
There is a new way using transforms. Apply this to the element to centre. It nudges down by half the container height and then 'corrects' by half the element height.
position: relative;
top: 50%;
transform: translateY(-50%);
-webkit-transform: translateY(-50%);
-ms-transform: translateY(-50%);
It works most of the time. I did have a problem where a div was in a div in a li. The list item had a height set and the outer divs made up 3 columns (Foundation). The 2nd and 3rd column divs contained images, and they centered just fine with this technique, however the heading in the first column needed a wrapping div with an explicit height set.
Now, does anyone know if the CSS people are working on a way to align stuff, like, easily? Seeing that its 2014 and even some of my friends are regularly using the internet, I wondered if anyone had considered that centering would be a useful styling feature yet. Just us then?
<div id="AlertDiv" style="width:600px;height:400px;border:SOLID 1px;">
<h1 style="width:100%;height:10%;text-align:center;position:relative;top:40%;">Yes</h1>
</div>
You can try the code here:
http://htmledit.squarefree.com/
Started a jsFiddle here.
It seems the horizontal alignment works with a text-align : center. Still trying to get the vertical align to work; might have to use absolute positioning and something like top: 50% or a pre-calculated padding from the top.
You could add padding to the h1:
#AlertDiv h1 {
padding:15px 18px;
}
You can use display: table-cell in order to render the div as a table cell and then use vertical-align like you would do in a normal table cell.
#AlertDiv {
display: table-cell;
vertical-align: middle;
text-align: center;
}
You can try it here:
http://jsfiddle.net/KaXY5/424/