Making a div use all the space available in a container - html

I have this container div with two elements:
<div id="container">
<div id="right">Some, list, of, words, that, I, use</div>
<div id="left">Person Name</div>
</div>​
And I want to make the #right div use up the space that is left by the #left div and hide the overflow.
Please also note that the #left div width is variable.
I came up with this css so far:
#container {
width: 250px;
border: 1px solid black;
padding: 10px;
}
#left {
white-space: nowrap;
border: 1px dotted #ccc;
display: inline;
}
#right {
text-overflow: ellipsis;
overflow: hidden;
border: 1px dotted #ddd;
white-space: nowrap;
display: inline;
float: right;
}
​
But the #right div still shows all text and end up on the following line, like displayed below:
I played with making the #right div not float, adding width: 100% but nothing seems to work...
Does anyone know how I can make both be displayed on the same line, but making the #right div have its overflow hidden?
jsFiddle link: http://jsfiddle.net/y4xnx/13/
Edit: fixed my explanation of which divs should do what.

It would help to put #left before #right:
<div id="container">
<div id="left">Person Name</div>
<div id="right">Some, list, of, words, that, I, use</div>
</div>​
Then a few changes in the css should do what you're wanting:
#left {
white-space: nowrap;
border: 1px dotted #ccc;
float: left;
margin-right: 5px;
}
#right {
text-overflow: ellipsis;
overflow: hidden;
border: 1px dotted #ddd;
white-space: nowrap;
}
Example: http://jsfiddle.net/grc4/MvXuN/

I tried to achieve whatever I understood from your question. Check the fiddle here: http://jsfiddle.net/y4xnx/20/
HTML:
<div id="container">
<div id="left">Person Name</div>
<div id="right">Some, list, of, words, that, I, use</div>
</div>​
CSS:
#container {
width: 250px;
border: 1px solid black;
padding: 10px;
white-space:nowrap;
overflow:hidden;
}
#left {
border: 1px dotted #ccc;
display: inline-block;
}
#right {
display:inline-block;
border: 1px dotted #ddd;
}
​

Related

Make right and left cells occupy minimum widths to display their contents?

I have some html as given in code below.
I am using display:table and display:table-cell for laying out the divs in a table-like manner without using html table. This works fine except that the widths of left and right cells are much bigger than the width of their contents.
A demo for this question is at following URL: http://js.do/sun21170/87371
Question: What CSS I can use to make the left and right cells automatically resize to their contents? I do not want to specify the widths of these cells.
I tried to solve this problem by setting the center div width to 100% which did what I was after but then the margins of left and right divs are not respected. So may be there is a better solution that I am missing!
HTML Markup
<script></script>
<style>
#rgcmd {
width:100%;
display:table;
position:relative;
border:solid 1px green;
}
.leftCell {
margin-right: 5px;
height:100%;
display:table-cell;
vertical-align:middle;
border-right:solid 1px red;
}
.rightCell {
margin-left:
5px;height:100%;
display:table-cell;
vertical-align:middle;
border-left:solid 1px red;
text-align:right;
}
</style>
<div id="rgcmd">
<div class="leftCell">Go Left</div>
<div style="display:table-cell; vertical-align:top;">
<span id="status">Your status is offline</span>
</div>
<div class="rightCell">Go Right</div>
</div>
Add white-space: nowrap to the left and right cells and width: 100% to the center cell:
#rgcmd {
width: 100%;
display: table;
position: relative;
border: solid 1px green;
}
.leftCell {
padding-right: 5px;
height: 100%;
display: table-cell;
vertical-align: middle;
border-right: solid 1px red;
white-space: nowrap;
}
.rightCell {
padding-left: 5px;
height: 100%;
display: table-cell;
vertical-align: middle;
border-left: solid 1px red;
text-align: right;
white-space: nowrap;
}
.centerCell {
width: 100%;
display: table-cell;
vertical-align: top;
}
<div id="rgcmd">
<div class="leftCell">Go Left
</div>
<div class="centerCell">
<span id="status">Your status is offline</span>
</div>
<div class="rightCell">Go Right
</div>
</div>

Stack divs vertically with different child alignment

