How do I expand the div#wrapper outside its parent by its child item's width.item like the image below?
I have tried translate but the result is not ideal. The hardest part is that I cannot programmatically expand it with the item's width, but hard code it with translate like 90px which causes a problem when the screen is resized (please use the developer tool to resize and you will get my point).
I cannot use position: absolute; because there are some elements before the #wrapper.
Below is my try:
#main {
background-color: gray;
display: flex;
height: 600px;
justify-content: stretch;
}
#first,
#second {
width: 50%;
}
#first {
align-items: center;
background-color: blue;
display: flex;
flex-direction: column;
padding: 30px 0 30px 30px;
}
#wrapper {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
transform: translateX(90px);
width: 100%;
}
.item {
background-color: orange;
border: 3px solid #000;
height: 50px;
width: 100%;
}
<div id="main">
<div id="first">
<p>Some text...</p>
<p>Some text...</p>
<p>Some text...</p>
<div id="wrapper">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</div>
<div id="second">
</div>
</div>
Do you mean something like this?
#main {
background-color: gray;
display: flex;
height: 600px;
justify-content: stretch;
}
#first,
#second {
width: 50%;
}
#first {
align-items: center;
background-color: blue;
display: flex;
flex-direction: column;
padding: 30px 0 30px 30px;
}
#wrapper {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
width: 100%;
position:relative;
margin-right:-66%;
}
.item {
background-color: orange;
border: 3px solid #000;
height: 50px;
width: 100%;
}
<div id="main">
<div id="first">
<p>Some text...</p>
<p>Some text...</p>
<p>Some text...</p>
<div id="wrapper">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</div>
<div id="second">
</div>
</div>
Related
I have a grid that uses display: contents. Unfortunately, this declaration still isn't widely supported. I'm trying to substitute it with something that's more widely supported and that wouldn't break my current grid design. Any help would be greatly appreciated.
Here's the code:
.grid-row {
display: contents;
}
.grid-row:hover > div {
background-color: #262626;
color: #fff;
}
.grid-cell {
background-color: #000;
color: #808080;
padding: .5rem;
min-height: 4rem;
display: flex;
align-items: center;
justify-content: flex-start;
position: relative;
}
.grid {
margin: 0 auto;
display: grid;
grid-gap: 1px;
background-color: #808080 !important;
border: 1px solid #808080 !important;
grid-template-columns: repeat(3, 1fr);
grid-template-columns: auto min-content min-content;
white-space: pre-wrap !important;
}
<div class="grid">
<div class="grid-cell"></div>
<div class="grid-cell">Price</div>
<div class="grid-cell">Quantity</div>
<div class="grid-row">
<div class="grid-cell">Item 1</div>
<div class="grid-cell">10.00</div>
<div class="grid-cell">1</div>
</div>
<div class="grid-row">
<div class="grid-cell">Item 2</div>
<div class="grid-cell">20.00</div>
<div class="grid-cell">2</div>
</div>
<div class="grid-row">
<div class="grid-cell">Item 3</div>
<div class="grid-cell">30.00</div>
<div class="grid-cell">3</div>
</div>
</div>
You could convert it into table:
.grid-row {
display: table-row;
}
.grid-row:hover > div {
background-color: #262626;
color: #fff;
}
.grid-cell {
background-color: #000;
color: #808080;
padding: .5rem;
line-height: 4rem;
display: table-cell;
position: relative;
}
.grid {
margin: 0 auto;
display: table;
background-color: #808080 !important;
white-space: pre-wrap !important;
border-spacing: 1px;
}
.grid > :first-of-type
{
width: 100%;
}
<div class="grid">
<div class="grid-cell"></div>
<div class="grid-cell">Price</div>
<div class="grid-cell">Quantity</div>
<div class="grid-row">
<div class="grid-cell">Item 1</div>
<div class="grid-cell">10.00</div>
<div class="grid-cell">1</div>
</div>
<div class="grid-row">
<div class="grid-cell">Item 2</div>
<div class="grid-cell">20.00</div>
<div class="grid-cell">2</div>
</div>
<div class="grid-row">
<div class="grid-cell">Item 3</div>
<div class="grid-cell">30.00</div>
<div class="grid-cell">3</div>
</div>
</div>
I have container with div elemenents
.container {
display: flex;
justify-content: space-between;
}
How to make one element positioned at the center on this block, and others to be space-between.
.container {
display: flex;
justify-content: space-between;
}
.container div {
height: 50px;
}
.one,
.four,
.seven {
background-color: red;
width: 200px;
}
.two,
.six {
background-color: green;
width: 100px;
}
.three,
.five {
background-color: yellow;
width: 150px;
}
.center {
width: 300px;
background-color: black;
}
<div class="container">
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
<div class="center"></div>
<div class="four"></div>
<div class="five"></div>
<div class="six"></div>
<div class="seven"></div>
</div>
jsFiddle
Based on how dynamic you want this to be, here is a suggestion where the items on the left and on the right side of the center element are wrapped.
The left and right get 50% each minus the width of the center (150px for each side), which will put the center in the middle.
Updated fiddle
Stack snippet
.container {
display: flex;
justify-content: space-between;
}
.container div {
height: 50px;
}
.left, .right {
display: flex;
justify-content: space-between;
flex-basis: calc(50% - 150px);
}
.one,
.four,
.seven {
background-color: red;
flex-basis: 200px;
}
.two,
.six {
background-color: green;
flex-basis: 100px;
}
.three,
.five {
background-color: yellow;
flex-basis: 150px;
}
.center {
flex-basis: 300px;
background-color: black;
}
<div class="container">
<div class="left">
<div class="one">
</div>
<div class="two">
</div>
<div class="three">
</div>
</div>
<div class="center">
</div>
<div class="right">
<div class="four">
</div>
<div class="five">
</div>
<div class="six">
</div>
<div class="seven">
</div>
</div>
</div>
By adding a pseudo to each side wrapper, we can also make it behave similar to how space-between work without the wrappers (though still with center centered).
In this fiddle demo (and below Stack snippet) I changed the width's so one easier can see how it behaves in full screen.
.container {
display: flex;
justify-content: space-between;
}
.container div {
height: 50px;
}
.left, .right {
display: flex;
justify-content: space-between;
flex-basis: calc(50% - 100px);
}
.left::after,
.right::before {
content: '';
}
.one,
.four,
.seven {
background-color: red;
flex-basis: 125px;
}
.two,
.six {
background-color: green;
flex-basis: 25px;
}
.three,
.five {
background-color: yellow;
flex-basis: 75px;
}
.center {
flex-basis: 200px;
background-color: black;
}
<div class="container">
<div class="left">
<div class="one">
</div>
<div class="two">
</div>
<div class="three">
</div>
</div>
<div class="center">
</div>
<div class="right">
<div class="four">
</div>
<div class="five">
</div>
<div class="six">
</div>
<div class="seven">
</div>
</div>
</div>
In this jsfiddle you'll see that there's enough space for a third block under the second block. Is it possible to push the third block under the second block so that there's only two rows?
https://jsfiddle.net/byokdm8o/
.list {
display: flex;
flex-flow: row wrap;
width: 20rem;
}
.item {
height: 5rem;
width: 5rem;
background-color: blue;
margin: .2rem;
}
.item:nth-child(1) {
height: 10rem;
width: 10rem;
}
<div class="list">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
If you float the .items left, they'll stack that way.
.list {
width: 20rem;
}
.item {
height: 5rem;
width: 5rem;
background-color: blue;
margin: .2rem;
float: left;
}
.item:nth-child(1) {
height: 10rem;
width: 10rem;
}
<div class="list">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
With such margins between those elements, the solution might be:
.list {
width: 20rem;
}
.item {
height: 5rem;
width: 5rem;
background-color: blue;
margin: .2rem;
float: left;
}
.item:nth-child(1) {
height: 10rem;
width: 10rem;
}
you need to use flexbox. i am sure there are other ways as well, but here is an amazing tut on flexbox if you are interested:
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
I'm trying to create this: http://imgur.com/G2TpjDR
What I have: https://jsfiddle.net/tzayffsv/
I use flexbox with params: flex-direction: column, flex-wrap: wrap. The Problem is that items going outside the screen when wrapping... Any ideas how to do this using flexbox (or mbe other ideas)? I don't look for solutions with javascript and positioning items as absolute..
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
</div>
This works. flex-flow: column wrap-reverse;
.box {
height:200px;
width:300px;
background:grey;
display: flex;
flex-flow: column wrap-reverse;
justify-content: center;
align-content: flex-start;
align-items: flex-end;
}
.item {
flex: 0 1 auto;
align-self: auto;
min-width: 0;
min-height: 40%;
border:1px solid black;
}
<div class="box">
<div class="item">A</div>
<div class="item">B</div>
<div class="item">C</div>
</div>
This is not directly related to flexbox, you only need to set the direction of the text to be right-to-left. This can be easily achieved with
.container {
direction: rtl;
}
.container {
direction: rtl;
display: flex;
flex-direction: column;
flex-wrap: wrap;
position: absolute;
top: 0;
right: 50px;
height: 600px;
}
.item {
width: 200px;
height: 140px;
line-height: 60px;
background-color: #618ae4;
margin-top: 10px;
margin-left: 10px;
text-align: center;
font-weight: bold;
font-size: 30px;
color: #fff;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
</div>
I created a container (non-flex) and divided it into an upper half with 4 rows of fixed-height list items, and the bottom half of the box to fill whatever space was left over. Inside that bottom box, my intention is to style 4 child boxes to fit 2x2. I could probably separate them into 2 row containers each with 2 boxes and that would work but I think it can be done without adding any elements.
I can't figure out how to style the 4 boxes in 2x2 orientation within the lower flexbox, since it has no fixed height, meaning inner elements are difficult to define the height for. How can I do this with flexbox?
/* Menu / Primary Styles */
#Menu {
display: flex;
flex-direction: column;
height: 100vh;
position: absolute;
z-index: 100;
right: 0px;
top: 0px;
width: 3in;
background-color: green;
}
#Menu .item {
width: 100%;
color: white;
height: 0.3in;
background-color: #ff4343;
border-style: solid;
border-width: 0 0 0 0.032in;
border-color: #ff6c6c;
}
#Menu .item:nth-child(odd) {
background-color: #ff5454;
}
#Menu .bottom {
flex: 1;
flex-wrap: wrap;
display: flex;
flex-direction: row;
width: 100%;
background-color: blue;
}
#Menu .bottom .box {
flex: 1;
/* width:50%; */
background-color: #f3f3f3;
}
#Menu .bottom .box:nth-child(even) {
background-color: #eaeaea;
}
<div id="Menu">
<div class="top">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="bottom">
<div id="" class="box">1</div>
<div id="" class="box">2</div>
<div id="" class="box">3</div>
<div id="" class="box">4</div>
</div>
</div>
Target result:
Here is example :
#Menu {
display: flex;
flex-direction: column;
height: 100vh;
position: absolute;
z-index: 100;
right: 0px;
top: 0px;
width: 3in;
background-color: green;
}
#Menu .item {
width: 100%;
color: white;
height: 0.3in;
background-color: #ff4343;
border-style: solid;
border-width: 0 0 0 0.032in;
border-color: #ff6c6c;
}
#Menu .item:nth-child(odd) {
background-color: #ff5454;
}
#Menu .bottom {
flex: 1;
flex-wrap: wrap;
display: flex;
flex-direction: row;
width: 100%;
background-color: blue;
}
.box {
flex: 1;
flex-basis:50%;
/*width:50%;*/
background-color: #f3f3f3;
}
.bottom div:first-child, .bottom div:last-child
{
background-color: #eaeaea;
}
<div id="Menu">
<div class="top">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="bottom">
<div id="" class="box">1</div>
<div id="" class="box">2</div>
<div id="" class="box">3</div>
<div id="" class="box">4</div>
</div>
</div>
In .box added flex-basis:50% (This defines the default size of an element before the remaining space is distributed. The main-size value makes it match the width or height, depending on which is relevant based on the flex-direction.). But I have to change about set background-color for .box
Change your bottom box css rules as follows. I hope this will work for you
#Menu .bottom .box {
/*flex: 1;*/
width:50%;
background-color: #f3f3f3;
}
The main problem is flex: 1 and absence of width for the box. The solution is below, not sure about the grey backgrounds:
/* Menu / Primary Styles */
#Menu {
display: flex;
flex-direction: column;
height: 100vh;
position: absolute;
z-index: 100;
right: 0px;
top: 0px;
width: 3in;
background-color: green;
}
#Menu .item {
width: 100%;
color: white;
height: 0.3in;
background-color: #ff4343;
border-style: solid;
border-width: 0 0 0 0.032in;
border-color: #ff6c6c;
}
#Menu .item:nth-child(odd) {
background-color: #ff5454;
}
#Menu .bottom {
flex: 1;
flex-wrap: wrap;
display: flex;
flex-direction: row;
width: 100%;
background-color: blue;
}
#Menu .bottom .box {
background-color: #f3f3f3;
width: 50%;
}
#Menu .bottom .box:nth-child(even) {
background-color: #eaeaea;
}
<div id="Menu">
<div class="top">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
<div class="bottom">
<div id="" class="box">1</div>
<div id="" class="box">2</div>
<div id="" class="box">3</div>
<div id="" class="box">4</div>
</div>
</div>
I hope this will help you out. It might not be the size you want but its a 2x2 flexbox. Maybe you could change the css to fit it as you want it.
.flex-grid {
display: flex;
flex-wrap: wrap;
height: 500px;
}
.grid-item {
flex: 1 1 calc(100% - 50px);
background: #F90;
border-top: solid 1px #000;
}
.grid-item:nth-child(odd) {
background: #F00;
flex: 0 0 50px;
}
<div class="flex-grid">
<div class="grid-item">
A1
</div>
<div class="grid-item">
B1
</div>
<div class="grid-item">
A2
</div>
<div class="grid-item">
B2
</div>
</div>