What is the best way to place a block with a different color directly beside another block?

example

my block

I'm interested in adding a black block after the red one with some text, similar to the example shown in the first screenshot.

Despite my attempts, I couldn't get it to work. As a newcomer to CSS, I'm facing some challenges...

h2 {
    width: fit-content;
    background-color: #FF0000;
}
h2:after {
    display: block;
    width: 50px;
    background-color: #000000;
}

Any suggestions on how I can achieve this effect?

Answer №1

Instead of using a pseudo-element, consider implementing a box-shadow.

body {
  background-image: url(https://images.unsplash.com/photo-1720247522784-ba24b938534d?crop=entropy&cs=srgb&fm=jpg&ixid=M3wzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE3MjI3MDM4NTZ8&ixlib=rb-4.0.3&q=85);
  background-size: cover;
  display: flex;
  justify-content: center;
}

h2 {
  width: fit-content;
  background-color: rgba(0, 0, 0, .25);
  padding: 0 1em;
  box-shadow: -.25em 0 red;
  color: white;
}
<h2>My Small Heading</h2>

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 browser encountered an issue trying to load a resource from a directory with multiple nested levels

I've stumbled upon an unusual issue. Here is a snapshot from the inspector tools. The browser is unable to load certain resources even though they do exist. When I try to access the URL in another tab, the resource loads successfully. URLs in the i ...

Is it possible to export a webpage while maintaining relative paths for CSS, images, and other assets?

I am currently working on a large enterprise web application and have created a basic prototype HTML page. This page consists of a list of CSS and JS includes with minimal markup. Surprisingly, there are a total of 57 CSS includes and 271 javascript includ ...

Best Method for Utilizing Selenium Locator for a Navigation Menu

I have been dedicating a significant amount of time to self-learning Selenium. I chose the website "actiTime.com" to practice what I have learned, and as I began automating scripts, I encountered an issue that has baffled me for two days. Since you are exp ...

Press the "Load More Categories" button in Wordpress to display additional categories

I am looking for a way to implement a "Load More" button on my website. Currently, I am using the get_categories function to display categories on my TV show website. Each category represents a series and the posts within each category are episodes. You c ...

The added class is not being successfully applied to the ClassList

I'm currently working on a page where I want the colors of a button and the background to switch when the button is clicked. document.querySelector("body.light").classList.toggle("dark"); document.querySelector("button.dark").classList.toggle("light" ...

Having difficulties loading my stylus stylesheet onto my jade HTML page using expressjs

Within my express application, I am attempting to load the style properties from a styles module engine. Unfortunately, I am facing difficulties in loading my style. Here is the structure of my folders: myNodeApp -node_modules -styles - ...

Ways to access the index position

$(function() { $(".menu ul").on("click", "li", function(e) { e.preventDefault(); alert($(this).closest("ul").prev().index()); alert($(this).index()); }); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" ...

Top tip for handling repetitive code with echo commands

I've been struggling with a question for quite some time now. Imagine you have a code that consists of multiple nested divs and spans, forming a square with an image inside. echo '<div> <div> <div> <div> <span> < ...

jQuery and Internet Explorer 8 have an issue where a toggler will only open the first child

I am facing an issue with a group of divs on my page. The code for the div is as follows: <div class="demo1"> <h3 class="expand2">1:00 pm – 4:00pm<br>Item 1<p></p> <p><strong>Presented by:</strong>< ...

Animating the translation of a rectangle using the RequestAnimationFrame method

First of all, I have developed Live Code QUERY: How can I create an animation where a rectangle continuously moves across the X-axis? Issue: Currently, my animation only plays once and then disappears offscreen. Even though the position x is reset to z ...

What steps should be taken to populate a grid row if a certain div element is not present?

I am currently using tailwindcss and have this specific HTML code snippet: REPL: https://play.tailwindcss.com/L8McFjGBVC <div class="grid grid-cols-12"> <div class="col-span-4 bg-red-300">1</div> <div class=&qu ...

Automating Jenkins Login in C# Script

Accessing jobs in Jenkins requires logging in with a username and password first, as I am unable to access them directly otherwise. When attempting to reach a job at "localhost:xxx/job/some_job_1," I receive a 404 Error unless I am logged in. Using the We ...

How can JavaScript be used to dynamically display submitted HTML form values on the same page, as well as how to perform calculations on the form data?

Currently, I am working on creating a form for collecting passenger information at an airport. The form includes fields for first name, last name, passenger weight, and cargo weight. Upon submitting the form, I aim to display the entered information along ...

Utilizing iPad, Safari, and CSS to effortlessly fill a page with flexbox properties

Trying to create a flex layout that expands the main area to fill the entire height of the display, with a footer always at the bottom when the content is small. However, if the main content exceeds the display size, it should behave like a normal page and ...

Tips for sorting items in Wordpress with JavaScript / on click?

I am currently utilizing a method similar to the one outlined in this resource from w3 schools for filtering elements within divs. However, I am facing challenges when attempting to integrate this script into my Aurora Wordpress site due to the removal of ...

Modifying the content of a div element on the main webpage by utilizing JavaScript from a separate page

(primary.html) <div> Insert Any Text Here </div> (secondary.html) <button class="start" onclick="updateContent()">Start</button> (script.js) function updateContent() { // How can I dynamically change the ...

Showing a pop-up on a click of a dynamically created table row, showcasing information specific to that row

I am facing a challenge with my dynamically generated table that is based on the JSON response from an AJAX call. What I am trying to achieve is to display additional data in a modal when a table row is clicked. This would be simple if the data was hard co ...

Adjusting the size of an element when hovering without causing any displacement of neighboring elements

I've been attempting to get this to work, but I'm running into some difficulties. Here's the example I'm currently working on: HTML: <ul> <li><img></li> <li><img></li> </ul> CSS ul{pos ...

Transfer a JavaScript value to PHP via POST method without the need for submitting or refreshing the page

My website includes a Google Map feature that updates geocoordinates when the marker is dragged and displays the address. Currently, JavaScript is passing the X and Y values to an HTML "ID" using the "GET" method. However, I now need to pass these values u ...

Potential issue: Firefox causes distortion in animated stroke-linecap="round" properties

While working on a project on Stack Overflow, I noticed a potential bug in Firefox when animating a line with stroke-linecap="round" and vector-effect="non-scaling-stroke" svg{border:1px solid} path{ animation: draw 1s ease-in; ...