Tips on creating multiple divs enclosing text

When attempting to wrap divs around a span element, I encountered difficulties in determining the height and width of the text. I opted not to use box shadows as they are intended for an animation. You can view the desired appearance via this image. For the current code, visit here.

Various methods were attempted, such as utilizing multiple spans that match each other's attributes, but incorporating the ::after pseudo-element led to distorted code. Implementing .mid and .max widths also interfered with the ::after code and transitions.

:root {
  --txt-color: #fff;
  --txt-box-bg: #424242;
  --main-bg: #000;
  --transparent: transparent;

  /* front page title text */
  --main-front-color: #e84118;
  --second-front-color: #fbc531;
  --third-front-color: #00a8ff;
}

* {
  margin: 0px;
  padding: 0px;
}
/* Text */
.head-text {
  font-family: "IBM Plex Sans", sans-serif;
  color: var(--txt-color);
}

.main-text {
  font-family: "IBM Plex Mono" monospace;
  color: var(--txt-color);
}

.bold {
  font-weight: bold;
}

.normal {
  font-weight: normal;
}

.light {
  font-weight: lighter;
}

/* Alignment */
.center {
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

/* Classes */
.front-page {
  background-color: var(--main-bg);
}

.full-page {
  background-size: cover;
  height: 100vh;
}

#title-text {
  position: absolute;
  display: inline-block;
  background-clip: text;
  color: transparent;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  white-space: nowrap;
  background-image: linear-gradient(#000,#000);
  background-size: 100% 100%;
  background-position: right;
  background-repeat: no-repeat;
  transition: 1s all;
  font-weight: bold;
  text-align: center;
  font-size: 90px;
  -webkit-text-stroke: 2px var(--main-front-color);
  animation: stroke-rainbow 13s linear infinite;
  z-index: 2;
}

#title-text::after {
  content: '';
  display: block;
  width: 0;
  height: 4px;
  background: var(--txt-color);
  transition: 1s all;
  animation: stroke-rainbow 13s linear infinite;
}

#title-text:hover {
  background-size: 0% 100%;
}

#title-text:hover::after {
  width: 100%;
}

@keyframes stroke-rainbow {
  0% {
    border-color: var(--main-front-color);
    -webkit-text-stroke-color: var(--main-front-color);
    background-color: var(--main-front-color);
    #title-text::after {

    }
  }
  25% {
    border-color: var(--second-front-color);
    -webkit-text-stroke-color: var(--second-front-color);
    background-color: var(--second-front-color);
  }
  50% {
    border-color: var(--third-front-color);
    -webkit-text-stroke-color: var(--third-front-color);
    background-color: var(--third-front-color);
  }
  75% {
    border-color: var(--second-front-color);
    -webkit-text-stroke-color: var(--second-front-color);
    background-color: var(--second-front-color);
  }
  100% {
    border-color: var(--main-front-color);
    -webkit-text-stroke-color: var(--main-front-color);
    background-color: var(--main-front-color);
  }
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://fonts.googleapis.com/css?family=IBM+Plex+Mono:400,400i|IBM+Plex+Sans:100,100i,400,400i,700,700i" rel="stylesheet">
    <title>Portfolio</title>
  </head>
  <body>
    <!-- Full page intro -->
    <div class="front-page full-page">
      <span id="title-text" class="center head-text">Hi</span>
      </div>
    </div>
  </body>
</html>

Answer №1

To achieve this layout efficiently, utilize flexbox. Begin by setting up a container div that contains nested divs with different colored borders. Place your text within the central div for emphasis.

Here is the HTML structure:

<div id="container">
  <div id="one">
    <div id="two">
      <div id="three">
        <div id="four">
          <div id="five">
            <div id="six">
              Hi!
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

And here is the corresponding CSS code:

#container {
  margin: 0 auto;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 60vw;
  height: 100vh;
}

.colored-box {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

#one {
  border: 2px solid yellow;
}

#two {
  border: 2px solid red;
}

#three {
  border: 2px solid rebeccapurple;
}

#four {
  border: 2px solid dodgerblue;
}

#five {
  border: 2px solid lightgreen;
}

#six {
  border: 2px solid brown;
}

