Utilize CSS block display for ::before and ::after pseudo elements within a div container

I have a CSS file that contains the following code snippet:

.loader {
    width: 250px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-family: helvetica, arial, sans-serif;
    text-transform: uppercase;
    font-weight: 900;
    color: white;
    background-color: gray;
    letter-spacing: 0.2em;
}

.loader::before, .loader::after {
    content: "";
    display: block;
    width: 15px;
    height: 15px;
    background:blue;
    position: absolute;
    /* animation: load .7s infinite alternate ease-in-out; */
}

.loader::before {
    top: 0;
}

.loader::after {
    bottom: 0;
}

Here is the corresponding HTML code:

<div class='loader'>Loading</div>

Initially, the output appears as shown in this image: https://i.stack.imgur.com/r5CKg.png

However, when I comment out the "display: block" property in the before and after selectors, the output changes to look like this: https://i.stack.imgur.com/YJkSS.png

I am perplexed by why the smaller blocks shift when the "display: block" property is removed. Can someone provide a simple explanation for me? I have been pondering over this for an hour but cannot grasp the reasoning behind the positioning of the two smaller block elements with and without the display block property. Why does the after block move to the right of the 'Loading' text instead of towards the left?

Additionally, if we remove the display property (defaulting to inline), shouldn't the three elements appear next to each other horizontally? However, I observe that the first block overlaps vertically with the text.

Answer №1

block keeps all elements in the same block, ensuring alignment when removed; alternatively, you can align them to the left or right.

.loader {
  width: 250px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-family: helvetica, arial, sans-serif;
  text-transform: uppercase;
  font-weight: 900;
  color: white;
  background-color: gray;
  letter-spacing: 0.2em;
}

.loader::before,
.loader::after {
  content: "";
  /*display: block;*/
  width: 15px;
  height: 15px;
  background: blue;
  position: absolute;
  /* animation: load .7s infinite alternate ease-in-out; */
}

.loader::before {
  top: 0;
  left: 0;
}

.loader::after {
  bottom: 0;
  left: 0;
}
<div class='loader'>Loading</div>

Answer №2

Using the CSS property display: block
will cause the element to span the entire width and start on a new line. In this scenario, both the squares before and after are on separate lines, utilizing the full width available.

As stated by W3.org:

The :before and :after pseudo-elements inherit any inheritable properties from the main element in the document tree they are attached to.

For more information, visit this Mozilla Developer page on the display property.

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

Position the div to float on the right side and then have it drop below on smaller screens

I'm currently working on implementing a design effect similar to the one shown below: on my website (), but I'm struggling with getting the HTML and CSS just right. When I move the map to float up on the right side, it doesn't drop below th ...

Trying to configure and use two joysticks at the same time using touch events

I have been grappling with this problem for the past two days. My current project involves using an HTML5/JS game engine called ImpactJS, and I came across a helpful plugin designed to create joystick touch zones for mobile devices. The basic concept is th ...

Unexpected issue with Ionic 4 subarray returning as undefined even though the index is accurate

When attempting to use console.log to view the value, I noticed that the value of noticeSet2[index] is undefined. However, when I print noticeSet, all the data in the array is displayed. Additionally, after printing the index using console.log, it correctl ...

The wrap() method in jQuery clones elements multiple times

I have created a simple function that wraps a div tag inside another div tag when the browser window exceeds a certain width of x pixels. However, I've encountered a strange bug when resizing the page more than once. Let me share with you the jQuery ...

When a user clicks on a child element of an anchor tag, the function will

Is it possible to return a specific function when a user clicks on a child of an anchor element? <a href="/product/product-id"> I am a product <div> Add to favorites </div> </a> const handleClick = (event) =>{ ...

Choose content within an HTML tag and modify its appearance

<body> This is text1 <div> This is text2</div> </body> I'm looking to style the text1 only. I attempted to use body{ color:red; } but both text1 and text2 ended up turning red. What I really need is something like th ...

Submit Button in CKEditor Not Working Without Ajax Submission

Having an issue with the ckeditor. Downloaded the latest version and integrated it into my form like this: <form action="/news.php?frame=edit&amp;id=185" enctype="multipart/form-data" method="post" accept-charset="utf-8"> <textarea class="edi ...

Is it possible for a kraken.js backend to manipulate the content of a webpage?

Currently using kraken.js, which is an express.js framework, to construct a localized website. Within the header section, there are 3 language options provided as links. FR | EN | DE My goal is to have the link corresponding to the currently set locale ( ...

The console is showing an error message stating that the variable "i" is not

Currently, I am developing the dashboard for my discord.js bot. While working on it, I encountered an issue where a variable I created to select an array from my JSON file is being reported as undefined by the console. $(function() { $.getJSON( "./data/ ...

Choose the appropriate button when two buttons have the identical class

Here is the HTML code I am working with: <a class="action_btn recommend_btn" act="recommend" href="recommend.php"> Recommend </a> Below is my jQuery implementation: $(".action_btn").click(function() { }); However, a problem arises beca ...

Two nested divs positioned below text within a parent div

I am styling a content div with text and two child divs positioned next to each other within the content div. My goal is to have the child divs display below the text, rather than beside it. body { text-align: center; } #first, #second { border: so ...

Why is the Django image URL redirecting to an HTML template instead of the media file?

I created a Django application with a template that includes an image. However, I encountered an issue where the server is attempting to display the image using the redirect link from the HTML template instead of retrieving it from the media file. For ins ...

Guide to positioning an icon at the center of a material card

I am new to CSS and Angular Material, and I need help centering the icon on the card following the design from Figma. I tried copying the CSS from Figma, but it didn't center the icon as expected. Can someone please share techniques to achieve this? S ...

What is the best way to call a JavaScript function within a PHP echo statement that outputs a <div> element?

Having trouble echoing this PHP code due to issues with single quotes, causing the HTML to end prematurely. Any suggestions on how to fix this? function button($conn){ $sql = "SELECT * FROM table"; $result= mysqli_query($conn, $sql); while($r ...

Is it possible to set the input form to be read-only?

I need to create a "read-only" version of all my forms which contain multiple <input type="text"> fields. Instead of recoding each field individually, I'm looking for a more efficient solution. A suggestion was made to use the following: <xs ...

Loop through each JSON object and insert it into an HTML element

I am working on looping through a JSON object and converting it into HTML. While I can iterate through all the objects in the JSON, I am having trouble extracting just the first object. var res = '[{"ID":"246","mobile":"samsung","feedback":"feedback ...

Stop capturing mouse and keyboard events within a specific div element on all web browsers

I manage a website with forms that have jquery autosave features and are updated using ajax. These forms include various types of input elements such as textboxes, jQuery UI datepickers, and time pickers... <div id="content"> <form id="line1"&g ...

Customizing the footer on various pages using Footer.php

For the past week, I've been working on updating our mobile site and have encountered an issue with the footer. It loads perfectly fine on the home page but crashes when viewing a regular page. Strangely enough, both pages are using the same footer.ph ...

The light/dark mode toggle is a one-time use feature

I've been experimenting with creating a button to toggle between light and dark modes on my website. Initially, it's set to light mode, but when I try switching to dark mode, it works fine. However, the issue arises when attempting to switch back ...

Is there an advantage to pre-rendering a circle on an HTML5 canvas for improved performance?

Is it more efficient to pre-render graphics for a basic shape like a circle on an off-screen canvas? Would there be noticeable improvements in rendering performance when dealing with 100 circles at game-like framerates? What about 50 circles or just 25? ...