Can a line be created using only CSS without the need for an additional HTML element?

There have been many inquiries regarding drawing lines without adding an additional HTML element. Consider this scenario with an HTML div element:

<div class="myDiv"></div>

Accompanied by the following CSS:

.myDiv{
  width: 100px;
  height: 100px;
  border: 2px solid black;
}

Now, is it feasible to create four vertical dotted lines in the background of the div, from top to bottom, using only additional CSS?

FIDDLE
https://jsfiddle.net/yjs5yqwo/
https://jsfiddle.net/rjykLrog/2/

EDIT
It's important that I can specify margin-left or a similar property to position the lines as needed.

Answer №1

Here's a clever solution using the pseudo element ::before

.myDiv {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  text-align: center;
  padding-left: 10px;
  width: 100px;
  height: 100px;
  border: 2px solid black;
  position: relative;
  font-size: 22px;
  font-weight: bold;
}

.myDiv::before {
  content: ".... .... .... .... ...."; /* your dots */
  position: absolute;
  top: 0; right: 0; bottom: 0; left: 0; /* stretches the before element over the entire surface of the main element */
  z-index: -1; /* places the ::before element underneath the main div content, as a background */
  letter-spacing: 6px;
  padding-left: 6px; /* letter-spacing adds px after letter, use padding at the beginning to off set it to remain centered. */
  font-size: 50px;
  color: hsla(0, 0%, 0%, 0.2);
  transform: rotate(-90deg); /* turns the .... sideways */
  margin-left: -38px;
  line-height: 22px;
}
<div class="myDiv">Four Ring</div>

Nifty fiddles

https://jsfiddle.net/Hastig/rjykLrog/1/ (1 x 4-dot line)

https://jsfiddle.net/Hastig/y7zp6aac/1/ (multiple 4-dot lines)

https://jsfiddle.net/Hastig/1qzf2drx (repeating linear gradient)

Answer №2

To ensure proper rendering of line feed characters in the content, you can escape them and then set the white-space property to pre. For example, use

content: ".\A.\A.\A."; white-space: pre;

Adjusting the spacing between the dots can be done using the line-height property.

Kudos to another answer that clarified the process of escaping newline characters in CSS content property: Newline character sequence in CSS 'content' property?

If you are considering using this method, please keep in mind that any addition in the content property is treated as regular content for screen readers. Therefore, there might be accessibility concerns with adding dots in this manner.

.myDiv
{
  position: relative;
  box-sizing: border-box;
  width: 400px;
  height: 100px;
  padding: 15px 12px;
  border: 2px solid black;
  font-size: 22px;
  font-weight: bold;
}
.myDiv::before
{
  position: absolute;
  content: "........................\A........................\A........................\A........................";
  top: 0;
  left: 10px;
  line-height: 22px;
  white-space: pre;
  letter-spacing: 5px;
  color: hsla(0, 0%, 0%, 0.2);
}
<div class="myDiv">
  Something goes here
</div>

Answer №3

I attempted to incorporate 4 vertical lines as per your request. Please provide feedback if my approach was incorrect. Thank you.

CSS:

.myDiv{
 padding-left: 10px;
 width: 500px;
 height: 100px;
 border: 2px solid black;
 font-size: 22px;
 font-weight: bold;
 z-index: 1;
 overflow: hidden;
 position: absolute;
 top: 0;
 left: 0;
}

 .q { 
    width: 100px;
    position: absolute;
    top:0;
    height: 100px;
    border-right: 2px dotted black;
    z-index: -1;
 }

 .w {
    width: 200px;
    height: 100px;
    z-index: -1;
    border-right: 2px dotted black;
 }

 .e {
    width: 300px;
    height: 100px;
    z-index: -1;
    border-right: 2px dotted black;
 }

 .r {
    width: 400px;
    height: 100px;
    z-index: -1;
    border-right: 2px dotted black;
 }

HTML:

<div class="myDiv">ASJKLJKLS
   <div class="q">
     <div class="w">
       <div class="e">
         <div class="r">
         </div>
       </div>
     </div>
 </div>

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

Generating an HTML table from SQL data using PHP

I'm attempting to create a dynamic HTML table using PHP to populate it with data from a MySQL database. I've tried using a while loop, but the issue is that it ends up displaying the same first row multiple times. <div class = "container"> ...

How to apply distinct CSS styles to individual pages in Orchard CMS?

If we have two pages within Orchard CMS - the homepage and the About Us page, is there a way to include a RoyalSlider on just the homepage without affecting the About Us page? Within Orchard CMS, I am utilizing the Contoso theme. Previously, I attempted t ...

Unusual div image alignment when dimensions are dynamically adjusted with Javascript animations

