There seems to be a strange behavior in my CSS where it is combining two divs into a single slightly larger box

Can anyone help me figure out what's wrong with the code I wrote? Below is the snippet provided.

html {
  background-image: url(back.png)
}
#main {
  text-align: center;
}
#visible {
  display: inline-block;
  background-color: #000c21;
    width: 80%;
    color: #c4c5c6;
    font-family: sans-serif;
    font-size:20;
}
html,body,#main,#visible { height:100%; }
.topnav {
  overflow: hidden;
  background-color: #182b3a;
  position: fixed;
  top: 0;
  width: 100%;
}

.topnav a {
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover,.dropdown:hover .dropbtn {
  background-color: #1a3c56;
  color: white;
}

.topnav a.active {
  background-color: #1a3c56;
  color: white;
}
.dropdown-content {
    display: none;
    position: absolute;
    background-color: #182b3a;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
}
.dropdown {
    float: left;
    overflow: hidden;
}

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

.dropdown:hover .dropdown-content {
    display: block;
}
<html>
<head>
<link rel="stylesheet" href="main.css">
</head>
<body>
    <div class="topnav">
  <a class="active" href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
  <a href="#pfft">herpiderp</a>
  <div class="dropdown">
    <button class="dropbtn">Dropdown 
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
</div>
    <div id=main>
      <div id=visible>
          herpiderp
      </div>
    </div>

</body>
</html>

I'm experiencing an issue where my drop-down element is interfering with my main div. Can someone explain why this is happening? I'm creating a website for a robotics project and need to implement a proper drop-down menu, but I can't seem to resolve this strange effect. Any assistance would be greatly appreciated.

Answer №1

It seems like there may be an unclosed div tag here. Perhaps you forgot to close a div element. Here is an updated version:

<div class="topnav">
    <a class="active" href="#home">Home</a>
    <a href="#news">News</a>
    <a href="#contact">Contact</a>
    <a href="#about">About</a>
    <a href="#pfft">herpiderp</a>
    <div class="dropdown">
       <button class="dropbtn">Dropdown 
          <i class="fa fa-caret-down"></i>
       </button>
       <div class="dropdown-content">
          <a href="#">Link 1</a>
          <a href="#">Link 2</a>
          <a href="#">Link 3</a>
       </div>
   </div>
</div>
<div id="main">
   <div id="visible">
      herpiderp
   </div>
</div>

Also, as suggested by @Romel Indemne, make sure to add double quotes where necessary.

Answer №2

Delete the overflow: hidden; property from the .topnav selector

Answer №3

html {
  background-image: url(back.png)
}

#main {
  text-align: center;
}

#visible {
  display: inline-block;
  background-color: #000c21;
  width: 80%;
  color: #c4c5c6;
  font-family: sans-serif;
  font-size: 20;
}

html,
body {
  min-height: 100vh;
}
#main,
#visible {
  height: 100%;
}

.topnav {
  background-color: #182b3a;
  position: fixed;
  top: 0;
  width: 100%;
}

.topnav a {
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
}

.topnav a:hover,
.dropdown:hover .dropbtn {
  background-color: #1a3c56;
  color: white;
}

.topnav a.active {
  background-color: #1a3c56;
  color: white;
}

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

.dropdown {
  float: left;
  position: relative;
}

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

.dropdown:hover .dropdown-content {
  display: block;
}
<div class="topnav">
  <a class="active" href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
  <a href="#pfft">herpiderp</a>
  <div class="dropdown">
    <button class="dropbtn">Dropdown 
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
      <a href="#">Link 1</a>
      <a href="#">Link 2</a>
      <a href="#">Link 3</a>
    </div>
    <div id="main">
      <div id="visible">
        herpiderp
      </div>
    </div>
  </div>
</div>

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

Is it possible to retrieve the value of this input field within a function call in HTML?

When working with an HTML input field, I encountered the need to limit manual input to 100. Although I already had minimum and maximum values set up for spinner functionality (up and down arrows), I wanted to create a reusable function instead of using inl ...

Adjust the size of a SVG group by hovering over a separate HTML element

I need some help with implementing a scale effect on group elements within an SVG. The goal is to have the group element scale up when hovering over a list item, but I am running into issues with the positioning because of the translate property. Does any ...

Struggling with the display property d-none in Bootstrap

I'm currently attempting to show a segment of my code exclusively on medium-sized screens and larger, while displaying another portion of the code solely on small and extra-small screens through the utilization of Bootstrap. I made use of Bootstrap&ap ...

