Vertically Align Div with Absolute Position - html

I am trying to vertically align a div (the "p" element in this example but it could be anything a div a link, an image, etc.) which needs to have its position property set to absolute. I followed this example: http://davidwalsh.name/table-cell-position-absolute
But I can get it to work. Here is the code:
<div style="width: 400px; height: 48px; background-color: #EEE; margin: 0 auto;">
<div style="display: table; position: relative; border: 1px solid blue; width: 100%; height: 100%;">
<div style="display: table-cell; border: 2px solid green;">
<div style="position:relative; overflow: auto; height: 100%;">
<p style="position:absolute; bottom:0; right:0; font-size: 18px; color: black; border: 1px solid orange;">VERTICALLY CENTER ME PLEASE!</p>
</div>
</div>
</div>
</div>
And the jsfiddle: http://jsfiddle.net/bqm7wudc/2/
Could someone suggest a solution please?

I actually sorted the problem that way:
<div style="display: table; position: relative; border: 1px solid red; width: 960; height: 48px; margin: 0 auto;">
<ul style="display: table; vertical-align: middle; border: 1px solid orange; padding: 0px; position:absolute; left: 700px; margin: 0; height: 100%;">
<li style="display: table-cell; vertical-align: middle; font-size: 20px; font-weight: 900; padding-right: 20px;">toto</li>
<li style="display: table-cell; vertical-align: middle; font-size: 20px; font-weight: 900; padding-right: 20px;">titi</li>
</ul>
</div>
The text is defined as the content of a list. The ul element is itself a table (positioned using the absolute mode), while the li elements are table cells. This seems to work.
Updated fiddle: http://jsfiddle.net/bqm7wudc/7/

Related

How do I center this element on the page? I can only center the text within colored background

It creates exactly what I need but I cannot center to whole element on the page. centering will only center the text within the background colored area. What am I doing wrong?
I tried many different code combination but cannot make this work.
GROW YOUR BUSINESS WITH US
<h1 style="display: inline-block; text-align: center; background-color: #273b86; color: #ffffff; padding: 5px; border-radius: 5px; width: 640px;">GROW YOUR BUSINESS WITH US</h1>
I would like whole element above to be centered on the page.
You also could just change the display attr to block and add margin:auto to it!
I placed it into a div with 1000px width for you to view, but you just need the h1
<div style="width:1000px;">
<h1 style="display: block; text-align: center; background-color: #273b86; color: #ffffff; padding: 5px; border-radius: 5px; width: 640px; margin:auto;">GROW YOUR BUSINESS WITH US</h1>
</div>
You can add div around H1 and add width and margin:auto like
<div style="width:650px; margin: auto;"><h1 style="display: inline-block; text-align: center; background-color: #273b86; color: #ffffff; padding: 5px; border-radius: 5px; width: 640px; ">GROW YOUR BUSINESS WITH US</h1><div>
Or like Huangism do
<h1 style="text-align: center; background-color: #273b86; color: #ffffff; padding: 5px; border-radius: 5px; width: 640px; margin: auto;">GROW YOUR BUSINESS WITH US</h1>
here is an example
.container{
width: 100%;
height: 100%;
border: 1px red solid;
display: inline-block;
height: 90vw;
}
.container > div{
width: 150px; /* very impotent */
margin:auto;/* center position */
border:1px #CCC solid;
text-align:center;
}
<div class="container">
<div> center div </div>
</div>

Image and text alignment issue

