What is the best way to format a pseudo-element to serve as the header for a paragraph?

We are utilizing Markdown (Kramdown) for generating a static website. When incorporating infoboxes, we can enhance paragraphs and achieve the following outcomes:

{:.note .icon-check title="This is a title"}
Lorem, ipsum dolor sit amet consectetur adipisicing elit.

Here is the converted HTML:

<p class="note icon-check" title="This is a title">
    Lorem, ipsum dolor sit amet consectetur adipisicing elit.
</p>

The style being used (SCSS) is as follows:

p.note {
  &::before {
    float: right;
  }

  position: relative;

  &[title]::after {
    content: attr(title);
    position: relative;
    top: 0;
    left: 0;
  }
}

.icon::before {
  content: "@";
}

Since we are relying on Icomoon icon fonts, where the content is defined in :before, the title has to be in :after.

  • We will not alter the Iconfont, so the icon must remain in :before
  • No extra distracting markup in the markdown, hence no HTML wrapper
  • No need for Javascript

An absolute positioning can be applied to the title, but it might be too limited to the paragraph-text itself as no margin can be established.

A demonstration using JSFiddle

Now, the question arises - how does one design a box with :after serving as a title on top, which also appears appealing when no title is specified?


main {
  width: 50%;
  display: block;
  place-items: center;
  margin: auto;
}

p.note {
  border: 3px solid red;
  border-radius: 3px;
  padding: 1em;
  position: relative;
}

p.note::before {
  margin: auto 0 0.5em 0.5em;
  float: right;
}

p.note[title]::after {
  content: attr(title);
  position: relative;
  font-weight: bold;
  top: 0;
  left: 1em;
}

.icon::before {
  font-size: 2em;
  content: "@";
}
<html>

<body>
  <main>

    <p class="note icon" title="This is a title">
      Lorem, ipsum dolor sit amet consectetur adipisicing elit. Dolorem quidem non placeat quibusdam velit, dicta adipisci saepe magnam fuga debitis qui, minima, eius error laboriosam distinctio eos natus et!
    </p>

  </main>
</body>

</html>

Preview of the design: https://i.sstatic.net/DKt6U.png Expected appearance: https://i.sstatic.net/8rPmh.png

Answer №1

Here's a creative idea utilizing shape-outside. By applying float to the before element for the title and using position:absolute for the after element with an icon, the shape-outside property can be used to create a unique shape that simulates the float behavior around the icon.

main {
  width: 50%;
  display: block;
  margin: auto;
  text-align:justify;
}

p.note {
  border: 3px solid red;
  border-radius: 3px;
  padding: 1em;
  position: relative;
}

p.note::after {
  margin: auto 0 0.5em 0.5em;
  position:absolute;
  top: 1em;
  right: 0.5em;
}


p.note[title]::before {
  content: attr(title);
  display:block;
  height:3.5em;
  float:right;
  width:100%;
  text-align:center;
  font-weight: bold;
  shape-outside:polygon(0 0,100% 0,100% 100%,calc(100% - 3em) 100%,calc(100% - 3em) calc(100% - 2em),0 calc(100% - 2em));
  /* To illustrate the shape */
  background:
    linear-gradient(rgba(255,255,0,0.3) 0 0) top/100% calc(100% - 2em) no-repeat,
    linear-gradient(rgba(255,255,0,0.3) 0 0) bottom right/3em 3em no-repeat;
  border:1px solid rgba(0,0,0,0.2);
   /**/
}

.icon::after {
  font-size: 2em;
  content: "@";
}

.icon:not([title])::after {
  display:none;
}

.icon:not([title])::before {
  font-size: 2em;
  content: "@";
  margin: auto 0 0.5em 0.5em;
  float:right;
}
<main>

  <p class="note icon" title="This is a title">
    Lorem, ipsum dolor sit amet consectetur adipisicing elit. Dolorem quidem non placeat quibusdam velit, dicta adipisci saepe magnam fuga debitis qui, minima, eius error laboriosam distinctio eos natus et!
  </p>