Utilize dropdown selections to generate a custom function for calculating prices using JavaScript

Currently, I am facing a challenge in writing the calcTotal function as I am uncertain about how to distinguish between the selected pizza and the quantity of pizzas requested. My goal is to assign specific prices to each type of pizza (Cheese $6, Pepperon ...

What is the correct way to customize colors for specific components in Material-ui?

Struggling with theming in Material-UI, particularly when it comes to customizing element colors. Some elements default to 'theme.palette.main.dark' and I want to override this behavior. For example, the TextField and SpeedDial components automa ...

CSS styling failing to meet desired expectations

Two different elements from different panels are using the same CSS styles, but one displays the desired text while the other does not. The CSS file in question is named grid.css. To see the issue firsthand on my live website, please visit: I am uncertai ...

Download the PDF instead of just viewing or downloading it through a link

I need help configuring a link so that when the user clicks "download here," a PDF file is automatically downloaded to their desktop. Can someone provide guidance on how to achieve this? ...

Bootstrap: Create a square by setting the height equal to the width of the .span2 element

I'm looking to create a div that is the height of a ".span2" (essentially a square) and is also responsive. Here's what I have so far: <div class="span2 square>Hello</div> .span {height: @span2;} However, I haven't been able to ...

Is it possible to transform the Bootstrap5 sidebar, currently positioned above the block content, into a sticky element on

After creating a Django blog using bootstrap5 and following guidance from the python crash-course book, I encountered an issue with my sidebar placement. Despite trying various methods, I struggled to "override" my blocks in order to keep my sidebar fixed ...

Creating a Full Height Layout with Two Columns Using Bootstrap 4

Looking for a solution similar to the one discussed in this thread. I'm struggling to figure out how to achieve the layout shown below using Bootstrap 4. The key requirement is that col 1, col 2, and col 3 must stretch to the bottom of their respecti ...

Scrolling seamlessly within a container that is fixed in position

For hours, I've been attempting to incorporate smooth scrolling into my project with no success. The issue lies in the container where the anchor tags are located. If it's set to fixed position or absolute, none of my attempts seem to work. In ...

Is there a way to hide the borders between cells within these divs?

I am working on an application screen using Vue.js and facing an issue with the divisions between cells. I want to hide these divisions so that the lines of the items appear continuous. I attempted to modify the classes of the columns to "col-md" and "col ...

Utilize MaterialUI's stepper component to jazz up your design with

Is there a way to customize the color of a material ui Stepper? By default, the material UI stepper's icons use the primary color for both "active" and "completed" steps. class HorizontalLinearStepper extends React.Component { state = { activeS ...

XPath allows for the selection of both links and anchors on a webpage

Currently, I am using Screaming Frog and seeking to utilize XPath for a specific task. My objective is to extract all links and anchors that contain a certain class within the main body content, excluding any links found within div.list. I have attempted ...

How come my div elements are overlapping as I resize the browser window?

While attempting to design a straightforward login form, everything appears to be working fine until the browser window is resized. The setup involves three <div> elements: center, center2, and center3, all enclosed within main. Additionally, there&a ...

Are there any known issues with Firefox, jQuery, and CSS animations working in tandem?

As I develop a website with jQuery to manage CSS class switching for animations, I have encountered a strange issue. While Google Chrome and Internet Explorer 9/10 work perfectly, Firefox (Aurora 24 and Firefox 23) does not perform the animations smoothly, ...

What's the reason for the font weight property in CSS not affecting the font family?

The code structure in my HTML file includes a link to Google Fonts with the following call: <link href="https://fonts.googleapis.com/css?family=Lato:400,700" rel="stylesheet"> Following that, I specify the font family and weight in the CSS section: ...

php code to show data in a single column of a table

I am working with a database record that includes an id and name, and I need to display it in a table where records are distributed in the pattern 1-2-1-2-4 across columns. Below is my current code that is generating the incorrect output: <table borde ...

Is it possible to embed a link within the double brackets using AngularJS?

If I have the following code in my HTML: {{text}} Now, let's say that "text" represents an address. Is there a way to make the displayed value of text clickable without directly modifying the HTML file? In other words, can we create a link effect us ...

Creating an email in Gmail with a pre-filled HTML body

Currently, I am developing a Django web application and seeking a way to enable my app to send emails via Gmail. I have explored creating a hyperlink that opens the compose window in Gmail, as well as prepopulating fields such as "to," "cc," "bcc," and bod ...