I took a solution from here earlier to solve some div alignment problems, just when I thought all was sorted - adding an image into any of my containers causes text to be misaligned elsewhere, see image and code below.
The image in the code is just a blue rectangle, any image will do.
Does anyone know how I can resolve this?
<style>
.row {
display: table;
width: 98%;
min-width: 440px;
border: solid 5px black;
border-top: none;
margin-top: -4px;
}
.left {
width: 150px;
border-right: solid 5px black;
display:table-cell;
background-color: black;
}
.middle {
width:auto;
display:table-cell;
font-size: 9.5pt;
padding-bottom: 20px;
}
.right {
width: 150px;
border-left: solid 5px black;
display:table-cell;
background-color: black;
padding-left: 5px;
overflow: auto;
}
</style>
<div class="row">
<div class="left">
<img alt="" src="/Images/Blue.png" border="0"></img>
</div>
<div class="middle">
Text which should be at the top
</div>
<div class="right">
</div>
</div>

Unable to define width of absolute container's children

I am trying to define the width of a few divs nested within an absolute parent. Their width is defined but does not appear to be taken in consideration by the browser.
Any ideas of what it is I am missing (or doing wrong)?
Thank you all in advance for your time and attention.
HTML
<div class="icons-container">
<div>
<img src="img/2d.svg" alt="2d animation icon">
</div>
<div>
<img src="img/3d.svg" alt="3d animation icon">
</div>
</div>
CSS
.icons-container {
width: 100%;
margin: 0 auto;
border: 1px solid #fff;
position: absolute;
bottom: 0;
}
.icons-container div {
width: 150px;
margin: 0 2px;
background: rgba(0,0,0,0.5);
display: inline;
vertical-align: middle;
border: 1px solid #fff;
}
.icons-container div img {
width: 50px;
}
Update .inline to .inline-block:
.icons-container {
width: 100%;
margin: 0 auto;
border: 1px solid #fff;
position: absolute;
bottom: 0;
}
.icons-container div {
width: 150px;
margin: 0 2px;
background: rgba(0,0,0,0.5);
display: inline-block;
vertical-align: middle;
border: 1px solid #fff;
}
.icons-container div img {
width: 50px;
}
<div class="icons-container">
<div>
<img src="img/2d.svg" alt="2d animation icon">
</div>
<div>
<img src="img/3d.svg" alt="3d animation icon">
</div>
</div>
use display inline-block instead of inline

Child-element in inline-block div changes the parents siblings position

I have a couple of divs with some content (another div inside). I want the parent divs to be positioned in a nice line and I want the child div to be relative to its parent.
I've made a JSFiddle To illustrate. Right now the divs are all nicely placed, but if you remove the '.btn' div in the last one, everything gets messed up.
To see what I mean, change the HTML in the JSFiddle to:
<div id="expand1" class="expand">
<div id="btn1" class="btn">>></div>
</div>
<div id="expand2" class="expand">
<div id="btn2" class="btn">>></div>
</div>
<div id="expand3" class="expand"></div>
What's going on here? How can I get the desired result?
You should use floats for a better layout:
.expand{
border:1px solid black;
width:400px;
height:400px;
display:inline-block;
float: left;
}
.btn{
cursor:pointer;
position:relative;
top:150px;
left: 150px;
border: 1px solid black;
width:40px;
height:40px;
vertical-align:middle;
text-align:center;
line-height:40px;
}
<div id="expand1" class="expand">
<div id="btn1" class="btn">>></div>
</div>
<div id="expand2" class="expand">
<div id="btn2" class="btn">>></div>
</div>
<div id="expand3" class="expand"></div>
.expand {
border: 1px solid black;
width: 400px;
height: 400px;
display: inline-block;
vertical-align: top;
}
.btn{
margin: -20px 0 0 -20px;
cursor: pointer;
position: relative;
top: 50%;
left: 50%;
border: 1px solid black;
width: 40px;
height: 40px;
vertical-align: middle;
text-align: center;
line-height: 40px;
}
<div id="expand1" class="expand">
<div id="btn1" class="btn">>></div>
</div>
<div id="expand2" class="expand">
<div id="btn2" class="btn">>></div>
</div>
<div id="expand3" class="expand"></div>
You can simply play with the position property to get the desired result.
just add position:relative to your .expand class
.expand {
position:relative;
border: 1px solid black;
width: 400px;
height: 400px;
display: inline-block;
}
and update the position to absolute to your .btn class
.btn {
cursor: pointer;
position: absolute;
top: 150px;
left: 150px;
border: 1px solid black;
width: 40px;
height: 40px;
vertical-align: middle;
text-align: center;
line-height: 40px;
}