</main>

<main>

  <p class="note icon" >
    Lorem, ipsum dolor sit amet consectetur adipisicing elit. Dolorem quidem non placeat quibusdam velit, dicta adipisci saepe magnam fuga debitis qui, minima, eius error laboriosam distinctio eos natus et!
  </p>

</main>

Alternatively, here is a simpler concept:

main {
  width: 50%;
  display: block;
  margin: auto;
  text-align:justify;
}

p.note {
  border: 3px solid red;
  border-radius: 3px;
  padding: 1em;
  position: relative;
}

p.note::before {
  margin: auto 0 0.5em 0.5em;
  float: right;
}

p.note[title]::after {
  content: attr(title);
  position: absolute;
  font-weight: bold;
  top: 0.5em;
  left: 0;
  right:2em;
  text-align:center;
}
p.note[title] {
  padding-top:2em;
}

.icon::before {
  font-size: 2em;
  content: "@";
}
<main>

  <p class="note icon" title="This is a title">
    Lorem, ipsum dolor sit amet consectetur adipisicing elit. Dolorem quidem non placeat quibusdam velit, dicta adipisci saepe magnam fuga debitis qui, minima, eius error laboriosam distinctio eos natus et!
  </p>

</main>

<main>

  <p class="note icon" >
    Lorem, ipsum dolor sit amet consectetur adipisicing elit. Dolorem quidem non placeat quibusdam velit, dicta adipisci saepe magnam fuga debitis qui, minima, eius error laboriosam distinctio eos natus et!
  </p>

</main>

Answer №2

Considering the limitations of the grid, we can turn back time and explore the possibilities of using display: table and display: table-caption to provide an alternative solution. Let's see if this approach can offer a suitable workaround for your situation ;)

* {
  box-sizing: border-box;
}

main {
  width: 50%;
  min-width:450px;
  display: block;
  place-items: center;
  margin: auto;
}

p.note {
  font-size:clamp(12px, 4vw, 30px);
  border: 3px solid red;
  border-radius: 3px;
  padding: 1em;
  display: table;
  padding-top: 1.5em;
}
p.note::before {
  margin: auto 0 0.5em 0.5em;
  float: right;
  font-weight:bold;
  color:tomato;
}
p.note[title]::after {
  content: attr(title);
  font-weight: bold;
  display: table-caption;
  margin-bottom: -1.6em;
  text-align: center;
  padding-right: 3em;
}

.icon::before {
  font-size: 2em;
  content: "@";
  line-height: 0.25;
}
<main>
 
 <p class="note icon" title="This is a title">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Dolorem quidem non placeat quibusdam velit, dicta adipisci saepe magnam fuga debitis qui, minima, eius error laboriosam distinctio eos natus et!
 </p>
 
</main>

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

Issues with retrieving $_POST values from a select form in PHP

When the button is clicked, I am sending a AJAX request to my PHP page using POST method with the selected data from a SELECT element. However, I am facing an issue where my PHP IF statements are not evaluating as true. It always defaults to the ELSE condi ...

Is it possible to consistently show the placeholder in mat-select regardless of the item currently selected?

I am looking to keep the mat-select element displaying the placeholder at all times, even if an option has been selected. Below is my HTML code: <mat-select [formControlName]="'language'" placeholder="Language"> <mat-option value=" ...

How can I access a component variable within the styles in Angular?

Is it possible to utilize a variable width in my Angular 5 component template? I would like to achieve something similar to this: <style> input:checked + .slider:before { transform: translateX({{width}}px); } </style> The &apo ...

Is there a different option available in place of the JavaScript confirm function?

I developed an application where I heavily utilized the javascript confirm function. confirm("Do you want to proceed"); However, I am not satisfied with the default appearance of the confirm dialog and would like to implement a customized version with be ...

I am encountering issues with an HTML table that is in need of fixing, and I must find a solution without relying on

Can someone help me troubleshoot the structure of this table in the given image? It seems like my rowspan and colspan attributes are not working as expected. I've only used CSS to specify the width and height of the cells. https://i.sstatic.net/VtHbH ...

