- Aligning list items using UL bullet style

I'm having trouble aligning my ul dropdown with the rest of my menu.

Even after removing the Bullet from my ul dropdown, it still maintains its position (I expected it to move all the way to the left side of the page).

I've attempted various styling options to align the ul dropdown with the rest of the menu, but I can't seem to figure it out.

Setting both the margin and padding to 0 causes the dropdown to align all the way to the left of the page (as shown in the snippet).

Adjusting the margin and padding values either moves it to the left side of the screen or puts it back in its original position.

 $('ul li.dropdown').hover(function() {
 
  $(this).find('.dropdown-menu').stop(true, true).delay(200).fadeIn(500);
  }, function() {
  $(this).find('.dropdown-menu').stop(true, true).delay(200).fadeOut(500);
});
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Raleway">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.bundle.min.js" integrity="sha384-feJI7QwhOS+hwpX2zkaeJQjeiwlhOP+SdQDqhgvvo1DsjtiSQByFdThsxO669S2D" crossorigin="anonymous"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>


<!-- this uses list-style-type: none; which results in not having a bullet ( my goal) but not aligning to the other links correctly-->

<div class="w3-bar-block">
    <a href="#" target="_blank" class="w3-bar-item w3-button w3-padding"><i class="fa fa-users fa-fw"></i>  Link 1</a>
    <a href="#" class="w3-bar-item w3-button w3-padding"><i class="fa fa-user fa-fw"></i>  link 2</a>
  <ul style="list-style-type: none;">
      <li class="dropdown">
        <a class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-dashboard fa-fw"></i>  DropdownHeader</a>
        <ul class="dropdown-menu">
             <li><a href="#">sub1</a></li>
             <li><a href="#">sub2</a></li>
             <li><a href="#">sub3</a></li>
             <li><a href="#">sub4</a></li>
  </ul>
      </li>
  </ul>
</div>


<!-- this uses list-style-type: none; padding: 0; and margin-0; which results in not having a bullet ( my goal) but not aligning to the other links correctly either. Does not matter what you change padding and margin to it will either stay to the left side of the screen or jump back to *original postion*-->

<div class="w3-bar-block">
    <a href="#" target="_blank" class="w3-bar-item w3-button w3-padding"><i class="fa fa-users fa-fw"></i>  Link 1</a>
    <a href="#" class="w3-bar-item w3-button w3-padding"><i class="fa fa-user fa-fw"></i>  link 2</a>
  <ul style="list-style-type: none; padding: 0; margin-0;">
      <li class="dropdown">
        <a class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-dashboard fa-fw"></i>  DropdownHeader</a>
        <ul class="dropdown-menu">
             <li><a href="#">sub1</a></li>
             <li><a href="#">sub2</a></li>
             <li><a href="#">sub3</a></li>
             <li><a href="#">sub4</a></li>
  </ul>
      </li>
  </ul>
</div>

Answer №1

It appears that the best approach would be to style the child elements of the root element within this particular structure. To achieve this, consider implementing the following CSS ...

.w3-bar-block > * { display: inline-block; }

Alternatively, if the entire menu structure is part of your menu, you might want to simplify your markup like so ...

<ul class="menu">
    <li><a href="#" target="_blank">Link 1</a></li>
    <li><a href="#">Link 2</a></li>
    <li>
      <a>DropdownHeader</a>
      <ul class="dropdown-menu">
         <li><a href="#">Submenu 1</a></li>
         <li><a href="#">Submenu 2</a></li>
         <li><a href="#">Submenu 3</a></li>
         <li><a href="#">Submenu 4</a></li>
            </ul>
    </li>
</ul>

Then apply CSS similar to the following ...

.menu      { list-style-type: none; }
.menu > li { 
   vertical-align: top; 
   display: inline-block; 
   padding: 5px;
   background: #CCC;
}

.menu li > ul         { display: none; }
.menu li:hover > ul { display: block; }

Check out this js fiddle for a demonstration ...

https://jsfiddle.net/sgh0tLv0/9/

Based on the assumption that the menu is vertical, the following HTML and CSS can also achieve the desired outcome ...

html:

<ul class="menu">
    <li><a href="#" target="_blank">Link 1</a></li>
    <li><a href="#">Link 2</a></li>
    <li>
      <a>DropdownHeader</a>
      <ul class="dropdown-menu">
         <li><a href="#">Submenu 1</a></li>
         <li><a href="#">Submenu 2</a></li>
         <li><a href="#">Submenu 3</a></li>
         <li><a href="#">Submenu 4</a></li>
            </ul>
    </li>
</ul>

css:

.menu > li { 
   vertical-align: top;  
   padding: 5px;
   background: #CCC;
}

.menu li > ul         { display: none; }
.menu li:hover > ul { display: block; }

...

