how to add space bettween elements in flex box - css

I need to display a list of scheduled jobs in an endless scroll list of angular material,
for each element in the list, I need to divide it into 3 columns using flexbox.
in each column, I want to display certain data.
for some reason, I'm not able to add space between columns inside the list.
here is my code in html
<app-nav>
<div class="grid-container">
<div class="row1">
<mat-grid-list cols="6" rowHeight="100px">
<mat-grid-tile [colspan]="1" [rowspan]="1" [style.background]="lightgreen">
<h2>All Scheduled Jobs</h2>
</mat-grid-tile>
<mat-grid-tile [colspan]="4" [rowspan]="1" [style.background]="lightgreen">
</mat-grid-tile>
<mat-grid-tile [colspan]="1" [rowspan]="1" [style.background]="lightgreen">
<button (click)="openCreateScheduledJobDialog()" mat-mini-fab color="primary"
aria-label="Example icon button with a plus one icon">
<mat-icon>plus_one</mat-icon>
</button>
</mat-grid-tile>
</mat-grid-list>
</div>
<div class="row2"> // the problem is in this part:
<cdk-virtual-scroll-viewport itemSize="70" class="example-viewport">
<mat-list>
<mat-list-item *cdkVirtualFor="let scheduledjob of scheduledJobs" class="example-item">
<div class="flex-container-list-item"> // here is the flex that i need to fix
<div class="details">
<h3 matLine>{{getJob(scheduledjob.jobID)}} Job</h3>
<h4 matLine> Status: {{getJobStatus(scheduledjob.isActive)}}</h4>
<p matLine>
<span> Start time: {{scheduledjob.startTime | date}} </span>
</p>
<p matLine>
<span> End time: {{scheduledjob.endTime | date}}</span>
</p>
<p matLine>
<span> Runs Every: {{scheduledjob.daysFrequency}} days
</span>
</p>
<p matLine>
<button mat-stroked-button color="primary"
(click)="openMachineDetailsDialog(scheduledjob.machineSerialNumber)">Machine Details</button>
</p>
</div>
<div class="edit"> <button mat-flat-button color="primary" (click)="openUpdateScheduledJobDialog(scheduledjob)">Edit</button></div>
<div class="delete"> <button mat-raised-button (click)="delete(scheduledjob)" color="warn">Delete</button>
</div>
</div>
<mat-divider></mat-divider>
</mat-list-item>
</mat-list>
</cdk-virtual-scroll-viewport>
</div>
<div class="row3">
</div>
</div>
</app-nav>
note that im using angular material cdk-virtual-scroll-viewport
here is the css:
.row1 {
grid-area: row1;
}
.row2 {
grid-area: row2;
}
.row3 {
grid-area: row3;
}
.grid-container {
display: grid;
grid-template-areas:
'row1 row1 row1'
'row2 row2 row2'
'row3 row3 row3 '
;
grid-gap: 2px;
padding: 2px;
}
.grid-container>div {
text-align: center;
padding: 10px 0;
font-size: 15px;
}
.example-viewport {
height: 600px;
border: 2px solid gray;
}
.example-item {
height: 50px;
}
/* ******************* list item flex ***************** */
.details {
background-color:lightcoral ;
}
.edit {
background-color: lightgreen;
}
.delete {
background-color: lightskyblue;
}
.flex-container-list-item {
display: flex;
border: 6px solid red;
justify-content: space-between;
--gap: 10rem;
flex-wrap: wrap;
}
.grid-container-list-item>div {
text-align: center;
padding: 10px 0;
font-size: 15px;
}

