Make the width of outer div to fit inner divs automatically - html

I have 2 inner divs inside an outer div, and I want to make the outer div to automatically fit to the width of the inner divs. Is that possible?
body {
font-size: 0;
}
#outer {
border: 1px solid black;
}
.inner {
font-size: 12px;
display: inline-block;
border: 1px solid red;
}
<div id='outer'>
<div class='inner'>text1</div>
<div class='inner'>text2</div>
</div>

Your outer div is a block-level element. You need to make it an inline-level element. Inline elements automatically take the size of the content it contains. In terms of the question you've asked, just setting :
display: inline-block
on your outer div will do the trick. See the code snippet below for the demo :
body {
font-size: 0;
}
#outer {
border: 1px solid black;
display: inline-block;
}
.inner {
font-size: 12px;
display: inline-block;
border: 1px solid red;
}
<div id='outer'>
<div class='inner'>
text1
</div>
<div class='inner'>
text2
</div>
</div>
Hope this helps!!!

Add "display: table;" to the #outer css:
For example:
#outer {
border: 1px solid black;
display: table;
}
using display: table is less intrusive as using inline

If you add position:absolute; or float:left; to #outer it will size to the two inner div's. For this instance, I would use the float. Floats are generally better for content that might change or expand/shrink with edits over time whereas absolute positioning should be used outside of the document flow or structure of the document, like a nav bar.
Edit: If you don't need other elements to flow around the outer div the display:inline-block method posted below will work, but if you have elements you want to flow around #outer then float:left would be the way to go. Say you have #outer as 50% of the width and want something else on the other half of the page using display:inline-block will put other elements below #outer.
CodePen link to show difference

Related

Div height and position with inner div display inline-block

I'm trying to figure out how is the inner div position, when it's displayed as inline-block and the outer div has line-height set.
<!DOCTYPE html>
<html>
<style>
.outer {
line-height: 20px;
width: 30px;
border: 1px solid black;
}
.inner {
display: inline-block;
width: 20px;
height: 5px;
border: 1px solid green;
}
</style>
<body>
<div class='outer'>
<div class='inner'></div>
</div>
</body>
</html>
https://jsfiddle.net/L4przovt/2/
What is surprising here is the positioning of the inner div. I'd expect it to be on top of the outer div but instead it's somewhere in the middle. Could you explain to me what makes it being positioned in this place?
This is possible duplicate of font-size vs line-height vs actual height but my question seems to be more specific. Please tell me if you think that previous answer should be enough for me.
The .inner div is set to display: inline-block. That activates the vertical-align property, which applies only to inline-level and table cell elements.
The default value for vertical-align is baseline. So that's what you're seeing – the inner div aligns to the baseline of the content.
To fix the problem, add this to your code:
.inner {
vertical-align: top;
}

css relative positioning breaks div into new line

I have the following fiddle for this question: http://jsfiddle.net/jcb9xm44/
There are 2 inline-block div's in a parent div:
<div class="outer">
<div class="inner1">
Y
</div>
<div class="inner2">
X
</div>
</div>
The css assigns a width to the parent div and 2 widths to the child div's.
.outer {
width: 210px;
border: 1px solid red;
}
.inner1 {
width: 200px;
border: 1px solid orange;
display: inline-block;
}
.inner2 {
width: 50px;
position:relative;
left: -50x;
display: inline-block;
border: 1px solid lightblue;}
I was expecting that both divs appear on the same line. Although the parent is not wide enough to hold both children, since the second child has a negative offset, it should be possible to fit them both on the same line. Why does it break the line?
Why does it break the line?
As you stated, it's because the parent element isn't wide enough for both elements. To change this behavior, you could add white-space: nowrap to the parent element in order to prevent the inline-block elements from wrapping.
Example Here
.outer {
width: 210px;
border: 1px solid red;
position:relative;
white-space: nowrap;
}
Side notes:
You had a typo - left: -50x -> left: -50px.
inline elements respect whitespace in the markup.
The element's border is included in its width calculations. Use box-sizing: border-box to include it.
You could alternatively use margin-left: -50px as Mary Melody pointed out.

Force an inline-block to wrap withour a br

