Pseudo Elements: The Transformative Power of Before and After

JS Fiddle

Looking for some CSS help:

<div id="strip"></div>

This is my setup:

width: 100%;
height: 130px;
background: grey;

I'm trying to add a pseudo-element after the strip, positioned at the bottom.

 #strip:after {
    content: "";
    display: block;
    width: 100%;
    height: 10px;
    background: blue;
}

There's also a pseudo-element before the strip.

 #strip:before {
    content: "";
    display: block;
    width: 100%;
    height: 10px;
    background: yellow;
}

The issue I'm facing is that the 'after' pseudo-element doesn't align with the bottom of the strip div. Can you point out where I might be making a mistake? Just looking for a solution to this specific problem. I understand there are other ways to achieve colored strips at the top and bottom of a div.

Answer №1

According to the CSS specification:

The pseudo-elements :before and :after behave as if they were actual elements inserted just inside their parent element.

To address your problem, you can implement the solution proposed by Nico O. The :after pseudo-element is absolutely positioned at the bottom of the main element.

#strip{
  width: 100%;
  height: 130px;
  background: grey;
  position: relative;
  padding-bottom: 10px;
}
#strip:after {
  content: "";
  display: block;
  width: 100%;
  height: 10px;
  background: blue;
  position: absolute;
  bottom:0;
}    
#strip:before {
  content: "";
  display: block;
  width: 100%;
  height: 10px;
  background: yellow;
}

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

Gleaming personalized select input/selectize input

I am striving to customize my Shiny select input in the following ways: Eliminate the label Set a unique background color: #2f2d57 Include a placeholder Enable users to both type-in and select Despite my efforts, I have not been successful in alignin ...

Unable to extract div HTML code from the provided HTML code

java script code $("#category select").change(function(){ var category = $('select option:selected').html(); $.ajax({ type:"Post", url:"collectiepost.php", data:"category="+category, cache:"false", success:function(html){ ...

Numerous instances of utilizing the identification feature to trigger the opening of a popup dialogue

After following this tutorial, I was able to create a pop-up box with a contact form. However, I am looking for a way to reuse this code in multiple locations without having to change the IDs for each button. If anyone has any advice on how to achieve thi ...

What is the best way to enable CSS IntelliSense for a Nuxt project?

Currently, I am undertaking a project using Nuxt.js and have set up a Docker image to manage all aspects of it. Additionally, I am looking to integrate MDBVue as a CSS library into my project. After some research, I learned that in order to utilize this li ...

Bug in Chrome causes CSS blur filter to malfunction

I came across a strange issue. Check out the sample gif below. https://i.sstatic.net/VonaW.gif In the gif above, you can see that the images on the right-hand side correctly apply the ease-out filter effect. However, the other images do not. This issue a ...

Positioning an HTML div

Currently, I am diving into the world of HTML and have included a relative div in my code. Although there are two additional divs positioned before it, they do not overlap; unfortunately, the first two still occupy space above the relative one. My desire ...

What are some ways to maintain the aspect ratio of an image with two dimensions?

Issue: The image is taller than the img-box container https://i.stack.imgur.com/zDBLM.png I have identified two potential solutions, but I am not satisfied with them: Removing the img-box wrapper Applying max-height and max-width properties to the img t ...

Creating a C# application that simulates a list within a text editor

I am currently working on developing a text editing tool that will serve as the front end for a collection of text strings. Users will have the ability to make edits within the editor, and I want to ensure that these changes can be seamlessly applied to th ...

Exploring json data with angularjs

I am facing a challenge with handling a complex JSON object by looping through it and displaying the data in a form. To tackle this, I have created a small service similar to the one shown below: service.myApprovalResults = function(){ var url = " ...

AvoidDefault functionality in Safari

I am encountering an issue with a textbox on my webpage. The code for the textbox is as follows: <input type="text" maxlength="5" size="2" value="1" id="paging_textbox"> Interestingly, there is no form tag enclosing this textbox on the page. When ...

Enhancing the appearance of elements using AJAX data submission

My current task involves utilizing jQuery to extract all content from a div along with its child elements. Here is the content within the DIV: <div class="content"> <h1 style="color:red">Test Title</h1> <p style="color:blue"> ...

Find your way to a designated location on a fluid Angular 2 webpage

Imagine I'm on a webpage where several items are displayed using *ngFor. Each item has a link that takes me to a different page when clicked. However, when I return to the original page using the back button, I'd like to be scrolled back to the s ...

Modify the size of a div based on the status of a checkbox using only HTML and CSS

Here is a small project that I created using only HTML & CSS. The goal is to modify the properties of the <div class="pro"> when the checkbox is checked. When this happens, all other <div class="pro"> elements should be hidden and the <art ...

Inconsistencies with Colorbox behavior in an external HTML document

Encountering a strange issue with colorbox while attempting to display a "div" element from another HTML file. The code functions perfectly, except for the final "div" in the external file. The structure of my external file is as follows; div1 img par ...

Is there a way to store a SAFEARRAY (an array of bytes) into an HTML hidden field?

Is there a way to extract an array of bytes from an active-x component, save it in an html-form input hidden field, and then send it to the server using form-submit? I'm not sure how to accomplish this. MIDL: HRESULT Data([out, retval] SAFEARRAY(VAR ...

Is it possible to apply CSS opacity only to the background color without affecting the text on top of it

Is it possible to set the opacity property specifically for the background of a div, without affecting the text inside it? I attempted to achieve this with the following code: background: #CCC; opacity: 0.6; However, it seems like the opacity change is ...

Floating multiple block-level elements within an <li> tag in HTML allows for greater control over the layout and

I am facing a complex issue that requires attention. Let me explain the situation: There is an unordered list consisting of list items displayed vertically with alternating background colors, all having the same width within a fixed-width div. Some list i ...

Is there a way to change HTML retrieved from an AJAX request before inserting it into the document?

I am utilizing an ajax call to retrieve an HTML page from the server; the ajax call is structured as follows: function loadHtml() { $.ajax({ type : 'GET', async : false, url : &apos ...

What exactly does original-title refer to in HTML coding?

I have been searching extensively, but I cannot find any information that explains the meaning and proper usage of the original-title in HTML code. I am unsure if original-title is a Tag or Attribute in HTML, and if it is the same as title? I came across ...

Looking for assistance with transferring a data attribute to a form redirection

I'm seeking assistance with a coding dilemma I've encountered. To provide some background, I have a list of items on my website, each featuring a 'Book Now' button that redirects users to different pages. Recently, I incorporated a mod ...