Applying a margin to a CSS div at the bottom may cause it to not

I'm struggling to achieve the desired outcome with my layout:

https://i.stack.imgur.com/CvLFl.png

I have successfully replicated the image, but only when my div is not floating on the page (without margin applied and without using position: absolute). Otherwise, the green rectangle is not visible.

Here is my HTML structure:

<div class="app">
  <div class="interface">
    <div class="view">
      <div class="body">
        <div class="top">
          Top content
        </div>
        <div class="middle">
          Middle content
        </div>
        <div class="bottom">
            Bottom content
        </div>
      </div>
    </div>
  </div>
</div>

In the .interface CSS, I have the following:

.interface
{
  position: absolute;
  top: 15%;
}

When using this CSS code, the green rectangle remains invisible. If I remove position: absolute (and consequently, stop applying top: 15%), I am able to see the green rectangle.

You can observe the issue in this JSFiddle: https://jsfiddle.net/v9euwdz3/

How can I ensure that the DIV is displayed at a specific level from the top without altering my HTML structure?

Answer №1

Here is the desired layout using flex:

.container {
  display: flex;
  flex-direction: column;
  background-color: blue;
  justify-content: space-between;
  height: 100vh;
}

.navbar {
  background-color: white;
  height: 15vh;
}

.header {
  background-color: green;
  height: 60px;
}

.content {
  background-color: yellow;
  flex-grow: 1;
}

.footer {
  background-color: red;
  height: 50px;
}
<div class="app">
  <div class="interface">
    <div class="view">
      <div class="container">
        <div class="navbar">
          SPACE
        </div>
        <div class="header">
          Top content
        </div>
        <div class="content">
          Middle content
        </div>
        <div class="footer">
          Bottom content
        </div>
      </div>
    </div>
  </div>
</div>

You can substitute margin-top: 15%; for a placeholder div if needed

.container {
  display: flex;
  flex-direction: column;
  background-color: blue;
  justify-content: space-between;
  height: 100vh;
}

.header {
  margin-top: 15vh;
  background-color: green;
  height: 60px;
}

.content {
  background-color: yellow;
  flex-grow: 1;
}

.footer {
  background-color: red;
  height: 50px;
}
<div class="app">
  <div class="interface">
    <div class="view">
      <div class="container">
        <div class="header">
          Top content
        </div>
        <div class="content">
          Middle content
        </div>
        <div class="footer">
          Bottom content
        </div>
      </div>
    </div>
  </div>
</div>

(I opted for vh over % to ensure proper display in this code snippet)

Answer №2

It's common knowledge that an element with a height set to `100%` will take up `100%` of its parent's height. If the parent's height is not explicitly defined, it can lead to errors. You might have encountered this issue when you set the width of `body` to `100%`, which wasn't the correct approach. Instead, setting it to `100vh` would make it fit the screen properly, especially on a computer. Additionally, I noticed some mistakes in your calculations, like subtracting percentages from pixels in `height: calc(100% - 150px)`. These were simple errors that needed correction.

html,
body {
  height: 100vh;
}

.app {
  position: relative;
  height: 100%;
  display: flex;
}

.interface {
  position: absolute;
  height: 100%;
  top: 15%;
}

.view {
  position: fixed;
  height: 100%;
  background-color: #ccc;
  width: 350px;
}

.body {
  position: relative;
  height: 100%;
}

.body .top {
  height: 15%;
  border: 1px solid #000;
}

.body .middle {
  height: 60%;
  border: 1px solid red;
}

.body .bottom {
  height: 20%;
  border: 1px solid green;
}
<div class="app">
  <div class="interface">
    <div class="view">
      <div class="body">
        <div class="top">
          Top content
        </div>
        <div class="middle">
          Middle content
        </div>
        <div class="bottom">
          Bottom content
        </div>
      </div>
    </div>
  </div>
</div>

If you want to view the result correctly in the snippet, make sure to observe it in full page mode. Also, note that when viewing the result through jsfiddle, there may be a scrollbar in the result section that hides part of the `footer`.

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

Eliminate the dark backdrop from html5 videos that only shows up for a brief moment

Is there a way to remove the brief black background that appears when loading an HTML5 video? I have tried using CSS without success. The HTML: <div id="start-screen"> <video id="video-element"> <source src="video-mp4.mp4" type="vide ...

The primary section is not extending completely to the bottom, resulting in the footer not being aligned correctly at the bottom of the

Despite the numerous similar questions on Stack Overflow, I am struggling to get my footer positioned correctly on my page. The <footer> stubbornly remains 800px above the bottom behind the <main>. Even with various height options such as 100%, ...

Instructions for compiling node-sass within the present directory for specific files

