Having trouble repositioning a div element in CSS

I am having an issue with my code.

The problem I'm facing is that I am unable to move the green div.free_space below the other two divs, div.main_pic and div.navigation. I don't understand why this is happening. As I am still learning, I would greatly appreciate it if you could point out what went wrong. If there are any mistakes or improvements that can be made, please let me know!

Thank you

Below is the code:

.main_pic {
    position: absolute;
    height: 600px;
    width: 92%;
    background: black;
    font-size: 20px;
    font-family: Century Gothic, Verdana;
    font-weight: normal;
    left: 4%;
    right: 4%;
    top: 200px;
}
.navigation {
    position: absolute;
    height: 50px;
    width: 30%;
    background: white;
    font-size: 15px;
    font-family: Century Gothic;
    font-weight: normal;
    left: 66%;
    right: 4%;
    top: 100px;
    vertical-align: middle;
}
.navigation ul {
    text-align: center;
    list-style:none;
}
.navigation li {
    display: inline;
    padding: 2.5%;
}
.navigation li a {
    text-decoration:none;
    color: #999999;
}
.navigation li a:visited {
    color:#4d4d4d;
}
.free_space {
    height: 600px;
    top: 900px;
    width: 100%;
    background: green;
}
<!DOCTYPE html>
<head>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div class="main_pic"></div>
    <div class="navigation">
        <ul>
            <li><a href="#">ITEM</a></li>
            <li><a href="#">ITEM</a></li>
            <li><a href="#">ITEM</a></li>
            <li><a href="#">ITEM</a></li>
        </ul>
    </div>
    <div class="free_space"></div>
</body>
</html>

Answer №1

While I can't guarantee the exact outcome, it seems like the goal is similar to what's demonstrated in the code snippet below. The key is to use absolute positioning to place one element on top of another without being affected by its content. In this case, the parent image requires position: relative to serve as a reference point for the absolute positioning. Additionally, make sure to align the elements based on their order in the document flow.

.main_pic {
  position: relative;
  height: 600px;
  width: 92%;
  background: black;
  font-size: 20px;
  font-family: Century Gothic, Verdana;
  font-weight: normal;
  margin: 0 auto;
}

.navigation {
  position: absolute;
  height: 50px;
  width: 240px;
  right: 0;
  background: white;
  font-size: 15px;
  font-family: Century Gothic;
  font-weight: normal;
  top: 100px;
}

.navigation ul {
  text-align: center;
  list-style: none;
}

.navigation li {
  display: inline;
  padding: 2.5%;
}

.navigation li a {
  text-decoration: none;
  color: #999999;
}

.navigation li a:visited {
  color: #4d4d4d;
}

.free_space {
  height: 600px;
  background: green;
}
<!DOCTYPE html>

<head>
  <link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
  <div class="main_pic">
    <div class="navigation">

      <ul>
        <li><a href="#">ITEM</a></li>
        <li><a href="#">ITEM</a></li>
        <li><a href="#">ITEM</a></li>
        <li><a href="#">ITEM</a></li>
      </ul>
    </div>
  </div>
  <div class="free_space"></div>
</body>

</html>

Answer №2

To create some space, you can use the CSS property margin-top. Here's an example:

.main_pic{
  margin-top:-200px;
    position: absolute;
    height: 600px;
    width: 92%;
    background: black;
    font-size: 20px;
    font-family: Century Gothic, Verdana;
    font-weight: normal;
    left: 4%;
    right: 4%;
    top: 200px;
}

Additionally, for more room added to your design:

.free_space {
    margin-top:600px;
    height: 600px;
    top: 900px;
    width: 100%;
    background: green;
}

Answer №3

To improve the layout, it is recommended to eliminate the position: absolute; attribute from the main_pic class.

