I am trying to render rows with the date on the left and some text on the right.
I cant seem to work out how I changed the widths of the columns though?
Currently my day and text column are the same width.
I'd like to have the day as 50px and the text as 300px.
Is it possible with this code?
.sb-flex-row {
flex-direction: row;
display: flex;
}
.sb-flex-column {
flex-direction: column;
display: flex;
}
.sb-flex-body {
display: flex;
}
.sb-flex-body div:not([class*="flex"]) {
border: 1px solid white;
flex: 1 1 50px;
width: 200px;
}
<div class="sb-flex-body">
<div class="sb-flex-row">
<div style="background: #0980cc;">day</div>
</div>
<div class="sb-flex-column">
<div style="background: #09cc69;">month</div>
<div style="background: #cc092f;">year</div>
</div>
<div class="sb-flex-row">
<div style="background: #0980cc;">text</div>
</div>
</div>
I cleaned up and make it maybe simpler for you to understand. You have an outer wrapper that is flexible. The default direction is row and that is ok. Then you have two blocks. one block dateBlock and the second is a text block. the dateblock contains two blocks (a,b). and b you can assign the direction to the column. Afterward, you can assign the width to the text block and Dateblock. That is it :-)
.wrapper {
display: flex;
gap:10px;
}
.dateBlock {
display: flex;
background: yellow;
gap: 20px;
padding: 10px;
box-sizing: border-box;
}
.textBlock {
background: green;
padding: 10px;
box-sizing: border-box;
width: 300px;
}
.monthYear {
display: flex;
flex-direction: column;
width:50px;
}
.day {
font-size: 30px;
width: 50px;
}
<div class="wrapper">
<div class="dateBlock">
<div class="day">day</div>
<div class="monthYear">
<div>month</div>
<div>year</div>
</div>
</div>
<div class="textBlock">text</div>
</div>
I rearranged your markup a bit to easily target these divs in the style sheet. You can see the changes I made to your CSS under /* changes */. Also, take note of the HTML I adjusted.
.sb-flex-row {
flex-direction: row;
display: flex;
}
.sb-flex-column {
flex-direction: column;
display: flex;
}
.sb-flex-body {
display: flex;
}
/* changes */
.sb-flex-body div:not([class*="flex"]) {
border: 1px solid white;
width: 50px;
}
.sb-flex-row > div {
min-width: 300px;
}
<div class="sb-flex-body">
<div class="sb-flex-column">
<div style="background: #0980cc;">day</div>
<div style="background: #09cc69;">month</div>
<div style="background: #cc092f;">year</div>
</div>
<div class="sb-flex-row">
<div style="background: #0980cc;">text</div>
</div>
</div>
Related
so I have a goal but I have no idea if it's possible or not:
Here's an image of what i got: https://screencast.com/t/88ImEmMif
And code:
<style type="text/css">
#contrainer {
width: 100%;
}
#main {
width: 600px;
margin:0 auto;
}
.wrapper {
display: inline-block;
}
.parent {
flex-direction: column;
}
.child {
border: 1px solid #0EA2E8;
margin: 2px;
padding: 5px;
display: inline-block;
}
</style>
<div id="container">
<div id="main">
<div class="anchors">
<div class="wrapper">
<div class="parent">
<?php
$links = array("https://google.com","foo", "bar", "hello", "world","google.com","google.com/adwords/","foo", "bar", "hello", "world","https://google.com/gmail","https://google.com/adwords/");
foreach($links as $link) {
echo '<div class="child">'.$link.'</div>';
}
?>
</div>
</div>
</div>
</div>
</div>
And here is what I'm looking to do: https://screencast.com/t/ze5fCya3wpbJ
So each of the boxes will have the same width in col's depending on the length in the box.
You could give a try to flex setting a fixed height:
example using lorem ipsum text. (your pHp is not the HTML output)
div {/* your .parent */
display: flex;
height: 4em;/* makes about 2 lines before wrapping to next */
flex-direction: column;
flex-wrap: wrap;
width: 500px;
margin: auto;
}
b {/* your .child */
border: solid 1px turquoise;
margin: 2px;
padding:2px;
}
<div>
<b>lorem</b>
<b>Pellentesque</b>
<b>habitant</b>
<b>morbi</b>
<b>tristique</b>
<b>senectus</b>
<b>et</b>
<b>netus</b>
<b>et</b>
<b>malesuada</b>
<b>fames</b>
<b>ac</b>
<b>turpis</b>
<b>egestas.</b>
</div>
An alternative would be using FlexBox on the outer div, applying the styles flex-wrap: wrap; and justify-content: space-between; to make them evenly spaced and distributed inside the outer div. Thanks to FlexBox each of the inner contents will have their own width.
.box {
width: 600px;
margin:0 auto;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
p {
margin: 0 0 5px;
padding: 3px 5px;
border: 1px solid black;
}
<div class="box">
<p>Bulbasaur</p>
<p>Ivysaur</p>
<p>Venusaur</p>
<p>Charmander</p>
<p>Charmeleon</p>
<p>Ash</p>
<p>Charizard</p>
<p>Squirtle</p>
<p>Wartortle</p>
<p>Blastoise</p>
<p>Ekans</p>
<p>Pikachu</p>
<p>Paras</p>
<p>Abra</p>
</div>
Consider the following fiddle: https://jsfiddle.net/7naxprzd/1/
Requirements are:
two columns with header and contents
tops of columns should align
bottom of columns should align
on top of each column there should be a horizontally centered arrow
The code is working properly if the arrows are eliminated by deleting the div with class .arrow-wrapper, but I need the arrows.
A solution could be to absolute position the arrow, but is there a way to solve this layout issue with flex without absolute positioning the arrow?
Should work for modern browsers and IE11.
.row-wrapper {
display: flex;
}
.col-wrapper-outer {
display: flex;
flex-wrap: wrap;
}
.arrow-wrapper {
width: 100%;
text-align: center;
font-size: 30px;
}
.col-wrapper {
width: 100%;
display: flex;
flex-direction: column;
border: 2px solid red;
color: white;
}
.col-wrapper .header {
background: blue;
}
.col-wrapper .contents {
flex: 1 0 auto;
background: green;
}
<div class="row-wrapper">
<div class="col-wrapper-outer">
<div class="arrow-wrapper">
↓
</div>
<div class="col-wrapper">
<div class="header">Please align tops.</div>
<div class="contents">Let me grow.</div>
</div>
</div>
<div class="col-wrapper-outer">
<div class="arrow-wrapper">
↓
</div>
<div class="col-wrapper">
<div class="header">please align tops.</div>
<div class="contents">Let me grow.<br>Please<br>align<br>bottoms.</div>
</div>
</div>
</div>
In your div with class col-wrapper-outer, instead of this:
.col-wrapper-outer {
display: flex;
flex-wrap: wrap;
}
Use this:
.col-wrapper-outer {
display: flex;
flex-direction: column;
}
Then add flex: 1 to .col-wrapper so it takes the full height of the container.
revised fiddle
.row-wrapper {
display: flex;
}
.col-wrapper-outer {
display: flex;
/* flex-wrap: wrap; */
flex-direction: column; /* NEW */
}
.arrow-wrapper {
width: 100%;
text-align: center;
font-size: 30px;
}
.col-wrapper {
flex: 1; /* NEW */
width: 100%;
display: flex;
flex-direction: column;
border: 2px solid red;
color: white;
}
.col-wrapper .header {
background: blue;
}
.col-wrapper .contents {
flex: 1 0 auto;
background: green;
}
<div class="row-wrapper">
<div class="col-wrapper-outer">
<div class="arrow-wrapper">
↓
</div>
<div class="col-wrapper">
<div class="header">Please align tops.</div>
<div class="contents">Let me grow.</div>
</div>
</div>
<div class="col-wrapper-outer">
<div class="arrow-wrapper">
↓
</div>
<div class="col-wrapper">
<div class="header">please align tops.</div>
<div class="contents">Let me grow.
<br>Please
<br>align
<br>bottoms.</div>
</div>
</div>
</div>
I am trying to achieve what you see at the bottom of the panel in the image below. Each of the 3 items are centered but the text is left aligned.
I have developed the following basic CSS and HTML code. I am trying to use flexbox as much as possible for responsive layout. Anyone have any pure HTML/CSS solution?
I understand that the p tag is a block level element. So what are my options without setting the width of the p tag? Or maybe there is another tag I could use?
The HTML and CSS code I have provided below has the basic structure only.
.panel {
display: flex;
border: 1px solid black;
min-height: 300px;
flex-direction: column;
max-width: 500px;
}
.panel-body {
display: flex;
flex: 1;
flex-flow: row wrap;
justify-content: center;
}
.panel-heading {
padding: 10px 10px;
}
.panel-body div.chart {
flex: 0 0 100%;
min-height: 150px;
background-color: green;
}
.panel-body div {
text-align: center;
flex: auto;
background-color: red;
display: flex;
flex-direction: column;
justify-content: space-around;
}
p {
border: 0;
padding: 0;
margin: 0;
text-align: left;
}
<div class="panel">
<div class="panel-heading">
HEADING
</div>
<div class="panel-body">
<div class="chart"></div>
<div>
<p>HIGH
<br/>144</p>
</div>
<div>MEDIUM
<br/>2</div>
<div>LOW
<br/>3</div>
</div>
</div>
Just changed styles of .panel-body div. Also there is no need for p tag here, consider removing it from markup. Demo:
.panel {
display: flex;
border: 1px solid black;
min-height: 300px;
flex-direction: column;
max-width: 500px;
}
.panel-body {
display: flex;
flex: 1;
flex-flow: row wrap;
justify-content: center;
}
.panel-heading {
padding: 10px 10px;
}
.panel-body div.chart {
flex: 0 0 100%;
min-height: 150px;
background-color: green;
}
.panel-body div {
/* Take 33.33% width, allow grow and shrink */
flex: 1 1 33.33%;
background-color: red;
/* become a flex-container */
display: flex;
/* align flex-items vertically */
flex-direction: column;
/* center vertically */
justify-content: center;
/* center horizontally */
align-items: center;
}
p {
border: 0;
padding: 0;
margin: 0;
text-align: left;
}
<div class="panel">
<div class="panel-heading">
HEADING
</div>
<div class="panel-body">
<div class="chart"></div>
<div>
<p>HIGH
<br/>144</p>
</div>
<div>MEDIUM
<br/>2</div>
<div>LOW
<br/>3</div>
</div>
</div>
Try this HTML code:
<div class="panel">
<div class="panel-heading">
HEADING
</div>
<div class="panel-body">
<div class="chart"></div>
<div><p>HIGH<br/>144</p></div>
<div><p>MEDIUM<br/>2</p></div>
<div><p>LOW<br/>3</p></div>
</div>
</div>
It appears that you originally only put the div containing "HIGH" and "144" in a <p> tag, which, according to your css code, is the attribute that is being styled to have left-aligned text. However, the content within the other 2 <div>s were not enclosed within a <p> tag, and so they were not being styled.
I am using flexbox to center content with justify-content: center which works as intended but flexbox also moves divs to be side by side which I don't want.
Here is an example
.flex {
display: flex;
justify-content: center;
}
.content {
width: 100px;
height: 100px;
background-color: #000;
margin: 10px;
}
<div class="flex">
<div class="content">
</div>
<div class="content">
</div>
</div>
How can I use flexbox while retaining the default one div on top of the other positioning.
You can set flex-direction: column and then you have to use align-items: center. Default flex-direction is row.
.flex {
display: flex;
align-items: center;
flex-direction: column;
}
.content {
width: 100px;
height 100px;
color: #000;
border: 1px solid black;
padding: 5px;
margin: 5px;
}
<div class="flex">
<div class="content"></div>
<div class="content"></div>
</div>
Try following code,
.flex {
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
}
.content {
width: 100px;
height: 100px;
background-color: #000;
margin: 10px;
}
<div class="flex">
<div class="content">
</div>
<div class="content">
</div>
</div>
Sorry for the really vague question title but I have no idea how to best describe it...
Take the following code example:
.wrapper {
border: 2px solid green;
}
.container {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
border: 2px solid red;
margin: 5px;
height: 64px;
}
.item {
display: flex;
border: 2px solid #0000ff;
padding: 3px;
}
.left {
justify-content: flex-end;
}
.right {
justify-content: flex-start;
}
<div class="wrapper" style="width: 100%">
<div class="container">
<div class="item left">FIXED</div>
<div class="item right">abc</div>
</div>
<div class="container">
<div class="item">FIXED</div>
</div>
<div class="container">
<div class="item left">FIXED</div>
<div class="item right">abcdefgh</div>
</div>
</div>
<br />
<div class="wrapper" style="width: 50%">
<div class="container">
<div class="item left">FIXED</div>
<div class="item right">abc</div>
</div>
<div class="container">
<div class="item">FIXED</div>
</div>
<div class="container">
<div class="item left">FIXED</div>
<div class="item right">abcdefgh</div>
</div>
</div>
Observations:
FIXED represents an element of fixed size, always same width/height.
The right side element can vary on size (mostly width).
The right side element is always aligned to the left.
The left side element is always aligned to the left.
What I'm trying to achieve:
The FIXED element should always be centered on the red row.
THE FIXED element on the first/third rows needs to be aligned to the right side of the FIXED element on the second row.
Here's an image to better demonstrate what I'm looking for:
EDIT: My final solution based on the #vals answer. I had to change this a bit because I'm using CSS Modules with selector composition in a React app and I need a single class per element.
.container {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
/* UNREQUIRED (FOR DEMONSTRATION ONLY) */
border: 2px solid red;
padding: 5px;
}
.container::before {
content: "";
flex: 1 0 14px; /* UNREQUIRED (FOR DEMONSTRATION ONLY) (14px -> 0) */
}
.container-single {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
/* UNREQUIRED (FOR DEMONSTRATION ONLY) */
border: 2px solid red;
padding: 5px;
}
.item {
display: flex;
flex: 0 0 auto;
/* UNREQUIRED (FOR DEMONSTRATION ONLY) */
border: 2px solid #0000ff;
padding: 5px;
}
.item-right {
display: flex;
flex: 1 0 0;
/* UNREQUIRED (FOR DEMONSTRATION ONLY) */
border: 2px solid #0000ff;
padding: 5px;
}
.item-left {
display: flex;
flex: 0 0 auto;
margin-left: auto;
/* UNREQUIRED (FOR DEMONSTRATION ONLY) */
border: 2px solid #0000ff;
padding: 5px;
}
<div class="container">
<div class="item-left">FIXED</div>
<div class="item-right">abc</div>
</div>
<br />
<div class="container-single">
<div class="item">FIXED</div>
</div>
<br />
<div class="container">
<div class="item-left">FIXED</div>
<div class="item-right">abcdefgh</div>
</div>
For anyone interested, the CSS selector composition goes like this:
.container {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.container::before {
flex: 1 0 0;
content: "";
}
.container-single {
composes: container;
}
.item {
display: flex;
flex: 0 0 auto;
}
.item-right {
flex: 1 0 0;
composes: item;
}
.item-left {
margin-left: auto;
composes: item;
}
I have changed the layout slighty, I have add a class on the single container.
Is it ok for the right element to grow ?
.container {
display: flex;
flex-direction: row;
border: 2px solid red;
padding: 5px;
}
.item {
display: flex;
flex-basis: auto;
flex-grow: 0;
border: 2px solid #0000ff;
padding: 5px;
}
.item.right {
flex-basis: 0;
flex-grow: 1;
}
.item.left {
margin-left: auto;
}
.container:not(.single):before {
content: "";
flex-basis: 14px; /* border 2px + padding 5px */
flex-grow: 1;
}
.single .item {
margin: 0px auto;
}
<div class="container">
<div class="item left">FIXED</div>
<div class="item right">abc</div>
</div>
<br />
<div class="container single">
<div class="item">FIXED</div>
</div>
<br />
<div class="container">
<div class="item left">FIXED</div>
<div class="item right">abcdefgh</div>
</div>