I have trouble with 5 div elements to make it responsive.
I have this this div elements like this:
<div class="AllCont">
<div class="ContQuint">
<div>Here Image 1</div>
</div>
<div class="ContQuint">
<div>Here Image 2</div>
</div>
<div class="ContQuint">
<div>Here Image 3</div>
</div>
<div class="ContQuint">
<div>Here Image 4</div>
</div>
<div class="ContQuint">
<div>Here Image 5</div>
</div>
</div>
The style CSS like this:
.AllCont {
display: table;
border-collapse : collapse;
width: 100%;
}
.ContQuint {
display: table-cell;
width: 20%;
}
And trying to do it responsive with this in CSS:
#media only screen and (max-width:1540px) {
.ContQuint {
display: table-row;
}
}
But i dontk want that. I want something like this:
| Elem 1 - Elem 2 - Elem 3 - Elem 4 - Elem 5 |
Then when the screen size decreases a little bit I want this:
| Elem 1 - Elem 2 - Elem 3 - Elem 4 |
| Elem 5 |
Then when decreases more:
| Elem 1 - Elem 2 - Elem 3 |
| Elem 4 - Elem 5 |
Then a little more:
| Elem1 - Elem 2 |
| Elem 3 - Elem 4 |
| Elem 5 |
And Finally:
| Elem 1 |
| Elem 2 |
| Elem 3 |
| Elem 4 |
| Elem 5 |
How can i do that with CSS or javascript?
I think what you're looking for is display: inline-block;! Plus display: block on the parent .AllCont element. Tables used to be useful for this kind of layout, but these days you'll have a better time checking flexbox (or even CSS grid) for something more sophisticated.
Should probabliy take a look into responsive layout, this will help you a lot understanding different patterns.
Responsive patterns by google
Either way i recommend the use of flexboxes or inline/block elements.
Solution using flexboxes :
.AllCont {
display: flex;
flex-direction: row;
flex-wrap: wrap;
border-collapse : collapse;
width: 100%;
}
.ContQuint {
width: 200px;
}
<div class="AllCont">
<div class="ContQuint">
<div>Here Image 1</div>
</div>
<div class="ContQuint">
<div>Here Image 2</div>
</div>
<div class="ContQuint">
<div>Here Image 3</div>
</div>
<div class="ContQuint">
<div>Here Image 4</div>
</div>
<div class="ContQuint">
<div>Here Image 5</div>
</div>
</div>
Solution using inline and block elements
.AllCont {
display: block;
border-collapse : collapse;
width: 100%;
}
.ContQuint {
display: inline-block;
width: 200px;
}
<div class="AllCont">
<div class="ContQuint">
<div>Here Image 1</div>
</div>
<div class="ContQuint">
<div>Here Image 2</div>
</div>
<div class="ContQuint">
<div>Here Image 3</div>
</div>
<div class="ContQuint">
<div>Here Image 4</div>
</div>
<div class="ContQuint">
<div>Here Image 5</div>
</div>
</div>
Hope this helps :)
Rather than giving a % width for .ContQuint, give a fixed width. otherwise the width reduces with screen width.
fiddle showing it
.ContQuint {
display: inline-block;
width: 150px;
}
Flexbox does this with flex-wrap: wrap;
.AllCont {
display: flex;
flex-wrap: wrap;
}
.ContQuint {
}
<div class="AllCont">
<div class="ContQuint">
<div>Here Image 1</div>
</div>
<div class="ContQuint">
<div>Here Image 2</div>
</div>
<div class="ContQuint">
<div>Here Image 3</div>
</div>
<div class="ContQuint">
<div>Here Image 4</div>
</div>
<div class="ContQuint">
<div>Here Image 5</div>
</div>
</div>
You may use flex, width + flex-wrap and min-width to spare a mediaquerie.
.AllCont {
display: flex;
flex-wrap: wrap;
}
.ContQuint {
width: 20%;
/* optionnal*/
min-width: 300px;/* this is alike your breaking point ... each 300px or else value you set */
padding: 1px;
box-sizing:border-box;
}
img {
/* optionnal*/
display: block;
width: 100%;
}
<div class="AllCont">
<div class="ContQuint">
<div>
<img src="http://dummyimage.com/300x200">
</div>
</div>
<div class="ContQuint">
<div>
<img src="http://dummyimage.com/300x200"> </div>
</div>
<div class="ContQuint">
<div>
<img src="http://dummyimage.com/300x200"> </div>
</div>
<div class="ContQuint">
<div>
<img src="http://dummyimage.com/300x200"> </div>
</div>
<div class="ContQuint">
<div>
<img src="http://dummyimage.com/300x200"> </div>
</div>
</div>
pen to play with
Related
How to make the box size to be evenly as perfect square and make the words inside of it to shrink. I want to make it square and the inside word to shrink to small size based on the square, but they just flexing because of the contents.
This is my code, what should I change/remove/add?
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.mainbox {
background: grey;
display: flex;
margin: 5px;
}
.mainbox div {
flex: 1;
border: 2px solid black;
}
#row {
display: flex;
flex-direction: row;
}
#column {
display: flex;
}
<!DOCTYPE html>
<html>
<head>
<title>FlexSpiral</title>
</head>
<body>
<div class="mainbox">
<div>box 1</div>
<div>
box 2
<div id="row">
<div id="column">
<div id="row">
<div>box 5</div>
<div>
<div>box 6</div>
<div id="row">
<div>
<div id="row">
<div>box 9</div>
<div>box 10</div>
</div>
<div>box 8</div>
</div>
<div>box 7</div>
</div>
</div>
</div>
<div>box 4</div>
</div>
<div>box 3</div>
</div>
</div>
</div>
</body>
</html>
I didn't quite understand the question, but here goes
You can take the margin from the ".mainbox" that it is giving margin on all sides of the mainbox class.
To leave the div box occupying its own content you can use in the styling: display:inline-block
Note: a good practice is to use class instead of id to identify the styling, and id more for future interaction when using script and interactions.
Forget how to code a div style table.
I haven't coded html in years and am pretty rusty. I'm trying to create a responsive div style table with the first div spans the entire column with 2 more divs next to it. A div with 2 cells on top and a div that spans the 2 cells on bottom.
I'm trying to create something that looks like this image.
<div class="table">
<div class="row">
<div class="cell">cell 1</div>
</div>
<div class="row">
<div class="cell">cell 1</div>
<div class="cell">cell 2</div>
</div>
<div class="row">
<div class="cell colspan">
<div><div>
cell 3
</div></div>
</div>
<div class="cell"></div>
</div>
Use flexbox. By assigning display: flex; to the .table, .row, and .column elements, child elements of each all become flexible and can easily be controlled to take up certain percentages of space within the table, and grow to fill all the available space like a table would.
The flex property takes a little getting used to. Here I used it to tell flex items to grow (the first value, flex-grow), and starting widths (the third value, flex-basis). This resource makes it pretty easy to understand: https://css-tricks.com/snippets/css/a-guide-to-flexbox/
* { box-sizing: border-box; }
.table,
.row,
.column {
display: flex;
}
.column {
flex: 1 0 50%;
flex-direction: column;
}
.first-column {
flex-basis: 33%;
}
.cell {
flex: 1 0 100%;
padding: 20px;
border: 1px solid dodgerblue;
}
.first-row .cell {
border-left: none;
}
.second-row .cell {
border-top: none;
border-left: none;
}
<div class="table">
<div class="column first-column">
<!-- just the one cell in this column -->
<div class="cell">cell 1</div>
</div>
<div class="column">
<!-- need 2 rows here -->
<div class="row first-row">
<!-- first row will have 2 columns -->
<div class="column">
<div class="cell">cell 2</div>
</div>
<div class="column">
<div class="cell">cell 3</div>
</div>
</div>
<div class="row second-row">
<div class="cell">cell 4</div>
</div>
</div>
</div>
<div>
<div>
hello1
</div>
<div>
hello2
</div>
</div>
I want the two inner div's to take/fill up the whole space of the outer div (height/vertically). But both inner div's should have the equal height
You can use CSS' flexbox, see my example below:
.row {
display: flex;
}
.col {
border: 1px solid;
flex: 1;
}
<div class="row">
<div class="col">
hello1
</div>
<div class="col">
hello2<br>
same height!
</div>
</div>
now I have such code, use bootstrap 2
<div class="row-fluid">
<div class="span4">Some text 1</div>
<div class="span4">Some text 2</div>
<div class="span4">Some text 3</div>
</div>
<div class="row-fluid">
<div class="span4">Some text 4</div>
<div class="span4">Some text 5</div>
<div class="span4">Some text 6</div>
</div>
Can I get 3 rows in 2 columns each without code change?
3 columns and 2 rows I use for desktop, and need 2 columns and 3 rows in mobile devices
I have found it
http://jsfiddle.net/gkZKq/
You could do:
.row-fluid { display: inline-block; width: 50%; float: left; }
.row-fluid [class*="span"] { float: none; margin-left: 0; }
http://jsfiddle.net/thespacebean/mDVfd/
I have a div with variable length lines of text in it. Right now I have something like
<div id="container" style="width: 500px">
<div>Text Line 1</div>
<div>Text Line 2 of different length</div>
<div>Text Line 3</div>
</div>
I can text-align: center the container, but I want each line to be left justified relative to the longest line which is truly centered, as opposed to each line being centered on its own.
Is there an easy CSS way to do this... or should I resort to using tables to lay this out?
Your html:
<div id="container">
<span>
<div>Text Line 1</div>
<div>Text Line 2 of different length</div>
<div>Text Line 3</div>
</span>
</div>
Your CSS:
#container {
width: 500px;
background: #eee;
text-align: center;
padding: 10px 0;
}
#container span {
display: inline-block;
background-color: #ccc;
}
#container span div {
text-align: left;
}
Demo: http://jsfiddle.net/G6ABA/
That should work:
<div id="container" style="width: 500px; text-align:center;">
<div style="text-align:left;">
<div>Text Line 1</div>
<div>Text Line 2 of different length</div>
<div>Text Line 3</div>
</div>
</div>
use
<span> </span>
and css
width:500px;
text-align:center;
position:absolute;
do you set pic or color to div background?
or better i said is it your div width important?
if not maybe this solution can solve your problem:
<div id="someID" style="width: auto; text-align:left;">
<div>line 1</div>
<div>line 2 is more longer</div>
<div>line 3</div>
</div>