http://s4.postimg.org/mbrpxn2d9/Untitled.png
Edit: Not a duplicate. The other question doesn't contain information about divs being automatically adjusted to the words on the inside.
I have 4 divs. I have 3 divs inside another div, and I'm trying to float one to the left, one to the center, and one to the right. I'm also trying to make the width and height of the divs on the inside to be automatically adjusted to the width and height of the words on the inside of the divs. I also want the divs on the inside to stack up on top of each other, instead of being on the same line. So far, I got the left div to float to the left, and the right div to float to the right, but I just cannot get the middle div to be centered, nor get it to adjust to the width and height of the word inside of it. Please take a look at my code:
#outer {
border: 1px solid black;
width: 500px;
height: 500px;
}
#innerLeft {
border: 1px solid red;
float: left;
}
#innerMiddle {
border: 1px solid red;
margin: auto;
}
#innerRight {
border: 1px solid red;
float: right;
}
<div id='outer'>
<div id='innerLeft'>Left</div>
<div id='innerMiddle'>Middle</div>
<div id='innerRight'>Right</div>
</div>
Depending on the output of the image, I think flexbox solution would be a good way to go.
Let the container have a flexible layout with column wrapping.
Align each item based on position in the container i.e. flex-start, center and flex-end
#outer {
display: flex;
display: -ms-flex;
flex-flow: column wrap; /* Wrap the items column wise */
justify-content: flex-start; /* Items to start from the top of the container */
border: 1px solid black;
width: 500px;
height: 500px;
}
#innerLeft {
align-self: flex-start; /* Equivalent to float: left of your code */
border: 1px solid red;
}
#innerMiddle {
align-self: center; /* Equivalent to margin: auto */
border: 1px solid red;
}
#innerRight {
align-self: flex-end; /* Equivalent to float: right */
border: 1px solid red;
}
<div id='outer'>
<div id='innerLeft'>Left</div>
<div id='innerMiddle'>Middle</div>
<div id='innerRight'>Right</div>
</div>
If changing your HTML just a bit is an option, you can add span elements in your divs which will give you want, and it will work in all browsers:
#outer {
border: 1px solid black;
width: 500px;
height: 500px;
}
#innerLeft {
text-align:left;
}
#innerMiddle {
text-align:center;
}
#innerRight {
text-align:right;
}
div > div > span {
border: 1px solid red;
}
<div id='outer'>
<div id='innerLeft'><span>Left</span></div>
<div id='innerMiddle'><span>Middle</span></div>
<div id='innerRight'><span>Right</span></div>
</div>
This is what you mean?? I had Edited
#outer {
border: 1px solid black;
width: 500px;
height: 500px;
}
#innerLeft {
border: 1px solid red;
/* width: 30%; */
float: left;
}
#innerMiddle {
border: 1px solid red;
float: left;
margin: 0 5px;
}
#innerRight {
border: 1px solid red;
float: right;
}
<div id='outer'>
<div id='innerLeft'>LeftLeftLeftLeft</div> <br>
<div id='innerMiddle'>MiddleMiddleMiddleMiddle</div> <br>
<div id='innerRight'>RightRightRightRight</div>
</div>
write your html tags like this hope it help!
<div id='outer'>
<div id='innerRight'>Right</div>
<div id='innerLeft'>Left</div>
<div id='innerMiddle'></div>
</div>

How do I make a div extend its width beyond the parent's width?

I have a wrapper div and a content div.
<style type="text/css">
#wrapper {
width: 500px;
border: 1px solid red;
overflow: auto;
}
#content {
border: 1px solid blue;
}
.column {
display: inline-block;
width: 300px;
}
</style>
<div id="wrapper">
<div id="content">
<div class="column">hello</div>
<div class="column">world</div>
</div>
</div>
The columns appear on two rows instead of the same row. If I style then with display: table-cell they each take up 50% of the width, but still won't expand the content beyond the wrapper's width.
How do I make the content div expand to fit both columns on the same row and cause scrolling on the wrapper div?
if you change you styles to the following you will achieve what you want:
#wrapper {
width: 500px;
border: 1px solid red;
overflow: auto;
}
#content {
display: inline-block;
border: 1px solid blue;
white-space:nowrap;
}
.column {
vertical-align:top;
display: inline-block;
width: 300px;
white-space:normal;
}
Example
Ohh I think I see the problem now, try:
#wrapper {
width: 500px;
border: 1px solid red;
overflow: auto;
}
#content {
white-space:nowrap;
border: 1px solid blue;
}
.column {
display: inline-block;
width: 300px;
}
http://jsfiddle.net/krowe/z3TS8/
In .column if you use display:table-cell, the column will be aligned in the same row as you need. And its working fine for me. Hope this is what you are looking for JSFiddle
#wrapper {
width: 500px;
border: 1px solid red;
overflow: auto;
display:table;
}
#content {
overflow: hidden;
display:table-row;
}
.column {
width:50%;
border: 1px solid blue;
display:table-cell;
}