.main_pic{
height: 600px;
width: 96%;
background: black;
font-size: 20px;
font-family: Century Gothic, Verdana;
font-weight: normal;
left: 4%;
right: 4%;
top: 200px;
}
.navigation{
position: absolute;
height: 50px;
width: 30%;
background: white;
font-size: 15px;
font-family: Century Gothic;
font-weight: normal;
left: 66%;
right: 4%;
top: 100px;
vertical-align: middle;
}
.navigation ul {
text-align: center;
list-style:none;
}
.navigation li {
display: inline;
padding: 2.5%;
}
.navigation li a {
text-decoration:none;
color: #999999;
}
.navigation li a:visited{
color:#4d4d4d;
}
.free_space {
height: 600px;
top: 900px;
width: 96%;
background: green;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="main_pic"></div>
<div class="navigation">
<ul>
<li><a href="#">ITEM</a></li>
<li><a href="#">ITEM</a></li>
<li><a href="#">ITEM</a></li>
<li><a href="#">ITEM</a></li>
</ul>
</div>
<div class="free_space"></div>
</body>
</html>

Answer №4

To improve the layout, try deleting the line "position: absolute;" from the style.css file. I have already verified the .main_pic class.

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 align text in the center of a div?

I just created this dropdown menu, but I am encountering an issue. Whenever I resize the div or h4 element, the text ends up at the top. Even after trying to solve it with text-align: center;, the problem persists. Here is a visual representation of what ...

Utilize CSS to selectively target specific sections of a repetitive pattern within a list

I have come across a list fragment that is generated automatically and I only have the ability to adjust the CSS for it. <ul class="menu"> <li class=" menuItem1 first parent"></li> <li class=" menuItem2 parent"></li> ...

Using PHP, eliminate all hyperlinks from the DOM HTML

I have a string called $processhtml that contains some HTML. My goal is to remove all link tags and their contents from the HTML using PHP. For example: "This is some text with <a href="#">link</a>" should become: "This is some text with" ...

Hide the background when the modal appears

I have successfully implemented a jQuery CustomBox modal on my website. However, I am facing an issue with hiding the content div behind the modal once it pops up. My goal is to make the content div reappear after the modal has been closed. You can view a ...

What is the method for placing a badge above a Font Awesome icon?

Is it possible to add a badge with numbers like 5, 10, or 100 on top of the Font Awesome symbol (fa-envelope)? I am looking for something like this: However, I am struggling to figure out how to place the badge on top of the symbol. You can see my attempt ...

Is there a way to disable certain CSS features in Chrome, like CSS grid, for testing purposes?

I'm exploring alternative layouts for my app in case CSS grid is not supported. Is there a way to disable features like CSS grid in Chrome or Canary? ...

Picture shifting when adjusting window size

I have been attempting multiple times to prevent an image on my website from moving when resizing the window, but I have not been successful. All I want is for this image to remain in the exact same position, no matter the size of the window. The image " ...

Implementing a new row insertion with jQuery/AJAX in PHP combined with a while loop

Here is a jQuery function along with a PHP file: <script> $(document).ready(function(){ $(".book_id_class").click(function(){ $("td:hidden").show(); }); }); </script> <table> <tr> <td class="text-center">Book ID</td> & ...

Media query failing to adjust properly

On my website's "about" page, I'm attempting to implement a media query but encountering an issue. In the original CSS, I structured the "information" section into 2 columns with right padding to prevent the content from extending all the way to ...

What method was used to incorporate a 2 pixel margin between the th elements on this website?

I've been analyzing the code thoroughly, but I am still unable to decipher how they managed to create a 2px margin between the th elements: The website in question is: Does anyone have any insights on this? ...

Avoiding double tapping to zoom on Chrome Android while using the desktop version of a website

Is there a way to disable zooming when double clicking in Google Chrome with the desktop site enabled? I attempted to use <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> without success. I al ...

Server blocking access to Javascript files

Having some trouble with my code, it works fine on Localhost but not on Fasthosts server. Seems to be an access issue to a folder on the server, here are the debug messages: GET (http://reggarockaboogie.co.uk/images/gallery/fld01/) 403 (Forbidden) k.cors ...

Greek symbols do not display correctly in SQL Server when accessed through PHP

At my server database, there is a table with collation set to greek_ci_ai, but I am facing an issue where Greek characters are displaying as question marks(????). I have attempted using header("Content-Type: text/html; charset=iso-8859-7") and <meta ...

CSS hover effects are not displayed when using the input type button

I'm having issues with my hover effects not showing: <input type="button" class="button" value="Click"> However, this code works fine: <button class="button">Click</button> The CSS codes are ...

Verifying if a number is present in a textbox when inserting text (without using setTimeout)

I have an input field similar to the one shown below: <input type ="number" id="numberTxt" placeholder="Number Only"/> To ensure that the value entered is a number, I am using the following function with the keypress event: <script> ...

I am looking to adjust the width of an element within an Iframe

In my single post, I have a social sidebar on the left side. Currently, the buttons in the sidebar are appearing in different sizes, but I want them all to be the same size as the "Twitter" button. To see the issue, please visit this link. I've tried ...

What could be causing my CSS code to not function properly within Vue.js?

Having trouble with css in VueJs. I can't seem to figure out what I'm doing wrong. Generally, my scoped style css works fine in vuejs, I can target ids and classes without any issues. The problem arises when trying to change the internal css va ...

Partial height side navigation bar

Check out this JS Fiddle: https://jsfiddle.net/wqbn6pe6/2/ This is a simple side navigation, but the grid layout is causing the sidebar not to stretch to the bottom even though it's set at 100% height. Any suggestions or solutions? Here's the ...

Is there a way to stop my <pre> code from being displayed on the page?

Currently, I am utilizing the google code prettifier found at http://code.google.com/p/google-code-prettify/ This tool functions similarly to stack overflow by enhancing and highlighting syntax when a block of code is input. However, I have encountered a ...

Chrome browser rendering image as PHP/CSS/HTML web navigation bar

I've hit a snag while working on my small website. The navigation of the website is functioning properly in Internet Explorer, but in Google Chrome, it's not clickable. Initially, I thought the issue might be related to the background image of ...