I have created a basic webpage using JavaScript that aims to scale an element from the center of the page over time when clicked. It is functioning properly for the SVG element I tested it with. However, when I use an image, it initially appears outside o ...

I am encountering an issue with Angular where the following error message is displayed: "src/app/app.component.html:18:20 - error TS2339: Property 'DepScreen' does not exist on type 'AppComponent'"

This code snippet is from my app.component.html file: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0" ...

The duplicate code is failing to display any output

Welcome to my first question here! Please excuse any rookie mistakes. I am currently working on a specialized calculator using HTML, JS (with jQuery), and CSS. This calculator is designed to handle multiple inputs and perform various calculations on a sing ...

How can you find the index of an HTML element while excluding any clearfix divs?

In the process of developing a CMS, I find myself in need of determining the index of the element I am currently working on. Consider the following stripped-down HTML structure: <div class="row"> <article class="col-md-4 col-sm-4 project" &g ...

ClickAwayListener in MUI has the potential to impact all the elements within a popper when attempting

As a new react developer, I have encountered a small problem with my ClickAwayListener. It should close the Popper component when clicking 'x' or outside of it, which it does successfully. However, I have another component inside my Paper, and wi ...

What steps can I take to troubleshoot the cause of my browser freezing when I try to navigate to a different webpage

Within this div, users can click on the following code: <div id="generate2_pos" onclick="damperdesign_payload();" class="button">Generate P-Spectra</div> Upon clicking, the damperdesign_payload() function is triggered, leading to a link to an ...

What is the best way to incorporate a search feature within Bootstrap cards?

I've been struggling to incorporate a search feature within my bootstrap cards. Despite trying various online methods, none have proven successful for me so far. Below is my HTML code - any assistance in implementing a search filter into my app would ...

A two-column bootstrap design with zero padding or gaps

This is the layout I am aiming for: Below is the code I currently have: <div class="row "> <div class="col-lg-6 row-eq-height push-right"> <img src="1.jpg" class="img-responsive"> </div> <d ...

Assign the two values to variables in the CSS script

My challenge lies in passing two values to the CSS within the <style> tags. The values are: (background-color: --placeholder; color: --placeholdtext;). I am able to pass either one of the values successfully, but not both at the same time. When I cop ...

What is the best way to transfer an argument from a parsed JSON value to an onclick function?

In our dataset, we have a specific table that contains valuable information. My main objective is to transfer an argument extracted from parsed JSON data to a separate JavaScript function known as newStory(value['stories']) using the onclick meth ...

What's the best way to apply a margin top to an image using Tailwind CSS?

Is there a way to adjust the top margin of an image in Tailwind CSS? I'm having trouble making my logo clearly visible and think giving it some space at the top might help. How can I do this within the Tailwind CSS framework? https://i.sstatic.net/Ae ...

I'm having trouble aligning this div in the center of the screen. It looks like the position is slightly off

<!DOCTYPE html> <html> <head> <style> body { background-color: #7a64fa; } .container { background-color: #ffffff; position: fixed; top: 50%; left: 50%; margin-top: -50px; ...

Ways to append each list item from one unordered list to the end of another based on their unique styles

I am in the process of making a website responsive and I am faced with the task of combining two different menus. In order to achieve this, I need to transfer all list items (li) from one unordered list (ul) to another. Provided below is a simplified vers ...

Extracting data from multiple web pages with R - An extensive collection of tables

Looking for assistance in extracting data from a website with multiple pages and importing it into R. Can anyone help me with this task? Website: ...

Arranging Multiple Files in Sequence Using HTML5 File API Instead of Uploading All Simultaneously

I am currently working on a BackboneJS/Marionette App and I want to enable users to upload multiple files. Right now, the functionality works when users select multiple files simultaneously, but I would like to give them the option to select one file init ...

The deletion feature on my virtual keyboard was malfunctioning when using JavaScript

The virtual keyboard feature I added to my website isn't working properly, specifically the delete function. How can I fix this issue? Below is my code: HTML Code <input type="text" maxlength="12" min="10" name="msidn" id="jkeyboard" min="1" sty ...

Is the JavaScript progress bar dysfunctional when used with object-oriented JavaScript code, but functions properly with arrow functions?

After posting a question about the JavaScript progress bar not working with object-oriented JavaScript code on Stack Overflow, I decided to try rewriting the script using arrow functions. Here is the new code: document.getElementById("barButton").addEve ...

I would like to create a layout featuring 3 columns spread across 2 rows, with each column showcasing only images

I've been experimenting with creating a column layout that spans three columns across the top and two rows down. So far, I've managed to create the first row but I'm unsure of how to split it into the second row. .container { display: ...