Display CSS: some divs fixed, some flexible (2) - html

somehow I am very bad at this so i need your help guys. The problem is similar to my previous problem (find it here: Display CSS: some divs fixed, some flexible).
This time I need #DIV-3 to be the flexible one that gets small and big as the window height changes and all other DIVS need to be fixed. So here is an illustration:
Can someone please help my with a fiddle like last time.
Thank you very much.
this is code example, but doesnt really matter probably:
#DIV-1{
position: fixed;
padding: 1em 2em;
top: 6.8em;
right: 0;
height: 9.5em;
width: 18%;
bottom: 75%;
}
#DIV-2{
position: fixed;
padding: 1em 2em;
width: 18%;
top: 16em;
bottom: 18em;
right: 0;
}
#DIV-3{
position: fixed;
padding: 1em 2em;
bottom: 0em;
right: 0;
width: 18%;
height: 18em;
overflow-y: auto;
}

I think this might be what you want (using the answer from your other question) ... horrible way to build something though haha.
http://jsfiddle.net/uKPEn/5/
.middle1 {
background: blue;
height: 100px;
top:50px;
}
.middle2 {
background: green;
top: 150px;
height: 100px;
}
.logo {
background: pink;
overflow: scroll;
top: 250px;
bottom:0%;
}
Not exactly sure, but you could consider using something like Isotope or Masonry for building stuff that fits together like that.

Use the CSS function calc()
html, body {
background-color: transparent;
height:100%;
width: 100%;
margin: 0px;
font-size:8pt;
}
#header {
box-sizing: border-box;
width: auto;
height: 6.8em;
border: 1px solid red;
}
#section {
box-sizing: border-box;
width: calc(100% - 18%);
height: calc(100% - 6.8em);
overflow-x: auto;
border: 1px solid blue;
float: left;
}
#DIV-1 {
box-sizing: border-box;
float: left;
border: 1px solid purple;
height: 9.5em;
width: 18%;
}
#DIV-2 {
box-sizing: border-box;
float: left;
border: 1px solid green;
height: 9.5em;
width: 18%;
}
#DIV-3 {
box-sizing: border-box;
float: left;
border: 1px solid orange;
height: calc(100% - 25.8em);
width: 18%;
overflow-y: scroll;
}
HTML
<div id="header">Header</div>
<div id="section">Section</div>
<div id="DIV-1">1</div>
<div id="DIV-2">2</div>
<div id="DIV-3">3</div>
Example on: http://jsfiddle.net/7baotd4o/
calc allows us to, surprise, calculate a value for CSS.
http://caniuse.com/#feat=calc

Related

Adding margin to div pushes it off the screen

I would like to have a 5px margin around each of my divs but when I add it in CSS, there is a 5px margin on every side except for in between the divs and on the bottom. The two divs also overflow off the page on the bottom. I understand this is because of the 5px margin on top pushing the divs off screen. I am unsure how to make it just add the margin all around and shrink the divs accordingly.
* {
box-sizing: border-box;
margin: 0;
}
html {
width: 100%;
height: 100%;
}
body {
background: black;
width: 100%;
height: 100%;
position: relative;
overflow: hidden;
}
.one {
background: red;
position: absolute;
width: 10%;
height: 100%;
left: 0;
border: 3px solid green;
border-radius: 5px;
margin: 5px;
}
.two {
background: blue;
position: absolute;
width: 90%;
height: 100%;
right: 0;
border: 3px solid green;
border-radius: 5px;
margin: 5px;
}
<div class="one">
</div>
<div class="two">
</div>
Resulting Page
Divs pushed off screen on bottom and no margin in-between divs. 5px margin on top, left, and right is present.
I am new to HTML and CSS so any helps greatly appreciated.
Use CSS Flex
/*QuickReset*/ * { box-sizing: border-box; margin: 0; }
html {
height: 100%;
}
body {
background: black;
height: 100%;
position: relative;
display: flex; /* Use flex! */
padding: 5px; /* Instead of children margin */
/* gap: 5px; /* Uncomment to add a gap between your child elements! */
}
.one,
.two {
border: 3px solid green;
border-radius: 5px;
}
.one { width: 10%; background: red; }
.two { width: 90%; background: blue; }
<div class="one">
</div>
<div class="two">
</div>
box-sizing: border-box does not include handling/including of margins in the overall width or height of the elements, only padding and borders. Therefore you have to subtract the margin values from the width or height values.
In your case you should use calc values on all height and width settings where there's a margin. I.e. if you have 5px margin (= on all sides), use for example calc(100% - 10px) where you want 100% width or height. Similar with other percentage values - see your adapted code below:
* {
box-sizing: border-box;
margin: 0;
}
html {
width: 100%;
height: 100%;
}
body {
background: black;
width: 100%;
height: 100%;
position: relative;
overflow: hidden;
}
.one {
background: red;
position: absolute;
width: calc(10% - 10px);
height: calc(100% - 10px);
left: 0;
border: 3px solid green;
border-radius: 5px;
margin: 5px;
}
.two {
background: blue;
position: absolute;
width: calc(90% - 10px);
height: calc(10% - 10px);
right: 0;
border: 3px solid green;
border-radius: 5px;
margin: 5px;
}
<div class="one">
</div>
<div class="two">
</div>
using the css calc function on the width of .two to subtract 10px (2x5px margins) from the 90% width, appears to give a reasonable margin.
width: calc(90% - 10px);
I am not sure why there is not a visible 10px (2x5px) margin between .one and .two though.
https://developer.mozilla.org/en-US/docs/Web/CSS/calc
* {
box-sizing: border-box;
margin: 0;
}
html {
width: 100%;
height: 100%;
}
body {
background: black;
width: 100%;
height: 100%;
position: relative;
overflow: hidden;
}
.one {
background: red;
position: absolute;
width: 10%;
height: 100%;
left: 0;
border: 3px solid green;
border-radius: 5px;
margin: 5px;
}
.two {
background: blue;
position: absolute;
width: calc(90% - 10px);
height: 100%;
right: 0;
border: 3px solid green;
border-radius: 5px;
margin: 5px;
}
<div class="one">
</div>
<div class="two">
</div>

