How to add padding to 100% div in Bootstrap? - html

I previously asked how to make a design akin to this, with a 100% height div and one below it with the same background but outside view:
The answer seemed pretty straightforward, just add padding to the top div, as seen here: http://jsfiddle.net/s6wnavqc/
HTML:
<div class="background">
<div class="bottom-div">
</div>
</div>
CSS:
.background {
background: url('http://www.mixflavour.com/wp-content/uploads/2014/11/Nature-Wallpaper-03.jpg');
position: relative;
height: 100%;
padding-bottom: 100px;
}
.bottom-div {
position: absolute;
bottom: 0;
background:rgba(0,0,0,0.5);
width: 100%;
height: 100px;
}
However, when I add Bootstrap the design completely breaks for unknown reasons! I mean, simply adding it does that, not even using any containers or rows. I figured this warranted its own thread, since the previous answer did correctly address my problem as stated.
Take a look at this fiddle to see the padding being completely ignored: http://jsfiddle.net/s6wnavqc/1/
Not even adding !important after works.
Does anyone know why and how to fix this? Do I have to use calc on the height instead? I'd rather not do that because it's not supported on some browsers.

Add box-sizing:content-box to .background. By default in bootstrap all elements are set to box-sizing:border-box which means padding is absorbed into height/width, not added to it.
From bootstrap:
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
Override:
.background {
background: url('http://www.mixflavour.com/wp-content/uploads/2014/11/Nature-Wallpaper-03.jpg');
position: relative;
height: 100%;
padding-bottom: 100px;
box-sizing: content-box;
}

Related

CSS | White spaces in 100% height elements

I'm writing here because I've got a problem with CSS. I have a .container div that contains another div set to position:absolute, top:0, left:0 and width:100%; height:100%. However I keep seeing these kind of white spaces, that when I zoom in the page disappear. Any solution?
.loop {
display: block;
position: absolute;
height: 36px;
background: white;
border: 2px solid black;
box-sizing: border-box;
width: 250px;
top: 7px;
left: 50%;
transform: translateX(-50%);
border-radius: 5px;
overflow: hidden;
transition: background 0.2s;
}
.goPrev,
.goNext {
position: absolute;
width: 36px;
height: 100%;
box-sizing: border-box;
top: 0;
display: flex;
justify-content: center;
}
.goMid {
position: absolute;
top: 0;
left: 36px;
width: calc(100% - 72px);
height: 100%;
font-family: "Poppins";
padding-top: 9px;
text-align: center;
box-sizing: border-box;
}
.goMid:hover,
.goPrev:hover,
.goNext:hover {
background: rgba(0, 0, 0, 0.3);
}
<body>
<div class="loop">
<div class="goPrev">
</div>
<div class="goMid">
Help me.
</div>
<div class="goNext">
</div>
</div>
</body>
That is just a draw.
Here you have the screenshot
Well, I'm not totally sure what to do, but the following changes seem to fix the problem for me. I changed:
Set .loop overflow from hidden to visible
Set .goMid top from 0 to -1px
The .goMid height from 100% to calc(100% + 2px)
When I moved the inner div underneath the border using top: -5px I still saw the whitespace until I changed the outer div overflow property to visible. Then if you stretch the inner div a little it seems to solve the problem. It helps that your outer div has a thick border.
.loop {
display: block;
position: absolute;
height: 36px;
background: white;
border: 2px solid black;
box-sizing: border-box;
width: 250px;
top: 7px;
left: 50%;
transform: translateX(-50%);
border-radius: 5px;
/* HERE */
overflow: visible;
transition: background 0.2s;
}
.goPrev,
.goNext {
position: absolute;
width: 36px;
height: 100%;
box-sizing: border-box;
top: 0;
display: flex;
justify-content: center;
}
.goMid {
position: absolute;
/* HERE */
top: -1px;
left: 36px;
width: calc(100% - 72px);
/* HERE */
height: calc(100% + 2px);
font-family: "Poppins";
padding-top: 9px;
text-align: center;
box-sizing: border-box;
}
.goMid:hover,
.goPrev:hover,
.goNext:hover {
background: rgba(0, 0, 0, 0.3);
}
<body>
<div class="loop">
<div class="goPrev">
</div>
<div class="goMid">
Help me.
</div>
<div class="goNext">
</div>
</div>
</body>
For what it's worth, I think #MarkP may have a good point. Combining absolute positioning and flexbox does feel like maybe a code smell. But, I don't know what the context is in your code and my flexbox-fu is a little shaky.
ACTUAL GENERATED VIEW:
I added text to all 3 nested divs and got the following centered display.
You can see all your text is bunched together. I am going to review your code and the offer a way forward. You may find you need to re-word your problem to allow us to help you better. My assumption is you are trying to set-up a tool to navigate through some type of media, such as images or pages.
CODE REVIEW
In review of your Code i can see you are trying to use a 3 part display using flexbox. Except you have also included absolute positions which prevents relative display of the divs alongside each other. Now i know you are concerned about white space but i am going to suggest a way to better use flex-box as well as debugging the whitespace, although it would be better to start again with a appropriate flexbox structure.
WHITE SPACE DEGUGGING
My suggestion first would be to remove CSS that could be causing this and then re-introduce the CSS progressively. If you are using Google Chrome you can use the insight tool to adjust the live CSS. Simply right-click on the area you wish to inspect and the CSS being used there will be displayed. You can edit directly in the CSS display and it will change the page behaviour, this is great for debugging and seeing what CSS improves your layout. Once you find the CSS you need you can replicate that in your code.
I would start with removing the following and see how you go:
Remove overflow:hidden;
When you look closer you can see the style code allows for 36px for each div on the left and the right. There may be an image missing from the .goPrev and .goNext divs, where your white space is now. Not sure if you copied your code from somewhere or wrote this from scratch?
TRY STARTING WITH A NEW FLEX-BOX STRUCTURE
I recommend creating your divs from scratch using one of the approaches found here: Common CSS Flexbox Layout Patterns with Example Code . Flexbox is super simple and a great way to build mobile responsive layouts.

