The styling in CSS does not accurately reflect its relationship to the parent

My code includes a div with the ID of "outer" nested inside a parent div with the ID of "Container". I'm attempting to adjust the properties of the outer div relative to its parent, meaning its width, height, and other attributes should be based on the Container div. However, for some reason, these adjustments are not being applied properly. As a result, my menu is also failing to adjust accordingly. Could someone please help me identify the mistake in my code? Thank you.

Here is the HTML code:

<body>
<div id="container">
<header>
<img width="60%" alt="Logo" src="images/logo.jpg">
<div id="divHeader"><br /><br />
<input type="text" placeholder="Enter text here">
<input type="radio" value="Pages" checked="true" name="searchOpt">Pages
<input type="radio" value="Contact People" name="searchOpt"/>Contact People
<input type="button"  value="Search"/>
</div>
</header>
<!--pop up menu-->
<div id="outer">
<div id="menu">
<ul id="nav">
    <li id="nav1"><a href="">News & Events</a></li>
    <li id="nav2"><a href="">Facilities</a></li>
    <li id="nav3"><a href="">Research</a></li>
    <li id="nav4"><a href="">Programmes</a></li>
    <li id="nav5"><a href="">Faculty</a></li>
</ul>
</div>
</div>
</div>
</body>

And here is the CSS code:

body { 
background-color:#0a1857;
}
#container
{
width:80%;
height:100%;
background-color:#FFFFFF;
position:fixed;
right:10%;
left:10%; 
}
header
{
width:100%;
height:15%;}
#divHeader
{
float:right;}
#outer
{
padding-left:20%;
width:80%;
height:40%;
background-image:url('images/image_31.jpg');
background-repeat:no-repeat;
background-position:center; }
#menu { 
position:relative;
top:220px;
right:170px;  
width:108%;
height:20%;
text-align:center;
}
#menu a { 
display: block; 
text-decoration: none; 
color: #3B5330;}
#menu a:hover { background: #B0BD97;}
#menu ul li {
font:12px georgia; 
list-style-type:none;
float: left; 
width: 20%; 
font-weight: bold; 
border-top: solid 1px #283923; 
border-bottom: solid 1px #283923; 
background: #979E71;
}
#menu ul li a { 
font-weight: bold;
padding: 15px 10px;}
#menu li{ 
position:relative; 
float:left;}
#popup_menu ul li
{
float:none;
}

Answer №1

Did you attempt to include the following:

display: block;

within the parent container?

Answer №2

I discovered the root cause of my issue. The child divs were not positioning themselves relative to the parent as intended, but rather according to the browser due to:

position:relative;

I made the switch to:

position:absolute;

Credit to this source

A Relative Positioned element is positioned relative to its default position. It allows the content of the element to be moved and overlap with other elements while still maintaining reserved space in the normal flow. Typically used for containers.

An Absolute Positioned element is positioned relative to the first parent element that has a position other than static. If no such element is found, the containing block becomes the html tag.

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

Generate HTML content based on the permissions from a REST API

I have a frontend that renders based on user permissions from a REST API. These permissions could include deleting users, creating users, adding users to workflows, and more. Most of these permissions are managed by an administrator. So my question is: H ...

Utilize JavaScript to trigger a div pop-up directly beneath the input field

Here is the input box code: <input type='text' size='2' name='action_qty' onmouseup='showHideChangePopUp()'> Along with the pop-up div code: <div id='div_change_qty' name='div_change_qty&ap ...

Angular's ngClass directive failed to be applied correctly

I am currently experimenting with the use of [ngClass] in Angular and it appears that it is not being applied as expected. Interestingly, [ngStyle] for similar CSS styles is working without any issues. What could I be doing wrong in this scenario? There ar ...

Tips for customizing the border radius style of the menu in Vuetify's v-autocomplete component

I am looking to customize the appearance of the drop-down list in the v-autocomplete component by adding a border-radius style, as depicted in the image below. The current design I have achieved closely resembles the visual shown below. Previously, I app ...

Is there a way to personalize the appearance of a specific page title in my navigation menu?

