Navigating with just a single instance of identical pseudo elements

I am attempting to replicate a design similar to this:

https://i.sstatic.net/pJPBj.png

In this design, the ribbon in the middle is an image, while the gradients surrounding it are created using CSS.

My objective is to have an element act as the title on a page, with the ribbon displayed beneath that title. The desired code structure would be something like this:

    <h1 class="page-title">This is a Title</h1>

To achieve this, I attempted the following approach:

    h1.page-title {
      display: block;
    }
    h1.page-title::after {
      content: url(./path/to/image);
      display: block;
    }  

While this successfully added the image below the title, I encountered difficulty incorporating the gradients, as multiple pseudo after-elements cannot be added simultaneously.

My goal is to accomplish this effect using only one HTML tag if possible, though I have also been unsuccessful in achieving this with two tags. Any assistance or guidance on this matter would be greatly appreciated.

EDIT: The final output I aim for should resemble this:

https://i.sstatic.net/rmQN7.png

Answer №1

By utilizing before and after pseudo elements, it is possible to transform a single DOM element into three separate components.

To achieve this effect, you should apply the background image directly to the h1 element itself and utilize the ::before and ::after pseudo elements for creating the additional lines.

Below is the CSS code that I have devised:

h1.page-title {
      position: relative;
  display: inline-block;
      background-image: url('https://media.istockphoto.com/photos/red-ribbon-picture-id473856462?k=6&m=473856462&s=612x612&w=0&h=VMdTIdIJCbQ8CjHmb90ywhRB561z_XKx09eI6qJXMF0=');
  background-repeat: no-repeat;
  background-position: 50%;
  text-align: center;
  left: 50%;
  transform: translateX(-50%);
    }
.page-title:before,
.page-title:after {
  content: "";
  position: absolute;
  height: 5px;
  border-bottom: 1px solid blue;
  top: 0;
  width: 300px;
}
.page-title:before {
  right: 100%;
  margin-right: 10px;
}
.page-title:after {
  left: 100%;
  margin-left: 10px;
}

Please note that in this example, I have not incorporated gradient effects within the lines as shown in your reference. This serves as a basic illustration of how to achieve the desired outcome using just one div. Feel free to customize the CSS as needed.

Answer №2

I stumbled upon this Site1 and also found useful information on Site2

By utilizing the properties of position: sticky;, z-index: 10; and incorporating an hr tag, I was able to create the following example:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<style>
/* Styles from https://css-tricks.com/examples/GradientBorder/*/

hr.style-six {
padding: 0;
border: none;
height: 1px;
background-image: -webkit-linear-gradient(left, rgba(0,0,0,0), rgba(0,0,0,0.75), rgba(0,0,0,0));
background-image: -moz-linear-gradient(left, rgba(0,0,0,0), rgba(0,0,0,0.75), rgba(0,0,0,0));
background-image: -ms-linear-gradient(left, rgba(0,0,0,0), rgba(0,0,0,0.75), rgba(0,0,0,0));
background-image: -o-linear-gradient(left, rgba(0,0,0,0), rgba(0,0,0,0.75), rgba(0,0,0,0));
color: #333;
text-align: center;
    position: sticky;
    width: 100%;
    left: 50%;
    top: 50%;
}


.img{
    background: #fff url(https://upload.wikimedia.org/wikipedia/wikimania2014/thumb/e/e2/Ask-Logo-Small.jpg/250px-Ask-Logo-Small.jpg) no-repeat center;
    background-size: 25px 25px;
    height: 25px;
    width: 25px;
    position: sticky;
    left: 50%;
    top: 50%;
    z-index: 10;
}

.main{
    height: 50px;
    position: relative;
}
</style>
</head>
<body>
<div class="main">
  <div class="img"></div>
  <hr class="style-six"/>
</div>
</body>

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

The div smoothly descended from the top of the page to the center under the control of jQuery

I am trying to implement a feature where a div slides down from the top of the page to the center when a button is clicked. However, my current code seems to be causing the div to slide from the bottom instead of the top. Ideally, I want the div to slide ...

Tips for showcasing the button label on a different page upon clicking the button

I need help with a functionality where the name of a button clicked on one page is displayed on another page. Here's an example: <a href="#" class="btn btn-danger btn-lg btn-block"> <?php echo $sql->name; ?></a> Currently, echo ...

Building a versatile memoization function in JavaScript to cater to the needs of various users

