Implementing CSS to achieve striped row styling in a paragraph (not within a table)

Is there a way to achieve alternating row shading within a wrapped paragraph element without using the :nth-child technique? For instance, if we have:

<p style="width:100px;">
row 0 -- hello
row 1 -- world
row 2 -- !!!
</p>

Some browsers may automatically create a soft return after "hello" and "world", resulting in all rows looking the same when rendered.

I'm looking to have rows 0 and 2 shaded differently than row 1, similar to how table rows can be styled with tr:nth-child(even) for alternating colored rows. Is there a solution for this scenario, or is there a practical alternative approach using different elements that doesn't involve breaking up the paragraph and inserting it into a table?

Answer №1

Apply a repeating gradient color effect:

p {
  line-height:1.2em; /* height of one line */
  color:transparent;
  background:repeating-linear-gradient(180deg, red 0 1.2em,grey 0 2.4em);
  -webkit-background-clip:text;
  background-clip:text;
}
<p style="width:100px;">
row 0 -- hello
row 1 -- world
row 2 -- !!!
row 3 -- hello
row 4 -- world
and so on
</p>

Use another syntax for the same effect:

p {
  line-height:1.2em; /* height of one line */
  color:transparent;
  background:linear-gradient(180deg, red 50%,grey 0) 0 0/100% 2.4em;
  -webkit-background-clip:text;
  background-clip:text;
}
<p style="width:100px;">
row 0 -- hello
row 1 -- world
row 2 -- !!!
row 3 -- hello
row 4 -- world
and so on
</p>

Answer №2

Transforming your paragraph into an unordered list using a programmatic JavaScript approach, detecting line break \n symbols.

var pRows = document.getElementById('p-rows');

pRows.innerHTML = pRows.innerHTML
  .split('\n')
  .join('</li><li>')
  .replace(/^<\/li>/, '<ul>')
  .replace(/<li>$/, '</ul>')
#p-rows ul {
  list-style: none;
  padding: 0;
}

#p-rows li:nth-child(odd) {
  background: #ddd;
}
<p id="p-rows">
row 0 -- hello
row 1 -- world
row 2 -- !!!
row 3 -- ???
row 4 -- ***
</p>

Answer №3

It can be quite challenging, but here's a solution that might work:

p {
  background-clip: text;
  background: -webkit-linear-gradient( red, blue, red);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;
}
<p style="width:100px;">
row 0 -- hello
row 1 -- world
row 2 -- !!!
</p>

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

Displaying the user's username upon logging into a website is a simple yet effective

I have successfully developed a simple web page using PHP and HTML. However, I am facing an issue in displaying the username after login. Additionally, I would like to hide the login and signup sections after successful login. Can anyone provide assistance ...

The functionality of CSS isn't up to snuff on Mozilla Firefox

I am having an issue with CSS styling on a <div> element in my PHP page. The CSS style is working perfectly in Google Chrome, but when I test my code in Firefox, the styling is not being applied. .due-money { background-color: #09C; color: whi ...

The information about the property is not appearing on the screen

I included the following CSS: nav label:AFTER { content: " ▾"; } However, when viewed in Chrome, it appears like this: content: " â–¾"; I have already added <meta charset="utf-8"/> to my JSP page and @CHARSET "utf-8"; in my CSS file. ...

Images in SCSS distorted due to styling issues

I am encountering an issue with a round image displaying properly on a browser, but appearing distorted after building the apk and running it on my android phone. The image's width is greater than its height. How can I ensure that it remains round? M ...

switching the content of an element using Javascript

I'd like to switch between two icons when clicking on .switch and apply the style of .nightTextNight to .nightText, my JavaScript code is working well everywhere except here. Is there a simpler way to achieve this? I currently have to create two clas ...

php Use cURL and the DOM to extract content with specified CSS styling

Unclear in the title, I want to copy all content from a specific div on an existing webpage (not owned by me). The code successfully extracts the content. Extractor code: // Get Data $curl_handle=curl_init(); curl_setopt($c ...

My divs are multiplying twice as fast with every iteration of the Javascript For Loop

Recently, I developed a script that generates a series of fields based on a number provided by the user (k). Initially, I had a script that would create the correct number of fields. However, I decided to arrange them like vectors on the screen, so I made ...

The adhesive navigation bar isn't adhering

I'm having issues with making my navbar inside a header sticky over the whole page. Despite trying various solutions like removing overflow, adjusting the flexbox, and setting a specific height on the parent element, I still can't get it to work. ...

Adaptive Table Layout that Creates a Page-breaking Design

I have a page layout with three columns: a fixed-width left-hand navigation, a responsive content column, and a fixed-width right-hand navigation. The issue arises when the content in the middle column includes HTML tables that sometimes exceed the availab ...

The issue of drop shadows causing links to not work properly in Internet Explorer

I am currently working on a website design that features a fixed menu positioned behind the body. When the menu icon is clicked, some jQuery code shifts the body to the left. To create the effect of the fixed menu being positioned underneath, I have added ...

Dynamic row addition in Material Design Lite table causes format to break

Here's the HTML markup for creating a checkbox using material-design-lite: <label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect" for="checkbox"> <input type="checkbox" id="checkbox" class="mdl-checkbox__input" /> <span c ...

Attach a button and arrow to the cursor while it is hovering

Can anyone help me figure out how to make a button magnetize the content (arrow) along with it when hovered over by the cursor? I found an example of what I want on this website , but I am struggling to implement it in my own code. I've searched thro ...

Achieve full width with flexbox column wrap while minimizing the height

How can I arrange elements in columns within a container with a fixed width and variable height, when the number of elements is unknown? My challenge is that I do not know the maximum width of the child elements, preventing me from setting specific column ...

What is the reason for the disparity between CSS box-sizing content-box and border-box?

Consider the following example where I only changed the CSS box-sizing property from content-box to border-box. The resulting output difference is significant. Give this code a try: .box1 { width: 300px; height: 100px; border: 1px solid blue; pa ...

Exploring the World of Print CSS, Embracing Bootstrap, and Master

In continuation of my previous query, I am facing an issue with setting up a filemaker foreach loop to display a group of images along with their names and IDs, accompanied by checkboxes. Upon checking the relevant checkboxes, the corresponding images are ...

Make sure the header spans the full width of the body

I am trying to create a header that spans the entire width of the body, but I am encountering some issues with white space on both sides. I initially attempted to set the width to 100% on the header div, but this did not eliminate the white space. I then e ...

Is there a way to eliminate unknown white gaps in the content?

I have encountered a very perplexing issue. When accessing this site on Safari, an additional 4-500px of scrollable whitespace is added. This is clearly not the intended behavior, but I am struggling to understand what is causing this space :S Upon inspe ...

What is the best way to incorporate CSS into JavaScript code?

Here is the code I am working with: <script type = "text/javascript"> function pic1() { document.getElementById("img").src = "../images/images/1.png"; } function pic2() { document.getEl ...

Centering content within a <th> tag

Hey there, I'm currently facing an issue with aligning a div inside another one. To illustrate the problem, I've set up a small fiddle which you can check out here: https://jsfiddle.net/jpq7xzoz/ The challenge arises when using the jQuery table ...

Is there a way to exclusively utilize CSS in order to achieve bottom alignment for this btn-group?

I currently have a series of div elements that I transformed into two columns. https://i.stack.imgur.com/xG7zT.png My goal is to align the PDF/XML button group at the bottom, creating a layout like this: https://i.stack.imgur.com/ijtuH.png This is an e ...