Bar Graph Effect: Aligning Multiple DIVs to Bottom when Side by Side

Here is an image of my problem:
http://www.rhexi.com/images/uploads/example.jpg
I am trying to align multiple side-by-side divs to the bottom within a parent div. The end result I am trying to achieve is a bar graph, where you have a parent container, and multiple bar divs a the bottom within the parent.
I have successfully embedded the child bar divs within the container div, but they are all aligned top. How do i get it to align bottom?
I do not want to use position: absolute and bottom: 0 since these bars need to be floating.
Here is my code:
<div style="width: 100px; height: 50px; padding-bottom: 1px; border: 1px solid #000;">
<div style="width: 20px; height: 20px; background: #000; margin-left: 1px; float: left;"></div>
<div style="width: 20px; height: 10px; background: #00f; margin-left: 1px; float: left;"></div>
<div style="width: 20px; height: 5px; background: #f00; margin-left: 1px; float: left;"></div>
</div>
Thanks for the help!
If you want to continue using this technique, but need skybondsor's answer to be aligned with the bottom of the "screen" without using absolute positioning on each bar, just make use of your margin style. Your margin-top should be equal to:
margin-top = height_of_graph - height_of_bar
So, in the example set by skybondsor, this worked for me:
<div style="width: 100px; height: 50px; padding-bottom: 1px; border: 1px solid #000;position:relative;">
<div style="width: 199px; height: 50px; position: absolute; bottom: 0;">
<div style="width: 20px; height: 20px; background: #000; margin-left: 1px; float: left; margin-top: 30px;"></div>
<div style="width: 20px; height: 10px; background: #00f; margin-left: 1px; float: left; margin-top: 40px;"></div>
<div style="width: 20px; margin-top: 45px; height: 5px; background: #f00; margin-left: 1px; float: left;"></div>
</div>
</div>
Hope this helps.
How about creating 3 divs instead of 1? Something like this:
<div style="width: 20px; height: 100%; margin-left: 1px; float: left;">
<div style="height: 80px;"></div>
<div style="height: 100%; background: #000;"></div>
</div>
..
I'm not sure if this is what rfausak is also getting at, but position: absolute, bottom: 0 is the only way to do this. Fortunately, nesting one level deeper will allow you to achieve the effect without losing your floats.
<div style="width: 100px; height: 50px; padding-bottom: 1px; border: 1px solid #000;position:relative;">
<div style="width: 199px; height: 50px; position: absolute; bottom: 0;">
<div style="width: 20px; height: 20px; background: #000; margin-left: 1px; float: left;"></div>
<div style="width: 20px; height: 10px; background: #00f; margin-left: 1px; float: left;"></div>
<div style="width: 20px; height: 5px; background: #f00; margin-left: 1px; float: left;"></div>
</div>
</div>
Maybe make each bar a div nested inside another div and then make the background of the container div the color you want your bar to be, with the nested div the color of the background of the chart. Then, rather than giving the nested div the height you desire, you can give it the inverse.
E.g. something like:
<div style="width: 100px; height: 50px; padding-bottom: 1px; border: 1px solid #fff;">
<div style="width: 20px; height: 50px; background: #000; margin-left: 1px; float: left;">
<div style="width: 20px; height: 30px; background: #fff;"></div>
</div>
<div style="width: 20px; height: 50px; background: #000; margin-left: 1px; float: left;">
<div style="width: 20px; height: 20px; background: #fff;"></div>
</div>
<div style="width: 20px; height: 50px; background: #000; margin-left: 1px; float: left;">
<div style="width: 20px; height: 10px; background: #fff;"></div>
</div>
</div>