Ways to ensure a ul li element perfectly fits inside a div element without any spaces

As a newcomer to CSS and HTML, I'm facing an issue with my ul li dropdown menu and login boxes. The background colors are red and blue, but there are gaps within the div that I want to fill with the height of the div.

Below is the code snippet:

@keyframes animation {
  from   {opacity: 0%;}
  to    {opacity: 100%;}
}

.maindiv{
  width: 100%;
  height: 30px;
  background-color: black;

}
.ul1{
    height: 30px;
    margin: 0px;
    padding: 0px;
    text-align: right;
    list-style-type: none;
    color: white;
}
.ul1 a{

    color: white;
    text-decoration: none;
}
body{
    animation-name: animation;
    animation-duration: 2s;
    font-family: Arial, sans-serif;
}
li{
    display: inline;
    width: 100%;
    height: 100%;
    margin-right: 100px;
    text-align: left;
    }

.dropdown{
    display: none;
    position: absolute;
    margin-left: 70%;
    color: black;
}

.more:hover .dropdown{
    display: block;
    
}

#table{
    height: 100px;
    width: 200px;
    margin-top: 10px;
    box-shadow: 0px 5px 5px gray;
}
.more{
    width: 200px;

    background-color: blue;
}

.more td:hover{
    border: 1px solid black;
}

#li5{
    font-weight: bold;
    background-color: red;
    margin-left: 20px;
}
h1{
    font-style: italic;
    color: blue;
    font-size: 50px;
    text-shadow: 0px 12px yellowgreen, 0px 19px red;
}
<h1>Retro Logo</h1>
<div class="maindiv">
<ul class="ul1">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
<li class="more"><a href="#">More +</a>
    <div class="dropdown"><table id="table">
    <tr><td>option 1</td>
    <td>option 2</td>
    </tr>
    <tr><td> option 3</td>
    <td>option 4</td>
    </tr>
    </table></div></li>
<li id="li5"><a href="#">Log in</a></li>
</ul>
</div>

I've tried adjusting margins and paddings to 0, but nothing seems to work. Thanks in advance :))

https://i.stack.imgur.com/EYHjS.png

Answer №1

To enhance the CSS, consider removing the height attribute within the styles for both .maindiv and .ul1.

Answer №2

If you're wondering how to evenly list your drop downs:

  1. Try setting your li elements to display: block. This allows them to accept height and width properties, as opposed to inline which doesn't respect these properties. Drop downs should follow the vertical axis, so inline is not ideal.

If you need to adjust the spacing between your container divs:

  1. Consider setting top or bottom margins on the body element or all div elements in rems units.

I hope this information proves helpful!

Answer №3

I made a modification to your li rule:

li{
    display: inline;
    height: 100%;
    margin-right: 10%;
    text-align: left;
    }

After implementing this change, it worked perfectly. Here's the updated code snippet for reference:

@keyframes animation {
  from   {opacity: 0%;}
  to    {opacity: 100%;}
}

.maindiv{
  width: 100%;
  height: 30px;
  background-color: black;

}
.ul1{
    height: 30px;
    margin: 0px;
    padding: 0px;
    text-align: right;
    list-style-type: none;
    color: white;
}
.ul1 a{

    color: white;
    text-decoration: none;
}
body{
    animation-name: animation;
  animation-duration: 2s;
    font-family: Arial, sans-serif;
}
li{
    display: inline;
    height: 100%;
    margin-right: 10%;
    text-align: left;
    }

.dropdown{
    display: none;
    position: absolute;
    margin-left: 70%;
    color: black;
}

.more:hover .dropdown{
    display: block;
    
}

#table{
    height: 100px;
    width: 200px;
    margin-top: 10px;
    box-shadow: 0px 5px 5px gray;
}
.more{
    width: 200px;

    background-color: blue;
}

.more td:hover{
    border: 1px solid black;
}