Use either the gap property on the flex container, or set [margin](http://developer.mozilla.org/en-US/docs/Web/CSS/margin) on your flex items.
Your current solution doesn’t seem to be working because you set a CSS custom property --gap: 10rem on your flex container, but then don’t actually apply that property (at least in the provided code).
You can use gap, and that will ensure a fixed amount between the flex items. Browser support for gap is good, with the exception of Safari before version 14.1.
Alternatively, you could set margin-right on all your flex items with the exception of the last item to achieve a similar effect.
Here’s a working (simplified) example:
.flex-container-list-item {
display: flex;
border: 6px solid red;
gap: 2rem;
flex-wrap: wrap;
}
.details {
background-color: lightcoral;
}
.edit {
background-color: lightgreen;
}
.delete {
background-color: lightskyblue;
}
<div class="flex-container-list-item">
<div class="details">
<h3>Job</h3>
<h4>Status: active</h4>
<p>
<span> Start time: 2 PM</span>
</p>
<p>
<span> End time: 4 PM</span>
</p>
<p>
<span> Runs Every: 3 days </span>
</p>
<p>
<button>Machine Details</button>
</p>
</div>
<div class="edit">
<button
mat-flat-button
color="primary"
(click)="openUpdateScheduledJobDialog(scheduledjob)"
>
Edit
</button>
</div>
<div class="delete">
<button mat-raised-button (click)="delete(scheduledjob)" color="warn">
Delete
</button>
</div>
</div>

Ideally you should do the followings and it should work :
.container{
display : flex;
flex-flow: row wrap;
justify-content: space-between;
}
.item{
margin: 10px;
height: 100px;
width: 100px;
}
.item1{
background-color: red;
}
.item2{
background-color: yellow;
}
<div class="container">
<div class="item item1"></div>
<div class="item item2"></div>
</div>
even though your question is not so clear. you can do following update on your HTML and css files :
.flex-container-list-item{
display: flex;
flex-flow: row wrap;
justify-content: space-between;
}
<div class="flex-container-list-item">
<div class="details">details contents</div>
<div class="action">
<button
mat-flat-button
color="primary"
(click)="openUpdateScheduledJobDialog(scheduledjob)"
>
Edit
</button>
<button mat-raised-button (click)="delete(scheduledjob)" color="warn">
Delete
</button>
</div>
</div>

Related

CSS grid - 1fr col keeps getting cut-off

I can't get this css grid to do these 2 things..
Value column should always show the entire string, not wrap, and not overflow
The hearts column should always stay lined up vertically
Here's an explanation of the problem in an image:
I've been working on this for too long, thank you for helping me!!! <3
Codepen simplified down: https://codepen.io/naaadz/pen/OJQZvgB
This is what I have to start with:
grid-template-columns: auto 150px auto 1fr;
I am not sure that it is possible with your current HTML structure.
If you change the grid element to hold your columns, you can achieve it.
Currently, you are having a lot of grids with a single row.
Below is an example of what I mean.
You can change the first column width to auto.
I saw that you are limiting it to 150px, so I used the minmax() property.
.box {
width: 300px;
background: lightgray;
overflow: hidden;
display: grid;
grid-template-columns: minmax(40px, 150px) 1fr;
gap: 10px;
padding: 3px 5px;
align-items: center;
}
.item {
display: flex;
align-items: center;
}
.font-icon {
margin-right: 10px;
}
.value {
text-align: right;
white-space: nowrap;
}
.truncate {
display: -webkit-box !important;
-webkit-box-orient: vertical;
-webkit-line-clamp: 1;
overflow: hidden;
word-break: break-all;
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<div class="box">
<div class="item">
<i class="font-icon material-icons">circle</i>
<span class="name truncate">Some value here</span>
</div>
<div class="item">
<i class="font-icon material-icons">favorite</i>
<span class="value item-right">16,000 UNITS</span>
</div>
<div class="item">
<i class="font-icon material-icons">circle</i>
<span class="name truncate">Some longer value here longer</span>
</div>
<div class="item">
<i class="font-icon material-icons">favorite</i>
<span class="value item-right">33,000 PPL</span>
</div>
<div class="item">
<i class="font-icon material-icons">circle</i>
<span class="name truncate">Some other value</span>
</div>
<div class="item">
<i class="font-icon material-icons">favorite</i>
<span class="value item-right">23,000,000 PPL</span>
</div>
</div>

Change css variable for a section without affecting layout

I have a variable called --primary-color defined at root like this:
:root {
--primary-color: red;
}
In a small section in my site I want to change the primary-color for all nested elements. I currently change the primary color like this:
<button class="PrimaryButton">red</button>
<div style="--primary-color: purple">
<button class="PrimaryButton">purple</button>
</div>
However notice i had to put the purple style on a div. This changes layout. Is there some element or someway I can change the variable without using a layout element like a div? I can't use as that affects global.
Here is example:
:root {
--primary-color: red;
}
.PrimaryButton {
background-color: var(--primary-color);
width: 100px;
height: 40px;
}
<button class="PrimaryButton">red</button>
<div style="--primary-color: purple">
<button class="PrimaryButton">purple</button>
</div>
Thank you!
Solution #1. If I understood your question correctly, you can keep a div in the structure, but make it not affect the layout too much with display: contents;. I've extended your example (with a CSS grid and a couple of elements added for clarity) so you can see that the div with display: content; has no effect on the structure:
:root {
--primary-color: red;
}
.PrimaryButton {
background-color: var(--primary-color);
width: 100px;
height: 40px;
}
.grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
}
<section class="grid">
<button class="PrimaryButton">red 1</button>
<div style="--primary-color: purple; display: contents;">
<button class="PrimaryButton">purple 1</button>
<button class="PrimaryButton">purple 2</button>
</div>
<div style="--primary-color: aquamarine; display: contents;">
<button class="PrimaryButton">aquamarine 1</button>
<button class="PrimaryButton">aquamarine 2</button>
</div>
<div style="--primary-color: pink; display: contents;">
<button class="PrimaryButton">pink 1</button>
</div>
</section>
Solution #2.. If you need to support older browsers, then CSS Combinators such as ~ or + (+ display:none for third-party elements) can help you. Example below:
:root {
--primary-color: red;
}
.PrimaryButton {
background-color: var(--primary-color);
width: 100px;
height: 40px;
}
.grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
}
.primary-color-changer,
.primary-color-only-one {
display: none;
}
.primary-color-only-one.purple+.PrimaryButton,
.primary-color-changer.purple~.PrimaryButton {
--primary-color: purple;
}
.primary-color-only-one.aquamarine+.PrimaryButton,
.primary-color-changer.aquamarine~.PrimaryButton {
--primary-color: aquamarine;
}
.primary-color-only-one.pink+.PrimaryButton,
.primary-color-changer.pink~.PrimaryButton {
--primary-color: pink;
}
.primary-color-only-one.green+.PrimaryButton,
.primary-color-changer.green~.PrimaryButton {
--primary-color: green;
}
.primary-color-only-one.color-default+.PrimaryButton,
.primary-color-changer.color-default~.PrimaryButton {
--primary-color: inherit;
}
<section class="grid">
<button class="PrimaryButton">red 1 (default)</button>
<div class="primary-color-only-one green"></div>
<button class="PrimaryButton">green 1(only one)</button>
<div class="primary-color-changer purple"></div>
<button class="PrimaryButton">purple 1</button>
<button class="PrimaryButton">purple 2</button>
<div class="primary-color-changer aquamarine"></div>
<button class="PrimaryButton">aquamarine 1</button>
<button class="PrimaryButton">aquamarine 2</button>
<div class="primary-color-changer pink"></div>
<button class="PrimaryButton">pink 1</button>
<div class="primary-color-changer color-default"></div>
<button class="PrimaryButton">red 2 (default)</button>
<button class="PrimaryButton">red 3 (default)</button>
</section>

