Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create vertical text that spans all rows in a CSS Grid?

Tags:

css

css-grid

Looking to create a section header for a grid (instead of taking up vertical space by having it above the section), like so:

enter image description here

It's easy of course to have the first column span the rows with grid-rows: 1 / span 3 but then if I transform the text and align/justify it, the div doesn't take up the full space of the column, so setting backgrounds/borders doesn't look right.

See trial pen

body {
  padding: 1em;
  font-family: sans-serif;
}

.my-grid {
  display: grid;
  grid-template-columns: 2rem 100px 10rem;
  background-color: #eee
}

.my-grid>* {
  border: 1px solid #ccc;
}

.section-title {
  grid-row: 1 / span 5;
  align-self: center;
  justify-self: center;
  transform: rotate(270deg);
  background-color: papayawhip;
  text-transform: uppercase;
}

input {
  border: 2px solid #000;
}
<div class="my-grid">
  <div class="section-title">Address</div>
  <div>Address 1</div>
  <input type="text">
  <div>Address 2</div>
  <input type="text">
  <div>City</div>
  <input type="text">
  <div>State</div>
  <input type="text">
  <div>Zip Code</div>
  <input type="text">
</div>
like image 204
redOctober13 Avatar asked Dec 22 '17 17:12

redOctober13


People also ask

How do I align text vertically in CSS Grid?

To align objects apply CSS to the parent element which becomes the grid container and the element's child which becomes the items in the grid. Approach: Use the align-content property of CSS grids to vertically align objects.

Which is better flex or grid CSS?

CSS grids are for 2D layouts. It works with both rows and columns. Flexbox works better in one dimension only (either rows OR columns). It will be more time saving and helpful if you use both at the same time.

What is grid span CSS?

The grid-column CSS shorthand property specifies a grid item's size and location within a grid column by contributing a line, a span, or nothing (automatic) to its grid placement, thereby specifying the inline-start and inline-end edge of its grid area.

What is 1fr in CSS Grid?

Each column is equal. 1fr=25% of the available space.


1 Answers

Yes, using writing-mode

The writing-mode CSS property defines whether lines of text are laid out horizontally or vertically and the direction in which blocks progress.

MDN

body {
  padding: 1em;
  font-family: sans-serif;
}

.my-grid {
  display: grid;
  grid-template-columns: 2rem 100px 10rem;
  background-color: #eee
}

.my-grid>* {
  border: 1px solid #ccc;
}

.section-title {
  grid-row: 1 / span 5;
  text-align: center;
  writing-mode:vertical-rl; /* vertical text */
  line-height:2rem; /* center in div */
  transform:rotate(180deg); /* spin to orient as required */
  background-color: papayawhip;
  text-transform: uppercase;
}

input {
  border: 2px solid #000;
}
<div class="my-grid">
  <div class="section-title">Address</div>
  <div>Address 1</div>
  <input type="text">
  <div>Address 2</div>
  <input type="text">
  <div>City</div>
  <input type="text">
  <div>State</div>
  <input type="text">
  <div>Zip Code</div>
  <input type="text">
</div>
like image 56
Paulie_D Avatar answered Oct 07 '22 18:10

Paulie_D