Customize Google Chrome to display a vibrant splash screen instead of a boring white blank page when

"I've been on the lookout for Chrome apps that can help make my screen darker or inverted to reduce eye strain. While I have found some apps that do the job, there's one thing they don't seem to be able to override - the White Blank page. W ...

Leveraging JavaScript along with the jQuery library and API to showcase information related to

Hey there! I have been working on this API that shows upcoming concerts, but I'm struggling to display the images associated with each concert. I've tried using for loops to iterate through the objects, and it seems like every sixth element has ...

Retrieve the text content within HTML tags that come after a specific paragraph using the DOM

I am looking to extract content from HTML documents. Specifically, I need the contents of all 'p' tags that come after 'h2' tags. Here is an example of the HTML to be parsed: <h1>Lorem ipsum</h1> <p> Lorem ipsum ...

How to use CSS to add a pseudo element to a table and position it outside of the parent's boundaries on the left

I am currently utilizing the "ag-grid" data-table library (along with Angular 2, although the framework is not crucial) which highlights a selected row (using the default class .ag-row) by adding the class .ag-row-selected to it. Each row contains numerous ...

`Scrolling through a horizontal menu with no scrollbar present`

Is there a way to create a horizontal menu that can scroll left or right without the scrollbar? I'm looking for a solution like adding on-screen arrow buttons or implementing mouseover functionality. What would be the best approach? div.scrollmenu ...

Ways to incorporate onload animation into a Pie chart with billboard js

I am currently working on implementing a pie chart with animation using billboard js. However, I am facing difficulties in applying the onload animation. Can anyone provide guidance on how to achieve this? For reference, you can view an example of the des ...

What is the best way to retrieve the input value from post.ejs?

app.js var express = require('express'); var bodyParser = require('body-parser'); var app = express(); var passport = require('passport'); var localStrategy = require('passport-local'); var axios = require("axi ...

Overlapping dynamic content causing CSS nested scrollbars to clash at full width of 100%

Displayed below is a layout with two nested divs, both featuring automatic vertical scrolling. Is there a CSS technique available to show the inner scrollbar while maintaining the inner div at 100% width? https://i.stack.imgur.com/HSKHH.png div { ba ...

jQuery Datatables have trouble accessing specific row information when the table is set to be responsive

Currently, I'm utilizing the jQuery DataTables plugin along with the responsive addon to dynamically display and hide columns based on the size of the browser window. One of the columns is labeled as Actions, which permits users to edit a record by c ...

Animating SVG from upper left corner to lower right corner

I am looking to create an animation on this SVG that starts from the top left and ends at the bottom right, similar to a gif. You can view the example I want to replicate in the following link: body { background: black } path { fill: white; } /* ...

Link embedded within a list item

How can I make the hyperlink element fill up the width and height of the li element inside? <ul> <li><a href="#" style="display: block; width: 100%; height: 100%;">this link to fill up the li element width and height</a><l ...

A sleek and streamlined scrollspy that dynamically updates the active class exclusively for anchor tags

Can anyone help me with this coding problem? I'm currently working on a minimalistic scrollspy code that adds an active class when the content is offset.top. The code works fine, but I want to change the active class to apply to the "a" tag instead of ...

Show alerts that automatically disappear after a set amount of time

I have successfully implemented code that displays alerts for a specific period of time, indicated by (alert alert-warning). Additionally, I want to display another type of alert, (alert alert-success), for a certain amount of time, after which the page sh ...

The Angular bootstrap datetimepicker doesn't support disabling past dates

Has anyone successfully disabled past dates on an angular bootstrap datetimepicker before? I've tried using the date-disabled attribute, but I can't seem to get it to work. <datetimepicker class="calendar-format" data-ng-model ...

Tips for reusing a form within a one-page website

I am looking for a way to handle a form's submit action using JavaScript. My goal is to hide the form when it is submitted and then be able to reuse it later. However, the code I currently have seems to have a hidden state that prevents the submit act ...