Child divs not wrapping to stay inside parent - html

Take a look at this fiddle. http://jsfiddle.net/mstwp/
The html is:
<div id="parent" style="width: 150px; display: inline-block; white-space: nowrap">
<div id="one" class="kids">
<span>Hi</span>
</div>
<div id="two" class="kids">
<span>Bob like to play on his violin</span>
</div>
</div>
with CSS:
#parent {
background-color: #aaaaaa
}
#one {
background-color: #ff0000
}
#two {
background-color: #00ff00
}
.kids {
display: inline-block;
padding: 2px;
white-space: normal;
}
How can I make div #two wrap earlier so that I can keep both child divs inside the parent div?

Just remove the width attached to the parent. It forces the parent div to increase its height to accomodate the content in the div #two. SO keep the parent div as such:
<div id="parent" style="display: inline-block; white-space: nowrap">
And this should do the trick for you.
Alternately, you could add a min-height to the parent div so that it automatically accomodates its width based on the content inside its child. So your parent div could look like this:
<div id="parent" style="min-width: 150px; display: inline-block; white-space: nowrap">
Hope this helps!!!
EDIT: Your child divs already wraps inside the parent div. This is 'revealed' when you add a small width to one of the child div named two.
See that here : http://jsfiddle.net/K2xn5/
If you want you div two to wrap within the parent div, then there's no point in attaching a fixed width to the parent. Instead, you will need to provide a fixed width to the div named two. This would then occupy the space AS DEFINED by the content. Your parent div would then just keep expanding based on the size of its children, in this case, div one and two.
Check out these fiddles :
Fixed width to child, no width to the parent: http://jsfiddle.net/K2xn5/1/
Child div wrapping itself based on the size of its content: http://jsfiddle.net/K2xn5/2/ and http://jsfiddle.net/K2xn5/3/
Hope this helps!!!

Looks to me like both child divs are being wrapped correctly? They're both inside the gray 150px parent box.
BTW this short tutorial helped me a lot with my CSS learning.
http://www.barelyfitz.com/screencast/html-training/css/positioning/

Related

Set same width to multiple vertical elements, according to widest one

I have a container containing multiple elements with a min-width and some padding to the right and left, the problem is in need them centered and in a column with each one in a separate row, the content of each one of them differs, causing the elements to have different width, like this
+--------- container ---------+
|child1 is too long|
|child2|
|child3 is long|
|child4|
+-----------------------------+
how can I make them all have the width of the largest element while maintaining a max-width in the same time, i.e. if the content gets too long, it breaks down to the next line while maintaining the width.
Attached below a screenshot, the above is the current situation, the one below is the desired result
You can wrap the items with a display:inline-block element,
and wrap that element in a text-align:center element so it would be centered:
.centered{ text-align:center; }
.wrapper{
display: inline-block;
font: 24px Arial;
text-align: left;
}
.wrapper > div{
border: 2px dashed pink;
margin-bottom: 4px;
}
<div class='centered'>
<div class='wrapper'>
<div contenteditable>try typing here</div>
<div contenteditable>aaaaa</div>
<div contenteditable>aaaaa aaaaa</div>
<div contenteditable>aa</div>
<div contenteditable>aaaaaaaaa</div>
</div>
</div>
The inner children are block level elements (<div>) which means they will take the whole width of their parent element, where the parent is an inline-block. This will result the parent is as wide as the widest child.

HTML: Text:align property working with span or with a tag