EDIT: For a cleaner approach, assign a common class like .colored-box to all numbered divs (e.g., #one, #two) in your HTML and define shared properties in CSS using that class.

Modified HTML:

<div id="container">
  <div id="one" class="colored-box">
    <div id="two" class="colored-box">
      <div id="three" class="colored-box">
        <div id="four" class="colored-box">
          <div id="five" class="colored-box">
            <div id="six" class="colored-box">
              Hi!
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

Updated CSS:

.colored-box {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

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

Column overflowing from row in Bootstrap grid on smaller screens

Is there a way to rearrange the display order of my Hello-div-2 and Hello-div-3 on small screens? Currently, the sequence is Hello-div-1, Hello-div-2, Hello-div-3, Hello-div-4. It needs to be Hello-div-1, Hello-div-3, Hello-div-2, Hello-div-4. ...

Setting up an image background in three.js: A beginner's guide

I tried setting up an image background for my scene with a globe in three.js, but unfortunately, the main object of my scene turned black (the same color as the background). I attempted using the following method: renderer = new THREE.WebGLRenderer({ anti ...

Having trouble selecting a dynamically changing div element in Selenium with Java using a click action

Every time I navigate to www.reddit.com and enter a query in the Search field, then press enter to go to the first valid link within a subreddit, sorting options are presented. By default, it is set to BEST but I wish to change it to TOP. My test class cod ...

Tips on causing a div to overflow instead of wrapping onto a new line

Are you looking to design a slider with 3 image containers all in the same row, where the third one overflows instead of wrapping to a new line? Here is an example: Desired Design: https://i.stack.imgur.com/dKyEg.png Current State: https://i.stack.imgu ...

Adjust div height to match the dynamic height of an image using jQuery

I'm facing an issue with my image setup. It has a width set to 100% and a min-width of 1024px, but I can't seem to get the 'shadow' div to stay on top of it as the window size changes. The height of the shadow div also needs to match th ...

What is the most effective way to reduce the spacing between columns in Bootstrap while maintaining consistent spacing on the outer edges?

I have set up my bootstrap columns as shown below: <div class="col-6"> <a href="gallerij/stockfotos/boeken" class="catphotowrap"> <div class="catphotodiv"> <img class=&quo ...

Button with embedded Bootstrap dropdown

I need help with centering a dropdown menu on my webpage. I followed the instructions, but it's still appearing next to the button instead of in the center. Below is the code snippet: <div class="btn-group"> <button type=" ...

The footer section of my website seems to have gone missing in the HTML/CSS coding

I'm having trouble with my website footer not displaying. I've tried using "position: absolute; top: 50px; left:100px;" in my HTML/CSS, but it's not working. Can anyone help me fix this issue? Here is a link to the code I'm working on: ...

Is there a way to center the text at 0% using text-align in Bootstrap?

My progress bar, designed with Bootstrap, currently displays the text in a way that is not centered as desired. The issue I am experiencing is that the "0/0" text shifts to the left and using text-align: center; does not seem to resolve this alignment pro ...

What is the method for inputting a value into a textfield using JavaScript?

Working with JavaScript $(document).ready(function() { var originaltext = $('#editable').val() $("#editable").keydown(function(e){ if(e.which == 9) { var content = $('#editable&ap ...

Adjust the image size within an Iframe to zoom in or out

Is there a way to enable Zoom in/Zoom out functionality for an image within an Iframe? <iframe src="http://localhost:8080/Folder/images/image.jpg?>" style="width:750px;height:580px; " frameborder="0"></iframe> My objective is to ha ...

Ways to utilize gridster by accessing the nativeElement of ElementRef

This issue is specific to the Firefox browser and does not occur in Chrome. My goal is to access the nativeElement for the gridster element. The version of angular-gridster2 being used is v17.0.0. In the HTML code: <gridster [options]="opt ...

Tips for traversing a list of elements that has become stale due to a webpage refresh

Hey there, I'm facing a challenge while trying to iterate over buttons that lead to new pages and then going back to click on the next button in the list. It's like navigating through a graph tree where every node needs to be accessed. Here is h ...

How to position preloader at the center in materializeCSS

Given this situation: I attempted to implement this Preloader with position:fixed and centered. My approach was as follows: .loader { position:fixed; top:50%; left:50%; -webkit-transform:translate(-50%,-50%); } <link rel="stylesheet" href="h ...

Tips for stopping an accordion from toggling in Bootstrap when a user clicks on a specific section of the toggle button

I am currently utilizing bootstrap for my project. The accordion feature I have implemented is standard, but I am looking for a way to customize its behavior. Specifically, when a user clicks on elements with the class 'option', I want it to sele ...

Dynamically adding a class name to the outer element of a component

I have implemented a custom tag using cq:htmlTag in one of my components. Instead of assigning a static class name from a CSS file to the "class" property, I need to use the 'template name' as the value for the class property. Is there a method ...

What is the process for including a static file on an http server?

Looking to create a chatroom using node.js. Utilizing the socket.io template for this project. var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); var fs = require(&ap ...

use css to remove container div tag

Looking for a solution to adjust the display of items in this structure: <div clas="page_cat_list"> <div class="page_cat_row"> <div class="page_cat_item">...</div> <div class="page_cat_item">...</div&g ...

What is the reason that <span> elements do not automatically wrap around to the next line when they are in line with each other without any white space?

Once upon a time, I believed that line breaks had no impact on the rendering of HTML due to minification. However, I recently discovered something peculiar. <!DOCTYPE html> <html> <body> <style> div { width: 200px; background: ...

Problem with child divs escaping outside the boundaries of the parent div

I'm encountering an issue with my HTML/CSS code. I have a parent div called "secClass" which contains two child divs - secClass1 and secClass2. The problem is that the content within the child divs is not staying contained within the parent div. Can y ...