CSS relative positioning takes precedence over fixed and absolute positioning

Forgive me if I seem a bit inexperienced with CSS. I've been trying to style the following...

#element0 {
 width: 100%;
 height: y;
 border: 1px solid black;
}   
#elementA {
 position: fixed;
 float left;
 width: x;
 height: y;
 border: 1px solid black;
}
#elementB {
 position: relative;
 float: left;
 width: x;
 height: y;
 border-right: 1px solid black;
}

/* HTML */
<div id="element0">Some div...</div>
<div id="elementA">Another div</div>
<div id="elementB">Final div...</div>

For some reason, when I try to position #elementB below #elementA, #elementB ends up on top of #elementA unless I declare its position as either static or absolute. However, this then requires manually setting the top/left properties and doesn't display correctly in all browsers. Any help would be appreciated!

Issue resolved! (had to set html, body:(width: 95%;)) Thanks for all the advice!

Answer №1

Firstly, there are a couple of corrections needed: The ID #0 must be renamed as IDs cannot start with a number. Additionally, you are missing a colon in the float property for #a. It's unclear why 'x' and 'y' are used for height/width – are they simply placeholders?

Fixed and absolute elements are removed from the document flow, meaning they do not occupy space like normal positioned elements.

As a result, the fixed element will have precedence over the relative one in your scenario because both can occupy the same position on the axis. If you had used absolute positioning with top: 0; left: 0, the outcome would be the same.

Furthermore, since one element is set to float (almost), this affects the positioning significantly. Floats are unique in how they layout elements, aligning as far up and in the float direction as possible. They remain in the flow but may move to the next line if their width exceeds the container size.

You could try:

<style>
 #a
  {
  float:left;
  height:100px;
  width:150px;
  background-color:black;
  }
  #b
  {
  clear:left;
  height:100px;
  width:150px;
  background-color:green;
  }
 </style>
 <div id='a'>aaaaa aaa</div>
 <div id='b'>bbb bbb</div>

'Clear' ensures an element appears below any preceding floated elements. In this case, #b will be placed on the line beneath #a. You could also adjust the margin or width of #a to prevent #b from appearing beside it, forcing #b to be positioned below #a instead.

Answer №2

It seems like there may be some errors in your CSS syntax, particularly with the width: x and height: y. Additionally, the float property for #a is missing a colon. Remember that IDs cannot begin with numbers, so it's necessary to rename #0. Below is the corrected code that ensures everything functions properly, without the need for fixed or relative positioning.

#zero {
 width: 100%;
 border: 1px solid black;
}   
#a {
 float: left;
 border: 1px solid black;
}
#b {
 float: left;
 border-right: 1px solid black;
}

Here is the corresponding HTML:

<div id="zero">Some div...</div>
<div id="a">Another div</div>
<div id="b">Final 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

How can I create a div with a curved design?

I am faced with a challenging task, that of converting a design into a static HTML page. The design in question features curves where the logo sits and at the bottom. I have made some progress, but there are still parts that need refinement. The original ...

What steps can be taken to ensure that the content in column two does not impact the positioning of content in column one?

Is there a way for the input text container to remain in its original position within the layout even after submitting text? Usually, when the data displayed container's height increases, it pushes the input text container down. Despite trying absolu ...

Tips on eliminating margins from rows on smartphones using bootstrap version 4.5

In my project, I am utilizing bootstrap 4.5 and aiming to create an edge-to-edge view for mobile devices. Here is the layout I currently have: <div class="container"> <div class="row"> <div class="col-md-9> ...

What is the best way to adjust the canvas size to match the background image?

Here is the CSS code for styling the canvas object that I am currently using: canvas{ border: solid black 2px; background:url("http://upload.wikimedia.org/wikipedia/en/a/aa/Bart_Simpson_200px.png"); background-repeat:no-repeat; } Is there ...

Using VBA to implement a variable with a "customizable name" during runtime

Can you assist me with a novel concept? I'm uncertain of its feasibility and even what to call it. I am interested in using a dynamically named variable. Is this achievable? As I parse an html file line by line, my code must determine which sheet to ...

The div element is not rendering the variable

I'm currently following a guide from Microsoft on creating metro apps using JavaScript. Here's the link. According to the tutorial, when you click the button, it should display "Hello nameentered!" But for some reason, this isn't happening i ...

Discrepancy in the vertical pattern repetition of the SVG background

When I use a simple div with an SVG set as a background image with vertical repeat, I noticed a gap of varying sizes on Chrome and Firefox depending on the screen size (please resize the window). Check out the issue here .bg { width: 50%; height: 2 ...

Is it possible to achieve a seamless interaction by employing two buttons that trigger onclick events to alternately

I have a specific goal in mind: I want to create two buttons (left and right) with interactive cursors. The left button should initially have a cursor of "not-allowed" on hover, indicating that it cannot be clicked at the beginning. When the right button ...

"Utilize Rotate3d() to rotate the content of a div element

Looking for a way to make a div fold down from the top in webkit. Currently experimenting with rotate3D, but struggling with the rotation being measured from the center of the div. Does that explanation clarify things? ...

The css fails to display properly upon the page being reloaded using a button

While diving into the world of learning React JSX, I've encountered a perplexing issue. After clicking the load button, the page redirects correctly but the CSS styling I've implemented fails to display. Here is the HTML code snippet: <!DOCTY ...

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 ...

tips on adjusting margins or paddings in option select

How can I use the margin or padding options for my select dropdown? I tried using &nbsp; <select class="select"> <option value="100"></option> <option value="250">$593</option> ...

How to switch between different ng-class styles

Within this ng-repeat loop, I intend to display the first two rows. Upon clicking "Show More," how can I switch the CSS from display:block to something else? The Show More button should toggle the visibility. JSON "one":[{ "name":"Raspberry Lemo ...

"Can the form and action occur on the same page, or should they be

When it comes to form actions, what is more effective: having the action on the same page or a different page? See the sample code below: <form action="act.php"> html code </form> <form action=""> html code </form> < ...

Acquire HTML information using the Robot framework and xpath techniques

My first task is to visit the website www.google.de. I aim to extract the word Deutschland from this section of the HTML code: <div class="logo-subtext">Deutschland</div> Since the id in this div class is not available, I must utilize xpath. ...

Swapping stylesheets using a hyperlink - a simple guide

Currently, I am creating a website that utilizes PHP, XHTML strict, and jQuery. The main goal is to ensure the site is adaptable for mobile and desktop devices by implementing the principles of Responsive Web Design. This involves serving different stylesh ...

Using a JavaScript onclick function to retrieve specific elements within the document

I'm attempting to extract the elements from the source that was clicked on, but for some reason it's not functioning correctly. Check out my JSFIDDLE Here's the HTML: <span class="populate" onclick="populate();" href="?id=1">Hello&l ...

Container for grid template columns and responsive window in a single row

Imagine having around 250 divs with the class slider-item styled in a certain way. You have a responsive grid in CSS called A which arranges these divs as columns/items when the window resizes, with a minimum item width of 240px. Check out how it looks bel ...

Issue with Chrome (2) preventing HTML5 video from loading using video.js

This particular question is identical to the one found at HTML5 video (video.js) not loading in Chrome, which unfortunately was left unresolved. When attempting to load only a mp4 source on Chrome, I encounter the following message in the console: Array[ ...

Adjust the color of the contact information when the hamburger menu is activated

I'm currently working on customizing my WordPress site, which uses the Enfold theme. I have implemented a burger menu for all screen widths, and now I want to change the color of the contact information (phone and email) when the burger menu is opened ...