Make div grow up left instead of down right

The div should grow up left, however, it does the opposite as of now.
The margin-left and top is necessary by the way.
Quick gif showcasing the issue: https://gyazo.com/ce51c504698395c26cffefb9b74e7e3e
html, body {
width: 100%;
height: 100%;
}
#a {
width: 50%;
height: 100%;
border: 1px solid black;
}
#img-wrapper {
margin-left: 10%;
margin-top: 20%;
width: 50%;
position: relative;
border: 1px solid red;
}
img {
width: 100%;
}
<div id="a">
<div id="img-wrapper">
<img src="https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2017/11/12225358/Pug-On-White-01.jpg" alt="">
</div>
</div>
Try this:-
#a {
width: 70%;
height: 100%;
border: 1px solid black;
position: relative;
}
#img-wrapper {
width: 40%;
position: absolute;
bottom: 0;
right: 0;
border: 1px solid red;
}
If you want your image going from right to left by increasing width property, you should give it float property:
#img-wrapper {
float: right;
margin-top: 0; // if you want it to start from top right edge
}
added margin-right: 10%; float: right;
#img-wrapper {
margin-right: 10%;
margin-top: 20%;
width: 50%;
position: relative;
border: 1px solid red;
float: right;
}
html,
body {
width: 100%;
height: 100%;
}
#a {
width: 50%;
height: 100%;
border: 1px solid black;
position: relative;
}
#img-wrapper {
margin-right: 10%;
margin-top: 20%;
width: 52%;
position: absolute;
border: 1px solid red;
right: 0;
bottom: 50%;
transform: translateY(50%);
}
img {
width: 100%;
}
<div id="a">
<div id="img-wrapper">
<img src="https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2017/11/12225358/Pug-On-White-01.jpg" alt="">
</div>
</div>
Sounds like the problem isn't about getting the image to "grow up left" but is about positioning the #img-wrapper.
You can solve this by positioning the #img-wrapper absolutely and specifying its bottom and right position. I've added a :hover style so you can see it 'grow' on hover.
A word of warning though. Positioning something of unknown/variable size using percentages is going to give you very mixed results at different viewport sizes. Perhaps what you want isn't quite as described but I think you should be looking at a more flexible solution such as using flexbox.
html, body {
width: 100%;
height: 100%;
}
#a {
width: 50%;
height: 100%;
border: 1px solid black;
position:relative;
}
#img-wrapper {
right: 30%;
bottom: 30%;
width: 50%;
position: absolute;
border: 1px solid red;
}
#img-wrapper:hover {
width: 70%;
}
img {
width: 100%;
}
<div id="a">
<div id="img-wrapper">
<img src="https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2017/11/12225358/Pug-On-White-01.jpg" alt="">
</div>
</div>
html, body {
width: 100%;
height: 100%;
}
#a {
width: 50%;
height: 100%;
border: 1px solid black;
position: relative;
}
#img-wrapper {
width: 50%;
border: 1px solid red;
margin: 20% 0 0 20%;
position: absolute;
top:0;
left:50%;
transform: translateX(-50%);
}
img {
width: 100%;
}

How to place a footer at the bottom of a page in CSS?

