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.
Related
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>
Hi I haven't got the right concept about center div in nested divs.Here is my html .I want to center white background div.
<div class="div1">
<div class="img"></div>
</div>
<div class="div2">
</div>
and CSS
.div1{
width :100%;
height: 300px;
background-color: black;
position: relative;
}
.img{
width: 100px;
height: 100px;
position: absolute;
background-color : white;
display: block;
margin: auto;
bottom: 0%;
margin-bottom: -50px;
text-align: center;
}
.div2{
width :100%;
height: 300px;
background-color: #e65100;
}
I tried to center div using text-align:center but it was not working.
and the output is here
.img {
...
left: 0;
right: 0;
}
fiddle: https://jsfiddle.net/ep9co5g5/
You can use left and translateX to center it since you are using position absolute,
you need to add this css to .img
left:50%;
transform:translateX(-50%);
see code snippet:
.div1{
width :100%;
height: 300px;
background-color: black;
position: relative;
}
.img{
width: 100px;
height: 100px;
position: absolute;
background-color : white;
bottom: 0%;
margin-bottom: -50px;
left:50%;
transform:translateX(-50%);
}
.div2{
width :100%;
height: 300px;
background-color: #e65100;
}
<div class="div1">
<div class="img"></div>
</div>
<div class="div2">
</div>
.div1{
width :100%;
height: 300px;
background-color: black;
position: relative;
}
.img{
width: 100px;
height: 100px;
position: absolute;
background-color : white;
display: block;
margin: auto;
bottom: 0%;
margin-bottom: -50px;
text-align: center;
left:0;
right:0;
}
.div2{
width :100%;
height: 300px;
background-color: #e65100;
}
<div class="div1">
<div class="img"></div>
</div>
<div class="div2">
</div>
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/
I have to make something like columns, but without table. This is example code:
<div class="main">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
<div class="endfloat"></div>
</div>
.left is on a left side, .center is in the middle and .right should be on a right side. But, .center should be also vertically aligned to the middle. Here is example and CSS example:
jsFiddle
Wrap the actual elements is a table-cell:
HTML
<div class="main">
<div class="table-cell">
<div class="left"></div>
</div>
<div class="table-cell">
<div class="center"></div>
</div>
<div class="table-cell">
<div class="right"></div>
</div>
</div>
SCSS
#mixin defaultDiv($bg, $height: 300px) {
width: 200px;
height: $height;
background-color: $bg;
display: table-cell;
}
.main {
outline: 1px solid red;
width: 600px;
display: table;
.table-cell {
display: table-cell;
width: 200px;
vertical-align: middle;
}
.left {
#include defaultDiv(green);
}
.center {
#include defaultDiv(blue, 200px);
}
.right {
#include defaultDiv(yellow, 250px);
}
}
JSFiddle demo: http://jsfiddle.net/3728vxa9/2/
Depending on if the height of the center element is in pixels or percent, you can place a div on top and below it. For instance, if it's height is 50 percent, place a div above and below it, each with a height of 25 percent.
HTML will look like this
<div class="main">
<div class="left"></div>
<div class="centerTop"></div>
<div class="center"></div>
<div class="centerBottom"></div>
<div class="right"></div>
<div class="endfloat"></div>
</div>
CSS will look like this
.centerTop {
height: 25%
}
.center {
height: 50%
}
.centerBottom {
height: 25%
}
Here are two examples of ways in which you could align a div in the middle:
Using HTML:
<div class="center" style="margin: 0 auto;"></div>
Styling in a separate CSS file:
.center { margin: 0 auto; }
If you are making three columns and want them to resize according to the window width, you set the value of their width to be 33%. Here is an example:
.center {
width: 33%;
}
.left {
width: 33%;
}
.right {
width: 33%;
}
Please see this link,
http://jsfiddle.net/n6t3qrux/
#mixin defaultDiv($bg, $height: 300px) {
float: left;
width: 200px;
height: 300px;
background-color: $bg;
}
.main {
outline: 1px solid red;
width: 600px;
height: 300px;
position: relative;
.left {
#include defaultDiv(green);
margin: auto;
position: absolute;
left: 0;
top: 0;
}
.center {
#include defaultDiv(blue, 200px);
margin: auto;
position: absolute;
left:0;
right: 0;
top: 0;
bottom: 0;
height: 200px;
}
.right {
#include defaultDiv(yellow, 250px);
margin: auto;
position: absolute;
right: 0;
top: 0;
}
.endfloat {
clear: both;
}
}
I wish this help you
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;
}