Unfortunately, I am unable to center the elements in the position: absolute parent.
Here is my code:
.cont-img {
width:400px;
position: relative;
}
.image {
width:400px;
height:400px;
background:black;
}
.desc {
position: absolute;
top:0px;
height:100%;
width:100%;
color:gold;
display:table;
}
.centered-items {
text-align:center;
display:table-cell;
vertical-align: middle;
}
<div class="cont-img">
<div class="image">
</div>
<div class="desc">
<div class="centered-items">
<div class="item1">
asdasd
</div>
<div class="item2">
asdasd
</div>
</div>
</div>
</div>
PS : I also tried the flex method but unfortunately it didn't work for me because the flex makes the elements not display.
All I need is to make the elements under this absolute parent centered vertically and horizontally.
CSS Positioning Method
For centering with absolute positioning it helps to use transform, in conjunction with the top and left offset properties. You don't need vertical-align or table properties.
.cont-img {
width: 400px;
position: relative;
}
.image {
width: 400px;
height: 400px;
background: black;
}
.desc {
position: absolute;
top: 0;
height: 100%;
width: 100%;
color: gold;
}
.centered-items {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%); /* 1 */
}
<div class="cont-img">
<div class="image"></div>
<div class="desc">
<div class="centered-items">
<div class="item1">asdasd</div>
<div class="item2">asdasd</div>
</div>
</div>
</div>
Notes:
Here's an explanation of how this works: Element will not stay centered, especially when re-sizing screen
CSS Flexbox Method
For centering with flexbox, here's an example:
.cont-img {
width: 400px;
position: relative;
}
.image {
width: 400px;
height: 400px;
background: black;
}
.desc {
position: absolute;
top: 0;
display: flex;
justify-content: center; /* 2 */
align-items: center; /* 2 */
height: 100%;
width: 100%;
color: gold;
}
<div class="cont-img">
<div class="image"></div>
<div class="desc">
<div class="centered-items">
<div class="item1">asdasd</div>
<div class="item2">asdasd</div>
</div>
</div>
</div>
Notes:
Here's an explanation of how this works: https://stackoverflow.com/a/33049198/3597276
Try this code
.cont-img {
width:400px;
position: relative;
}
.image {
width:400px;
height:200px;
background:black;
position:relative;
}
.desc {
position: absolute;
top:0px;
height:100%;
width:100%;
color:gold;
display:table;
}
.centered-items {
text-align:center;
display:table-cell;
vertical-align: middle;
}
<div class="cont-img">
<div class="image">
<div class="desc">
<div class="centered-items">
<div class="item1">
asdasd
</div>
<div class="item2">
asdasd
</div>
</div>
</div>
</div>
</div>
Just give height to cont-img class like below
.cont-img {
width:400px;
height:400px;
position: relative;
}
Add a height:400px to .cont-img class. Then you can use absolute position and css transform to centralize your content as seen in https://css-tricks.com/centering-percentage-widthheight-elements/ .
.centered-items {
position: absolute;
left: 50%;
top: 50%;
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
Check out this JSfiddle for more. I also added display flex css in the fiddle in case you changed your mind.
For more information on css flexbox, you could visit https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Related
I am trying to align 3 boxes and 2 images.
2 boxes vertically with there is an img between
3rd box standing right of them with an arrow img pointing it like this:
Example box css:
.box{
width: 80px;
height: 80px;
background: #8C8C8C;
margin:0 auto;
border: solid 3px #8B0000;
}
Use flexbox and divide to 3 section(3 divs) inside the wrap div
.wrap{
display:flex;
}
.box{
width: 80px;
height: 80px;
background: #8C8C8C;
margin:0 auto;
border: solid 3px #8B0000;
}
.img1 img{
width: 80px;
height: 150px;
}
.part2 img{
width: 150px;
height: 80px;
}
.part2,.part3{
margin-top: 100px;
}
.part3 .box{
margin-left: 40px;
}
<div class="wrap">
<div class="part1">
<div class="box">
</div>
<div class="img1">
<img src="https://material.angular.io/assets/img/examples/shiba1.jpg"/>
</div>
<div class="box">
</div>
</div>
<div class="part2">
<img src="https://material.angular.io/assets/img/examples/shiba1.jpg"/>
</div>
<div class="part3">
<div class="box">
</div>
</div>
</div>
If you have constant number of elements. (3 boxes and 2 images with fixed width and height), you can create wrapper box with relative position, then add position absolute to child nodes and write needed positions. for example:
.wrapper {
width: 100%;
box-sizing: border-box;
position: relative;
}
.child-arrow {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
You can use flexbox to position your boxes by rows and columns.
#cont1 {
display:flex;
flex-direction:row;
}
.box{
width: 80px;
height: 80px;
background: #8C8C8C;
border: solid 3px #8B0000;
}
<div>
<div class="box">Box 2
</div>
<div id="cont1">
<div> IMG1 </div>
<div> IMG2 </div>
<div class="box">Box 3</div>
</div>
<div class="box">
Box 1
</div>
</div>
You should try to use the float/clear attribute:
.box1{
float: left;
}
.box2{
float: right;
}
.image{
float: left;
}
.box3{
float: left;
}
Afterwards just use position: relative within your classes fix the positioning.
I have the following HTML and CSS. As you can see I have my main div divided into two parts of 70 and 30%. Also, the inner divs have a display property set to table cell. On the right inner div, I have image slides which don't fit the div 100% both vertically and horizontally. I would also want to display the top-layer text on hovering on the images. How do I fit the images to the available space? How do I display the text exactly at the center of the images on image hover?
HTML:
<div id="investment">
<div id="left ">
</div>
<div id="right">
<img class="slides" src="..../img01.jpeg">
<img class="slides" src="..../img02.jpeg">
<img class="slides" src="..../img03.jpeg">
<p id="top-layer">text here</p>
</div>
</div>
CSS :
#left {
width: 70%;
display: table-cell;
vertical-align: middle;
}
#right {
position: relative;
width: 30%;
display: table-cell;
vertical-align: middle;
}
#right #top-layer{
position: absolute;
top: 0;
left: 0;
display: table-cell;
vertical-align: middle;
}
I think you are looking for following solution.
May be it will halp you!
You are using table-cell but table-cell property doen't work untill it's parent don't have table property.
Horizontally align -
#investment {
display: table;
width: 100%;
}
#left {
width: 70%;
display: table-cell;
vertical-align: middle;
}
.slide {
display: table;
}
#right {
position: relative;
width: 30%;
display: table-cell;
vertical-align: middle;
}
#right img {
max-width: 100%;
}
.imageBlock {
position: relative;
display: table-cell;
}
.imageBlock p {
position: absolute;
opacity: 0;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
margin:0;
}
.imageBlock:hover p {
opacity: 1;
}
#right #top-layer {
position: absolute;
opacity: 0;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: table-cell;
vertical-align: middle;
}
#right:hover #top-layer {
position: absolute;
opacity: 1;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: table-cell;
vertical-align: middle;
}
<div id="investment">
<div id="left ">
THIS IS BLANK SPACE AND WIDTH = 70%
</div>
<div id="right">
<div class="slide">
<div class="imageBlock">
<img class="slides" src="https://dummyimage.com/300x200/ff00ee/ff00ee">
<p>HOVER TEXT</p>
</div>
<div class="imageBlock">
<img class="slides" src="https://dummyimage.com/300x200/ffeerr/ffeerr">
<p>HOVER TEXT</p>
</div>
<div class="imageBlock">
<img class="slides" src="https://dummyimage.com/300x200/dddddd/dddddd">
<p>HOVER TEXT</p>
</div>
</div>
</div>
</div>
Vertically align -
#investment {
display:table;
width:100%;
}
#left {
width: 70%;
display: table-cell;
vertical-align: middle;
}
#right {
position: relative;
width: 30%;
display: table-cell;
vertical-align: middle;
}
#right img {
min-width:100%;
}
.imageBlock {
position:relative;
}
.imageBlock p {
position:absolute;
opacity:0;
top:50%;
left:50%;
transform:translate(-50%,-50%);
margin:0;
}
.imageBlock:hover p {
opacity:1;
}
#right #top-layer {
position: absolute;
opacity:0;
top: 50%;
left: 50%;
transform:translate(-50%,-50%);
display: table-cell;
vertical-align: middle;
}
#right:hover #top-layer{
position: absolute;
opacity:1;
top: 50%;
left: 50%;
transform:translate(-50%,-50%);
display: table-cell;
vertical-align: middle;
}
<div id="investment">
<div id="left ">
THIS IS BLANK SPACE AND WIDTH = 70%
</div>
<div id="right">
<div class="imageBlock">
<img class="slides" src="https://dummyimage.com/300x200/ff00ee/ff00ee">
<p>HOVER TEXT</p>
</div>
<div class="imageBlock">
<img class="slides" src="https://dummyimage.com/300x200/ffeerr/ffeerr">
<p>HOVER TEXT</p>
</div>
<div class="imageBlock">
<img class="slides" src="https://dummyimage.com/300x200/dddddd/dddddd">
<p>HOVER TEXT</p>
</div>
</div>
</div>
I am trying to get blue container in the middle of pink one, however seems vertical-align: middle; doesn't do the job in that case.
<div style="display: block; position: absolute; left: 50px; top: 50px;">
<div style="text-align: left; position: absolute;height: 56px;vertical-align: middle;background-color: pink;">
<div style="background-color: lightblue;">test</div>
</div>
</div>
Result:
Expectation:
Please suggest how can I achieve that.
Jsfiddle
First of all note that vertical-align is only applicable to table cells and inline-level elements.
There are couple of ways to achieve vertical alignments which may or may not meet your needs. However I'll show you two methods from my favorites:
1. Using transform and top
.valign {
position: relative;
top: 50%;
transform: translateY(-50%);
/* vendor prefixes omitted due to brevity */
}
<div style="position: absolute; left: 50px; top: 50px;">
<div style="text-align: left; position: absolute;height: 56px;background-color: pink;">
<div class="valign" style="background-color: lightblue;">test</div>
</div>
</div>
The key point is that a percentage value on top is relative to the height of the containing block; While a percentage value on transforms is relative to the size of the box itself (the bounding box).
If you experience font rendering issues (blurry font), the fix is to add perspective(1px) to the transform declaration so it becomes:
transform: perspective(1px) translateY(-50%);
It's worth noting that CSS transform is supported in IE9+.
2. Using inline-block (pseudo-)elements
In this method, we have two sibling inline-block elements which are aligned vertically at the middle by vertical-align: middle declaration.
One of them has a height of 100% of its parent and the other is our desired element whose we wanted to align it at the middle.
.parent {
text-align: left;
position: absolute;
height: 56px;
background-color: pink;
white-space: nowrap;
font-size: 0; /* remove the gap between inline level elements */
}
.dummy-child { height: 100%; }
.valign {
font-size: 16px; /* re-set the font-size */
}
.dummy-child, .valign {
display: inline-block;
vertical-align: middle;
}
<div style="position: absolute; left: 50px; top: 50px;">
<div class="parent">
<div class="dummy-child"></div>
<div class="valign" style="background-color: lightblue;">test</div>
</div>
</div>
Finally, we should use one of the available methods to remove the gap between inline-level elements.
use this :
.Absolute-Center {
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
refer this link: https://www.smashingmagazine.com/2013/08/absolute-horizontal-vertical-centering-css/
Use flex blox in your absoutely positioned div to center its content.
See example https://plnkr.co/edit/wJIX2NpbNhO34X68ZyoY?p=preview
.some-absolute-div {
display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -moz-flex;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
-moz-justify-content: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
-moz-align-items: center;
align-items: center;
}
Center vertically and horizontally:
.parent{
height: 100%;
position: absolute;
width: 100%;
top: 0;
left: 0;
}
.c{
position: absolute;
top: 50%;
left: 0;
right: 0;
transform: translateY(-50%);
}
For only vertical center
<div style="text-align: left; position: relative;height: 56px;background-color: pink;">
<div style="background-color: lightblue;position:absolute;top:50%; transform: translateY(-50%);">test</div>
</div>
I always do like this, it's a very short and easy code to center both horizontally and vertically
.center{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="center">Hello Centered World!</div>
EDIT: 10/22 as nowdays, display flex or grid is widely implemented i would suggest to use one or the other (display:table/table-cell will still work if you need compatibility with old or exotic browsers , like my TV...)
flex
.a{
position: absolute;
left: 50px;
top: 50px;
}
.b{
display:flex;
align-items:center;
background-color: pink;
height: 56px;
}
.c {
background-color: lightblue;
}
/* move the flex demo aside */
.a.b{left:100px}
You even need less markup
<div class="a">
<div class="b">
<div class="c">test</div>
</div>
</div>
<div class="a b">
<div class="c">test</div>
</div>
grid (similar at that point)
.a{
position: absolute;
left: 50px;
top: 50px;
}
.b{
display:grid;
align-items:center;
background-color: pink;
height: 56px;
}
.c {
background-color: lightblue;
}
/* move the grid demo aside */
.a.b{left:100px}
You even need less markup
<div class="a">
<div class="b">
<div class="c">test</div>
</div>
</div>
<div class="a b">
<div class="c">test</div>
</div>
Original answer 02/2015 (still efficient everywhere) use with very old or exotic browsers not implementing yet flex or grid
You may use display:table/table-cell;
.a{
position: absolute;
left: 50px;
top: 50px;
display:table;
}
.b{
text-align: left;
display:table-cell;
height: 56px;
vertical-align: middle;
background-color: pink;
}
.c {
background-color: lightblue;
}
<div class="a">
<div class="b">
<div class="c" >test</div>
</div>
</div>
Here is simple way using Top object.
eg: If absolute element size is 60px.
.absolute-element {
position:absolute;
height:60px;
top: calc(50% - 60px);
}
An additional simple solution
HTML:
<div id="d1">
<div id="d2">
Text
</div>
</div>
CSS:
#d1{
position:absolute;
top:100px;left:100px;
}
#d2{
border:1px solid black;
height:50px; width:50px;
display:table-cell;
vertical-align:middle;
text-align:center;
}
You can do it by using display:table; in parent div and display: table-cell; vertical-align: middle; in child div
<div style="display:table;">
<div style="text-align: left; height: 56px; background-color: pink; display: table-cell; vertical-align: middle;">
<div style="background-color: lightblue; ">test</div>
</div>
</div>
I am using some CSS classes to manipulate my validation summary. Currently, I only show the first error. I wish to align this error inside its div. However my vertical-align properties seem to be having no effect. I can see that they are applied when I inspect the element with firebug in Firefox, yet they don't render as vertically aligned (they sit at the top). The other elements of the classes render correctly.
My div
<div style ="float: left; max-width: 200px; height: 75px; vertical-align:middle">
#Html.ValidationSummary(false)
</div>
My CSS classes
.validation-summary-errors li {
color: #b94a48;
vertical-align: middle;
}
.validation-summary-errors ul li:nth-child(n+2) {
display: none;
}
JSfiddle: http://jsfiddle.net/9ejZz/1/
vertical-align only apply to cell elements such as <td> or elements that have the css rule display:table-cell
See this example : http://jsfiddle.net/V9by8/
You should provide a jsfiddle for your problem, I could help you center it
UPDATE
Updataed with your code http://jsfiddle.net/9ejZz/6/
Six methods for centering something vertically. Pick your poison. This question seriously comes up once a week...
http://www.vanseodesign.com/css/vertical-centering/
Line-height
<div id="parent">
<img src="image.png" alt="" />
</div>
#parent {
line-height: 200px;
}
#parent img {
vertical-align: middle;
}
Table
<div id="parent">
<div id="child">Content here</div>
</div>
#parent {display: table;}
#child {
display: table-cell;
vertical-align: middle;
}
Negative Margins
<div id="parent">
<div id="child">Content here</div>
</div>
#parent {position: relative;}
#child {
position: absolute;
top: 50%;
left: 50%;
height: 30%;
width: 50%;
margin: -15% 0 0 -25%;
}
Stretching
<div id="parent">
<div id="child">Content here</div>
</div>
#parent {position: relative;}
#child {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 50%;
height: 30%;
margin: auto;
}
Equal Padding
<div id="parent">
<div id="child">Content here</div>
</div>
#parent {
padding: 5% 0;
}
#child {
padding: 10% 0;
}
Floater Div
<div id="parent">
<div id="floater"></div>
<div id="child">Content here</div>
</div>
#parent {height: 250px;}
#floater {
float: left;
height: 50%;
width: 100%;
margin-bottom: -50px;
}
#child {
clear: both;
height: 100px;
}
I know there are a ton of posts about this issue. After reading all them I feel like I am close, but it still isn't working for me.
HTML:
<div class="product">
<div class="image">
<a href="#">
<img src="http://i.imgur.com/rthFtAb.jpg" />
</a>
</div>
</div>
CSS
.product {
height:225px;
min-height:225px;
max-width:220px;
background-color:#ff00ff;
}
.image {
min-height:225px;
display:table-cell;
vertical-align:middle;
}
.image img {
max-width:100%;
}
http://jsfiddle.net/SBqU5/
What am I doing wrong here?
Six methods for centering something vertically.Pick your poison. Your method would fall under the "Table" option.
http://www.vanseodesign.com/css/vertical-centering/
Line-height
<div id="parent">
<img src="image.png" alt="" />
</div>
#parent {
line-height: 200px;
}
#parent img {
vertical-align: middle;
}
Table
<div id="parent">
<div id="child">Content here</div>
</div>
#parent {display: table;}
#child {
display: table-cell;
vertical-align: middle;
}
Negative Margins
<div id="parent">
<div id="child">Content here</div>
</div>
#parent {position: relative;}
#child {
position: absolute;
top: 50%;
left: 50%;
height: 30%;
width: 50%;
margin: -15% 0 0 -25%;
}
Stretching
<div id="parent">
<div id="child">Content here</div>
</div>
#parent {position: relative;}
#child {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
width: 50%;
height: 30%;
margin: auto;
}
Equal Padding
<div id="parent">
<div id="child">Content here</div>
</div>
#parent {
padding: 5% 0;
}
#child {
padding: 10% 0;
}
Floater Div
<div id="parent">
<div id="floater"></div>
<div id="child">Content here</div>
</div>
#parent {height: 250px;}
#floater {
float: left;
height: 50%;
width: 100%;
margin-bottom: -50px;
}
#child {
clear: both;
height: 100px;
}
if you use display:table-cell; and max-width; parent should be display:table; table-layout:fixed and width:xxpx. DEMO
.product {
height:225px;
width:220px;
background-color:#ff00ff;
display:table;
table-layout:fixed;
}
.image {
display:table-cell;
vertical-align:middle;
}
.image img {
max-width:100%;
}
If you are able to define a width and height to your image, you can use `position: absolute'.
.image {
position: relative;
height: 100%;
}
.image img {
width: 220px;
height: 105px;
position: absolute;
top: 50%;
left: 50%;
margin-left: -110px;
margin-top: -52px;
}
DEMO
Note that the negative left and top margins are half of their width and height, respectively.