I'm trying to create a page with a fixed div above the main div. The main div should be below the fixed div but instead it overlaps the fixed div.
Adding margin-top positions the main div below the fixed div but, it also clips the same amount off of the bottom of the page.
What am I doing wrong?
Live Demo
body {
background-color: #00FFFF;
}
.fixed {
width: 100%;
position: fixed;
background-color: #C0C0C0;
}
.main {
background-color: #FFFFFF;
}
.expand {
height: 800px;
}
<div class="fixed">
fixed div line 1<br />
fixed div line 2<br />
fixed div line 3<br />
</div>
<div class="main">
<div class="expand"></div> <!-- For demo purposes only -->
</div>
To achieve the effect that you're looking for, you can use a combination of display: table on a parent and display: table-row on each of the direct descendants.
Set height: 100% on html, body, .wrap, .main, .inner.
Wrap the current "parents" in a div with class .wrap
Set display: table and width: 100% on .wrap.
Set display: table-row on .fixed, .main
Wrap the text content of .main in a div with class .inner
Set overflow: auto on .inner
This way, no matter what the size of the top div is, the .main will always be underneath .fixed.
display: table is supported by IE > 7 and all other modern browsers.
html, body, .wrap, .main, .inner {
height: 100%;
margin: 0;
}
.wrap {
box-sizing: border-box; /* Include border width in sizing */
border: 8px solid #00FFFF; /* Use a border instead of relying on margins */
display: table;
width: 100%;
}
.fixed, .main {
display: table-row;
}
.inner {
overflow: auto;
}
.expand { height: 800px; }
.fixed { background-color: #C0C0C0; }
<div class="wrap">
<div class="fixed">
fixed div line 1<br />
fixed div line 2<br />
fixed div line 3<br />
</div>
<div class="main">
<div class="inner">
<div class="expand"></div> <!-- For demo purposes only -->
</div>
</div>
</div>
You just need to add margin-top to the main div that is equal to the height of the fixed div. For example, if you have a 24px tall fixed div:
#fixed-div {
position: fixed;
height: 24px;
}
#main-div {
margin-top: 24px;
}
Try adding "position: relative" on the top div.
<div style="width: 100%; opacity: .6; position: fixed; margin-left: auto; margin-right: auto; background-color: #C0C0C0; position: relative;">
fixed div line 1<br />
fixed div line 2<br />
fixed div line 3<br />
</div>
You need a top margin on you main div with a value greater then your fixed divs height So if your fixed div is 60px high Make a margin-top:62px for the main div also you could get rid of margin-left and right and do all margins like this margin:60px auto; if the 60px is no good for you on the bottom of the main div you could do margin:60px auto 0px auto;
Hope this helps
--Joshua Joseph Bissot
Have good answer, curtsey of Joshua K.
I'll define a class named no-js (can be any class name) which sets position of 'fixed' div to relative. That will put the "fixed" div above the main div - though not actually fixed in place. Then with JavaScript I set the div to position: fixed and set the margin-top of the main div to the height of the "fixed" plus a fudge factor of say 5 or 6 px.
That way if JavaScript is enabled, the "fixed" div will actually be fixed and the margin-top of the main div will move it down to show the entire div.
If JavaScript is not enabled, the "fixed" div will still be visible, and above the main div, but it will scroll with the page.
Can I mark an answer as best answer? If so, how?
Stop the presses;
I could just set the position of the "fixed" div to relative (in a CSS class or using stye= on the element) and use JavaScript to change it to fixed and set the margin-top of the main div accordingly. That, I think, removes the need for another class named no-js or such.
I have created another page incorporating the changes needed to accomplish the task. It is at http://thetesting.site/fixeddiv/fixedheader-fixed.html
Related
I have a home-component which displays an aside-component and a div.
Those two should be displayed next to each other with the same height.
(The div inside the home-component shoud match the height of the div inside the aside-component)
I got a working solution attached where i used a "display: contents" on the app-aside tag. This is not a usable solution, since its not supported by Edge.
https://stackblitz.com/edit/angular-vkgwsb
i also tried "display:table" and "display: table-cell" which is also not working.
app.component.html
<div class="wrapper">
<div class="container">
<div class="content"></div>
<app-aside></app-aside>
</div>
</div>
app.component.css
.wrapper {
width: 1200px;
margin: 0 auto;
}
.container {
display: flex;
width: 100%;
}
.content {
min-height: 500px;
width: 840px;
}
aside.component.html
<div class="sidebar"></div>
aside.component.css
.sidebar {
min-height: 200px;
width: 360px;
}
Both Divs (content and sidebar) should have the same height.
Your aside element has the same height as your .content div, because of the display: flex on the container. The problem is the sidebar div inside the app-aside component. Remove the .sidebar div (and style the app-aside using the :host {} selector in the css) OR add height: 100% to the .sidebar div.
It appears that all the elements nested inside my parent divs are overflowing from the bottom border of my parental divs.
As you can see the image divs overlay the parent and the paragraph on the header
Similar questions have to deal with floating elements, but this is not the applicable here since I don't use those
Why is "position:relative" ?
Here is the code,
and a ready fiddle for your, very much appreciated ,tweaks.
https://jsfiddle.net/r96fxfgj/
<!DOCTYPE html>
<html>
<title>DISSECTIONS</title>
<head>
<link rel="stylesheet" type="text/css" href="dissections.css">
</head>
<body>
<div id="header">
<p><span>/<sup>*</sup></span>DISSECTIONS</p>
</div>
<div id="main">
<div class="photo" id="one"> </div>
<div class="photo" id="two"> </div>
<div class="photo" id="three"> </div>
<div class="photo" id="four"> </div>
<span class="stretch"></span>
</div>
<div id="footer">
<button id="about"> ABOUT </button>
<button id="contact"> CONTACT </button>
</div>
</body>
</html>
body {
overflow: hidden; /*prevents scrolling*/
font-family: courier;
}
div {
width: 98vw;
}
p{
font-size: 8vh;
}
span {
font-size: 15vh;
}
sup {
font-size: 8vh;
}
#header {
border: 2px solid black;
height: 20vh;
padding: 0;
}
#main {
border: 2px solid red;
height: 60vh;
margin-top: 5vh;
margin-bottom: 5vh;
padding: 0;
text-align: justify; /*justify*/
}
.stretch { /*justify*/
width: 100%;
display: inline-block;
}
.photo {
border: 2px solid black;
height: 100%;
display: inline-block;
vertical-align: top;
width: 20vw;
margin-left: 1%;
margin-right: 1%;
background-repeat: no-repeat;
background-image: url(
http://www.publicdomainpictures.net/pictures/10000/nahled/1001-12380278201dXT.jpg);
}
#footer {
border: 2px solid blue;
height: 10vh;
bottom: 0;
}
There are a few separate but similar issues here. Most boil down to you're unintentionally setting a specific height for the parent which is smaller than the things it contains.
In general it's best to set specific heights or widths only when your design actually needs those specific sizes -- otherwise just let the content flow dictate the size of its parents.
text in header overflowing the container: Fonts are a bit weird when it comes to sizing -- the value you put in font-size will match the text itself, but will also scale the padding above and below the text to a (typically) larger value (this is in addition to the normal padding attribute found on other elements). You can fix this by setting values for the text's line-height (a cheap but often-used hack for short headers is line-height:1px, which will remove all the extra padding. Don't use this if there's any chance the text will wrap to a second line, though, or the second line will overlap the first.)
images overflowing #main: you're setting #main as a percentage of the viewport height, but images at 100% of their actual size -- so depending on the window size the images may end up larger than or smaller than the container. Either use the same units for both, or don't set a height on #main at all (therefore letting the images dictate the height of the container.)
position:relative -- I don't see this in your code but I've seen it confuse a lot of people: position:relative counterintuitively doesn't affect the DOM node you attach it to, it affects the absolute-positioned children of that node. If a parent has position:relative, then any children with position:absolute will be placed relative to the parent's position instead of relative to the full window. If you're not using position:absolute (and you shouldn't unless absolutely necessary!) then you don't need position:relative.
This seems to be a box sizing problem.
Add this snippet to the top of your CSS (I always include it in my CSS reset), so that every element includes its children's padding and borders in its width/height:
* {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
If you don't want to apply it to everything, you can apply the styles instead to your .main and .photo classes.
As for your header, the content is overflowing because you have a set height, if you set the height to auto the header height adapts. However if you want to preserve the height of the header, you can change the overflow property to overflow:hidden, which hides overlapping content; or overflow:auto which adds a scroll bar on overflow.
I have a fluid width div which is within another block-level element.
HTML
<div></div>
CSS
div {
width:50%;
min-height: /*equal to width */;
}
I want the min-height set equal to the width so that it is a square shape unless there is too much content. At that point, it should expand vertically. Can this be achieved with CSS?
What I've tried:
Using percentage paddings or margins, but this only sets height, not max--height and pushes the content down.
Because padding is relative to an elements width, you can use a pseudo element to force a min height by using padding-top: 100%;:
div {
float: left;
margin: 10px;
width: 25%;
background: lightGreen;
position: relative;
}
div:before {
content: "";
display: block;
padding-top: 100%;
float: left;
}
<div></div>
<div>
div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content. div with content.
div with content. div with content. div with content. div with content. div with content. div with content.
</div>
<div>
div with content. div with content.
</div>
One option would be to use viewport percentage units. In this case, 50vw is 50% of the viewport. If the div is a root element and its width is relative to the viewport, then this work work as expected. You would otherwise have to calculate the height relative to the viewport for this to work.
For example:
div {
width: 50vw;
min-height: 50vw;
background: #000;
}
<div></div>
Based on #Turnip's answer, here's what I ended up using this:
/* Maintain Height While Loading Trick */
figure {
background: #999;
width: 100%;
overflow: hidden;
line-height: 1;
}
figure:before {
content: "";
display: block;
padding-top: 100%;
float: left;
}
/* some other CSS that's probably in your project already */
div {
max-width: 25%;
margin: 10px;
}
img {
max-width: 100%;
height: auto;
vertical-align: top;
}
<div>
<figure>
<img src="https://picsum.photos/4000/4000" alt="">
</figure>
</div>
<h1>Some stuff below the image. Is it jumpy?</h1>
I'm trying to do a 4-frame design with css, as in this code:
http://jsfiddle.net/7qBKJ/1/
But I don't want to use position:absolute;, and I'm trying to do it like this:
topframe: block;
left,right and centerframes: inline-block;
And I want to ensure there is, say, 200px of width in both rightframe and leftframe, and the remaining parts should be filled by centerframe. How can I manage this without absolute positioning?
I tried this, but it moves the frames up and down, when the screen width decreases :
http://jsfiddle.net/V4vAc/2/
in this fiddle, centerframe aligns with leftframe, since they are both inline-block, with centerframe rule margin-left:0px; but I have no idea how to set centerframe's right to align with rightframe's left, without specifying a width.
So how can I make #centerframe's width equal to screen width - 400 px ?
Thanks !
What you have to do is to put both sidebars first in the flow of the document. Then you float the first sidebar right and the second one left.
<div id="left"></div>
<div id="right"></div>
<div id="center"></div>
/* main.css */
#left {
width: 200px;
height: 100px;
background-color: blue;
float: left;
}
#right {
width: 200px;
height: 100px;
background-color: red;
float: right;
}
#center {
width: auto;
height: 100px;
background-color: green;
margin: 0 200px;
}
http://jsfiddle.net/samgh/JjsFF/
If you want center to be the full width underneath the two sidebars, you can remove the margins. Hope this helps.
Two divs are side by side, one is floating left with a width of 25%, the other just has a width of 75%. But when padding is applied on the right hand div, the padding doesn't work properly.
Here is a JSfiddle example:
http://jsfiddle.net/88upt/
<div id="top">
</div>
<div id="middle">
</div>
<div id="bottom">
</div>
CSS
#top {
float: left;
background-color: green;
width: 25%;
height: 100%;
}
#middle {
background-color: blue;
padding: 30px;
min-height: 30%;
}
#bottom {
background-color: red;
min-height: 70%;
}
Can someone explain to me why this is happening?
Thanks
Floating something is kind of like making it's position absolute. It will hover on top of it's neighboring containers. Add a margin-left equal to the width of the floated element to make the container the correct width.
http://jsfiddle.net/88upt/4/
#middle {
background-color: blue;
padding: 30px;
min-height: 30%;
margin-left:25%
}
EDIT Elaborating a bit more.
The floated element pushes the content of the sibling elements over. It will not push the left side of the content's element over. The padding is there it's just hidden by the floating element.
Add overflow = "auto" in the #middle.
#middle {
background-color: blue;
padding: 30px;
min-height: 30%;
overflow: auto;
}
In this way, you don't need to know the width of floating element.
Width doesn't factor in padding.
Source: http://www.w3schools.com/css/css_boxmodel.asp
The width only applies to content, not padding, border, or margin.
You can find more information here.