CSS Grid + Flexbox to create new layout for mobile

I might or might not need flexbox for this issue, but I'm still learning and trying to understand how I can get the title to be on top of the add button, which should fill about 96% of the row's width under the title.
I created a stackblitz to demonstrate current code. Also the desktop version should allow title and add button to occupy same row at 70% 30% grid like in stackblitz demo.
This is the desired result when you collapse the browser window to mobile
html
<div class="listingBody">
<div class="listingParent">
<div class="listingCard">
<div class="topRow">
<span id="title">title</span><span
id="addButtonSpan">
<button id="addButton" type="button">
Add Button</button>
</span>
</div>
</div>
</div>
</div>
css
#title {
text-align:center;
}
.topRow {
display: grid;
grid-template-columns: 70% 30%;
}
This is how I would go about it using just flexbox not css grid.
I use a media query to do the mobile stacked layout with flex-direction: column
.topRow {
display: flex;
}
.topRow #title {
width: 70%;
}
.topRow #addButtonSpan {
width: 30%;
}
#media only screen and (max-width: 600px) {
.topRow {
flex-direction: column;
}
.topRow span {
margin: 0 auto;
text-align: center;
}
.topRow #title {
width: 96%;
}
.topRow #addButtonSpan {
width: 96%;
}
}
<div class="listingBody">
<div class="listingParent">
<div class="listingCard">
<div class="topRow">
<span id="title">title</span>
<span id="addButtonSpan">
<button id="addButton" type="button">
Add Button
</button>
</span>
</div>
</div>
</div>
</div>

Create a horizontal scrollable "slider" with angular 9 and material

we have the following component
<div layout="row">
<div *ngFor="let a of cardsArray">
<mat-card class="card-card">
<mat-card-header>
<div mat-card-avatar class="example-header-image"></div>
<mat-card-title>IT29TB92881102020233</mat-card-title>
<mat-card-subtitle>Deposit</mat-card-subtitle>
</mat-card-header>
<mat-card-content>
<p>
Valid thru <b>02/23</b>
</p>
</mat-card-content>
<mat-card-actions>
<button mat-button><mat-icon>list_alt</mat-icon>See Statement</button>
<button mat-button><mat-icon>share</mat-icon>Copy Requisites</button>
</mat-card-actions>
</mat-card>
</div>
</div>
.card-card {
max-width: 320px;
}
div[layout="row"] {
overflow: auto;
}
It displays a list of "cards" verically.
How should I create a horizontally scollable list of "cards" and keep it responsive yet?
Thanks
Thanks, actually the "issue" was in css, but okay, might be useful for angular starters...
.card-card {
max-width: 320px;
margin-top: 10px;
margin-bottom: 10px;
margin-right: 5px;
margin-left: 5px;
}
div[layout="row"] {
overflow-x: auto;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}

Bootstrap Form Spacing

