Place button on bottom of div or screen - html

I want to position my button on the bottom of a div or the bottom of the screen (but in a non-fixed position). My code structure looks like this:
div-1
div-2
div-3
button
I want to put the button at the bottom of div 1, which height is set using jQuery (The height is the height of the screen, so putting the button at the bottom of the screen may also be a solution)
What I've tried so far:
CSS
.button {
position: fixed;
bottom: 10px;
left: 50%;
margin-left: -104.5px; /*104.5px is half of the button's width*/
}
This centers the button (what I want) and it places it at the bottom of the screen, but the position is fixed, so if I scroll down the button goes down aswell.
I've also tried setting the button's position to absolute and div-1's position to relative, this didn't work either.
Edit: The div's height is variable, so margins may not be such a good option

just do the button position:absolute without putting the div relativ
.button {
position: absolute;
bottom: 10px;
left: 50%;
margin-left: -104.5px; /*104.5px is half of the button's width*/
}
.test{
height:1000px;
}
<div class="test">
<div>
<div>
<button class="button">
test
</button>
</div>
</div>
</div>

Try using VW instead of px.
HTML:
<button class="button">TEST</button>
CSS:
.button {
position: fixed;
bottom: 10px;
left: 47vw;
width: 6vw;
}
EDIT:
HTML:
<div class="div">
<button class="button">TEST</button>
</div>
CSS:
.div{
position: relative;
border: 1px solid black;
width: 500px;
height: 250px;
}
.button {
position: absolute;
bottom: 5px;
left: 50%;
width: 50px;
margin-left: -25px;
}
I was looking the code instead of the question so i forget that the real question was add the button on the bottom of div or screen.
The parent div has to be position: relative; and the button position: absolute;

if width div 50% then left must 25% if width div 70% then left must 15%
.center{
position:fixed;
bottom:20px;
left:20%;
width:60%;
}
.center .btn{
background:red;
width:100%;
color:white;
font-weight:bold;
border-radius: 64px;
padding:10px;
}
<div class="center">
<button class="btn">Login</button>
</div>

I believe these Stack Overflow posts might be of help to you:
1) How do I get a div to float to the bottom of its container
2) HTML/CSS positioning float bottom
If this doesn't help can you please also provide your HTML code.

You should use position: absolute on your button when parent element height and width is 100% (of document or page).
<div class="div-1">
<div class="div-2">
<div class="div-3">
<button>
Just a button
</button>
</div>
</div>
</div>
and css with little reset:
* {
margin: 0;
padding: 0;
}
html, body {
width: 100%;
height: 100%;
}
.div-1 {
position: relative;
height: 100%;
width: 100%;
}
.div-2, .div-3{
width: inherit;
height: inherit;
}
button {
position: absolute;
bottom: 10px;
left: 50%;
transform: translateX(-50%);
}
Here is JSfiddle

Here is the responsive width:
position: absolute;
bottom: 23px;
left: 10%;
width: 80%;

Related

place and size button under text with css

I have been trying to come around this problem, but I can't find a way to make it work. Therefore I have come here for help.
My idea is that I want to place and size the button under the 99 / 99 text as seen on the image
Image of the html with the code
My html
<div class="btnAttackUpgrade">
<button class="btnUpgrade btnAttackSize" onclick="meleeupgPage();totalClick();"></button>
</div>
<div class="btnRangedUpgrade">
<button class="btnUpgrade btnRangedSize" onclick="btnTest2();totalClick();"></button>
</div>
<div class="btnSlayerUpgrade">
<button class="btnUpgrade btnSlayerSize" onclick="btnTest3();totalClick();"></button>
</div>
My CSS
.btnUpgrade {
position: absolute;
border: 0 none;
outline: none;
}
.btnAttackSize {
width: 31%;
height: 9%;
}
.btnRangedSize {
width: 31%;
height: 9%;
}
.btnSlayerSize {
width: 31%;
height: 9%;
}
div.btnSlayerUpgrade {
position: relative;
top: 67%;
left: 34.5%;
}
div.btnRangedUpgrade {
position: relative;
top: 34%;
left: 1%;
}
div.btnAttackUpgrade{
position: relative;
top: 0.5%;
left: 1%;
}
I know if I remove position: relative; completely from lets say btnSlayerUpgrade it sizes the button as I want, but then it will not stay under the text. It wont be effected by the width and heigth changes. See the picture After I removed position relative
What stop the button from resizing when it has a position to it in css
If understand your question correctly I think the easiest and most responsive way to do this is to put both elements as siblings in a container with display:flex and with centred children.
HTML
<div class="container">
<div class="btnAttackUpgrade"></div>
<button class="btnUpgrade btnAttackSize" onclick="meleeupgPage();totalClick();"></button>
<div>
CSS
.container{
display:flex;
//flex-direction: column; (default)
justify-content:center;
align-items:center;
}
.btnAttackUpgrade{
//unnecessary
}
.btnUpgrade .btnAttackSize{
//unnecessary
}

fixed div on top displaying layer problem

I am trying to make a div fixed on the top but looks like the layer overlaps.
CSS:
#fsancy {
background-color:#ddd;
position: fixed;
display: block;
width: 200px;
height: 100px;
left: 50%;
top: 0%;
margin-left: -100px; /*half the width*/
}
HTML:
<div class="container" id="fsancy">
<div class="row">
<div class="col-lg-12 text-center fluid fixme" id=""
style="background-color: #ff0033; max-width: 100%; color: #ffffff; font-size: xx-large">Share £200 With A
Friend
</div>
</div>
Picture example
Fixed position elements are not part of the regular document flow, so in your particular case you have to add some margin-top to the first regular element which is high enough to avoid the overlap / fit under the fixed header.
#Michelbach Alin, use position absolute and z-index properties for fix as a layer.
{
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
}
Reference.
https://www.w3schools.com/cssref/pr_pos_z-index.asp

