change proportions of 3 columned display: table / table-cell - html

I have this simple setup:
.container {
display: table;
width: 70%;
text-align: center;
}
div {
border: 1px solid #336;
}
.column {
display: table-cell;
}
<div class="container">
<div class="column">Column 1.</div>
<div class="column">Column 2 is a bit longer.</div>
<div class="column">Column 3.</div>
</div>
jsFiddle: http://jsfiddle.net/aqk1yy1d/
This table-cell behavior expands with window resize. I would like the center cell/div to be fixed to its content and not expand. Basically the sides should expand but not the inner cell, wich should be the size of its content.
I don't see how I can do this without setting a defined width somewhere, but that in not ok, because I will have different length of content in that middle cell....
Any pointers?

The trick is to set both the left and right column to take up 50% of the width of the table. The center column gets a width of 1px. If there is content larger than 1px in the center column it will force the center column to grow.
The first example only has text inside it, which will wrap at the first moment. To mitigate this add something like white-space: nowrap to keep all text on a single line or make sure that you have content with a width.
.container {
display: table;
width: 70%;
text-align: center;
margin-bottom: 10px;
}
div {
border: 1px solid #336;
}
.column {
display: table-cell;
}
.left,
.right {
width: 50%;
}
.center {
width: 1px;
}
.center-content {
white-space: nowrap;
}
<div class="container">
<div class="column left">Column 1.</div>
<div class="column center">Column 2 is a bit longer.</div>
<div class="column right">Column 3.</div>
</div>
<div class="container">
<div class="column left">Column 1.</div>
<div class="column center"><div class="center-content">Column 2 is a bit longer.</div></div>
<div class="column right">Column 3.</div>
</div>

If you can't find a better solution, you could try using javascript to set the width dynamically. Change your html to something like this:
<div class="container">
<div class="column">Column 1.</div>
<div id="column2Outer" class="column">
<div id="column2Inner" style="display: inline-block">Column 2 is a bit longer.</div>
</div>
<div class="column">Column 3.</div>
</div>
The javascript would be as follows:
$("#column2Outer").css("width", document.getElementById("column2Inner").clientWidth);
You would call this on $(document).ready() or whenever the content changes. You would of course also have to remove the border from the inner column so you can't tell it's a nested div

Related

How to align items in straight line using Flex and Space-between

As can be seen in the image, when the width of first column increase, it pushes the contents of the second column also. I want the items of second column in straight line.
One way of doing could be giving fixed width to first column, but I don't want to do this because, the count of columns is dynamic. Sometimes, there will be only three columns, so I will to increase width dynamically which I don't want to do.
#box {
display: flex;
width: 100px;
margin: 0 -5px;
}
.item {
background: gray;
width: 50px;
height: 50px;
margin: 0 5px;
}
<div id='box'>
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
</div>
Demo: http://jsfiddle.net/GLpUp/
You don't have to set an exact 'fixed' width, but try to set the width 'proportionally' with %.
When all divs have 100% they will all have the same width.
If you know your second column doesn't contain much content, simply reduce the percentage. Like this all columns will have the same width as their neighbours above or below them.
This would be a solution using flexbox. I agree with the comments, that using grid or table instead would be advisable.
#row {
display: flex;
width: 100%;
}
.item {
width: 100%;
height: 50px;
border: 1px solid teal;
}
.item:nth-of-type(2) {
width: 30%;
}
<div id='row'>
<div class='item'>I have a lot of content</div>
<div class='item'>just 1</div>
<div class='item'>...</div>
<div class='item'>...</div>
</div>
<div id='row'>
<div class='item'>This is a different length</div>
<div class='item'>but</div>
<div class='item'>the columns have the same</div>
<div class='item'>widths as in row before</div>
</div>

Give border bottom on the second div [duplicate]

This question already has answers here:
Fill the remaining height or width in a flex container
(2 answers)
Closed 3 years ago.
I have two DIV-s, which must fill the space of their upper div. Size of the first div can differ - depends on the text inside the div. What I want is to show dots to the end of remaining space.
My solution:
.drugi {
border: 1px dashed black;
}
<div>
<div>this is testing</div>
<div class="drugi"></div>
</div>
Problem is output:
Dots are occupying the whole space. What I would like it to be is this:
Please note: I've already tried solutions like this and this. My question differs, because I don't have fixed width of either DIV-s, so second DIV must simply fill remaining space with dots, without overlapping the first DIV.
You can use flexbox.
.drugi{
border-bottom: 1px dashed black;
flex: auto;
}
.d-flex {
display: flex;
}
<div class="d-flex">
<div>this is testing</div>
<div class="drugi"></div>
</div>
<div class="d-flex">
<div>this is</div>
<div class="drugi"></div>
</div>
Use Flex
.outer{
display:flex;
}
.drugi{
border-bottom:1px dashed black;
flex:1;
}
<div class="outer">
<div>this is testing this is testing</div>
<div class="drugi"></div>
</div>
Try This:
HTML:
<div class="Table">
<div class="row">
<div class="Table_title">this is testing</div>
<div class="Table_border"></div>
</div>
</div>
CSS:
.Table .row {
display: table;
padding-bottom: 10px;
}
.Table_title{
display: table-cell;
white-space: nowrap;
}
.Table_border{
border-bottom: 1px dashed black;
width: 100%;
display: table-cell;
}