Ive been working on this form but I am now having trouble getting things to line up properly. On smaller sizes its not to big an issue, but on large screens the problem is very noticeable.
Two main issues I am finding is that I have some social links that is getting extra spacing from somewhere, though there is no padding or margins between them that I can see. Also the actual form itself is not filling in the whole space. Im not exactly sure how to fix the spacing issues here.
Anyone offer any tips?
Here is a JsFiddle of the code the code saved to SO and Jsfiddle are exact same, though it appears they are giving different results. Not sure why, though clearly I probably messed something up here.
https://jsfiddle.net/Tsukiyono/aq9Laaew/283640/
Here is another link showing things on my gitpages link:
https://tsukiyonocm.github.io/test/
edit A image of the layout I am trying to achieve.
https://imgur.com/a/al8KEMh
body {
background-color: black;
}
/** Contact
---------------------------------------------------------*/
#contactus {
color: black;
max-height: 62.5rem;
width: auto;
}
.contact-title {
font-family: futura-pt-condensed, sans-serif;
font-weight: 700;
padding: 1rem 0;
}
.contact-info {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
}
.custom-contact {
max-height: 15rem;
}
.social {
font-weight: 700;
}
.custom-social {
max-height: 15rem;
width: auto;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
.social-contact ul {
list-style: none;
display: flex;
justify-content: center;
padding: 0;
}
.social-contact ul li {
padding: 0 1rem;
}
.btn-sub {
letter-spacing: 1.5;
color: black;
background-color: #fff;
font-weight: 700;
border-radius: 0.15rem;
white-space: normal;
}
.btn-form {
width: 100%;
letter-spacing: 1.5;
color: black;
background-color: #fff;
font-weight: 700;
border-radius: 0.15rem;
}
form {
max-width: 100%;
margin: 0 .5rem;
}
.form-control {
background-color: rgb(46, 46, 46);
border: 1px solid white;
border-radius: 0.2rem;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />
<section id="contactus">
<div class="container">
<div class="row">
<div class="col-12 text-center">
<h1 class="display-3 contact-title">Contact Us</h1>
</div>
</div>
<div class="row">
<div class="col-12 col-lg-6">
<div class="row contact-info d-flex flex-md-row flex-lg-column">
<div class="col-12 col-md-6 custom-contact">
<h3>- Our Office -</h3>
<address>
SOCKEROO Marketing<br>
123 Address Rd.<br>
Pittsburgh, PA<br>
15222<br>
TEL - 555.555.5555<br>
Email - sockeroomarketing#gmail.com
</address>
</div>
<div class="col-12 col-md-6 custom-social">
<p class="social">- Say Hello -</p>
<div class="social-contact">
<ul class="">
<li><i class="fab fa-inverse fa-twitter-square fa-lg"></i></li>
<li><i class="fab fa-inverse fa-facebook-square fa-lg"></i></li>
<li><i class="fab fa-inverse fa-linkedin fa-lg"></i></li>
<li><i class="fab fa-inverse fa-instagram fa-lg"></i></li>
</ul>
</div>
<button class="btn btn-sub">Subscribe to Our Newsletter</button>
</div>
</div>
</div>
<div class="col-12 col-lg-6 m-0 p-0">
<form action="">
<div class="form-group">
<input type="text" class="form-control" placeholder="Name">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Company or Organization">
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Email Address">
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Phone Number">
</div>
<div class="form-group">
<textarea name="comment" class="form-control" placeholder="Tell us a bit about your project, timeline, and budget"></textarea>
</div>
<button class="btn btn-form">Submit</button>
</form>
</div>
</div>
</div>
</section>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
In regards to the inconsistency between JSFiddle and Github pages, I believe the problem lies in the way bootstrap implements their col-md-6 class. Namely, the flex-basis property.
For reference, this is what I see their class to be, based on the element inspector in chrome:
#media (min-width: 768px)
.col-md-6 {
-ms-flex: 0 0 50%;
flex: 0 0 50%;
max-width: 50%;
}
(A little context: flex is shorthand for defining flex-grow, flex-shrink, and flex-basis, in that order. You can find that info in this article on CSS-Tricks: here and a little more detail on flex basis just above it, here).
As you can see, they're setting flex basis to 50%. Once I set both the jsfiddle and gitpages' flex property to flex: 0 0 auto, the spacing was consistent on both of those pages, leading me to wonder if flex-basis is calculating 50% to be one value in the JSFiddle and another value in your Github pages. As for a solution, what you can do is create a custom class, let's say .cust-col-md-6, and copy the ruleset defined by bootstrap, but change the flex-basis of the property to auto:
#media (min-width: 768px)
.cust-col-md-6 {
-ms-flex: 0 0 auto;
flex: 0 0 auto;
max-width: 50%;
}
Then from here you can set that as your class instead of using .col-md-6:
<div class="col-12 cust-col-md-6 custom-social">

Resources