Float div vertical align div [duplicate] - html

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Best practice vertical-align center content on div
How can I vertical align center .content for the both squares using same CSS.
Example
Thanks

Here's the solution -> http://jsfiddle.net/Sha6m/12/.
You need to add display: table-cell; to the inner .content, set their vertical-align to middle (vertical-align: middle;) and give them the same height and width of their parent .area (100% width and height unfortunately don't work here, so when you alter the .area's height and width, you also have to do it for the .content div).

style='vertical-align:middle' ..

You're applying display table-cell to the wrong elements. You have to wrap them in a container, and apply display: table cell to that container. Check my example -> http://jsfiddle.net/Sha6m/10/

When u got a fixed height i would just add a line-height: 100px; and remove your vertical-align and display
jsfiddle modification

Related

Unexpected right margin from paragraph in html [duplicate]

This question already has answers here:
What is the difference between block and inline-block with width: 100%?
(2 answers)
Closed 4 years ago.
I'm working on HTML.
I tried to center div in parent.
But there's always right margin extended out of the div.
So, now I changed to just a plain text, but it doesn't solve the problem.
How to fix this one?
That margin is because p is a block element.
Add following CSS.
p.myDiv {
display: inline-block;
}
Instead of margin: 0px;
Try margin:0 auto;
Try this one for your code.
<body>
<p class="mydiv">ok</p>
</body>
.mydiv {
width: 20%;
margin: 0 auto;
}
Paragraph is a block level element which takes the entire width by default. If you give width 20% remaining space will be filled with margin. If you don't want the remaining margin use inline block element. Check out w3scholl website for block and inline block elements.
Centering in CSS: A Complete Guide canu you read CSS TRICKS
Centering things in CSS is the poster child of CSS complaining. Why does it
have to be so hard? They jeer. I think the issue isn't that it's difficult to
do, but in that there so many different ways of doing it, depending on the
situation, it's hard to know which to reach for.

Make Parent DIV Shrink-wrap Contents [duplicate]

This question already has answers here:
Shrink-to-fit div and paragraph, based on image?
(6 answers)
Closed 8 years ago.
I have a div with an img and a p tag. I want the div to "shrink-wrap" the image and let the text organically wrap. However, I'm trying to do this with absolutely no javascript or constraints (ie width=300px / 50% etc) as my frame needs to be fluid.
Here's an example of something similar to what I have now: How can I make the outer dive match the size of the "Google" image without using fixed sizes or javascript?
http://jsfiddle.net/pVF74/
div {
border:1px solid black;
display:table;
width:1%;
}
http://jsfiddle.net/pVF74/2/
Add display: inline-block; to your div's class.
Example here http://jsfiddle.net/r9rLr/

To center 2 div inside a div wrapper without centering all my text

Other than using display: inline-block and text-align:center, do we have anyway to make 2 div center inside a div wrapper? I don't want my text to be center.
Use text-align: center; on the wrapper div and text-align:left; on the child divs.
you can also use the margin: 0 auto; on the child divs instead of using text-aligns. But they will each have to have a width (px or %) and each div will be in its own row.
Using CSS margin is best way of centering a DIV. Add another DIV to hold the two centered ones with:
margin: 0 auto;
Make a 2nd wrapper for the two divs, with a specified width and set it's margin auto.

How to center text and image on one line inside a %width div?

I am really struggling with this and I have no idea why. I want to have text and an image on 1 line and centered inside a 100% width div. Here's a jsfiddle..
http://jsfiddle.net/JnbeJ/
floated elements automatically become block-level. It's impossible to center them via text-align: center. The only way for you to do is to make them inline-block like so: display: inline-block. I added vertical-align: top; for the h to be at the top. The working example is here: http://jsfiddle.net/skip405/JnbeJ/4/
Your image and text can't float left and be centred at the same time...
You have a div that is 100% width (btw/ divs are 100% to begin with), and trying to center a div inside it that is also 100% width. You can either put a width on the inner div, or make it inline-block.
Updated fiddle.
You are using a wrapper with class name "centered" so instead of making both elements (display: inline-block;), just add this to style your wrapper:
.centered {display: inline-block; margin: 0 auto;}
You also have an additional (text-align: center;) in your containers css that does not need to be there.

How can I make a div horizontally center itself in another div? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to center horizontally div inside parent div
I have the following:
<div id="a">
<div id="b">abc</div>
</div>
My first div "a" is styled to be 100% of the screen width. I would like to make div "b" appear horizontally in the center. I know I can do this with tables but is there a way to do this with just div's and CSS?
Hope someone can help
You can set the margin-left and margin-right properties to auto. According to CSS2.1 ยง 10.3.3 you need to specify a width (other than auto) though:
#b {width: 300px; margin: 0 auto;}
you can do that by setting a fixed width to the child div such as and setting left/right margin auto. Like:
div#b{width:40px;margin:0 auto;}
The preferred way to do this would be to set the width of the child div and set the left and right margin to auto. Auto assigns equal amount to both left and right margins.
div#b { width: 100px; margin: 0 auto;}
If you are not sure about the width of the child, you can also assign text-align:center to the parent div. This might not be the best way but gets the job done.
div#a {text-align:center;}