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

What is the best way to iterate through JSON objects stored in a single location?

Currently, I am dealing with a single JSON object structured as follows: Object --> text --> body --> div Array[20] --> 0 Object --> text --> body --> div Array[20] --> 1 Object --> text --> body --> div Array[20] --> . ...

I'm having trouble figuring out the code for the path in the POST request for my basic login form application

I have built a basic login form application in node.js. Although I can successfully display the login form and input login data, my POST request to capture the login data is not functioning correctly. I am encountering a POST http://localhost:3000/client 4 ...

Completes a form on a separate website which then populates information onto a different website

Creating a website that allows users to login and view various complaint forms from government websites or other sources. When a user clicks on a link to file a complaint, they will be redirected to the respective page. However, I am looking for a way to ...

Only conceal the table column if space is limited

A scenario involves an HTML table with two columns that need to be displayed fully, with the second column aligned to the right. Currently, this has been achieved by setting the width of the first column to 100%. However, a challenge arises where the words ...

MUI: Issue with pseudo element appearing cropped outside of Paper container

I am facing an issue where a red arrow pseudo element '::before' is partially cut off outside its container '.MuiPaper-root'. I need the arrow to remain visible, any suggestions on how to fix this? Here is the relevant code snippet and ...

Ways to display URL parameters on an HTML page without using PHP

I'm currently working on a website using HTML (without PHP) and I'm facing an issue with displaying URL parameters on an appointment confirmation page. The appointment details are being successfully passed through the URL parameters, but I'm ...

Is it possible to separate the words in my navigation bar to create more spacing and change the color of the text to white?

I've been attempting to create a navigation bar without much success. My goal is to position the words "Main Page", "About Me", and "Bibliography" with space between them, aligned left, center, and right respectively. Additionally, I want to change t ...

Invalid template detected within the Kendo dropdown component

I am trying to create a template that will only be displayed if the data value is "Low". This is how I have set up my template column: template: '# if( data=="Low" ){#<span><i class="fa fa-square blue"></i> <span># } ' U ...

Issue with fluid layout in CSS - Unable to specify height in pixels and minimum height property not functioning as expected

While working on a liquid layout, I have set all my width and height values in percentages. However, during the design process, I needed to check how my CSS was rendering so I temporarily set the height in pixels. Eventually, the height in percentage wil ...

The div is not showing the image as expected

Having some trouble creating a slideshow within my angular application. Struggling to get the images to display in the html code. To tackle this, I decided to create a separate component specifically for the slideshow (carousel.component). In the main app ...

What is the method for identifying the selected radio button that has been dynamically generated after clicking the submit button?

Despite this question being asked multiple times before, I am struggling to find a solution that fits my specific scenario. Initially, I have a static radio button that I create. The HTML code is as follows: <body> &l ...

Increment counter when a key is pressed in Angular 4 and decrement when the backspace key is pressed

As a beginner in Angular, my goal is to create a feature where: The character count in a label increases as text is entered into a textarea. When the backspace key is pressed, the counter should decrease. If the user attempts to enter more characters tha ...

The process of making an image fill an entire div using Html and CSS

What is the best way to make an image fill an entire div using Html and CSS? ...

Ensure that a div with fluid width remains consistently centered within another div

I have a bunch of boxes filled with content. The number of boxes in each row varies depending on the width of the browser window. Is there a way to ensure that all the boxes are always horizontally centered on the page? For guidance, you can check out th ...

Transforming an unordered list to function similarly to a select input

I am looking to style a ul list to function as a select form element. I have successfully populated a hidden input using my code. Now, I am trying to make the ul behave like a select input when either the keyboard or mouse is used. Previously, I had some ...

Converting a table into JSON format

I'm working on developing a live application that will showcase data in a table format like the one below: Machine Production Scanned Delta Mach 1 2500 1000 1500 Mach 2 1500 1300 200 Mach 3 6500 5000 1500 Mac ...

Create a circular shape using CSS that dynamically adjusts its size to match the width

Can you help me with a CSS challenge? I need to create circles around elements on a webpage, but my code is not working perfectly. The width of the circle is off and I can't seem to center it properly. The width does not match the content (it is alw ...

Having difficulty getting the sign operator to show up in a text field

Whenever the ADD div is clicked, "+" should be displayed on the textbox. The same goes for SUBTRACT, MULTIPLY, and DIVIDE. However, I am struggling to make the operators show on the textbox. Here is what I have managed to come up with so far. <!D ...

What is the process for including id parameters within the URL of an HTML page?

As I work on building a website with ReactJS and webpack, I encounter the need to access URL parameters. One specific challenge I face is creating a universal blog post view page that can be dynamically loaded based on the blog id parameter in the URL. Rat ...

What is the best way to retrieve the DOM of a webpage that uses MP4 file extension?

I'm attempting to access the DOM within a page that has an MP4 extension. The page isn't a video or audio file, it's just HTML with an MP4 extension "". I've included the code I'm using to try and access it using Simple HTML DOM. T ...