#li5{
    font-weight: bold;
    background-color: red;
    margin-left: 20px;
}
h1{
    font-style: italic;
    color: blue;
    font-size: 50px;
    text-shadow: 0px 12px yellowgreen, 0px 19px red;
}
<h1>Retro Logo</h1>
<div class="maindiv">
<ul class="ul1">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
<li class="more"><a href="#">More +</a>
    <div class="dropdown"><table id="table">
    <tr><td>option 1</td>
    <td>option 2</td>
    </tr>
    <tr><td> option 3</td>
    <td>option 4</td>
    </tr>
    </table></div></li>
<li id="li5"><a href="#">Log&nbsp;in</a></li>
</ul>
</div>

Answer №4

If you're looking to add a drop-down button, the solution doesn't have to be overly complicated. Here's a more modern approach compared to the older methods.

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.dropbtn {
  background-color: #3498DB;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
  cursor: pointer;
}

.dropbtn:hover, .dropbtn:focus {
  background-color: #2980B9;
}

.dropdown {
  position: relative;
  display: inline-block;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f1f1f1;
  min-width: 160px;
  overflow: auto;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.dropdown a:hover {background-color: #ddd;}

.show {display: block;}
</style>
</head>
<body>

<h2>Clickable Dropdown</h2>
<p>Click on the button to open the dropdown menu.</p>

<div class="dropdown"&g
t;
  <button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">
    <a href="#home">Home</a>
    <a href="#about">About</a>
    <a href="#contact">Contact</a>
  </div>
</div>

<script>
/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {
    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
</script>
</body>
</html>

If you need a full navigation bar with a drop-down element, consider implementing the following code:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
.navbar {
  overflow: hidden;
  background-color: #333;
  font-family: Arial, Helvetica, sans-serif;
}

.navbar a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.dropdown {
  float: left;
  overflow: hidden;
}

.dropdown .dropbtn {
  cursor: pointer;
  font-size: 16px;  
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}

.navbar a:hover, .dropdown:hover .dropbtn, .dropbtn:focus {
  background-color: red;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: #ddd;
}

.show {
  display: block;
}
</style>
</head>
<body>

<div class="navbar">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <div class="dropdown">
  <button class="dropbtn" onclick="myFunction()">Dropdown
    <i class="fa fa-caret-down"></i>
  </button>
  <div class="dropdown-content" id="myDropdown">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
  </div> 
</div>

<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Click on the "Dropdown" link to see the dropdown menu.</p>

<script>
/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(e) {
  if (!e.target.matches('.dropbtn')) {
  var myDropdown = document.getElementById("myDropdown");
    if (myDropdown.classList.contains('show')) {
      myDropdown.classList.remove('show');
    }
  }
}
</script>
</body>
</html>

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

Having trouble identifying the CSS style being applied to a specific element

In this image, a red border is seen on the input fields indicating an error with some CSS style being applied. Despite this, it is difficult to determine which CSS selector is causing this as Firebug does not reveal any applied styles. https://i.stack.img ...

Changing HTML with defined PHP boundaries

I have a basic HTML wrapper and a PHP function that reads data from a file, splitting it into div tags for things like comments or forum posts. The text file is divided by delimiters to indicate the start of each section. The script correctly increments a ...

Experiencing blank areas when I try to minimize the screen while using Chrome

While using Chrome, my webpage looks perfect in normal mode. However, when I minimize the screen, white space appears at the bottom and some content gets hidden. The page is built using reactjs, html, and css. Interestingly, it works fine for IE in both no ...

How can I remove specific items from a session array when they are selected?

I am seeking a solution that will allow me to remove a specific item from an array when that item is clicked. For example, if I click on "Two", it should disappear. See a demonstration here: CODE: <form method="post"> <input type="text" i ...

Accepting PHP multidimensional array through ajax

My PHP code includes a script to open a database, fetch data, and encode it into JSON format. include_once($preUrl . "openDatabase.php"); $sql = 'SELECT * FROM dish'; $query = mysqli_query($con,$sql); $nRows = mysqli_num_rows($query); if($nRow ...

Issues with the functionality of jQuery append and AngularJS ng-if not functioning

In my application using checkboxes, I control the visibility of charts with the ng-if directive and implement drag-and-drop functionality with angular-Dragula. The use of ng-if is essential for me as it allows me to manipulate visible div IDs and organize ...

Concealing a child component when hovering over its parent element with styled-components

I have a react component structured like this - const MyComponent = () => ( <ContainerSection> <DeleteButtonContainer> <Button theme="plain" autoWidth onClick={() = ...

Organizing an HTML layout

Looking for a way to sort a div structure based on a parameter, I came across a small JavaScript that seems to not work as expected. It appears that the sorting function is not parsing the values perfectly... This is the logic I am using for sorting: < ...

What causes the image to not appear at the center bottom of the page when using IE for loading?

Why does the loading image not appear at the center bottom of the page in IE? This function loads content when the page is loaded and also loads content when scrolled to the bottom. When you load the page index.php, you will see the loading image at the ...

What are the steps to integrate <br> in a JavaScript code?

I have recently started learning about web development and I'm facing a challenge with this implementation. var obj = [[{ name: "John", age: 30, city: "New York"}, { name: "Ken", age: 35, city: "New Orleans"}]]; ...

Issues with scaling background videos in HTML5

I'm having trouble making a video scale properly with the browser window while still covering the entire area. Does anyone know why my current approach isn't working? HTML: <div class="bgVideoWrap"> <video id="bgVideo" loop="true" aut ...

Use the double data type in MySQL to handle null values when blank form fields are submitted

Currently, I am facing an issue with a form that I have created to update a mysql database using PHP and HTML. In my MySQL database, I have set the data type as double (3,1). The problem arises when I submit data to the database and leave some form input f ...

Enhancing Plotly Graphs with Custom Node Values

Encountering an issue while attempting to assign values to nodes on a plotly graph. Code: <html> <head> <script src="https://cdn.plot.ly/plotly-latest.min.js"></script> <style> .graph-container { ...

Leverage React.js by incorporating a Header-Imported JavaScript Library

I recently developed a React.js web application and am exploring the use of Amplitude Analytics, which provides a Javascript SDK found here. According to the guidelines, I need to include a <script></script> within the <head></head> ...

Issue with scroll down button functionality not functioning as expected

Is there a way to create a simple scroll down button that smoothly takes you to a specific section on the page? I've tried numerous buttons, jQuery, and JavaScript methods, but for some reason, it's not working as expected. The link is set up co ...

Exploring the jungle. Cursor acting strange while dragging

I am currently working on implementing drag-and-drop functionality in my project, utilizing slip.js from slip.js. To enhance the cursor during dragging, I have assigned class="draggable" to each draggable <tr>. The CSS code for this class is: .drag ...

Adjust the height of a div in JQuery to fit new content after specifying a height previously

I have a division element with an initial height of 0 and opacity set to zero, its overflow is hidden, and it contains some content. <div style='height: 0px; opacity: 0px; display: none; overflow: hidden; border: 1px solid #000;' id='myd ...

Is it advisable to hold off until the document.onload event occurs?

I'm working with a basic HTML file where I need to generate SVGs based on data retrieved through an AJAX call. Do I need to ensure the document is fully loaded by enclosing my code within a document.onload = function() { ... } block, or can I assume ...

Accessing the form element in the HTML outside of the form tag in Angular 2

I am attempting to achieve the following: <span *ngIf="heroForm?.dirty"> FOO </span> <form *ngIf="active" (ngSubmit)="onSubmit()" #heroForm="ngForm"> <div class="form-group"> <label for="name">Name</label& ...

Implementing Bootstrap in Wordpress using functions.php

The code snippet below is stored in my functions.php file within a Child theme directory. <?php function theme_styles() { wp_enqueue_style( 'bootstrap_css', get_template_directory_uri() . '/bootstrap.min.css' ); wp_enqueue_ ...