Absolute Positioning with Footer not working

I have no idea how to fix this.
Putting things on position: relative will null out the bottom: 0px, and will also create tons of white space on pages that don't fit the entire height due to lack of content.
Putting it on absolute makes it cover content of pages that do have content long enough to generate a scroll bar.
.footer {
width: 100%;
height: 150px;
background: #3167b1;
position: absolute;
bottom: 0px;
}
This should be working right? For some reason it just doesn't. Is it Wordpress? Never had this problem before and I have already gone through and cleaned up a lot of issues that may have caused it.
EDIT:
Silly me... I forgot the html here.
Right now it has nothing in it so it is just:
<div class="footer"></div>
I have it like that just to test it.
To see what is happening you can visit it here:
http://www.yenrac.net/theme
I hope that helps clarify some things.
I have also created this theme from scratch.
If I got your question right, this should work:
http://jsfiddle.net/9qq1dtuf/
html {
position: relative;
min-height: 100%;
}
body {
margin-bottom: 170px;
}
.footer {
width: 100%;
height: 150px;
background: #3167b1;
position: absolute;
bottom: 0px; left: 0;
}
Please try bellow css
.footer {
position: fixed;
bottom: 0;
height: 150px;
background: #3167b1;
width: 100%;
left: 0;
}
<div class='footer'>
</div>
Well, I doubt it's Wordpress ...unless you are using a pre-made theme (or something along those lines). It's pretty hard to see what you've done without seeing the HTML. But anyways, heres what I think might have been the problem:
You have selected the footer element that has a class of "footer". I'm going to go ahead and make an educated guess that you meant to select the footer element by its name (NOT it's class). So maybe it's just a small little tiny bitty fix (i.e. remove the "." before footer in your CSS):
footer {
width: 100%;
height: 150px;
background: #3167b1;
position: absolute;
bottom: 0px;
}
Just add this to your css:
body {
margin: 0;
padding: 0;
background: #efefef;
font-family: 'Lato', serif;
padding-bottom: 174px; //add this line - height of footer + margin from content
}
I added 24px margin from content as an example. It would be best if you added this to your css:
* {
box-sizing: border-box;
}
or just for the body
body {
box-sizing: border-box;
}
So as your added padding does not add to your height and you get unnecessary scroll-bars.

Why isn't this DIV centered? [duplicate]