I have a div, containing two <a>. The width of the div is larger than the sum of the widths of the two <a>.
I want the two <a> to wrap, but not to take all the div width. I want them to take the width they usually take when set to display:inline-block, but to wrap like if set to display: block.
I need a css only solution.
Here is a jsfiddle to explain my example. Thanks!
<div class="content">
<a>
<span>content1</span>
</a>
<a>
<span>content2</span>
</a>
</div>
Here is one way you might try:
div{
width:300px;
border: 1px solid red;
overflow: auto;
}
a{
display: block;
background-color: #dddddd;
float: left;
clear: both;
}
Float the a to the left and use clear: both.
Apply overflow: auto to the parent div to retain the floats within the block.
See demo at: http://jsfiddle.net/audetwebdesign/cKW4t/2/
I assumed that by wanting the elements to wrap, you mean each starts on a new line.
Not sure exactly what you are asking for but try moving the display:block/inline-block to .content instead of a.
Does that solve your problem?
div{
width:300px;
border: 1px solid red;
display: block;
}
a{
background-color: #dddddd;
}
jsfiddle
Here you go:
a {
display: inline;
background-color: #dddddd;
}
a+a:before {
content:"\a";
white-space: pre;
}
I would like to add that if there is content inside you div, or if you div has a min-height, there might be a better solution.

css : prevent div width from expanding to available width

How can I prevent div from expanding? I want div with elements not to take 100% of available space and have width that it's children have. I need this for centering parent div horizontally. The trick is that child elements should share float:left or diplay: inline-block and fluid width, so there can be few rows of child elements.
I can not wrap each row in its own div since it will break responsive design.
You should use display: table; It will shrink to the size of it's contents and can also be centered and positioning without having to assign a given width.
DEMO http://jsfiddle.net/kevinPHPkevin/9VRzM/
You can set the width property of the children to fit-content. Doing so will make these elements take up only as much horizontal space as they need and is available within the parent.
You can also set width to max-content but this will ignore the width of the parent and content will extend as far as any descendants need and possibly overflow the parent.
Example:
Problem setup:
.parent {
width: 15rem;
height: 5rem;
border: 1px solid blue;
}
.child {
border: 1px solid red;
}
<div class="parent">
<div class="child">
Content for child
</div>
</div>
Solution:
.parent {
width: 15rem;
height: 5rem;
border: 1px solid blue;
}
.child {
width: fit-content;
border: 1px solid red;
}
<div class="parent">
<div class="child">
Content for child
</div>
</div>
Support for fit-content is pretty good (caniuse?). There's support for fit-content on pretty much all the major desktop browsers (except IE), and unknown support on some of the mobile browsers.
If you truly want the parent div to collapse around its child elements (for whatever reason, based on what you're trying to accomplish) and you then want to center that div, then #Vector's answer is spot on, use display: table with margin: 0 auto.
If it's ok for the div to remain expanded to the full width of the container in which you're trying to center your children, then you have at least a couple more options, again depending on your particular situation.
You can use text-align: center.
.content {
text-align: center;
border-style: solid;
border-width: thin;
}
.content span {
display: inline;
border-style: solid;
border-width: thin;
}
<div class="content">
<div>Test</div>
<div>Test</div>
</div>
You could also use the newer display: flex with justify-content: center, depending on the level of browser compatibility you're supporting, of course.
.content {
display: flex;
justify-content: center;
border-style: solid;
border-width: thin;
}
.content div {
border-style: solid;
border-width: thin;
}
<div class="content">
<div>Test</div>
<div>Test</div>
</div>
have you tried using display: inline-block? DIV will take up 100% width because they are block elements.
If you modify DOM adding extra html elements inside a div and that cause it to be expanded, a simple solution would be to add that css to the root element of those extra html elements:
max-width: fit-content;
That will prevent them to expand the parent div's width.

Floated div - fill available width

I have to divs floated, one is on the left, the other on the right. What i want to do (without js) is that the right div fills the available space (width: 100%). The problem is, that the left div has an dynamic width, else I could simply use margin-left.
I also tried display: table-cell; but that won't allow me to use margin, only border-spacing.
Any suggestion?
You can probably do it like this, works in IE8 and better, in FF, in Safari. You could use padding instead of margin, as in this example:
<style>
.c_0 {
display: table;
width: 100%;
border: 4px solid orange;
}
.c_1 {
display: table-cell;
width: 20%;
border: 1px solid red;
padding-right: 20px;
}
.c_2 {
display: table-cell;
border: 1px solid blue;
}
</style>
<div class="c_0">
<div class="c_1">
has a width and padding instead of margin
</div>
<div class="c_2">
has the rest
</div>
</div>
EDIT
This only works with "%" on the first row. I saw it too late, that you want pixels.