I am creating a simple site and I found out that I can center <img> tags with text-align: center; but id does not work on block elements like <div> for example. Can someone explain to me how exactly the text-align works and if it's god practice to use it to align images?
Thank you very much in advance :)
UPDATE
I also know about the margin: 0 auto; which in some situations works and in other situations it refuses to center elements.
It totally depends on your requirement, there are various ways to do so, one is that img is an inline element, so setting it to block and using margin: auto; will center your img
img {
display: block;
margin: auto;
}
Demo
Another way is which you are trying to attempt, which needs text-align: center; to be declared on the parent element and not the img tag itself, cuz I doubt you are using text-align: center; for the img tag and not for the parent... Where in the above solution we are targeting the img tag, so there's a difference between the two..
Demo
What's the good point here?
You don't have to make the img a block level element.
No need of margin: auto;
What's the bad point here?
If you have p elements inside the div, they will be centered aligned as well, because the align property is inherited.
Demo
So what to do now? Align the p elements to the left using text-align: left; property, and here we will be targetting p like
This will keep the image centered and will align the p to the left
.wrap {
text-align: center;
}
.wrap p {
text-align: left;
}
Demo
Last but not the least, we can also use CSS Positioning where you can have a parent element set to position: relative; and than we use position: absolute; for the img tag, and than we assign top: 50% and left: 50% and than we deduct the 1/2 of total height and width of your img by using margin-top and margin-left properties respectively. This method comes handy in case where you need the image vertically as well as horizontally centered. (This method will require fixed height and width to be declared)
<div>
<img src="img.png">
</div>
img {
display: block;
margin-left: auto;
margin-right: auto;
}
My advice is to use text-align property for the texts only (of course there could be exceptions). And for centering images you can simply do like this:
img{
display: block;
margin: 0 auto;
}
A 'div', or any block element, without a declared width, can't be centered with margin: 0 auto;, but images will. 'Auto' counts pixels for you and make an element appear in the center
See W3:
http://www.w3.org/TR/CSS21/text.html#alignment-prop
Whether you want to use margin:0 auto; or text-align:center , this really depends on what you're doing exactly and the surrounding markup, context.
I always try to use text-align:center; when possible. I also define images as a block which helps auto margins.
Also a great article by Chris at CSS tricks: http://css-tricks.com/centering-in-the-unknown/
The property text-algin:center basically does what it says. Its supposed to center text. You can either apply directly to the text that you want to center or apply it to the parent div. In this case every text within this div will be centered.
The better practise to center, e.g., images, is to use margin: 0 auto; as many have already suggested. Specifying auto tells the browser to automatically determine the left and right margins itself and to set them equal. It therefore guarantees that both margins have the same size, which is why the object is centered horizontally.
Hope that helps!
Related
I have a box that contains 2 absolutely positioned header and paragraph elements, I am experimenting on ways on how to center text and I tried tinkering using the method from this site.
Now the issue here isn't about ways on how to position text, because that's not my problem so you should not address that.
My problem is this.
How to make the parent element contain it's children?
Since the parent element contains elements that are absolutely positioned, that means the children are not in the flow and in turn the parent collapses.
Now this is a shocker, because I discovered that the containing/wrapping methods used in the same way for floats DO NOT WORK AT ALL.
Clearfix method.
Clearing div method
Overflow method.
They do not work at all!
The only method that decently works is the "Explicit Height" method which is very inefficient, unpredictable (might break on smaller windows) and not fluid.
Here is a simple, fixed-width rounded corner box who's "h1" and "p" elements I'd like to center using the above referred method.
Sample CSS,
body {
margin: 8px;
padding: 0;}
.box {
margin: 0;
padding: 0;
width: 335px;
position: relative;
background: #40331A url(bottom.gif) no-repeat left bottom;}
.inner {
background: url(top.gif) no-repeat left top;}
.box h2 {
width: 180px;
height: 27px;
top: 50%;}
.box p {
width: 120px;
height: 20px;
top: 50%;}
.box h2, .box p {
margin: 0;
position: absolute;}
Sample markup,
<div class="box">
<div class="inner">
<h2>This is a heading.</h2>
<p>This is a paragraph.</p>
</div>
</div>
So my question.
Is there any simple way to make a parent element contain its absolutely positioned children?
The entire point of absolutely-positioned elements is that the parent of those elements should be laid out as if those elements were never there. If you want the layout of your parent element to take into account its children, don't position them absolutely.
Since you're not including any relevant properties other than position: absolute, it seems completely superfluous, given that absolutely-positioned elements default to their static positions anyway. Just remove that declaration. It doesn't get any simpler than that.
The reason none of those methods work is because they are designed for floats, and absolutely-positioned elements are not floats:
You can't really "clear" anything that isn't a float.
Ditto.
The reason setting overflow works for floats is due to a consequence of the way floats participate in block layout, which does not apply to absolutely-positioned elements. What exactly this entails falls outside the scope of this question, but read up on block formatting contexts.
I am able to center horizontal list with text-align:center, but I wonder how can I keep it centered inside container, but has rows aligned left.
My container has percent width, so I need it working when resizing window and blocks are reordering
Please check the sample image below to understand my problem:
UPDATE:
Please find JsFiddle as per request
I need to center my <ul> inside div.container
Use this:
ul {
margin: auto;
}
li {
float: left;
}
See this fiddle:
You already know to center the <ul> with margin: auto;
The key is to adjust the <li> within it.
You can do that by using float: left;
Alternatively: you can set display: inline-block;
Both have a similar effect, but aren't identical. Play w/it.
By providing margins & percentage widths, you can play w/size and separation of the elements.
Since these are all block-level elements, they'll stack up & wrap automatically.
By floating or changing display of the <li> you keep them left-aligned within their parent element (the <ul>).
Also, by using separate CSS classes instead of targeting the <li> element directly, you leave things flexible in case you want to have a right-aligned list, or some other options later.
Wrap your boxes within another div.
You can then center that div with display: block; margin: 0 auto;, while keeping the boxes left-aligned.
I was trying to center text in two adjacent divs and I can not understand what I am doing wrong.
Basically I have 2 divs each taken 50% of the window. The first div contains an image (which I successfully centered) and I am trying to center the text in the second div. So here is my Fiddle and I am using the following css:
.thumbnail-descr{
text-align: center;
min-height: 10px;
display: table-cell;
vertical-align: middle;
font-size: 26pt;
color: #bbb;
}
There is no point of having original DOM structure or CSS (the main thing is to have 2 divs taking all the width and one has a centered image another one has a text. How can I achieve it?
What I understand from the example is that you want to vertically center "Descr". There are several tricks to do that:
Adjust the padding and use box-sizing border-box to have better control of the height.
Use flexbox (still not broadly available): https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Flexible_boxes
If you know before hand that you'll have only one line of text you can use line-height.
(See the update for another option)
For example see this Fiddle:
.square{
width: 45%;
height: 200px;
border: 3px dotted #ddd;
display:inline-block;
margin: 0 10px 0 10px;
line-height: 200px;
}
But take a few things into account:
This will work only if you have one line of text, because on text wrap it will be broken.
This is not the normal use of line height, it's taking advantage of a side effect: text is vertically centered to the line-height.
This trick is used by some CSS frameworks (ie Bootstrap) to center the text on some components.
Update
I forgot another option, since you have one div inside the other you can use position: relative on the parent, and use absolute position for the child using top: 50% and a negative top margin. You'll need to setup the top margin to the half of the child height. (that's how modals are usually centered):
.square { position: relative; /*..*/ }
.thumbnail-descr{
position: absolute;
top: 50%;
margin-top: -10px;
/*...*/
}
See http://jsfiddle.net/diegof79/M4fKM/1/
Also you asked why your solution is not working... this can help to understand the reasons: http://phrogz.net/css/vertical-align/index.html
Before proceeding to giving you the solution, you could have he exact same result with a lot less code, giving so many classes for so little content can only lead to huge code.
The span class you are giving the text-align:center, doesn't fill up the whole width of the parent, so, where would it center the text since its width is equal to the text?
You can either put a 'text-align:center" to the span's parent, the square class, but I would use a different approach in general.
Not sure if you need a span.
If you remove span tag and use same css for the div styles, or simply ad a span class name to a your div class name- works perfectly.
i think the issue happen because the width in the description div
Try this suggestion:
warp the with div, the div will use thumbnail-descr class
<div class='square'>
<div class="thumbnail-descr"><span>Descr</span><div>
</div>
Update the thumbnail-descr
.thumbnail-descr{
text-align: center;
min-height: 10px;
background-color:red;
vertical-align: middle;
font-size: 26pt;
color: #bbb;
width:100%;
}
hope this help
I have to design a circle with two h3 in it. Both of them should line up at the left side but still be centered in the circle.
I already have positioned everything right but I dont know how to left-align both <h3>.
Here is the js fiddle.
Ok I guess I explained it wrong. I want the <h3> lined-up left. But both maybe wrapped up in another element should be centered in the circle referring to the <h3> with the greatest width. But the text in there is a sample text. I cannot give a wrapping element a fixed width and position it with margin: 0 auto.
Wrap the H3 elements in their own div and position it using margin: 0 auto.
Once that is done, you can simply left or right align the text within the div.
eg:
<div class="h3Wrapper">
<h3>Foo</h3>
<h3>Bar</h3>
</div>
CSS:
.h3Wrapper { margin: 0 auto; }
h3 { text-align: left;}
Remove the text-align:center and use margin:auto. Please check http://jsfiddle.net/B3vdz/7/. I hope this is how you want it.
Is this how yout want it?
I used a new div for the text.
#lefft{
width: 90px;
margin: 0 auto;
padding-top: 50px;
position: relative;
}
http://jsfiddle.net/B3vdz/1/
When centering things in html and css, I find two approaches - either applying on the element :
display:block;
margin:0 auto;
or using:
display:inline-block;
text-align:center; (on the parent element)
I always wonder which is better and why. Thanks!!
This is a classic and important question.
Elements can either be inline (meaning they all sit next to each other - like a span tag) or they can be block (meaning the stack up on top of each other - like a div tag).
Margin: auto, while a bit odd when you first see it, is the best and only way to center a block level (position static), element.
For anything that is display: inline (like a span tag) - the only way to center it is if you specify text-align: center on the parent. This will center everything display: inline inside it that is position: static;
Display: inline-block is a hybrid of the two that is relatively new (but supported as far back as ie7 if you use the hack mentioned in another answer). With inline-block, you get the benefits of inline, in that you can you stick a bunch of things next to each other and have them all be centered (think of a nav where all nav items are all centered), but ALSO have each item take advantage of some of the stuff you get with display: block - the most important one being HEIGHT.
Imagine a scenario where each nav item has a background image that is 80px tall - you can't make an inline element have a height of 80 - so you'd have to make each element display: block - only then can you give it a height. So to make them all appear next to each other, you'd float them. The problem is if you float them, you can't have them all be centered on the page unless you give a fixed width to the nav and margin: auto THAT. This means the nav has a fixed width - might be okay, but there are times when the nav has to have dynamic elements, different widths for different languages, etc.
Enter display: inline-block.
Block elements should always be centered using
.block {
margin-left: auto;
margin-right: auto;
width: 600px;
}
as stated by the w3c: http://www.w3.org/Style/Examples/007/center.en.html#block
text-align: center;
is well-named: use it to center texts.
Update
You can also use display: flex now:
.parent {
display: flex;
justify-content: center;
}
.block {
width: 200px;
}
There is no better way in this situation both approach will work and both are corrected. Just one thing display:inline-block doesn't work on IE7 (if you support this browser) you will need a hack to make it work
display: inline-block;
*display: inline;
zoom: 1;