So I have this HTML-
<div class="title" id="questionTitle">
<input type="button" class="button green" value="Save"/>
<input type="button" class="button blue" value="Preview"/>
<input type="button" class="button yellow" value="Save And Exit"/>
<input type="button" class="button red" value="Exit Without Saving"/>
</div>
and I'm trying to position the buttons so that they will be in the center of the .title div.
My CSS is
.button{
top:50%;
left:50%;
height:30px;
-moz-border-radius:10px;
border-radius:10px;
font-family:Arial;
font-weight:normal;
position:absolute;
top:50%;
}
.title{
-moz-border-radius:15px;
border-radius:15px;
width:30%;
height:100px;
background:#BDBDBD;
}
When I do position:absolute, all the elems position at the center of the page, which to me doesn't make sense because the ancestor of each .button is a div with id questionTitle, so it should be positioned relative to that.
Why are the buttons being positioned at the center of the page, and how do I make it so that they are positioned in at the center of the div?
Position: absolute makes the attribute ditch it's container. It basically frees the element to do whatever it wants.
I did a fiddle and fixed your problem. I changes it to position: relative and centered them. Obviously since you have a ton of variance in percentages, this isn't destined to work in the first place.. this is as good as it will get.
http://jsfiddle.net/jaEpt/1/
First of all if you position an element absolute it will be absolute positioned relative to the first non static positioned element in it's ancestors and here it gets to the body ancestor and fits it to 50%/50% of the body dimensions. For reference on positioning check here: http://www.w3schools.com/cssref/pr_class_position.asp.
Place a 'position:relative' in your title css for the desired outcome. Also if you want to position the button perfectly centered then subtract half of the pixels of the button you are trying to center. You can do this by applying a negative margin of half of the button's width.
I have tested the solution as far as IE6 and it works.
As Jonathan said, you shouldn't be using position absolute as it will is not relative to the parent. Perhaps you don't need to use position at all. Just let the browser do the right thing. Use this to display the buttons vertically:
.button {
margin-left: 10%;
margin-right: 10%;
width: 80%;
}
http://jsfiddle.net/7dYAA/
If you want the buttons from left to right, leave positioning alone and just add some padding: .title { padding: 10px 10%; } and/or margins/padding to the buttons. See http://jsfiddle.net/HY8UR/
adding break tag.
<input type="button" class="button green" value="Save"/><br>
<input type="button" class="button blue" value="Preview"/><br>
<input type="button" class="button yellow" value="Save And Exit"/><br>
<input type="button" class="button red"
value="Exit Without Saving"/>
</div>
css
.button{
height:30px;
-moz-border-radius: 10px;
border-radius: 10px;
font-family: Arial;
font-weight: normal;
}
.title{
text-align: center;
-moz-border-radius: 15px;
border-radius: 15px;
width: 30%;
height: auto;
background: #BDBDBD;
}
You can use text-align: center; on the parent:
<div class="title">
<input type="button" value="Save"/>
<input type="button" value="Preview"/>
<input type="button" value="Save And Exit"/>
<input type="button" value="Exit Without Saving"/>
</div>
.title { text-align: center; border: 1px solid #090; background: #efe; padding: 20px; }
.title [type=button] { display: inline; }
http://jsfiddle.net/chovy/cDafD/1/
Related
I cannot figure out why my container (main-container) background is not stretching with the content inside. It looks like the container background is stuck on initial view height. When I scroll pass the initial view height, the rest of it is white.
Here is the css
.main-container {
margin: 0;
padding: 0;
font-family: 'montserrat';
height: fit-content;
}
.main {
position: absolute;
top: 65%;
left: 50%;
transform: translate(-50%, -50%);
width: 400px;
background: greenyellow;
border-radius: 10px;
margin-bottom: 40px;
padding-bottom: 20px;
}
<div class="main-container">
<div class="main">
<h1>Signup</h1>
<form method="POST">
<div class="txt-field">
<input type="text" required>
<span></span>
<label>First Name</label>
</div>
<h1>Upload Image</h1>
<button class="btn btn-primary">Upload<i class="fa fa-upload fa-1x"></i></button>
<input type="submit" value="Signup">
</form>
</div>
</div>
This is what it looks like initially.
This is what it looks like when I scroll down
The reason is that you have position: absolute on your .main class - any elements positioned absolutely will be taken out of the regular document flow and will have no effect on the layout of their parent(s).
It looks like you are using absolute position to try and center the .main element. Have you considered using a flexbox on .main-container instead? Using a flexbox with justify-content: center and align-items: center is an easy way to center an element inside its parent while keeping the regular document flow.
I have an div element that has position: absolute;. I want it to always be on the right edge and not cover other elements.
Here is my HTML:
.color2 {
background-color: #ff0078 !important;
color: white !important;
}
.colorW {
background-color: white;
color: black;
border: 1px #d4d4d4 dotted;
}
.condition-input {
display: inline-block;
padding: 3px 7px;
line-height: 1;
text-align: center;
white-space: nowrap;
vertical-align: middle;
border-radius: 10px;
font-size: 0.9em !important;
width: 180px;
height: 19px;
background-color: #fff200;
color: black;
}
.condition-button-group {
position: absolute;
right: 12px;
}
<div>
<span class="badge color2">Height</span>
<span class="badge colorW">==</span>
<input type="text" class="form-control condition-input" />
<div class="d-inline condition-button-group">Text</div>
</div>
But on the page I see the following. I don't want the "Text" to cover the left on the input, there should be an indentation. How to fix it?
If you use absolute, your parent div needs to be positioned relative so that the absolutely positioned element is absolute within the parent and not the DOM as a whole (or other ancestors set to relative).
<div class="wrapper">
<span class="badge color2">Height</span>
<span class="badge colorW">==</span>
<input type="text" class="form-control condition-input"/>
<div class="d-inline condition-button-group">Text</div>
</div>
Style:
.wrapper{
position:relative;
}
.condition-button-group {
right:0;
top:0;
}
Check out this link: Position absolute but relative to parent
You may need to play with the style a bit to position it exactly where you want, but this is the route to take to do what you are trying to do.
You can add padding-right: 70px; to the parent div which is equal to the .condition-button-group class.
Updated code should looks like as below.
<div class="parent">
<span class="badge color2">Height</span>
<span class="badge colorW">==</span>
<input type="text" class="form-control condition-input"/>
<div class="d-inline condition-button-group">Text</div>
</div>
.parent{
position:relative;
padding-right:70px;
}
Hope this helps.
if you create any plunker that should be great.
I have a problem with the buttons on my webpage.
This is what they look like when I enter fullscreen
When I click next, and go back to this same page it then turns into this
I tried no matter what I try they always end up not aligned when I click next and go back to this same page.
The style of the buttons
.button1{
bottom: -70px;
right:0px;
position: absolute;
}
.button2{
bottom: 28px;
left:8px;
position: absolute;
}
The buttons
</div>
<div class="button1">
<button type="submit" name="Next">Next</button>
</div>
</form>
<form action="Time.html">
<div class="button2">
<button type="submit" name="Next">Back</button>
</div>
</form>
Also you can see that they don't belong in the same form.
The form where Next button belongs
.form1 {
border: 8px solid #7B93F4;
left:0;
position: absolute;
top: 43%;
width: 98.9%;
text-align:center;
}
Any idea on how can I fix this and prevent this from happening?
You could change the layout a bit, floating the buttons:
.cont {
overflow: hidden;
}
.button1{
float: left;
}
.button2{
float: right;
}
<div class="cont">
<button class="button1">button1</button>
<button class="button2">button2</button>
</div>
Floating is more flexible then position absolute.
There is an error with your HTML code. Edit it so that it'll be clear.
The two buttons are contained in different form elements which are stacked on top of themselves because they are block elements. Put them in the same form element and use the style below.
You should avoid positioning absolutely when you can. I'll suggest you float the buttons instead.
Your HTML might look something like this
<form>
<div class="button1">
<button type="submit" name="Next">Next</button>
</div>
<div class="button2">
<button type="submit" name="Next">Back</button>
</div>
</form>
and your CSS like this
.form1 {
/* your other codes */
overflow: auto;
}
.button1{
float: left;
}
.button2{
float: right;
}
Hope it works.
At the moment I have no control of my buttons, I want my buttons to be stacked horizontally with 30% width of #user-buttons. I read on the forum that the solution is to put Display: inside-block. But it didn't work. Any suggestion on how I should continue?
#user-buttons {
width: 100%;
border: 1px solid;
text-align: center;
background-color: red;
}
.button {
font-size: 10px;
font-weight: bold;
width: 30%;
}
<div id="user-buttons" class="controls">
<input type="button" class="button" value="Hi">
<input type="button" class="button" value="Bye">
<input type="button" class="button" value="Maybe">
</div>
The container buttons need to be of a fluid width width:100% so it dosent overflow the containers width.
Code:
div.controls input
{
width: 100%;
}
Here is the fixed Example.
The links don't change the "holder" height at all, really I don't know what is wrong
HTML:
<div id="holder">
<a class="button" href="#"><span>home</span></a>
<a class="button" href="#"><span>example</span></a>
<a class="button" href="#"><span>another examp.</span></a>
<a class="button" href="#"><span>hello</span></a>
<a class="button" href="#"><span>abc</span></a>
</div>
CSS:
*{
margin:0;
padding:0;
}
#holder{
width:100%;
background: #000;
border: 1px solid red;
}
.button {
color: #444;
display: block;
float: left;
height: 24px;
margin-right: 6px;
padding-right: 18px;
}
.button span {
display: block;
line-height: 14px;
padding: 5px 0 5px 18px;
}
.button:hover {
background: #333 url('./images/bg_button_a.jpg') no-repeat scroll top right;
outline: none;
}
.button:hover span {
background: #333 url('./images/bg_button_span.jpg') no-repeat;
}
Elements do not automatically expand to contain floated descendents.
Adding overflow: hidden is one type of clearfix and so long as you don't mind elements being clipped at the element's bounding box, the easiest.
#holder {
overflow: hidden;
}
Further Reading.
It's because they are float:left, such subelements don't expand the dimensions of their parent.
Easiest way to counteract this is to add
<div style="clear:both;"></div>
at the end of the children. E.g:
<div id="holder">
<a class="button" href="#"><span>home</span></a>
<a class="button" href="#"><span>example</span></a>
<a class="button" href="#"><span>another examp.</span></a>
<a class="button" href="#"><span>hello</span></a>
<a class="button" href="#"><span>abc</span></a>
<div style="clear:both;"></div>
</div>
Opening span but closing li could cause some trouble.
<a class="button" href="#"><span>hello</li></a>
Floated elements to not extend the height of their container. Essentially, they are all bound to the top of the container (which visually is not the case).
Use a div with clear:left at the bottom of the container to expand it to contain the floated elements.
Floated elements do not count towards the height of their parent.
Setting overflow: hidden; on #holder is a neat trick to rectify that.