This question already has answers here:
Best way to center a <div> on a page vertically and horizontally? [duplicate]
(30 answers)
Closed 9 years ago.
I have a DIV that I want to keep centered and docked to the bottom of the page. I've pretty much achieved that except that it lists to the right. I've setup a jsFiddle to demonstrate the problem.
I suspect that I need to adjust margins but so far my attempts have been fruitless. I tried adding:
margin: 0;
but nothing. What am I missing?
If you mean the little bit of space to the left, then I would think adding left: 0; would solve the problem.
#footer {
position: fixed;
bottom: 0;
width: 100%;
border: 1px solid red;
text-align: center;
margin: 0;
left: 0;
}
You need to watch though because you are setting the width to be 100% and also setting a border which makes the div wider than 100%.
I suspect this is the default margin/padding on the body
body {
margin: 0;
}
will fix it.
You need to set left to 0 (zero) and box-sizing to border-box!
Check this: http://jsfiddle.net/pR4P5/7/
#footer {
position: fixed;
bottom: 0;
width: 100%;
border: 1px solid red;
text-align: center;
margin: 0;
box-sizing: border-box;
left:0;
}
Have a nice day,
Alberto
Remove border: 1px solid red; and add left: 0; to #footer.
Here is the JSFiddle.
Try replacing margin: 0; with left: 0;. This should lock the div onto the left of the page, centering it. I would also not use a border, that will add on some pixels to the width and height, making it less centered. I would instead use only border-top: 1px solid red;.
Hope I helped you solve your annoying problem.
Paulie_D's answer is correct you need to add margin: 0 to the body. I'd imagine you think its not properly centred because of the border on the footer.
Try adding:
box-sizing: border-box;
to your footer element.
Change the width: 100% to
left: 0;
right: 0;
So it would become: http://jsfiddle.net/pR4P5/6/
Complete CSS:
#footer {
position: fixed;
bottom: 0;
left:0;
right: 0;
border: 1px solid red;
text-align: center;
margin: 0;
}
Result would be 100% width while you can keep your border of 1px.
Note when defining width 100% and adding a border you might be better of adding box-sizing to it.
Also, margin: 0; to body is a great way to avoid headaches.
(box-sizing: content-box; - http://quirksmode.org/css/user-interface/boxsizing.html)
To Center the div:
html, body {
margin:0;
padding:0;
width:100%;
height:100%;
}
To keep the borders included in the positioning, too:
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
DEMO: http://jsfiddle.net/pR4P5/8/
Read More at:
CSS Reset: http://meyerweb.com/eric/tools/css/reset/
box-sizing: https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing

How can I absolutely position a block of multi-line text relative to the bottom of the last line?

I'm struggling with a problem that seems like it should be solvable using CSS - it's conceptually simple and I'd assume a relatively common problem, but how to achieve it is eluding me at the moment!
I have a container spanning 100% width and 100% height, and I need to absolutely position a block of text which have a variable number of lines so that the bottom of the bottom line of the text block is at a fixed vertical position in the container. I can only seem to position it relative to the top of the text, which is no good as it sometimes overflows out of the container when there are more lines.
Here is a JSFiddle of my failed attempt at positioning relative to the top of the text.
CSS:
html, body, l-container {
width:100%;
height:100%;
}
.l-container {
overflow: hidden;
width: 100%;
height: 100%;
position: relative;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.caption {
position: relative;
width: 360px;
margin-left: -180px;
left: 50%;
top: 82%;
text-align: center;
}
HTML:
<body>
<div class="l-container">
<div class="caption">Multiline multiline multiline multiline multiline multiline multiline multiline multiline multiline multilinemultilinemultilinemultiline
</div>
</div>
</body>
Does anybody know if this is achievable in pure CSS? I'd rather not have to use JS for layout, but this problem is making me tear my hair out.
If you set bottom: 0 and leave out any top styling, the absolutely positioned div should expand up.
As you mentioned in your question, it sounds like you need absolute positioning. Your CSS is only showing relative positioning.
Replace this CSS:
.caption {
position: relative;
width: 360px;
margin-left: -180px;
left: 50%;
top: 82%;
text-align: center;
}
...with this:
.caption {
position: absolute;
width: 360px;
margin-left: -180px;
left: 50%;
bottom: 0;
text-align: center;
}
See an example in action here:
http://jsfiddle.net/be73v/1/
On an unrelated note, might I suggest you add "margin: 0; padding: 0;" to your "html, body, l-container" css to remove the unnecessary scrollbars.

position:fixed cause margin-top disappear?

Things i want achieve is quite simple
just at top a fixed position element that do not move while scrolling
down the document.
and after is a div#content have some margin-top from the top edge
and center in the window.
so the code is:
html
<div class='head-container' id="headerCom">
<header id="a"></header>
</div>
<div id="content" role="main"></div>
CSS
* {
margin: 0;
padding: 0
}
body {
width: 100%;
height: 100%;
position: relative;
}
.head-container {
position: fixed;
top:0;
left:0;
width: 100%;
height: 100px;
background: red;
_position:absolute; // make the ie6 support the fixed position
_top: expression(eval(document.documentElement.scrollTop)); // make the ie6 support the fixed position
}
header {
display: block;
width: 960px;
height: 100px;
margin: 0 auto;
position: relative;
zoom: 1;
background: blue;
}
#content {
border: 1px solid black;
margin: 130px auto 0 auto;
width: 960px;
height: 1000px;
background: #999;
margin-top: 150px;
}
all the modern browser is well support,but in ie(ie7,ie8,ie10) do not work correctly,things is just like it ignore the margin-top i set to the div#content;
so far i have checkout the other question on stackoverflow,and i try almost everthing i could.
when i change the margin-top of the div#content to the padding-top,things okay.
When i put a div.clear(clear:both)in between the div.header-container and the div#conetent,the things goes okay;
Or i follow other questions' solution that it caused by the hasLayout, and then take out the width and height of the div#content, the things is also okay, but in this way, i will need to put another div#inner-content inside the div#content, and set width and height to it to see the result.
so i am quite confused by the hasLayout, and i am not quite sure i am completely understand what it is and not quite sure what is happening in here in my code.
So actually can all you help me with this, is there any other solution could fix this problem, and explain this wired things to me?
Thank you anyway.
It works fine for me once I get rid of the last margin-top attribute. Do you know you have set it twice? Once with margin and them again with margin-top. If you edit just margins first value it wouldn't work because the last one will override the first one.