Vertically center text inside an absolutely-positioned div

I'm trying to vertically center text inside a div that is positioned absolutely.
I have tried table-cell approach with no luck. This is a responsive layout, so I'm trying to avoid setting fixed heights and prefer not to use Javascript either.
Thanks
Link to jsbin demo
HTML & CSS:
<div class="page-banner" style="background: url(http://www.bimga.com.php53-3.ord1-1.websitetestlink.com//wp-content/uploads/BIMGA_Website_InteriorPage_Banners_About.jpg) no-repeat scroll 0 0 / cover transparent">
<img style="visibility:hidden" src="http://www.bimga.com.php53-3.ord1-1.websitetestlink.com//wp-content/uploads/BIMGA_Website_InteriorPage_Banners_About.jpg">
<div class="left">
<div class="page-banner-text">this text needs to be verticall centered</div>
</div>
</div>
<style type="text/css">
.page-banner {
margin-bottom: 35px;
list-style: none;
width: 100%;
padding-left: 0;
position: relative;
}
.page-banner img {
width: 100%;
height: auto;
}
.page-banner .left {
background-color: rgba(10, 65, 142, .75);
position: absolute;
z-index: 1;
left: 0;
top: 0;
height: 100%;
text-align: center;
width: 50%;
}
</style>
We could use a transform like so:
Have a jsBin!
CSS
.page-banner-text {
top: 50%;
transform: translateY(-50%);
position: absolute;
}
More information on this technique.
What you can do is, set the text position to absolute.
Then give it a top: 50%; and give it a top margin of minus half its height.
I would not prefer using position absolute and top: 50% for better multi browser support (espesially on older IE versions) so I would prefer adding line-height: x em; in your .page banner class. Em because you have defined the height by % so it needs to always be on the center no matter the actual pixel height.
.page-banner .left:after {
content: "Background text";
position: absolute;
top: 40%;
left: 35%;
z-index: -1;
}

Center a search bar in a container div that contains many other divs

I have a container div, that contains many other divs, and in one of the divs, a search bar. Hence the html looks like this:
<div id="header-middle">
<div id="header-search">
<form>
<input type="text">
</form>
</div>
<div id="header-middle-left">
</div>
<div id="header-transition">
</div>
<div id="header-middle-right">
</div>
</div>
the css I have for it is:
#header-middle{
width: 400px;
height: 64px;
float: left;
}
#header-search{
position: absolute;
left: 50%;
top: 50%;
margin-top: -32px;
margin-left: -200px;
}
#header-middle-left{
width: 241px;
height: 64px;
float: left;
background-image:url('foo');
background-repeat:repeat-x;
}
#header-transition{
width: 19px;
height: 64px;
float:left;
background-image:url('foo2');
}
#header-middle-right{
width: 140px;
height: 64px;
float:left;
background-image:url('foo3');
background-repeat:repeat-x;
}
the divs mainly just are there for style, they use images as backgrounds, and so theres no harm in the search bar covering them. However, I need it to be centered in the containing div, and cover small parts of the rest of them. Any help would be very appreciated. Also any advice about css in general would be appreciated.
You should edit your CSS to achieve this, change #header-middle and #header-search to use the following code :
#header-middle {
width: 400px;
height: 64px;
position: relative;
}
#header-search {
position: absolute;
left: 50%;
top: 50%;
margin-top: -11px;
margin-left: -75px;
width: 150px;
height: 22px;
}
Here is a DEMO.
You can use position:relative for #header-search to make its position relative to the container div #header-middle.
For centering you can use exact pixels if the width & height of the search box and the container div is static. This is not the best way but it should work. If they are dynamic than you can calculate with javascript.
http://jsfiddle.net/rWNkj/

Nested divs with mixed height mode (% & px) Keep the '%' div fill out space not used by the 'px' div

I want the "blue" container to always be 70px high, while the previous "green" div always max out the height available when the div is resized with javascript.
I've played around with it for a while without finding a proper solution. Help will be appreciated.
As promised, here's my answer.
absolute inside relative positioning is the easiest way to do this.
Live Demo
HTML:
<div id="parent">
<div id="left">height: 100%</div>
<div id="right">Content</div>
<div id="rightFooter">height: 70px</div>
</div>
CSS:
#parent {
position: relative;
height: 200px
}
#left, #right, #rightFooter {
position: absolute
}
#left {
width: 200px;
top: 0;
bottom: 0;
left: 0
}
#right {
top: 0;
right: 0;
bottom: 70px;
left: 200px;
overflow-y: auto
}
#rightFooter {
height: 70px;
right: 0;
bottom: 0;
left: 200px
}
Would something like this work?
Live Demo
Added an animation of the height so you can see the content extending.
Markup
<div id="parent">
<div class="left">
Lefty
</div>
<div class="right">
<div id="rightContent">
right Content
</div>
<div id="rightFooter">
Right Footer
</div>
</div>
<div class="clear"></div>
</div>
CSS
#parent{
height:300px;
}
.left{
float: left;
width: 33%;
background: red;
height:100%;
}
.right{
float : left;
width: 66%;
height:100%;
}
#rightContent{
height: 100%;
background: blue;
}
#rightFooter{
background: yellow;
height: 70px;
float: right;
width: 100%;
margin-top: -70px;
}
.clear{
clear:both;
}
Bah, before the comments come this is a partial solution, the text for the content area will bleed into the footer... looking at a solution for this, or someone else might be able to modify my markup/css to account for that.
Made an example for you here :)
you need to have a left floated div for the left content and a wrapper for the two other right divs, also floated left.
Take a look :)