centering a CSS div, having problems with the middle

http://codepen.io/willc86/pen/hpFLe
Hey guys I have a code pen link on top so you guys can see it. I am pretty much having problems centering the middle box. How do I do that. When I do center it, the middle box seems to favor one side when I zoom out of the browser
this is my code
#box{
border: 3px solid red;
}
#space{
text-align: center;
}
#leftcolumn {
width: 300px; border: 1px solid red; float: left; margin: 40px;
margin-right: 20px;
}
#rightcolumn {
width: 300px; border: 1px solid red; float: right;
margin: 40px; margin-left: 20px;
}
#mcolumn {
width: 300px; border: 1px solid red; float: left; margin: 40px;
}
.clear {
clear: both;
}
and my HTML
<div id="box">
<div id="space">
<div id="leftcolumn"><p>LEFT</p></div>
<div id="rightcolumn"><p>RIGHT</p></div>
<div id="mcolumn"><p>mcolomn</p></div>
<div class="clear"></div>
</div>
</div>
Middle block sticks to one side because of the "float: left" rule. To be centered it needs no float. You can just add 'auto' horizontal margin without any float and it will work fine.
Here is modified example: http://codepen.io/anon/pen/pitod
(there's a trick with top padding for parent container to avoid problems with top margins, but you can solve that however you like)
hope it will help you, #mcolumn is centered now
#mcolumn {
width: 300px;
border: 1px solid red;
margin: 40px auto;
display: inline-block;
}
Demo

CSS Problems with div at the bottom

I have a div that is at the bottom of my page. It's CSS is:
#news-bottom {
color: white;
position: fixed;
white-space: nowrap;
overflow: hidden;
height: 66px;
bottom: 0;
right: 390px;
left: 180px;
padding: 5px;
border: 1px solid #CCC;
background-color: black;
}
And I have the div's content like on the image:
content html:
<span>
<span><b>Teste</b></span>
<span>Teste com BBCodes</span>
<img style="border: 1px solid #CCC; padding:2px; margin-left: -3px;" src="images/news/empty.png">
</span>
How do I make for my div's content show up something like this:
I can't use line breaks or tables, because the div's position is fixed, and now I don't know what to do...
Something like this JSFiddle should work.
What we do is apply the background-color to the children of the container div. Then we give them some padding, which creates the space between them. Voila!
HTML:
<div id="news-bottom">
<span>
<span><b>Teste</b></span>
<span>Teste com BBCodes</span>
<img style="border: 1px solid #CCC; padding:2px; margin-left: -3px;" src="images/news/empty.png">
</span>
CSS:
#news-bottom {
color: white;
position: fixed;
white-space: nowrap;
overflow: hidden;
height: 66px;
bottom: 0;
right: 390px;
left: 180px;
padding: 5px;
border: 1px solid #CCC;
}
#news-bottom span span,
#news-bottom span img {
background-color: black;
padding: 5px;
}​​
If you want fixed position, you can make several divs and change their left property to what you like. You can use % positioning so they look almost the same on different resolutions.
But, I'd recommend using float in a fixed container <div>, and combine with #jmeas suggestion of margins. Something like this:
HTML
<div class='container'>
<div class='arrow'></div>
<div class='item'>One</div>
<div class='item'>Two</div>
<div class='item'>Three</div>
<div class='arrow'></div>
</div>
CSS
#container {
...fixed...
}
.arrow {
float:left;
width:10%;
}
.item {
float:left;
margin-left:5px;
width:20%;
}