Put and align text with the first div of three (flexbox)

I'm trying to make this layout (I've made a picture to explain what i want to do):
So I've 4 divs where I'm going to put some text inside. I've used flexbox and justify content to align them center, but i want to put a text "Latest News" that is aligned with the first div (in this case Element 1).
I'm not able to think about an elegant solution to my problem, so I'm here to ask for help.
.wrapper{
display: flex;
justify-content: center;
}
.box{
height: 200px;
background-color: red;
margin: 10px;
width: 200px;
}
<p class="section-title">Latest News</p>
<div class="wrapper">
<div class="box">Element 1</div>
<div class="box">Element 2</div>
<div class="box">Element 3</div>
<div class="box">Element 4</div>
</div>
There are a few ways you can do it, and it depends how dynamic your box elements are going to be.
One simple solution that works for n boxes is to include the section title to the first box and give it position: absolute whilst adding margin-top to the wrapper to make space for the title.
https://codepen.io/anon/pen/MJpOrM
.wrapper {
display: flex;
justify-content: center;
margin-top: 40px;
}
.box {
height: 200px;
background-color: red;
margin: 10px;
width: 300px;
}
.section-title {
position: absolute;
top: 0px;
}
<div class="wrapper">
<div class="box">
<p class="section-title">Latest News</p>
Element 1
</div>
<div class="box">Element 2</div>
<div class="box">Element 3</div>
<div class="box">Element 4</div>
</div>
Considering that you have a fixed width for your boxes, the easiest solution is to make the section-title a fixed width too:
.section-title {
width: 1260px; //This is merely 300 * 4 + the margin
margin: auto;
}
https://codepen.io/anon/pen/BpWmJV

Why is div clearing?

I have two divs with a float:left property and some width to them. They are wrapped in a container which has an overflow: hidden property.
When I change the size of my browser to be smaller in the horizontal direction I want a horizontal scroll bar to appear when the width of the window is too narrow for to display the two divs side by side. But what is happening is that the div is clearing down underneath the first div.
How can I make it so that the div does not go down when the window is too small and instead will always stay in the same row as the first div and a horizontal scroll bar will appear?
Here is my html:
<div class="col-container">
<div class="col col-1">
Content 1
</div>
<div class="col col-2">
Content 2
</div>
<div class="col col-3">
</div>
</div>
and here is my css:
.col-container{
overflow: hidden;
}
.col {
float: left;
}
.col-1{
width: 30%;
margin-right: 30px;
}
.col-2{
background-color: blue;
}
and here is a fiddle with this html and css.
To make sure the two-cols don't clear you must set a minimum-width to their container:
.col-container{
overflow: hidden;
min-width: 150px;
}
Here is your jsfiddle with the above update:
https://jsfiddle.net/3s963o9o/2/
Another option is not to use float, but to change the structure to flexbox.
use flexbox for that instead
.col-container {
display: flex
}
.col {
flex: 1;
}
.col-1 {
flex: 0 30%;
margin-right: 30px;
background: red;
}
.col-2 {
background-color: blue;
}
<div class="col-container">
<div class="col col-1">
Content 1
</div>
<div class="col col-2">
Content 2
</div>
<div class="col col-3">
</div>
</div>

Expanding line to full width with only CSS

Maybe someone has any bright ideas how to replace my current solution to one of the problems via only CSS? I have a working JS solution, just curious if there is a CSS-only one.
A) Elements here have the same width, only the number of elements changes (can be more or less).
B) The line should expand to full width that is empty left here (the problem is here).
C) Text is dynamic (always other width).
Is it possible to set the B) element, so it would fill the width?
Yes it is possible with Flexbox, you can just set flex: 1 on lines and they will take rest of free space.
.content,
.a {
display: flex;
align-items: center;
}
.box {
width: 50px;
height: 50px;
margin: 5px;
background: red;
}
.line {
flex: 1;
border-bottom: 1px dashed black;
}
<div class="content">
<div class="a">
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
</div>
<div class="line"></div>
<div class="text">Lorem ipsum.</div>
<div class="line"></div>
<div class="text">Text</div>
</div>