I'm currently working on customizing the menu of my WordPress theme to display a different color for the active page name. Although my CSS code works well for all page names except the current one: .navbar-nav li a { font-family: georgia; fo ...

What steps can I take to avoid scaffold.css from taking precedence over my custom CSS?

I'm having trouble with this. It seems like a small issue, but I'm still very new to learning about rails. Any help you can provide would be greatly appreciated! Solution: To resolve the problem, I accessed the file responsible for adding head ...

What methods can users employ to effectively manage and maintain their in-app purchases using my application?

Hey there, I could really use some assistance. Recently, I created a mobile app for a client that allows users to purchase and watch video courses. However, I'm facing an issue where when a user buys a course and then refreshes the app, they are redir ...

Issues with hover functionality in Javascript, CSS, and HTML

Seeking assistance with my JavaScript, HTML, and CSS development, I ran into an issue while trying to create a hovering function for my webpage. Despite my efforts, the links are not displaying anything when hovered over, and the divs meant for specific ho ...

Issue with form validation, code malfunctioning

I am struggling to figure out why this validation isn't working. Here is the HTML code I'm using: <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type='text/javascript' src="scripts. ...

Creating a curved edge for a shape in React Native can add a stylish and

Issue Description I am working on an app and struggling with styling the bottom navigation bar. It's more complex than what I've done before, especially the curved top edge of the white section and the area between the blue/green circle and the ...

What is the best way to display a child div without impacting the position of other elements within the same parent container?

As I work with a div html tag within a login form, encountering an error inside this form has presented a challenging issue. The error div sits at the top of its parent div, and ideally, upon activation, should remain within the form div without disrupting ...

Is there a way to prevent an inline SVG from appearing at a random height without specifying its height or the height of its parent element?

Displayed here is a simplified Bootstrap 3 layout. The middle column in the row features an inline SVG that transitions from a blue box to a green box. I've opted for styling SVG with browser CSS because I find it interesting! Additionally, this appr ...

Is it possible to automatically adjust the cursor position in a textarea when the language is switched?

I am working with a textarea that needs to support both English and Arabic languages. In English, text is typically left-aligned, so the cursor should start on the left-hand side. However, in Arabic, text is right-aligned, meaning the cursor should begin ...

Prevent users from inputting multiple minus signs by setting the input type to number and disabling the

Is there a way to prevent the input type number from accepting "--" as input? I am looking for a solution to disable this behavior. Additionally, the input field seems to allow for various incorrect formats like: -12-12 -- -121- 23-323- ...

Is it possible for the font size to adjust based on the heading (h1, h2, h3) I choose when using CSS to specify a particular font size?

What will happen if the following HTML code is used: <h1> This is text</h1> <h2> This is also text</h2> and CSS is applied to change the font size like this: .h1 { font-size: 30px } .h2{ font-size: 30px } Will both texts ...

Utilizing jQuery UI to dynamically alter CSS classes

I'm currently working on a project and could use some assistance. I have divs that are droppable using jQuery UI. While everything is functioning properly and I can drag and drop, I am looking to modify the opacity of the div I am dragging by changing ...

Tips for shifting a designated row upward in Chrome using JavaScript

Currently, I am working on a Javascript function that moves up a selected row. However, the issue I am facing is that when the row moves up, the old row is not being removed. For instance, if I move up the 'bbbb' row, it successfully moves up bu ...

Is it possible to trigger multiple button clicks using JQuery simultaneously?

Hello everyone, I am new to StackOverflow and this is my first question here. I hope it's not too silly. Thank you in advance for your help! I am trying to achieve a functionality where one button click triggers the clicks of multiple other buttons u ...

Styling a div element in CSS with absolute positioning and the ability to

I am looking to set specific positions and dimensions for the div elements within my HTML document. Refer to this image for reference: https://i.stack.imgur.com/ANBVm.png For each of these div elements, I want a vertical scrollbar to appear if the content ...

Instructions for integrating AJAX into a PHP script

I am looking to incorporate AJAX into my PHP file so that when I delete an item from a list, the data automatically reloads and the list is updated with the new information. I have created a list of all my data along with a delete button. Below is the PH ...