I tried to create a page footer for each page. The objectif is to center the footer and to place it at the bottom of the page. You can check my JSFiddle, or see the code directly as following.
HTML
<div id="page1" class="page">
<div class="footer">
<p>1</p>
</div>
</div>
CSS
div.page {
height: 300px;
width: 180px;
border: 1px solid #000;
margin: auto;
margin-bottom: 5px;
text-align: center;
}
div.footer {
background-color: #DDD;
width: 100%;
bottom: 0; /* doesn't work */
}
p {
width: 15px;
color: white;
background-color: red;
text-align: center;
margin: auto;
bottom: 0;
}
I saw to similar question about How to position div at the bottom of a page ?. However, when i applied its proposition, bottom + position setting, footers in each page are all merged together, placed at the bottom of the navigator's windows. Here's the related JSFiddle
Can somebody help ? Thanks a lot.
You are missing position: relative; applied to the class="page".
This way the element which has absolute position applied knows that needs to be bottom:0 relative to the parent element .page.
div.page {
height: 300px;
width: 180px;
border: 1px solid #000;
margin: auto;
margin-bottom: 5px;
text-align: center;
position:relative;
}
div.footer {
background-color: #DDD;
width: 100%;
bottom: 0; /* it works now */
position: absolute;
}
p {
width: 15px;
color: white;
background-color: red;
text-align: center;
margin: auto;
bottom: 0;
}
DEMO http://jsfiddle.net/9xzb9m48/3/
Try this:
div.page {
height: 300px;
width: 180px;
border: 1px solid #000;
margin: auto;
margin-bottom: 5px;
text-align: center;
position: relative;
}
div.footer {
background-color: #DDD;
width: 100%;
position: absolute;
bottom: 0;
}
p {
width: 15px;
color: white;
background-color: red;
text-align: center;
margin: auto;
bottom: 0;
}

giving margin to a fluid div shows horizontal scrollbar

Yes now i've got the grid outertcontainer fixed but somehow its not expanding 100%.
Please tell me where am i doing it wrong.
i am giving the emitted css.
if you see it in full screen you can see the outer container div is still not 100%.
Thanks.
Here is the
FIDDLE
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box; }
body {
margin: 0;
padding: 0; }
img {
max-width: 100%; }
.bordered {
border: 1px solid black; }
.redbordered {
border: 1px solid red; }
.greenbordered {
border: 1px solid green; }
.outerContainer {
max-width: 68em;
margin-left: auto;
margin-right: auto;
min-height: 300px;
width: auto;
margin-left: 133px;
background-color: crimson; }
.outerContainer:after {
content: "";
display: table;
clear: both; }
.outerContainer .leftSide {
float: left;
display: block;
margin-right: 0%;
width: 50%;
background-color: blue;
min-height: 200px; }
.outerContainer .leftSide:last-child {
margin-right: 0; }
.leftNav {
height: 100%;
width: 133px;
background-color: black;
position: fixed;
left: 0px; }
Of course, position:fixed is exactly for that purpose, to have the element fixed in that position, overlapping everything below it. To have a regular 2 columns layout with no overlapping, just try this (there are many ways to do it, this is just one using CSS without changing your HTML)
body {
margin: 0;
padding: 0;
display:block;
min-height:100%;
}
.leftNav {
height: 100%;
width: 10%;
background-color: black;
display:inline-block;
opacity: 0.7;
}
.rightContainer {
background-color:silver;
min-height: 300px;
display:inline-block;
}
.fluid {
width: 89%;
}
jsFiddle
remove width 100% in fluid and make rightContainer as width auto and margin-left 100px
.rightContainer
{
background-color:silver;
min-height: 300px;
width:auto;
margin-left:100px;
}

Alignment with relative and absolute positioning

How could I center the blue box inside the red one ?
I see that the left side of the blue box is exactly in the middle of the red box, but I would like to center the whole blue box, not its left side. The dimensions of the boxes are not constant. I want to align regardless of boxes dimensions. Example to play with here. Thanks !
HTML:
<div id="rel">
<span id="abs">Why I'm not centered ?</span>
</div>
CSS:
#rel {
position: relative;
top: 10px;
left: 20px;
width: 400px;
height: 300px;
border: 1px solid red;
text-align: center;
}
#abs {
position: absolute;
bottom: 15px;
width: 300px;
height: 200px;
border: 1px solid blue;
}
If you're able to change the <span> tag to a <div>
<div id="rel">
<div id="abs">Why I'm not centered ?</div>
</div>
Then this piece of CSS should work.
#rel {
position: absolute;
top: 10px;
left: 20px;
width: 400px;
height: 300px;
border: 1px solid red;
text-align: center; }
#abs {
width: 300px;
height: 200px;
border: 1px solid blue;
margin: auto;
margin-top: 50px; }
I think it's better to use more automation for the enclosed box as less changes would be needed should you change the size of the container box.
You could add left:50px to #abs if that's all you want...
#abs {
position: absolute;
bottom: 15px;
width: 300px;
height: 200px;
border: 1px solid blue;
left:50px;
}
If you are going to define dimensions like that (200px x 300px and 300px x 400px), here's how it can be centered:
#rel {
position: relative;
top: 10px;
left: 20px;
width: 400px;
height: 300px;
border: 1px solid red;
text-align: center;
}
#abs {
position: absolute;
width: 300px;
height: 200px;
border: 1px solid blue;
margin: 49px 0 0 49px;
}
You can check at my solution here at http://jsfiddle.net/NN68Z/96/
I did the following to the css
#rel {
position: relative;
top: 10px;
left: 20px;
right: 20px;
width: 400px;
height: 300px;
border: 1px solid red;
text-align: center;
display: table-cell;
vertical-align: middle;
}
#abs {
display: block;
bottom: 15px;
width: 300px;
height: 200px;
border: 1px solid blue;
margin: 0 auto;
}
This should work
#abs {
position: absolute;
left: auto;
right: auto;
bottom: 15px;
width: 300px;
height: 200px;
border: 1px solid blue;
}