Exploring the potential of CSS Grid with dynamic content

I have a paragraph that contains text, possibly a span element, as well as some pseudo-content using the :before and :after selectors:

<p>Regular text with no span</p>
<p>Extra content with a <span>span</span></p>

My goal is to position the pseudo-content at the start and end of each paragraph like so:

«   Regular text with no span               »
«   Extra content with a [span]            »

I attempted to achieve this using grid-template-columns, but I'm struggling due to the presence of the additional span element. Is there a better approach using flexbox instead?

Below is a code snippet for reference:

p {
  display: grid;
  width: 40em;
  grid-template-columns: 2rem ? ? 2rem;
  span {
    border: thin solid #ccc;
    padding: 0 0.25rem;
  }
  &:before {
    content: "«";
  }
  &:after {
    content: "»";
  }
}
<p>Regular text with no span</p>
<p>Extra content with a <span>span</span></p>

Answer №1

For this particular case, it's best to stick with a default flow layout rather than using Flexbox or CSS grid.

p {
  span {
    border: thin solid #ccc;
    padding: 0 0.25rem;
  }
  &:before,
  &:after {
    content: "«";
    display: inline-block;
    width: 1.5rem;
  }
  &:after {
    content: "»";
    /* you may not want the below */
    float: right;
    text-align: right;
    /* */
  }
}
<p>Normal content without a span</p>
<p>Additional content with a <span>span</span></p>

Answer №2

If it's possible to adjust your HTML structure, it would be beneficial to enclose the entire content within another layer. This will prevent any issues with random <span> elements intersecting since they will be contained within the same grid cell.

article {
  display: grid;
  grid-template-columns: 2rem auto 2rem;
  width: 40em;
  align-items: center;
  
  span {
    border: thin solid #ccc;
    padding: 0 0.25rem;
  }
  
  &:before {
    content: "«";
  }
  
  &:after {
    content: "»";
  }
}
<article><p>Normal content without a span</p></article>
<article><p>Additional content with a <span>span</span></p></article>


If restructuring is not feasible, utilizing flexbox could be a solution, although this might lead to complications if the paragraph wraps onto a new line.

p {
  display: flex;
  width: 40em;
  
  span {
    border: thin solid #ccc;
    padding: 0 0.25rem;
  }
  
  &:before {
    content: "«";
  }
  
  &:after {
    content: "»";
    margin-inline-start: auto;
  }
}
<p>Normal content without a span</p>
<p>Additional content with a <span>span</span></p>

Another approach is to combine grid layout with display: contents, treating the inner <span> as plain text. However, this may limit styling options like borders or paddings, and compatibility considerations should be taken into account. Check its support status.

p {
  display: grid;
  width: 40em;
  grid-template-columns: 2rem auto 2rem;
  span {
    display: contents;
    color: red;
    border: thin solid #ccc;
    padding: 0 0.25rem;
  }
  &:before {
    content: "«";
  }
  &:after {
    content: "»";
  }
}
<p>Normal content without a span</p>
<p>Additional content with a <span>span</span></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

Numerous DIVs with floating elements and border styling

Trying to align two divs with one floating to the left and with a width of 80%, while the other is floated to the left with 20% width, is causing some issues. I want to add a border on the right side of the first div and apply a box-shadow of 5px since eac ...

Adaptive stationary slideable navigation bar

By utilizing Bootstrap, I have successfully implemented a sidebar feature. Initially, only the Font Awesome icon is visible, but upon hovering, it smoothly slides to the left revealing more content. Challenge: Unfortunately, the sidebar lacks responsivene ...

Expand and Collapse Button for Customizing Table Height Individually

I trust everything is going smoothly. A challenge I'm facing involves implementing a button to expand a specific row within a table. Currently, clicking the "show more/show less" button extends all rows when the goal is to do it for each individual ta ...

How do I go about aligning the first row to the center vertically?

When I use the align-items-center class in the row section, it just doesn't seem to work as expected: <div class="container"> <div class="row align-items-center"> <div class="col-12 bg-warning"> hello ...

Turn off hover effect for MUI DataGrid

