My Goal is to have a DIV that contains other fixed sized DIVs. Those DIVs should contain a line break (only) if necessary. So it should look like the following:
No line breaks necessary:
_______________________________
| ____ ____ ____ |
| |__| |__| |__| |
| |
| |
|_____________________________|
Line break necessary:
_______________________________
| ____ ____ ____ ____ |
| |__| |__| |__| |__| |
| ____ |
| |__| ... |
|_____________________________|
Currently, the best I could come up is the following:
<div id="outer">
<div>
<p>Some text</p>
</div>
...
</div>
<style>
#outer {
overflow: auto;
}
#outer div {
display: inline;
border: 1px solid black;
}
#outer div p {
width: 60px;
height: 60px;
Display: inline-block;
}
</style>
Does anybody have an idea on how to achieve this?
Hope this will help you and resolve your query, let me know any issues
#thomas
#outer {
overflow: auto;
border:1px solid #ff0000;
padding:10px;
}
#outer div {
display: inline-block;
border: 1px solid black;
margin:0px 1% 2% 1%;
}
#outer div p {
width: 60px;
height: 60px;
margin:0px;
display: inline-block;
}
<div id="outer">
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
...
</div>
Try like this: Demo
#outer div {
display: inline-block; / Use inline-block */
border: 1px solid black;
}
#outer div p {
width: 60px;
height: 60px;
}
here your code is working fine i have tried to change the display inline-block to the div to make the block in a row
#outer {
overflow: auto;
}
#outer div {
display: inline-block;
padding: 5px;
border: 1px solid #808080;
text-align: center;
line-height: 32px;
margin: 15px;
}
#outer div p {
width: 60px;
height: 60px;
}
<div id="outer">
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
<div>
<p>Some text</p>
</div>
</div>
and here is the demo working code for this
Demo code
Related
I need a responsive CSS grid, containing at most three columns and at least one, to have equal space around each column. This is easily done, with just one row, with flexbox and justify-content: space-evenly as seen here:
Even Spacing Using FlexBox
But I need multiple rows. So how can I get even column spacing with css grid?
Here is what I have currently with a grid. The far left and right margins are half the size of the inner margins
No Even Spacing With Grid
I've thought about using a flexbox for each row in the grid but the idea sounds like it could be more work than it's worth. Thanks
JSFiddle
Equal space between the images vertically and horizontally
The images aren't the direct children of the grid/flex container so some adjustments need to be made to make the images take full width/height of their parents.
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
img {
/* at least fill the parent */
min-width: 100%;
/* don't exceed the parent */
max-width: 100%;
}
a,[item] {
display: inline-block;
}
/* Not needed */
body * {
padding: 10px;
border: 1px solid;
}
<div container>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
</div>
CSS Grid
Using css grid it should be easy to have 3 items per row and have even space all around.
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
img {
min-width: 100%;
max-width: 100%;
}
a,[item] {
display: inline-block;
}
/* Solution */
[container] {
width: 100%;
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-gap:20px; /* even spacing using grid-gap, You can use percentage values if you want*/
padding:20px;
}
[item]{
padding: 10px;
background:red;
}
/* Not needed */
body * {
padding: 10px;
border: 1px solid;
}
<div container>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
</div>
Flexbox
Tricky requires a bit of care
First we need 3 items per row we can just say that like css grid so we calculate, we give each item a third of the overall width of the parent flex-basis:calc(100% / 3);
Second we add the space around using margins say margin:20px, Now the tricky bit is that margins add to width of the item so we need to substract that space from the overall width of the container then calculate the third which will become flex-basis(100% - (20px * 6)) / 3);
6 because each element will have 2 margins left/right 2 x 3=6
* {
padding: 0;
margin: 0;
box-sizing: border-box;
}
img {
min-width: 100%;
max-width: 100%;
}
a,[item] {
display: inline-block;
}
/* Solution */
[container] {
width: 100%;
display: flex;
flex-wrap:wrap;
}
[item]{
padding: 10px;
background:red;
flex:0 0 calc((100% - (20px * 6)) / 3);
margin:20px;
}
/* Not needed */
body * {
padding: 10px;
border: 1px solid;
}
<div container>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
<div item>
<a>
<img src="https://via.placeholder.com/305x200">
<p>Some Text</p>
</a>
</div>
</div>
Not sure if this can be done. This where I am after many tries.
I want to layout an HTML page down 3 columns, with different kinds of things in each column. I've tried many things and have run out of ideas. Is it that this can only be done in a table?
Or is there a way to arrange the columns like the below sample grid without changing HTML using CSS only?
What I want:
----------------------------------------------------
| Column left 1| Column center | Column right |
---------------| |----------------
| Column left 2| |
---------------| |
| Column left 3| |
---------------| |
|-------------------|
What I get:
----------------------------------------------------
| Column left 1| Column left 2 | Column left 3 |
---------------|-------------------|----------------
-------------------------------------
| Column center | Column right |
| |----------------
| |
| |
| |
| |
|-------------------|
Here's my last attempt code...
* {
box-sizing: border-box;
}
.column {
float: left;
width: 33.33%;
padding: 10px;
height: 200px; /* Should be removed. Only for demonstration */
}
.row:after {
content: "";
display: table;
clear: both;
}
<h2>Three Columns</h2>
<div class="row">
<div>
<div class="column" style="background-color:#aaa;">
<h2>Column 1</h2>
<p>Some text..</p>
</div>
<div class="column" style="background-color:#aaa;">
<h2>Column 1 a</h2>
<p>Some text..</p>
</div>
<div class="column" style="background-color:#bbb;">
<h2>Column 1 b</h2>
<p>Some text..</p>
</div>
</div>
<div class="column" style="background-color:#bbb;height: 650px">
<h2>Column 2</h2>
<p>Some text..</p>
</div>
<div class="column" style="background-color:#ccc;">
<h2>Column 3</h2>
<p>Some text..</p>
</div>
</div>
Any help or suggestions would be appreciated. Thanks in advance.
Since you asked a change ONLY in CSS, here is my suggestion: use ****display: flex****. In my opinion, it's the best option. To learn more about it, it's my favorite link
* {
box-sizing: border-box;
}
.column {
padding: 10px;
margin: 5px;
}
.row:after {
content: "";
display: table;
clear: both;
}
.row {
display: flex;
flex-direction: row;
align-items: flex-start;
}
.row div {
display: flex;
flex-direction: column;
}
I hope that helps and works for you.
You can try display: flex; this gives you a little more control over how you set up you containers. You can also adjust padding to remove the space between elements.
.row {
display: flex;
}
.column {
flex: 1;
}
.col-1-cont {
padding: 1rem;
}
<h2>Three Columns</h2>
<div class="row">
<div class="column" id="column-1">
<div class="col-1-cont" style="background-color:#aaa;">
<h2>Column 1</h2>
<p>Some text..</p>
</div>
<div class="col-1-cont" style="background-color:#aaa;">
<h2>Column 1 a</h2>
<p>Some text..</p>
</div>
<div class="col-1-cont" style="background-color:#bbb;">
<h2>Column 1 b</h2>
<p>Some text..</p>
</div>
</div>
<div class="column" style="background-color:#bbb;height: 650px">
<h2>Column 2</h2>
<p>Some text..</p>
</div>
<div class="column" style="background-color:#ccc; height: 200px;">
<h2>Column 3</h2>
<p>Some text..</p>
</div>
</div>
I think you are applying the column class to the wrong elements.
You are not telling the wrapper "" of the left column to be a column, but just a normal div (=block element = 100% width).
Try it like this:
* {
box-sizing: border-box;
}
.column {
float: left;
width: 33.33%;
padding: 10px;
height: 200px; /* Should be removed. Only for demonstration */
}
.row:after {
content: "";
display: table;
clear: both;
}
<h2>Three Columns</h2>
<div class="row">
<div class="column"><!-- THIS is the wrapper, that creates the column and should be 33% -->
<div style="background-color:#aaa;"> <!-- these columns should be the full width in the 33% column, that wraps them and gives them their width -->
<h2>Column 1</h2>
<p>Some text..</p>
</div>
<div style="background-color:#aaa;">
<h2>Column 1 a</h2>
<p>Some text..</p>
</div>
<div style="background-color:#bbb;">
<h2>Column 1 b</h2>
<p>Some text..</p>
</div>
</div>
<div class="column" style="background-color:#bbb;height: 650px">
<h2>Column 2</h2>
<p>Some text..</p>
</div>
<div class="column" style="background-color:#ccc;">
<h2>Column 3</h2>
<p>Some text..</p>
</div>
</div>
I have absolutely positioned div.container. div.wrapper inside it. and two divs as flex columns in div.wrapper. These columns have backgrounds.
How can I make these backgrounds go to the end of the longest column (i.e. make them the same height) and not only to the visible height of the container? I can't remove position: absolute from div.container.
.container {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
display: flex;
flex-direction: column;
overflow: auto;
}
.wrapper {
display: flex;
align-items: stretch;
width: 100%;
}
.div1 {
background-color: yellow;
}
.div2 {
flex: 1;
background-color: green;
}
<div class="container">
<div class="wrapper">
<div class="div1">
<p>Some long content here</p>
<p>Some long content here</p>
<p>Some long content here</p>
<p>Some long content here</p>
<p>Some long content here</p>
<p>Some long content here</p>
<p>Some long content here</p>
<p>Some long content here</p>
<p>Some long content here</p>
</div>
<div class="div2">
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
</div>
</div>
</div>
CodePen link
Remove the flex properties on the container.
I also removed align-items: stretch; width: 100%; from the .wrapper as it is their default value's and doesn't need to be set.
.container {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
/* commented out these 2 lines
display: flex;
flex-direction: column;
*/
overflow: auto;
}
.wrapper {
min-height: 100%;
display: flex;
/* commented out these 2 lines as they are not needed
align-items: stretch;
width: 100%;
*/
}
.div1 {
background-color: yellow;
}
.div2 {
flex: 1;
background-color: green;
}
p {
padding: 20px 10px;
}
<div class="container">
<div class="wrapper">
<div class="div1">
<p>Some long content here</p>
<p>Some long content here</p>
<p>Some long content here</p>
</div>
<div class="div2">
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
<p>Some even longer content here</p>
</div>
</div>
</div>
I am trying to wrap my head around flexbox and I have hit a wall with something very simple that I can't seem to get right.
I have 6 columns that I want all the same height with 3 on each row. I have set the flex items to have a width of 33% but I have not set a height on anything. I thought once the flex items had content it would take the one with the greatest height and match everything to it.
Here is my markup:
.container {
width: 800px;
padding: 0 15px;
}
.row-flex {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
margin-right: -15px;
margin-left: -15px;
flex-wrap: wrap;
}
.flexbox {
flex: 0 0 33%;
padding: 0 15px;
}
.icon-box-1 {
position: relative;
margin-bottom: 50px;
background: #fff;
text-align: center;
border: 1px solid #eee;
padding: 10px;
}
<div class="container">
<div class="row-flex">
<div class="flexbox">
<div class="icon-box-1">
<h1>
One
</h1>
<h3>Title</h3>
<p>lots of random text, lots of random text, lots of random text, lots of random text</p>
</div>
</div>
<div class="flexbox">
<div class="icon-box-1">
<h1>
Two
</h1>
<h3>Title</h3>
<p>lots of random text</p>
</div>
</div>
<div class="flexbox">
<div class="icon-box-1">
<h1>
Three
</h1>
<h3>Title</h3>
<p>lots of random text, lots of random text, lots of random text, lots of random text</p>
</div>
</div>
<div class="flexbox">
<div class="icon-box-1">
<h1>
Four
</h1>
<h3>Title</h3>
<p>lots of random text, lots of random text, lots of random text</p>
</div>
</div>
<div class="flexbox">
<div class="icon-box-1">
<h1>
Five
</h1>
<h3>Title</h3>
<p>lots of random text, lots of random text, lots of random text</p>
</div>
</div>
<div class="flexbox">
<div class="icon-box-1">
<h1>
Six
</h1>
<h3>Title</h3>
<p>lots of random text, lots of random text, lots of random text</p>
</div>
</div>
</div>
</div>
The equal height columns feature comes from align-items: stretch, which is an initial setting on a flex container. It makes flex items consume all available space in the cross axis.
If you check the actual size of each flex item, you will see that they are, in fact, equal height on each row (demo).
The problem is that the content of each item (.icon-box-1) is not stretching full height. It's just height: auto (content height), by default.
In order for the content to stretch full height, add display: flex to each flex item, so align-items: stretch goes into effect, like on the parent (demo).
Then use flex: 1 to make the content fill the remaining space on the main axis (demo).
.container {
width: 800px;
padding: 0 15px;
}
.row-flex {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
margin-right: -15px;
margin-left: -15px;
flex-wrap: wrap;
}
.flexbox {
flex: 0 0 33%;
padding: 0 15px;
border: 1px dashed red; /* for illustration */
display: flex; /* new */
}
.icon-box-1 {
flex: 1; /* NEW */
position: relative;
margin-bottom: 50px;
background: #fff;
text-align: center;
border: 1px solid #eee;
padding: 10px;
}
<div class="container">
<div class="row-flex">
<div class="flexbox">
<div class="icon-box-1">
<h1>
One
</h1>
<h3>Title</h3>
<p>lots of random text, lots of random text, lots of random text, lots of random text</p>
</div>
</div>
<div class="flexbox">
<div class="icon-box-1">
<h1>
Two
</h1>
<h3>Title</h3>
<p>lots of random text</p>
</div>
</div>
<div class="flexbox">
<div class="icon-box-1">
<h1>
Three
</h1>
<h3>Title</h3>
<p>lots of random text, lots of random text, lots of random text, lots of random text</p>
</div>
</div>
<div class="flexbox">
<div class="icon-box-1">
<h1>
Four
</h1>
<h3>Title</h3>
<p>lots of random text, lots of random text, lots of random text</p>
</div>
</div>
<div class="flexbox">
<div class="icon-box-1">
<h1>
Five
</h1>
<h3>Title</h3>
<p>lots of random text, lots of random text, lots of random text</p>
</div>
</div>
<div class="flexbox">
<div class="icon-box-1">
<h1>
Six
</h1>
<h3>Title</h3>
<p>lots of random text, lots of random text, lots of random text</p>
</div>
</div>
</div>
</div>
Try adding "display: flex" to .flexbox and "flex: 0 1 auto" to the icon box so it fills the heights out. Here ya go:
.container {
width: 800px;
padding: 0 15px;
}
.row-flex {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
margin-right: -15px;
margin-left: -15px;
flex-wrap: wrap;
}
.flexbox {
flex: 0 0 33%;
padding: 0 15px;
display: flex;
flex-wrap: wrap;
flex-direction: column;
}
.icon-box-1 {
position: relative;
margin-bottom: 50px;
background: #fff;
text-align: center;
border: 1px solid #eee;
padding: 10px;
flex: 1 0 auto;
}
<div class="container">
<div class="row-flex">
<div class="flexbox">
<div class="icon-box-1">
<h1>
One
</h1>
<h3>Title</h3>
<p>lots of random text, lots of random text, lots of random text, lots of random text</p>
</div>
</div>
<div class="flexbox">
<div class="icon-box-1">
<h1>
Two
</h1>
<h3>Title</h3>
<p>lots of random text</p>
</div>
</div>
<div class="flexbox">
<div class="icon-box-1">
<h1>
Three
</h1>
<h3>Title</h3>
<p>lots of random text, lots of random text, lots of random text, lots of random text</p>
</div>
</div>
<div class="flexbox">
<div class="icon-box-1">
<h1>
Four
</h1>
<h3>Title</h3>
<p>lots of random text, lots of random text, lots of random text</p>
</div>
</div>
<div class="flexbox">
<div class="icon-box-1">
<h1>
Five
</h1>
<h3>Title</h3>
<p>lots of random text, lots of random text, lots of random text</p>
</div>
</div>
<div class="flexbox">
<div class="icon-box-1">
<h1>
Six
</h1>
<h3>Title</h3>
<p>lots of random text, lots of random text, lots of random text</p>
</div>
</div>
</div>
</div>
I have a div with the below content.
<div class="outsideDiv">
<div>
<p>Some text</p>
</div>
<div class="imgContainer">
<img />
</div>
</div>
The imgContainer is right of the div which contains the p
The outsideDiv doesn't have a pre-defined height. It adjusts to the height of the p element. So if the p element has 500px height then the height of outside div will be 500px. My website is responsive, therefore the height of p (and so the height of outsideDiv) it isn't fixed and it changes depending on screen. This section works perfect.
I want the height of imgContainer to be equal with the height of the outsideDiv.
I have tried with position absolute and relative and it works but I don't want to use this way. Also I tried inherit height but no luck.
.imgContainer {
background: rgba(0,0,0,.5);
position: absolute;
width:100%;
height:100%;
bottom: -100%;
}
.outsideDiv {
position: relative;
}
<div class="outsideDiv">
<div>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p><p>Some text</p><p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
<p>Some text</p>
</div>
<div class="imgContainer">
<img />
</div>
</div>
You can achieve this using flexbox.
body{
margin:0;
}
.outsideDiv{
background:red;
display:flex;
}
.outsideDiv > div{
float:left;
width:50%;
}
.outsideDiv p{
height:500px;
}
.imgContainer{
background:blue;
float:right;
width:50%;
height:100%:
}
<div class="outsideDiv">
<div>
<p>Some text</p>
</div>
<div class="imgContainer">
<img />
</div>
</div>
Here is Demo : https://jsfiddle.net/r73kh3bo/
If you don't want to use Flexbox because of browser support, here's another example: https://jsfiddle.net/r73kh3bo/1/
Basically you can just create a parent div with display: table and each child inside with display: table-cell will have the same height.