Dealing with paragraph formatting within list elements using personalized CSS styles

In this example, a paragraph is shown inside a list item (<li>) without any special styling:

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

<li>
  <p>
    hehehe
    ...
  </p>
</li>

However, when a custom list style type is applied, the layout changes:

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

The corresponding HTML code is:

<li class="docs-test">
  <p>
    hehehe
    ...
  </p>
</li>

Here is the CSS used for this customization:

.docs-test {
  color: red;
  list-style-type: none;
}

.docs-test::before {
  content: '\2022 ';
  color: #828282;
  display: inline-block;
  margin-right: 10px;
}

.docs-test p {
  display: inline-block;
}

While list-style-image could be an alternative for some cases, it's not applicable to all scenarios. How can paragraphs within custom list items retain their position and behave like standard unstyled list items? The objective is to maintain consistent handling of paragraphs across styled and non-styled lists.

Visit this link for live demo and code

Answer №1

If you're looking for a way to achieve the desired styling, here is one approach:

To position the generated content from the ::before pseudo-element to the left of the <li> block element, use absolute positioning.

Start by adding position: relative to the .docs-test CSS rule so that the pseudo-element can be positioned relative to its parent li.

Next, include position: absolute along with appropriate top and left offset values to achieve the desired styling effect.

Make sure to reset the margins on the p element to zero for consistency.

.docs-test {
  color: red;
  list-style-type: none;
  display: inline-block;
  position: relative;
}

.docs-test::before {
  content: '\2022 ';
  color: #828282;
  display: block;
  text-align: center;
  position: absolute;
  outline: 1px dotted gray; /* for demo only */
  width: 20px;
  left: -20px;
  top: 0;
}

.docs-test p {
  display: inline-block;
  margin: 0;
}
<ul>
  <li>
    <p>
      hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe
    </p>
  </li>

  <li class="docs-test">
    <p>
      hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe hehehe
      </p>
  </li>
</ul>

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

What is causing the discrepancy in height between my parent DIV and its children?

I've been grappling with this issue for quite some time now and unfortunately, I haven't been able to come up with a solution. Within my design, I have a frame that consists of a top box, a left box, a right box, and a middle box that contains th ...

Syntax for retrieving response with jQuery AJAX

Currently, I am in the process of working on an AJAX request that triggers a URL when a specific button is clicked. The fundamental aspects are running smoothly, ensuring that the GET request is activated towards the URL upon button click. I have also cros ...

Solving the Color Change Mystery in Your Dash Application

My goal is to design a sleek black dashboard, so I acquired a CSS stylesheet that should give my dashboard a black theme. Following the instructions, I created an assets folder in the root directory of my app and added my CSS and JavaScript files there. Da ...

Guide on customizing the checkbox label class upon @change event in Vue?

Query: I have a list of checkboxes connected to an array of names. The objective is to alter the class of a specific name when its checkbox is clicked. Issue: Clicking on a checkbox causes all names to change class, instead of only affecting the one ass ...

How can I remove HTML tags from a string using .NET?

Does anyone know of an efficient way to strip HTML tags from a string of HTML text? ...

Disregarding the Syntax of jQuery Mobile

Is there a way to override jQuerymobile's formatting and classes when styling an element? I want to apply my own custom style. I attempted using $.mobile.ignoreContentEnabled = true; in the pageInit function, followed by adding data-role="none" to th ...

Having trouble with loading background images in Angular 4?

After researching extensively on stack overflow, I am still struggling to resolve this issue. The main problem I am facing is related to adding a background image to the header tag in my code. Unfortunately, no matter what I try, the background image refu ...

The Angular checked functionality is not working as expected due to the presence of ngModel

Just getting started with Angular here. I’m working on a checkbox table that compares to another table and automatically checks if it exists. The functionality is all good, but as soon as I add ngModel to save the changes, the initial check seems to be ...

Tips for incorporating a smooth fade in and out effect into images within a Bootstrap 5 carousel

I'm currently working with a Bootstrap 5 carousel and trying to implement a transition effect where clicking the "next" arrow will smoothly fade out the current image before fading in the next one. This is necessary because the images in my carousel v ...

Incorporating a spontaneous color element into formatting text links with Jquery

Looking to add some flair to my website by styling all text links with a random color using jQuery. I've been able to generate the random color variable, but struggling to integrate it into my CSS stylesheet or create a script that applies the color t ...

Is it possible to update the minimum date message?

How can I customize the minimum date message in my code from "please select a value that is not earlier than 2018-10-02" to "please select a value that is not earlier than 02-10-2018". https://i.sstatic.net/NYaZO.png Is there any way to change the date ...

Finding the starting position of the current line in a multiline textarea using only Javascript

I am specifically seeking a solution using pure JavaScript to align with the scope of my project. Please note that I will not mark jQuery answers as correct, but they are welcome for reference by future users. Additionally, I am not interested in utilizing ...

Step by Step Guide to Creating Vote Tallying Buttons with XHTML

I'm trying to create a voting system where users can choose between two images. However, all the tutorials I've found focus on text or check-box options. How can I make clickable buttons tally votes for each image and then display the results on ...

Tips for organizing SQL queries in a grid layout

I have a database table called People in SQL and I am displaying the names of people from this table on my website. Each name is represented as a column in the People table. My goal is to arrange these names in a grid layout, similar to a table, but with ...

Ensure that only one menu with a specific class is open at any given time

My goal is to ensure that both menus cannot be open simultaneously. The desired behavior is as follows: When one menu is already open and you click on another, the first one should automatically close. For a better understanding of what I am trying to achi ...

What is the best way to navigate through images only when hovering?

I have a website that showcases a collection of images in a creative mosaic layout. Upon page load, I assign an array of image links to each image div using data attributes. This means that every image in the mosaic has an associated array of image links. ...

Is it possible to adjust the width and margin without specifying a left or right float?

I am attempting to adjust the width and margins of a tag without using float. I have tried the following code, but it does not seem to be working: .float-right .createAccount{ float: left; margin-left: -11px; width: 140px;} ...

Stop Duplicate Entries from Showing in Your Angular Project

In my application, there is a form that enables users to search for doctors based on the criteria they select from drop-down menus. Below is the structure of the form: https://i.sstatic.net/itguq.png When a user chooses a city and clicks the search butt ...

Enhance the appearance of Google Maps by incorporating gradient effects, box shadows, or other similar visual

How can I add a linear gradient to Google Maps? Below is my code snippet. HTML <div id="map"></div> CSS #map{ height:100vh; background: linear-gradient(90deg, #f7f8fa 0%, rgba(247, 248, 250, 0) 18.73%), linear-gradient(180deg, # ...

Error: Syntax error detected. Unexpected blank space found. Expecting either "-" symbol, identifier, or variable

Error: Syntax error: unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\Source Code\displaysalesdetailed.php on line ...