The title doesn't describe my problem but did'nt find a better one.
So, I have one div containing 3 divs, I wan't two columns with div A in the first one and div B and C in the second, on large screens and one only column on small screens. I could use #media but I think flex can do it without.
This is what I tried:
<div id="container">
<div>
</div>
<div>
</div>
<div>
</div>
</div>
#container
{
border:1px solid #aaa;
height:300px;
width:500px;
max-width:100%;
display:flex;
flex-flow:column;
flex-wrap:wrap;
}
#container > div
{
border:1px solid #aaa;
margin:2px;
flex-grow:1;
min-width:200px;
height:10px;
}
#container > div:first-of-type
{
height:300px;
flex-grow:1;
}
Demo
I fixed the height to force the content to go to 2 columns but then it never goes to onel colum.
Just remove the last css element:
https://jsfiddle.net/j2brc3t3/1/
Live example:
#container
{
border:1px solid #aaa;
height:300px;
width:500px;
max-width:100%;
display:flex;
flex-flow:column;
flex-wrap:wrap;
}
#container > div
{
border:1px solid #aaa;
margin:2px;
flex-grow:1;
min-width:200px;
height:10px;
}
<div id="container">
<div>
</div>
<div>
</div>
<div>
</div>
</div>
#container {
border: 1px solid #aaa;
width: 600px;
max-width: 100%;
display: flex;
text-align: center;
flex-wrap: wrap;
}
#in1 {
background: #a1d;
margin: 5px;
height: 200px;
flex-grow: 1;
min-width: 200px;
}
#in2 {
flex-grow: 1;
display: flex;
flex-flow: column;
min-width: 200px;
}
#in2>div {
background: #e25;
flex-grow: 1;
}
/*
#in2 div:first-child {
margin-bottom: 5px;
}
*/
<div id="container">
<div id="in1">div1</div>
<div id="in2">
<div>div2</div>
<div>div3</div>
</div>
</div>
I found a near solution:
Demo
It answer my question but show a new issue with margins, I need no margin around the container, just 5px between the inner divs, if someone has the perfect solution...
Related
How should I style using flexbox to get a layout like this? I'm hesitant to use grid as it has limited support on IE11. I'd love to make it possible to add more or less small divs and not to add more containers for small divs
HTML for the layout is looking like this:Desired Layout image
.container {
display: flex;
}
.large {
height: 200px width: 150px;
border: 1px solid black;
}
.small {
height: 100px;
width: 150px;
border: 1px solid black;
}
<div class="container">
<div class="large">1</div>
<div class="small">2</div>
<div class="small">3</div>
<div class="small">4</div>
<div class="small">5</div>
</div>
I would use css-grid over flexbox, here is an example:
tutorial here, current specifications here
.container {
display: grid;
grid-template-columns: 60px 60px 60px 60px 60px;
grid-template-rows: 30px 30px;
grid-auto-flow: column;
}
.container div {
border:1px solid gold;
}
.item-a {
grid-column: 1;
grid-row: 1 / 3;
}
<section class="container">
<div class="item-a">item-a</div>
<div class="item-b">item-b</div>
<div class="item-c">item-c</div>
<div class="item-d">item-d</div>
<div class="item-e">item-e</div>
</section>
I added a parent div for .small divs and I used margin and flex-wrap. But I suggest use grid for you.
.container {
display: flex;
}
.large {
height: 220px;
width: 150px;
border: 1px solid black;
box-sizing:border-box;
margin:10px;
}
.small_cont {
width:340px;
display:flex;
height:200px;
flex-wrap:wrap;
}
.small {
height: 100px;
width: 150px;
border: 1px solid black;
box-sizing:border-box;
margin:10px;
}
<div class="container">
<div class="large">1</div>
<div class="small_cont">
<div class="small">2</div>
<div class="small">3</div>
<div class="small">4</div>
<div class="small">5</div>
</div>
</div>
I'm trying to figure out a way to make one of the grid elements automatically fill the width gap between other elements with fixed width, so together they will fill the width of their parent element no matter how wide the parent element is. Can someone please enlighten me if what I'm trying to do is possible and if it is, how should it be done properly.
Here's an image of what I'm trying to achieve:
This is one way to do what you're asking for, using flexbox.
<style>
.row {
display: flex;
display: -webkit-flex;
flex-direction: row;
-webkit-flex-direction: row;
}
.col-left {
flex: 1;
min-width: 120px;
}
.col-center {
flex: 3;
}
.col-right {
flex: 1;
min-width: 120px;
}
</style>
</head>
<body>
<section>
<div class="row">
<div class="col-left" style="background-color: lightgreen; height: 20em;">
</div>
<div class="col-center" style="background-color: violet; height: 20em;">
</div>
<div class="col-right" style="background-color: lightgreen; height: 20em;">
</div>
</div>
</section>
</body>
DEMO
Here's a css but you'll need to mention the width in % of the remaining to occupy the entire space.
.container {
width:600px;
height:200px;
border:1px solid;
background:yellow;
display:table-row;
}
.left {
width:200px;
height:20%;
background:red;
overflow:hidden;
display:table-cell;
}
.right {
height:200px;
width:20%;
background:blue;
display:table-cell;
}
.middle {
height:200px;
width: 60%;
background:green;
display:table-cell;
}
if you are hardcoding the width of the divs to some pixels then you can make use of calc property and subtract by 100% of the container width to get the result
All:
Right now, I want to implement a horizontal scroll header bar. The layout I used is:
<div id="header_cnt">
<div id="header_mn">
<div class="mn_item">KWD1</div>
<div class="mn_item">KWD2</div>
<div class="mn_item">KWD3</div>
<div class="mn_item">KWD4</div>
<div class="mn_item">KWD5</div>
</div>
</div>
And the style:
#header_cnt {
width:100%;
height:60px;
position: fixed;
}
#header_mn {
background-color: lightyellow;
width:100%;
height:60px;
overflow: hidden;
padding:0px;
}
#header_mn .mn_item {
display:inline-block;
width:200px;
}
The effect I want for first step is:
The header menu fix to top, and all menu items sit in one line and the overflow part get hidden rather than wrap to next line.
[UPDATE] I figure out myself, the way I find out is using FLEXBOX:
Forget structure above,just use some new but similar structure:
<head>
<style>
body, html {
padding:0px;
margin:0px;
}
section {
border: 1px solid gray;
/*padding: 1em;*/
height:300px;
width:100%;
display: flex;
flex-direction: row;
align-items: flex-start;
justify-content:flex-start;
position:fixed;
overflow: auto;
}
div {
min-width: 250px;
height: 50px;
border: 1px solid steelblue;
}
</style>
</head>
<body>
<section>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</section>
</body>
Thanks
You could use flexbox to create this header, check it out here You can also check out flexbox here
CSS
#header_cnt {
width:100%;
height:60px;
position: fixed;
background:#d7d7d7;
}
#header_mn {
background-color: beige;
width:100%;
height:60px;
overflow: hidden;
padding:0px;
display:flex;
flex-direction:row;
justify-content:space-around;
}
#header_mn .mn_item:first-child{
margin-left:10%;
}
#header_mn .mn_item {
display:inline-block;
width:200px;
align-self:center;
justify-content:center;
}
Hope this helps
You need to float:left on those .mn_item
I still don't have Reputation to comment, so here is my tip.
You can set min-width of #header_mn to a multiple of .mn_item.
i am attempting to align 3 sub-div elements in one line, first element with left floating, third - with right and 2nd - centered.
Below my draft. What I have missed?
http://jsfiddle.net/yDzL9/
In my real e-shop the problem is in aligning of 2nd div which contains input (button), but in jsfiddle's example the problem with the third div.
Use display: inline-block for all sub elements and set white-space: nowrap; property to parent div.
Example-
HTML:
<div id="container">
<div class="wishlist">
</div>
<div class="cart">
</div>
<div class="compare">
</div>
</div>
Method 1 (Without Floated div):
CSS:
#container {
height:26px;
width:300px;
background-color:#009900;
white-space: nowrap;
}
#container div{
display: inline-block;
width:100px;
height:26px;
}
Working Example
Method 2 (With Floated div)
CSS:
#container {
height:26px;
width:300px;
background-color:#009900;
white-space: nowrap;
text-align: center;
}
#container div{
display: inline-block;
width:50px;
height:26px;
}
.wishlist{
background-color:blue;
float: left;
}
.cart{
background-color:black;
}
.compare{
background-color:red;
float: right;
}
Working Example
Using the display: flex css rule it will work as you want it to!
HTML:
<div id="container">
<div class="wishlist"></div>
<div class="cart"></div>
<div class="compare"></div>
</div>
CSS:
body {
margin: 0;
padding: 0;
}
#container {
height:26px;
width:300px;
padding: 10px;
display: flex;
background-color:green;
}
#container > div {
height: 90%;
width: 90px;
}
.wishlist {
float: left;
background-color:blue;
}
.cart {
margin: 0 auto;
background-color:yellow;
}
.compare {
float: right;
background-color:red;
}
As shown in this jsfiddle, and here is another one.
I have two divs like this
<div style="border:1px solid #000; float:left">Div 1</div>
<div style="border:1px solid red; float:left">Div 2</div>
I want them to display on the same row, so I used float:left.
I want both of them to be at center of the page as well, so I tried to wrap them with another div like this
<div style="width:100%; margin:0px auto;">
<div style="border:1px solid #000; float:left">Div 1</div>
<div style="border:1px solid red; float:left">Div 2</div>
</div>
But it doesn't work. If I change the code to this
<div style="width:100%; margin-left:50%; margin-right:50%">
<div style="border:1px solid #000; float:left">Div 1</div>
<div style="border:1px solid red; float:left">Div 2</div>
</div>
then it's going to the center, but the horizontal scrollbar is there and it seems like it's not really centered as well.
Can you please kindly suggest to me how can I achieve this? Thanks.
Edit: I want the inner div (Div 1 and Div 2) to be center align as well.
You could do this
<div style="text-align:center;">
<div style="border:1px solid #000; display:inline-block;">Div 1</div>
<div style="border:1px solid red; display:inline-block;">Div 2</div>
</div>
http://jsfiddle.net/jasongennaro/MZrym/
wrap it in a div with text-align:center;
give the innder divs a display:inline-block; instead of a float
Best also to put that css in a stylesheet.
Could this do for you? Check my JSFiddle
And the code:
HTML
<div class="container">
<div class="div1">Div 1</div>
<div class="div2">Div 2</div>
</div>
CSS
div.container {
background-color: #FF0000;
margin: auto;
width: 304px;
}
div.div1 {
border: 1px solid #000;
float: left;
width: 150px;
}
div.div2 {
border: 1px solid red;
float: left;
width: 150px;
}
both floated divs need to have a width!
set 50% of width to both and it works.
BTW, the outer div, with its margin: 0 auto will only center itself not the ones inside.
Align to the center, using display: inline-block and text-align: center.
.outerdiv
{
height:100px;
width:500px;
background: red;
margin: 0 auto;
text-align: center;
}
.innerdiv
{
height:40px;
width: 100px;
margin: 2px;
box-sizing: border-box;
background: green;
display: inline-block;
}
<div class="outerdiv">
<div class="innerdiv"></div>
<div class="innerdiv"></div>
</div>
Align to the center using display: flex and justify-content: center
.outerdiv
{
height:100px;
width:500px;
background: red;
display: flex;
flex-direction: row;
justify-content: center;
}
.innerdiv
{
height:40px;
width: 100px;
margin: 2px;
box-sizing: border-box;
background: green;
}
<div class="outerdiv">
<div class="innerdiv"></div>
<div class="innerdiv"></div>
</div>
Align to the center vertically and horizontally using display: flex, justify-content: center and align-items:center.
.outerdiv
{
height:100px;
width:500px;
background: red;
display: flex;
flex-direction: row;
justify-content: center;
align-items:center;
}
.innerdiv
{
height:40px;
width: 100px;
margin: 2px;
box-sizing: border-box;
background: green;
}
<div class="outerdiv">
<div class="innerdiv"></div>
<div class="innerdiv"></div>
</div>
I would vote against display: inline-block since its not supported across browsers, IE < 8 specifically.
.wrapper {
width:500px; /* Adjust to a total width of both .left and .right */
margin: 0 auto;
}
.left {
float: left;
width: 49%; /* Not 50% because of 1px border. */
border: 1px solid #000;
}
.right {
float: right;
width: 49%; /* Not 50% because of 1px border. */
border: 1px solid #F00;
}
<div class="wrapper">
<div class="left">Div 1</div>
<div class="right">Div 2</div>
</div>
EDIT: If no spacing between the cells is desired just change both .left and .right to use float: left;
Better way till now:
If you give display:inline-block; to inner divs then child elements of inner divs will also get this property and disturb alignment of inner divs.
Better way is to use two different classes for inner divs with width, margin and float.
Best way till now:
Use flexbox.
http://css-tricks.com/snippets/css/a-guide-to-flexbox/
Please take a look on flex it will help you make things right,
on the main div set css display :flex
the div's that inside set css: flex:1 1 auto;
attached jsfiddle link as example enjoy :)
https://jsfiddle.net/hodca/v1uLsxbg/