There is a function in my code that calculates bounds: function Canvas() { this.resize = (e) => { this.width = e.width; this.height = e.height; } this.responsiveBounds = (f) => { let cached; return () => { if (!cache ...

I am having trouble displaying all the div elements with the same #id simultaneously

Click here for the code snippet The code provided seems to have a problem where hello2 and hello3 are not displaying. Is this issue related to the fact that '#id can only be used once'? Making it a class does not seem to work either. How can thi ...

display a container and navigate to the specific link

Greetings! Could someone please help me make this code function properly? I am attempting to display the DIV (slidingDiv) and navigate to the selected ANCHOR (#anchor-01 + #anchor-02 + #anchor-03)... However, the code currently only allows me to go to the ...

What is the best way to place a child on top of a different parent in the stack?

There is a collection of figure tags, each with an expandable figcaption. Inside each figure, there is a clickable div that reveals the figcaption when clicked. However, the visible figcaption only overlaps the figure it belongs to and not others. Is there ...

Menu options submerged beneath the surface of a concealed container with overflow property

Attempting to enhance the ui-grid by adding more options to each row using the "3 dots" approach. However, encountered an issue where the dropdown menu extends beyond the screen due to the use of position: absolute. Unable to switch to position: relative b ...

Responsive Bootstrap 5 table - occupies full width on mobile devices, adjusts automatically on desktop screens

Is there a way to create a table that is full width on mobile using pure bootstrap classes, but only auto width on desktop? I attempted to achieve this with the following code: <table class="table table-bordered w-md-auto w-100 text-start mb-2&quo ...

Disappearance of logo div causes video to vanish

Hey there fellow coders! I'm facing a little issue. I'm attempting to replicate my webpage on another page, but every time I try to remove the logo, the video disappears as well. It's quite puzzling. I have the code in place, but it seems l ...

How to grab selected HTML content with jQuery

I have been scouring the internet for a solution to this issue. My goal is to convert text selection start and end points into their corresponding HTML selection values. For example: HTML: test text **some <span style="color:red">te**st t</span& ...

Steps for launching an audio fileHow to start playing an audio

I am trying to automatically play an audio file audio when the window opens by using the playaudio() function. How can I achieve this? window.onload = function(playAudio();) var audio = new Audio("https://s3.amazonaws.com/audio-experiments/examples/el ...

Passing arguments inside the source attribute of an image or link tag in Node.js and

As a beginner in NodeJS, I am facing an issue with passing arguments inside links and image sources. In my template.html file, I have included various scripts and elements to create a search form. The issue arises when I try to incorporate values from the ...

Currently at the beginning stages of mastering CSS and encountering difficulties with the display property

I am facing an issue with my code .name-bar, .email-bar { border-color: gray; border-style: solid; display: inline-block; } .email-bar { margin-top: 5px; } .div-one, .div-two { border-color: gray; border-style: solid; width: 200px; h ...

tips for laying out checkboxes in a 3 by 3 grid on a form design

Hello there! I am seeking assistance on how to organize the list of cuisine checkboxes retrieved from the database. Currently, they are not displaying side by side as desired and I would like them to be shown in a 3 by 3 format. Below is the code snippet f ...

What could be the reason why only my drawLine function is functioning correctly?

As I embark on learning canvas and its control, I've encountered an issue with my code. The drawLine function, which should draw a dot or line on the canvas when triggered by onMove, is not rendering anything on the canvas surface. Can someone please ...

Incorporating a timestamp to a specific cell

I'm currently developing a straightforward web application that involves: Adding table rows to an HTML site. Including a function that inserts the current time into a table cell when clicked. However, I am facing a challenge in finding examples. Wh ...

Generating an HTML table using data from a specified URL source

As a beginner, I am working on creating a dashboard for my smart home using OpenHab2.0. One of the key features I want to include is a widget that displays the next 5 tram departures from my local stop. The table structure for this information looks like: ...

Customize the appearance of a <label> tag when it is inside an <input> tag that is marked as invalid

In an ideal scenario, I wish for the text on a label to change color when the input value is considered invalid. For instance: <form> <label> Enter your name: <input type="text"> </label> </form> Ideall ...

Create a curve using two distinct colors in JavaScript

I am currently experimenting with a canvas and I have a specific challenge in mind. My goal is to draw an arc using two different colors, where the first half of the arc will be green and the second half red. Check out the code snippet below: // CANVAS co ...

position the div correctly above the table

I am currently working on creating a scheduler that has specific requirements: Show the working hours for each resource Show the interviews, allow changing the interview duration by resizing, and move the interviews using drag-and-drop functionality. I ...