In my project, the directory structure looks like this: - folder1 - styles1.scss - folder2 - styles2.scss I want to utilize node-sass through the command line to generate the following output: - folder1 - styles1.scss - styles1.css - folder2 ...

Issues with CSS3 Animation failing to trigger upon hovering over links

Background: Looking to design a visually appealing hover effect for links in the future Current Demo Link: You can view the demo here specifically on Firefox Browser. body { color:#ffffff; font-family:"Helvetica"; font-size:12pt; backgr ...

Tips for implementing text-overflow ellipsis in an HTML input box?

On my website, I have input fields where I've added the following CSS styling: .ellip { white-space: nowrap; width: 200px; overflow: hidden; -o-text-overflow: ellipsis; -ms-text-overflow: ellipsis; text-overflow: ellipsis; } H ...

AngularJS causing issues with Materializecss dropdown menu operation

I'm currently working on a web application using Materializecss and AngularJS for the front-end. However, I've encountered an issue with the dropdown menu component of Materialize not functioning as expected. Here's an example of the dropdo ...

When utilizing the tab key on Chrome, the iFrame fails to scroll

We are currently facing an issue with our web application that is embedded inside an iFrame. On one of our HTML pages, there are numerous textboxes with a large amount of content, requiring users to scroll down to navigate through it all. When using the ...

HTML does not support the use of certain symbols

Currently, I am in the process of designing a homepage for my school project. However, I have run into an issue. The content that I want to be displayed on my homepage is as follows: "Daudzspēlētāju tiešsaites lomu spēle (Massively Multiplayer Online ...

Can the style of certain words within a paragraph be altered using CSS only, without the need for any HTML tags?

Is it feasible to alter the appearance of certain words within a paragraph using CSS solely, without applying any HTML tags to those particular words? Awaiting a positive response. Thank you! ...

Can you explain the distinctions among <Div>, <StyledDiv>, and <Box sx={...}> within the MUI framework?

When exploring the MUI documentation, I came across a table of benchmark cases that can be found here. However, the differences between the various cases are not clear to me. Can someone please explain these variances with real examples for the following: ...

Overlay displayed on top of a single div element

Trying to implement a loading overlay in an Angular project within a specific div rather than covering the entire page. Here's a Fiddle illustrating my current progress: #loader-wrapper { top: 0; left: 0; width: 100%; height: 100%; ...

jQuery Animation Issue: SlideDown Effect Not Working as Expected

I'm feeling quite confused about this situation. I've been attempting to utilize the slideDown function in jQuery, but when I click on the 'information' div, it just jumps instead of animating smoothly. I suspect that one of the cause ...

Tips for adjusting the width of the scrollbar track (the line beneath the scrollbar)

How can I style a scrollbar to match this specific design? https://i.stack.imgur.com/KTUbL.png The desired design specifies that the width of -webkit-scrollbar-thumb should be 5px and the width of -webkit-scrollbar-track should be 2px. However, it is pro ...

Toggle class on element based on presence of class on another element

If I have 4 boxes and the user is required to choose one. I aim to assign an active class to the box that the user clicks on. However, if the user selects another box, I want to remove the class from the first clicked box and apply it to the newly clicked ...

"Implement a feature in JavaScript that allows users to click on images to navigate

I have a navigation feature on my website that allows me to cycle through images using the following code: <a href="JavaScript:previousImage()">Previous</a> | <a href="JavaScript:nextImage()">Next</a> <span id='num' ...

Slider UI handle in jQuery sliding out of the div container

Could you assist me with a technical issue I am facing? I need to know how and where to prevent the .ui-slider-handle from exceeding the div when it reaches 100%. Feel free to check out the live preview at https://codepen.io/Melwee/pen/Exjwoyo I have inc ...

Issues with HTML Email display

Hey there, I am facing an issue while trying to send an HTML email. I am aware that Outlook does not support a lot of CSS formatting, but I can't figure out why this simple setup is breaking. Is there a quick fix or alternative solution? Take a look a ...

The CSS styling for changing the background-color and color of list items (li) is

Trying to customize the background-color and color of li, but despite inspecting it, the changes are not reflecting on the page. Does anyone know why this is happening and how to fix it? Update: Here's a link to the JSFiddle. However, when I run it ...

The CSS stylesheets are not compatible with IE7 and IE8

I am encountering an issue with my local site where the CSS links are not being included in the page, particularly in IE7 and IE8. The technologies I am using include WordPress 3.6, Bootstrap 3, Modernizr, and jQuery. Source Code <!DOCTYPE HTML PUBLI ...

Alter the CSS display based on the user's userAgent if they are using an iPhone and window.navigator.standalone is set to true

Looking to create a unique webAPP with different content displays based on how it is accessed. If the user opens the webAPP through their Safari browser Or if they open the webAPP from a webclip on their home screen The detection is functioning, as conf ...