If you prefer to maintain your current HTML structure (although I don't recommend it), you could make the necessary adjustments by following these steps ...

Add the menu class to the root div element ...

<div class="w3-bar-block menu">

Then include the following CSS (in addition to the menu CSS provided above) ...

.w3-bar-block > * { display: block; margin: 10px; }
.w3-bar-block > ul { margin-left: -30px; }
.w3-bar-block > ul > li { margin-left: 0; }

Updated fiddle link:

https://jsfiddle.net/sgh0tLv0/19/

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

Struggling to locate desired href links using BeautifulSoup

I'm currently working on compiling a list of hrefs from the Netflix job portal: . Each job posting on this platform comes with an anchor and a specific class: <a class=css-2y5mtm essqqm81>. To ensure accuracy, here is the complete anchor: <a ...

How can I use JavaScript or CSS to identify the specific line on which certain words appear in the client's browser?

Imagine I have the following HTML structure: <div> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco labor ...

Retrieve webpage content using an ajax request in JavaScript

I'm working on an HTML page with an Ajax call to load table content. <html> .... <script sec:haspermission="VIEW_NOTE" th:inline='javascript'> require(['app/agent/viewGlobalAgent'], function (){ ...

Obtaining information from within the <div> element containing data in <p> tags

Seeking to extract information from the specified tag. Successfully completed the following steps. Document doc = Jsoup.parse(currMsg); Elements ele = doc.select("p"); This code returns <p>data</p> I am looking for only data. Next, attempte ...

Tips for stopping the web page from moving due to scrollbar placement?

On my website, I have DIVs that are center-aligned. However, some pages require scrolling while others don't. Whenever I switch between these two types of pages, the appearance of a scrollbar causes the page to move slightly to the side. Is there a wa ...

Storing information in a PHP database

I have created two pop-up divs on my website. Initially, the first div - div1, is displayed and it contains dropdown menus. Inside this div1, there is a button called Next which, when clicked, closes the first div and opens the second div; div2. Div2 consi ...

Is it possible to choose only half of the elements using the :nth-child selector in

Consider this code snippet: <ul> <li>Hello Universe</li> <li>Hello Universe</li> <li>Hello Universe</li> <li>Hello Universe</li> </ul> Can we select exactly half of the total elements by ...

What is the best way to ensure that my mobile website with fixed-width always appears fully zoomed in?

How can I ensure that my fixed width website always appears fully zoomed in on Webkit browsers such as iPhone and Android? Currently, the site looks good on an iPhone but appears too small or zoomed out on higher resolution Android phones. I have tried u ...

Accessing variables within a frame using JavaScript.Is there a way to access variables within

I am having trouble accessing a Frame that is included within a frameset. The structure looks like this: IFRAME \ FRAMESET \FRAMESET \ FRAME I have been struggling to find a solution, I can only access the iframe ...

Wordpress Issues with Displaying Arabic Text Correctly

The issue arises when displaying Arabic text in menus, both the top menu and main menu. The text appears distorted and unreadable, with characters like 2Ø®Ø§Ù†Ù‚Ø§Û showing up. However, in other areas of the page such as the body content, the ...

When a HTML file is piped or streamed into a browser, it is displayed as plaintext

I'm currently working with an Express handler router.get('/', ac.allow('Admin'), function (req, res, next) { let html = path.resolve(__dirname + '/../coverage/lcov-report/index.html'); fs.createReadStream(html).pip ...

How can I make a div or iframe stretch to fill the remaining space using javascript, jQuery, or css?

I'm looking for a way to make the middle iframe dynamically fill the remaining space between two fixed height iframes (one at the top and one at the bottom) regardless of the window screen size. Is there a technique or method that can achieve this? th ...

What is the process by which the browser displays pseudo-element selectors?

One thing that's been on my mind is the potential resource cost of using *:after. Would the pseudo element be the primary selector, or would all elements still be processed by the client? In simpler terms, what impact does *:after have compared to ju ...

The image remains unchanged in the JavaScript function

My attempt to swap images using jQuery has hit a snag. Upon running the page, it appears that the chase() method finishes executing before the animation has completed. The goal was to create an illusion of chasing between two images by repeatedly replaci ...

Invoking partial view via Ajax

I am using Ajax to call a partial view, but it is returning as undefined. This is my controller code: [HttpGet] public ActionResult CallPartial() { if (Request.IsAjaxRequest()) { return PartialView("~/Views/Partial1"); ...

Mastering the Art of Utilizing $(this) in jQuery

$(".item_listing").hover(function(){ $(".overlay_items").display(); },function(){ $(".overlay_items").hide(); } ); This is my jQuery code. I am trying to display the "overlay_items" div when the "item_listing" div is hovered ov ...

Styling elements with CSS to create horizontal and vertical lines between them

Struggling to replicate the design elements from this image using CSS. Specifically, I need help creating a horizontal yellow line over the title "nossos numeros" and vertical blue lines between "Cursos", "Alunos", and "Aulas". I am working with Bootstrap ...

How do I retrieve the download URL for the file created using Python in my Heroku App?

After developing my Flask App, I uploaded several files to the Heroku filesystem. I'm aware that these files are removed every time the dyno restarts. Now, in my HTML/JavaScript frontend, I'd like to provide users with a download button for thes ...

Issue with event.stopPropagation() in Angular 6 directive when using a template-driven form that already takes event.data

I am currently developing a citizenNumber component for use in forms. This component implements ControlValueAccessor to work with ngModel. export class CitizenNumberComponent implements ControlValueAccessor { private _value: string; @Input() place ...

Using jquery to update the overall quantity of likes

Utilizing jquery, html, and php, I have successfully implemented a like button on my page. However, I am currently facing an issue with displaying the total number of likes without refreshing the page. Upon clicking the like button, the jquery and php scr ...