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

Error: Papa is not defined. The file was loaded from the CDN in the header section

I have integrated the cdn hosted lib for PapaParse in my HTML header. However, when I execute my JavaScript file and it reaches the function where I call Papa.unparse(data); It throws an error stating that Papa is undefined. This has left me puzzled as I h ...

Avoiding unnecessary white space when positioning HTML elements

I am working with two images, imgA and imgB. ImgA is 2000px high while imgB is 1000px high. After placing them one after the other, I use relative positioning to move imgB up in order to overlap imgA. Expectedly, the browser window should be 2000px high, ...

What is the best way to incorporate a sliding effect into my cookie form?

I created a cookie consent form for my website and I'm looking to include a sliding effect when it first appears and when it disappears after the button is clicked. How can I implement this sliding effect into the form? Here's the HTML, CSS, and ...

Utilizing Grunt bake to generate unique files with various content from a single template

Is it possible to use bake to display multiple contents within the same styled template? https://github.com/MathiasPaumgarten/grunt-bake For example, if my template looks like this: <div style="background-color:red"> </div> And I have Cont ...

Determining the Missing Focus: Discovering the Vacant '":focus'" in Jquery

I am currently working on jQuery and facing an issue with input fields that have assigned ID's and attached .blur() events. I am struggling to identify the specific item that has gained focus: $("#"+field).blur(function() { console.log ($(this).attr ...

What could be causing the appearance of two vertical scrollbars after transitioning from ReactJS to NextJS?

During the development of a project in plain ReactJS, I added these two lines to my index.css: overflow-y: scroll; overflow-x: hidden; The purpose was to ensure a vertical scrollbar always appeared without causing a horizontal scrollbar even when there wa ...

Maintain the aspect ratio of an image within a flex container when the height is adjusted

I'm currently facing an issue with an image in a flex container. My goal is to maintain the image's ratio when the height of the container or window is decreased or resized. Check out this sample fiddle html, body { height: 100%; } .wrappe ...

React, facing the challenge of preserving stored data in localStorage while accounting for the impact of useEffect upon

Seeking some assistance with the useEffect() hook in my Taking Notes App. I've set up two separate useEffects to ensure data persistence: one for when the page first loads or refreshes, and another for when a new note is added. I've added some l ...

Optimizing Your HTML5 Code: Best Practices

Lately, I've come across some articles on HTML5 best practices and they all seem to emphasize the following guideline: "Prefer using id attribute over name attribute" But is this really the best approach? How can I effectively manage forms in PHP wi ...

I am struggling to capture the user's input and display it on a webpage using HTML and JavaScript. Can you help me identify where I may be going wrong in this process?

Good day! I am fairly new to the programming world and have recently embarked on my first JavaScript project. I've chosen to create a simple budgeting app. However, I'm struggling with displaying user input on the page. Something seems off with ...

Issues with the functionality of Bootstrap

Upon loading the page, I am encountering an issue with a collapse on the 'main' id. It fails to collapse initially and only functions correctly after being clicked. I have not utilized 'collapse in', so I am unsure why this behavior per ...

Issues with Image Placement in Internet Explorer

My cat image on my website appears significantly lower than all the other images when viewed on Internet Explorer (v11). While it looks perfect in Chrome, I cannot figure out why this discrepancy exists. For the development of this website, I utilized pred ...

Using Python in Selenium to set the value of a dropdown menu within an <input> element

I am working with an HTML page that has a form containing a dropdown tag. My goal is to set the value of this dropdown tag using Selenium. When I retrieve the input element, here is the code snippet: driver.find_element_by_xpath("/html/body/div[2]/div/di ...

What is the best way to add HTML formatted text into Outlook?

In my Emacs, I've generated this syntax-highlighted code snippet and now want to paste it into an Outlook email with rendered HTML (without the actual HTML code). <pre> <span style="color: #a020f0; background-color: gtk_selection_bg_color;"& ...

Personalized CSS styling for Jquery Mobile version 1.3

Having trouble with styling a page in jquery using CSS, specifically aligning #navgroup to the center of the footer and removing an additional white counter from the slider. Here is my code: <!doctype html> <html lang="en> <head> < ...

Generate code in Yii2 using the active form

This is the code I am using in Yii2: <?= $form->field($model, 'username')->label(false); ?> <?= $form->field($model, 'password')->label(false); ?> It produces the following output: <div class="form-group fi ...

Tips for extracting tables from a document using Node.js

When converting an XML document to JSON using the xml-js library, one of the attributes includes HTML markup. After parsing, I end up with JSON that contains HTML within the "description":"_text": field. { "name": { ...

Trouble exporting to Excel on Chrome and Firefox browsers

I am having an issue with exporting tables to Excel using JS. The <table> tag contains some descriptions and a class (<table class="someClassName" border="0" cellpadding="0" cellspacing="0">). When the table is exported to Excel, the Excel fil ...

What is the best way to display a collection of square Views in a FlatList in a react native application?

How can you create a scrollable square grid with two columns of squares in a FlatList using react-native and ensure it is responsive to different screen sizes? Which CSS properties in react-native are necessary to achieve square shapes for each <Item/& ...

Guide on automatically populating login data in an html5 form using a python script

I'm currently attempting to create a script that can automatically populate the values of an HTML5 form (specifically Name and Password fields). However, I am struggling with how to actually input these values. After searching various sites, I have y ...