Styling Your Text within Grid Boxes Using CSS

After creating a simple CSS Grid, I am struggling with placing text inside the tiles at a specific position. The concept is illustrated in this image:

https://i.sstatic.net/13G7j.jpg

I aim to have the text within the blue box.

.tiles {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(5, 2fr);
  grid-column-gap: 0px;
  grid-row-gap: 0px;
  height: 1150px;
  cursor: pointer;
}

.box1 {
  background: #3030c9;
  grid-area: 1 / 1 / 2 / 3;
}
<div class="tiles">
  <div class="box box1">
    <h1 id="text">Test</h1>
  </div>
  <div class="box box2">Test</div>
  <div class="box box3">Test</div>
  <div class="box box4">Test</div>
</div>

Answer №1

To align the box1 class vertically, you can set it as a flex column with center alignment and then adjust the left margin of the header.

.tiles {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(5, 2fr);
  grid-column-gap: 0px;
  grid-row-gap: 0px;
  height: 1150px;
  cursor: pointer;
}

.box2 {
  background: #7d30c9;
}
.box3 {
  background: #c93030;
}
.box4 {
  background: #30c9c9;
}

.box1 {
  background: #3030c9;
  grid-area: 1 / 1 / 2 / 3;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.box1 h1 {
  margin-left: 1em;
}
<div class="tiles">
  <div class="box box1">
    <h1 id="text">Test</h1>
  </div>
  <div class="box box2">Test</div>
  <div class="box box3">Test</div>
  <div class="box box4">Test</div>
</div>

Answer №2

There are multiple ways to achieve this. Unfortunately, I couldn't open your screen shot. Nevertheless, if you wish to replace text at a specific position within a box, one approach is to select the text using its id.

#text{
  margin-left:40%;
  margin-top:20%; // You can specify values in pixels (px) or rems 
}

To align the text within the box, you can add it to the box class. This will ensure that your text is aligned correctly based on the box dimensions.

.box1 {
  background: #3030c9;
  grid-area: 1 / 1 / 2 / 3;
  text-align :center; // You can choose between center, left, or right alignment
}

Answer №3

To achieve the desired layout, apply the CSS properties position:relative to .box1 and position:absolute to your h1 tag. Additionally, specify the left and top values for your h1 element as needed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Embedding @keyframes within @media queries

I need to resolve an issue involving curly brackets. However, all opening brackets have a corresponding pair. There is an error on line 212 Expected RBRACE at line 212, col 2. @-webkit-keyframes move { Oddly enough, when I test the code separatel ...

Using Jquery to toggle the visibility of a DIV element and adding a blinking effect when it becomes visible

I have a hidden div that is displayed when a link is clicked, and I want it to blink as well. Here is my current setup: The link to display the hidden div: <a class="buttons" href="#" onclick="show(this)">join us</a> The hidden div to be di ...

Deliver real-time notifications to linked users by leveraging socket.io and node.js

Is there a solution for sending real-time updates to connected clients every second without using the setInterval() function in nodejs and socket.io? Please provide an alternative method that fits my specific scenario. ...

What causes the small black line to appear on the right side of an image when it is turned into a link?

When I turn an image into a link, why does it always seem to display a tiny black line to the right of the image? ...

The excessive width of the navigation bar resulted in additional blank space on the right side of

My navigation bar is causing me trouble as it appears to be too wide, creating extra space on the right side. I've tried adjusting the width with some code, but as I'm working on making the site responsive, shrinking the code down to around 320px ...

Unforeseen issues arise when working with CSS selectors

Exploring CSS with a series of buttons and encountering some unusual behavior. Here's the code: https://codepen.io/buoyantair/pen/JNgqXG?editors=1100 Let me share the snippets for Pug Script and Sass file: Pug Script header.row .col ...

Is there a hierarchy to be followed within the <head> element?

I have been developing websites using HTML for nearly 3 years now, but since I had to teach myself, there are still a few things I am unsure about. One question that has recently become important is whether it is possible to create groups within the <h ...

Laravel 4.2 email template not applying CSS inline style for text color

Working on setting up email functionality in Laravel 4.2 and I've got an email template ready to go. The data for the emails is stored in an array, but I'm running into an issue where if I pass a parameter like $variable, the styles are only bein ...

Could we customize the appearance of the saved input field data dropdown?

I'm currently working on styling a form that includes an input field. The challenge I'm facing is that the input field needs to be completely transparent. Everything works fine with the input field until I start typing, which triggers the browser ...

What process allows for this Twitter API response to be converted into HTML format automatically?

It is common knowledge that Twitter operates in an API-centric manner, meaning all Twitter apps retrieve their data through the API. Upon accessing (or .xml for those who prefer), a JSON formatted result with plaintext content and no additional formattin ...

Can we limit the number of items to just two per row in a grid with two columns?

I'm currently facing issues with aligning items within a two-column grid. When looking at this image, you'll notice that the alignment is off. Specifically, 'Example 3' does not align properly with 'Example 4'. This seems to b ...

When utilizing Angular, the mat-datepicker is displayed underneath the modal, even after attempting to modify the z-index

I am encountering a problem with a mat-datepicker displaying below a modal in my Angular application. Here are the key details: Html: <div class="col-12"> <mat-form-field appearance="fill"> <mat-label>Start Date ...

Jumbotron featuring videos in a Bootstrap Carousel

I am attempting to create a bootstrap carousel featuring videos within a jumbotron. Despite my best efforts, I am struggling to make it work correctly. The videos appear too small within the jumbotron and seem to be overlaid by other elements. I have exper ...

The color of the text in Tailwind is displayed in a refreshing blue instead of the

In my current project, I am utilizing React JS with Node, and npm as the package manager. Within the project, I have incorporated various packages such as heroicons and tailwind. To style the text in my footer as grey, I have applied the "text-slate-500" ...

Styling a half circle in React Native

Despite my efforts, I have not been able to find anything quite like this: example Could anyone provide guidance on implementing this with react? ...

Deactivate a dropdown option in Angular's uib-dropdown

Currently in my angular template, I am working on a dropdown menu using angular-ui. The requirement is to disable certain list items based on a property of the "company" object defined in the ng-repeat. I've already experimented with the disabled tag ...

How do I implement a dynamic input field in Angular 9 to retrieve data from a list or array?

I'm looking to extract all the strings from the Assignes array, which is a property of the Atm object, and populate dynamic input fields with each string. Users should be able to update or delete these strings individually. What approach can I take us ...

Adjust the size of the image as needed in order to fit within a container of fixed

Is there a clever way to use CSS and/or JavaScript/jQuery to adjust the size of images only when necessary for them to fit within a fixed-height container alongside the remaining content? Upon reviewing the images in the 'new this week' and &apos ...

Bootstrap - Struggling to achieve responsiveness on the page

I've recently designed a website with a mega menu feature. However, as I try to resize the page, the layout doesn't adjust well, and some elements appear to be overflowing beyond the screen. Unfortunately, I can't share the entire code. Cou ...

Using Angular directives to pre-select a default option

I am currently working on a select HTML tag with two options, and I want to set the first option as the default choice. My select element includes Angular directives like ng-change and ng-model. I have attempted to achieve this by adding selected = "select ...