I'm having trouble figuring out how to disable the hover effect on a row in the MUI Datagrid. I've searched through the API documentation but couldn't find a straightforward solution. Any suggestions on how to achieve this? <DataGrid ...

Placing <IconButton> at the bottom right of <Paper> using React and Material UI without utilizing FAB

Issue: I'm working on implementing a react/material-ui design where I want to show an edit icon floating in the bottom right corner of a paper element. I've managed to get it to float in the bottom right of the entire screen, but that's not ...

What steps can I take to minimize the excessive white space below the footer on my website?

I have been attempting to resolve this issue by using multiple methods, but none of them have successfully fixed it. overflow-x: hidden; min-height: 100%; margin: 0px; https://i.sstatic.net/2ctwo.jpg Code: body: background-color: White; margin: 0px; p ...

Adjusting Size Dynamically for Tooltips Based on Content Length

In my angular app using ng-bootstrap v4.2.1 on an older codebase, I have successfully created a tooltip with some very long sentences as content. I am trying to make the tooltip 800px wide, but when I set the width, only the first few words fill the space. ...

Tips for adjusting the height of the Bootstrap navbar and ensuring the text remains centralized

I've attempted various methods to customize the Bootstrap navbar. I have successfully changed its color and adjusted its height. However, there is still an issue with the text alignment.Check it out here. ...

Guide to incorporating vertical scrolling for a grid of items in Material UI

Is there a way to implement vertical scrolling in a grid container so that when the height of components exceeds a certain maximum, they can be scrolled through vertically? ...

Stop text from breaking out of the flexbox container

I am trying to integrate an image with a text link into a flexbox container. However, I am encountering an issue where the container breaks and appears incorrectly on smaller screens: https://i.sstatic.net/4DvY1.png <el-container> <el-main& ...

Harnessing the power of Bootstrap 4: Adding text and icons to your <progress> bars

I'm attempting to add an icon overlay on a progress bar within Bootstrap 4 alpha. <progress class="progress progress-primary" value="100" max="100"> <i class="fa fa-check-circle-o"></i> </progress> The desired outcome shoul ...

Incorporate a smooth transition effect to a dynamically resizing div button

In my current setup, I have 3 buttons that can toggle between different visible divs. In the future, I may add more buttons to switch between additional divs. At the moment, the first div is active (display set to block), while all other divs are hidden ( ...

Changing font color of a selected item in Material-UI's textview

I have a select textview in my react app and I am wondering how to change the font color after selecting an item from this textview. <div> <TextField id="standard-select-currency" select fullWidth l ...

Change the color of unordered list items only at the same level

Hey there! I'm working with this HTML code: <ul id="nav" class="nav-1"> <li><a href="#">Loans</a></li> <li><a href="#">Bancassurance</a> <ul class="nav-2"> <li><a href="#"&g ...

Exploring Bootstrap 4: Creating list-inline elements for various screen sizes

Is there a way to align list items on top of each other for a breakpoint <=575px (xs), and align the items next to each other for a breakpoint >575px (sm) using Bootstrap display properties? I found some information on How to display a list inline u ...

Converting an image to a link is not effective

Currently facing a challenge trying to turn a list of images into clickable links. Here's how I have set them up in my HTML code: <a href="plantvb1.html" class="plant1"><img src="img/plnt1_.png"></a> and this is how they are style ...

Add CSS styling to a section of a canvas

I want to use a specific cursor png for just a portion of a canvas. Currently, I have code that applies the cursor to the entire canvas like so: .myClass { cursor: url('../img/myCursor.png') 7 33, auto; /* img hotspot centred*/ } However, I ...

Unique fluid layout with varied behavior

I am on the hunt for a layout that caught my eye some time ago, but unfortunately I forgot to save it. It featured 3 main columns that cleverly transformed into 2 when resized, causing the 3rd column to shift to the row below. It wasn't your typical l ...

What is the best way to automatically clear an input field after a specified delay?

On my landing page, I have a single input field where users can enter their email address. Upon successful entry... success: function(result){ console.log(result.status); if(result.status == true) { $('input').attr("style", "color:green" ...