Variability in the positioning of list item markers depending on the child's display property

Looking at this code:

<ul>
    <li>
        <div id="container">
            <button>toggle display</button>
            <div>text</div>
            <div>text</div>
        </div>
    </li>
</ul>

If the #container element's computed CSS display property is set to block, the list item marker (bullet ) shows up beside the first line of content, to the left of the <button>.

However, when the #container's display CSS property is changed to inline-block, the list item marker appears beside the last line of text. You can see this in action in this example. Tested on Chrome 35 and Firefox 30.

Is there a way to keep the item marker in the same position in both cases? (preferably where it is with the display: block container)

By adjusting the vertical-align of the inline-block element, you can influence the item marker's placement. However, there is still a difference in positioning since the vertical-align property doesn't affect block elements.

Is there a solution to ensure that the bullet always appears in the exact same spot, regardless of the #container's display property?

An ideal solution would only involve CSS, but modifying the markup is an option as well.

Answer №1

Based on information from http://www.w3.org/TR/CSS21/visudet.html:

The baseline of an 'inline-table' element is determined by the baseline of the first row of the table.

For an 'inline-block' element, the baseline is the baseline of its last line box under normal flow, unless there are no in-flow line boxes or if the 'overflow' property is set to a value other than 'visible', in which case the baseline is the bottom margin edge.

Check out these two demonstrations as well:

It seems like both block and inline-table share the same baseline. In this case, using inline-table instead of inline-block would be the solution.

Answer №2

One way to resolve this issue is by enclosing the #container within an inline-block element:

.container-outer {
    display: inline-block;
    width: 100%;
    vertical-align: top;
}

Check out the Fiddle

By doing this, the bullet is now positioned in relation to the new outer inline-block element, creating the desired consistency without altering the display characteristics.

The only drawback is that the bullet is now aligned with the top of the element (due to the inline-block display and vertical-align: top) rather than aligning with the first line of content as it would with a block element. However, this change is hardly noticeable.

Answer №3

It is quite puzzling why the bullet seems to target the last block inside an inline-block element. However, a simple solution to this issue is to switch the blocks within the inline-block to a table. This change resolves the problem, as demonstrated in the following example: http://jsfiddle.net/3BYKp/1/.

#container > div {
    display: table;
}

Answer №4

Below is a less conventional method to achieve the desired outcome:

li {
    list-style-type: none;
}
li::before {
    content: "\25CF";
    margin-left: -20px;
    position: absolute; # credit goes to DRD
}

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

Pinpointing a specific region within an image by utilizing offsets

Would it be possible to detect a specific area within an image using a predefined set of image offsets? Here is the image and accompanying offsets: Sample Image Below are the sample image offsets for the highlighted area: Offset(63.4, 195.2) Offset(97.7 ...

Reverse animation transition not activating in CSS

In an attempt to create a side navbar using HTML/CSS, I implemented hover animations where hovering over an icon would cause it to double in size and disperse the rest of the items in the navbar. While I successfully achieved this animation with JavaScript ...

Revealing Transition Element on mouseover

My side-menu consists of icons that display text to the right upon hovering over them. I want the text to show up and the div to expand smoothly with a sliding transition, ideally without the need for additional JavaScript. Is there a way to achieve this? ...

What is the best way to place this dropdown in a fixed position within a parent element with overflow scrolling

I am facing an issue with a dropdown menu inside a parent div. The parent div has a fixed height and is set to overflow: auto. My goal is to have the menu extend beyond the boundaries of the parent when opened, but currently it just increases the height of ...

The click function for the responsive navbar hamburger is not functioning properly

Having some trouble with the code not working in responsive mode. I've tested it on a 600px screen and the hamburger button doesn't seem to work (I click it and nothing happens). I've gone through both the CSS and JS multiple times but can&a ...

Is it possible to customize the CSS styles of a jQuery UI modal dialog box?

Having trouble with my CSS styles not applying to a dialog modal added to my website using jQuery UI, even when using '!important'. I suspect that the predefined jQuery or UI CSS styles from a CDN link are preventing me from overriding them. The ...

New feature in Bootstrap 4 beta allows carousel arrows to be positioned outside of the

I created a text-based carousel using Bootstrap 4 beta. Is it possible to move the arrows off of the slider area and position them outside of it? I have tried searching on Google and in other forums, but I couldn't find a solution. The issue is that s ...

Synchronizing CSS3 Animation Events and Event Listeners in jQuery: A Complete Guide

I've been exploring different ways to utilize CSS3 Animation Events and Event Listeners across browsers for more precise control over CSS3 animations. While I know it's possible, I'm struggling to implement it due to my current limitations d ...

How about a CSS one-by-one black pixel that's not opaque?

I've been attempting to achieve a 70% transparent black background on an element, but unfortunately, I had to use a pixel to address browser compatibility problems. Using CSS alone would make the entire content of the element transparent as well. Her ...

Press the list button to reveal an enlarged box

Each item on this list contains a hidden box that should be shown after it is clicked. However, the issue arises when expanding one item causes other items to expand as well. Refer to the image for clarification: Image Link Is there a way to only expand ...

Steps to altering the color of a second button with a button click

My goal is to create a button click function that allows only one button to be clicked at a time. For instance, when btn1 is clicked, its background color changes from transparent to green. Then, if btn2 is clicked, btn1's background color should chan ...

Unable to update the information within a DIV using Python Django, as well as the webpage following a jQuery action

Exploring new ideas through the lens of two variables in an HTML template, messages and users, a series of buttons trigger a jQuery function upon being clicked. This function sends a post request to a Django server, which then returns an update to the mess ...

A crisscrossing dashed design running along the edges of the text block

<div class="search"> <p>SEARCH</p> </div> https://i.sstatic.net/yxOga.png I am attempting to incorporate a dashed lateral padding similar to the one shown in the image, but only on the sides of the text. Is there a method I can ...

Support for TrueType fonts in web browsersLet me know if you

If I have a custom font in TTF format and want to check if @font-face supports it, what script can I use? What are the available options? ...

How to effectively use flexbox to resize child elements in a CSS layout

My goal is to create a layout where a child element fills the entire flex box of a flex layout. Here is an example of the HTML structure I am using: <div class="parent"> <div class="child1"> should have 100px height </div&g ...

Tips for incorporating a CSS class representing an image into a CodeIgniter form submission

<?php $data = array( 'class' => "btn btn-primary btn-lg", 'name' => 'report' ); echo '<span class="glyphicon glyphicon-exclamation-sign"></span> '; echo form_subm ...

Having trouble getting my parallax slideshow to work with jquery preventDefault

-UPDATE- After countless hours of online courses, tutorials, and programming, I finally completed my website! Check it out here: The site is almost where I want it to be, but there are a few remaining challenges: 1) AJAX: I'm struggling to get the ...

Having trouble with your CSS on your mobile website?

After developing a website and implementing media queries for responsiveness, everything seemed to be in perfect working order. The website displayed flawlessly on emulators like () and Chrome's inspect element, mirroring my desired mobile design. How ...

Utilizing the overflow feature in conjunction with a specified height

Take a look at this image, the overflow:hidden property is not working as expected and some text is still visible that I want to hide without adjusting the height. How can I achieve this? Focusing on the highlighted area in Red, it is causing irritation. ...

Adjusting the alignment of Bootstrap navbar

I'm new to CSS and I want to center the brand logo and links in the navbar. HTML <nav class="navbar navbar-expand-md center"> <a class="navbar-brand" href="http://www.dimsum.dk" target="_blank" ...