I have the following grid: https://codesandbox.io/s/react-and-scss-forked-bwy6v?file=/src/index.js and Im looking for a better/shorter way to write this grid.
My first time using a grid.
This is actually a small part of a responsive website that i'm building.
.addTransaction {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 50px;
& div {
border: solid 1px red;
}
& .buySell {
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 2;
display: grid;
grid-template-columns: 0.5fr 0.5fr;
column-gap: 5px;
& .sell {
width: 100%;
}
& .buy {
width: 100%;
}
}
& .token {
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 2;
grid-row-end: 3;
}
& .quantity {
grid-column-start: 1;
grid-column-end: 1;
grid-row-start: 3;
grid-row-end: 4;
}
& .price {
grid-column-start: 2;
grid-column-end: 2;
grid-row-start: 3;
grid-row-end: 4;
}
& .date {
grid-column-start: 1;
grid-column-end: 1;
grid-row-start: 4;
grid-row-end: 5;
}
& .exchange {
grid-column-start: 2;
grid-column-end: 2;
grid-row-start: 4;
grid-row-end: 5;
}
& .totalSpent {
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 5;
grid-row-end: 6;
}
& .addToCalc {
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 6;
grid-row-end: 7;
}
& .submit {
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 7;
grid-row-end: 8;
}
}
import styles from "./styles.module.scss";
const App = () => (
<div className={styles.addTransaction}>
<div className={styles.buySell}>
<div>
<button className={styles.buy}>BUY</button>
</div>
<div>
<button className={styles.sell}>SELL</button>
</div>
</div>
<div className={styles.token}>token</div>
<div className={styles.quantity}>quantity</div>
<div className={styles.price}>price</div>
<div className={styles.date}>date</div>
<div className={styles.exchange}>exchange</div>
<div className={styles.totalSpent}>totalSpent</div>
<div className={styles.addToCalc}>addToCalc</div>
<div className={styles.submit}>submit</div>
</div>
);
Please suggest a better way
Thanks
Some ways to make it shorter:
grid-column: auto / span 2;
.firstClass, .secondClass { example-property: 4px; }
The result is a lot shorter
.addTransaction {
display: grid;
grid-template-columns: 1fr 1fr;
grid-auto-rows: 50px;
& div {
border: solid 1px red;
}
& .buySell {
display: grid;
grid-template-columns: 0.5fr 0.5fr;
column-gap: 5px;
& .buy,
& .sell {
width: 100%;
}
}
& .buySell,
& .token,
& .totalSpent,
& .addToCalc,
& .submit {
grid-column: auto / span 2;
}
}
https://codesandbox.io/s/react-and-scss-forked-t8hcw?file=/src/styles.module.scss
Some other options:
.grid-template-areas
for really complex layouts. It can be a readability lifesaver over manually specifying grid-column
or grid-row
multiple times.display: contents
to flatten the hierarchy in CSS
& .addTransaction {
/* ... */
column-gap: 5px;
/* ... */
& .buySell {
display: contents;
& button {
width: 100%;
}
}
}