I tried the below code wherein I wanted to center a link , I dont know why these 2 below piece of code didnt work
Code1:
<span class="my-class">
example​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
</span>​
Code2:
example​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
The piece of code which worked was:
<div class="my-class">
example​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
</div>​
Could you please tell me why the above 2 codes didnt work?
The first doesn't because the anchor a is inside an inline element, which just grow to its content's size, and their parent, the body, does not have the property text-align: center set.
The second doesn't because its parent, in this case the body, need to have the rule text-align: center
The third does because the my-class most likely has the text-align property set to center, and as a div is a block element it spawn the full width of its parent, in this case the body, hence the anchor will center inside it.
So, to center an inline (and inline-block) element, its parent need the propertytext-align: center set, and to center a block element, like a div, it has to have a width, less wide than its parent, and its margin's left/right set to auto.
Sample
.centered-span {
text-align: center;
}
.centered-div {
width: 50%;
margin: 0 auto;
}
<span class="centered-span">Hey there (not centered)</span>
<div class="centered-span">
<span>Hey there - span</span>
<div>
<div class="centered-div">Hey there - div</div>
span and a elements do not behavior as blocking element because they are supposed to be used inline. You either will need to set it by setting up a display and width attribute or wrapping it around a parent. Instead, you could use a ul>li>a hierarchy and set their attributes properly.
.aBox {
display: block;
width: 200px;
text-align: center;
background-color: lightyellow;
}
.notBox {
background-color: lightblue;
text-align: center;
}
<span class="aBox">
Hey, it's a link
</span>
<span class="notBox">
Hey, it's a link
</span>
A span element is an in-line element which is only as wide as its content. Whereas a div element is a block level element and will be as wide as the page or its containing div.
When using the `text-align: center;' property, you must place it on the element containing the element that you want to center.

How use CSS to make a child image use percentage with of parent's parent?

I have a child element that is an image. It's floated next to another div inside a parent. I want the image's width to be a percentage of the parent's parent. However, its parent does not have a set width to work off (because the text in the other child div can vary). Is there a way to still use % to set the image's width relative to the grandparent?
<div class="grandparent">
<div class="parent">
<!-- I want this to be 30% the width of grandparent -->
<img class="myImage" src="someimage.jpg" />
<div class="text">Some text here</div>
</div>
</div>
Parent would simply use overflow: hidden; and grandparent has a width of width: 100%; and the two children are both float: left;.
EDIT: The parent div must shrink to the child divs so it doesn't run over sibling divs. That's why it has no width but uses overflow: hidden.
If you set the parent div to 100% it should take the width of the grandparent. That will allow you to properly use 30% on your image.
.grandparent {
width: 400px;
}
.parent {
width: 100%;
}
.myImage {
width: 30%;
}
Fiddle

Horizontal scrolling div without fixed height with all contained divs the height of the tallest child

JSFiddle: Example
I'm looking for a way to have a container div with a height equal to its tallest child.
Every other child should be sized to the height of the container.
I also need the container to scroll horizontally if the children would exceed its width.
So far I have tried using inline-block and float: left but have not gotten the results I want. Setting height: 100% on the children also doesn't have the desired effect. Using overflow: auto on the container still wrapped the last element onto the next line.
I'm pretty confident that I could accomplish this with JavaScript by grabbing the height of the tallest child and resizing appropriately and setting a fixed height on the container. I'd prefer to not use JavaScript though and I think this might be possible just with CSS trickery.
HTML
<div>
<div class='boxed'><ul><li>1</li></ul></div>
<div class='boxed'><ul><li>1</li><li>2</li></ul></div>
</div>
<div class='container'>
<div class='boxed'><ul><li>1</li></ul></div>
<div class='boxed'><ul><li>1</li><li>2</li></ul></div>
<div class='boxed'><ul><li>1</li><li>2</li></ul></div>
<div class='boxed'><ul><li>1</li><li>2</li></ul></div>
<div class='boxed'><ul><li>1</li><li>2</li></ul></div>
<div class='boxed'><ul><li>1</li><li>2</li></ul></div>
<div class='boxed'><ul><li>1</li><li>2</li><li>3</li></ul></div>
<div class='boxed'><ul><li>1</li><li>2</li></ul></div>
<div class='boxed'><ul><li>1</li><li>2</li></ul></div>
<div class='boxed'><ul><li>1</li><li>2</li></ul></div>
<div class='boxed'><ul><li>1</li><li>2</li></ul></div>
<div class='boxed'><ul><li>1</li><li>2</li></ul></div>
</div>
CSS
.boxed { display: inline-block; border: 1px solid black; }
.container { overflow-x: auto; }
For the first issue, you could make the elements display:table-cell as opposed to inline-block. This will cause them to take the height of the tallest sibling element. I'd also suggest adding vertical-align:bottom to the elements in order to align them to the bottom of the parent element. If that's not the behavior you were going for, there is middle, and top too.
UPDATED EXAMPLE HERE
.boxed {
border: 1px solid black;
display:table-cell;
vertical-align:bottom;
}
For the second issue, just add white-space:nowrap to the parent element to prevent the children elements from wrapping. A horizontal scrollbar will be created when the children elements exceed the width of the parent elements.
.container {
border:1px solid red;
white-space:nowrap;
overflow-x: auto;
}

Make multiple variable-height divs inline, yet maintain a fixed width container

Here's the sketch: http://jsfiddle.net/jondum/efVjj/20/
The goal is to have each of those divs on the same line.
If I add a fixed height to each of them it would appear to work, but I would like to avoid setting an explicit height on each element.
So how do I get those buggers all on the same line?
If you want to have them on one line horizontally, you can try to use display: inline-block with white-space: nowrap on a parent, so the blocks would be on one line: http://jsfiddle.net/kizu/efVjj/26/
You've set the width of the parent container at 400px and the three child divs each at 400px.
400 x 3 = 1200. Set the width of the parent container to at least the size of its child elements.
.main-container
{
width: 1200px;
}
One option is to use absolute positioning.
`
<div class="element" style="background:blue;position:absolute;left:0px;">
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
</div>
<div class="element" style="background:green;position:absolute;left:400px;">
<br/><br/><br/><br/><br/><br/><br/><br/>
</div>
<div class="element" style="background:red;position:absolute;left:800px;">
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
</div>
.main-container
{
width: 1200px;
overflow:hidden;
}
.element {
float:left;
width: 400px;
}