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>
Related
I'm creating a grid type layout, the contents of which will be centered, like here.
.outer {
width: 100%;
height: 100px;
margin-top: 10px;
margin-bottom: 10px;
position: relative;
background: pink;
text-align: center;
}
.inner {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100%;
}
<div class="outer">
<div class="inner">
<h1>I'm Centered</h1>
</div>
</div>
I've used text-align: center; but there should be a better way to center the contents vertically too. My issue arises trying to do the same where two of these are next to each other with centered content, like this;
.outer {
width: 50%;
float: left;
position: relative;
background: pink;
}
#media only screen and (max-width: 500px) {
.outer {
width: 100%;
float: left;
position: relative;
background: pink;
}
}
.inner {
position: relative;
}
.inner-position {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100%;
}
<div class="outer">
<div class="inner">
<div class="inner-position">
<p>I should be centered</p>
</div>
</div>
</div>
<div class="outer">
<div class="inner">
<div class="inner-position">
<p>I should be centered</p>
</div>
</div>
</div>
It's looking even worse in a snippet for some reason but something like this would be desired;
I can get the column layout or I can center content. I need to be able to do both.
EDIT
.container {
width: 100%;
height: 500px;
background: pink;
margin-top: 10px;
margin-bottom: 10px;
}
.col {
width: 50%;
float: left;
position: relative;
}
#media only screen and (max-width: 500px) {
.col {
width: 100%;
float: left;
position: relative;
}
}
.inner {
position: relative;
}
.inner-details {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100%;
}
<div class="container">
<div class="col">
<div class="inner">
<div class="inner-details">
<h1>Middle 1</h1>
</div>
</div>
</div>
<div class="col">
<div class="inner">
<div class="inner-details">
<h1>Middle 2<h1>
</div>
</div>
</div>
</div>
To center items you can use display: flex on the container div and also use
align-items: center; // vertical
justify-content: center; // horizontal
To achieve the image you attached you don't need so many containers, this can be done simply like in this example:
.container {
width: 100%;
height: 300px;
margin-top: 10px;
margin-bottom: 10px;
display: flex;
flex-wrap: wrap;
}
#media only screen and (max-width: 500px) {
.inner-details {
width: 50%;
float: left;
position: relative;
}
}
.inner-details {
background: pink;
display: flex;
flex-grow: 1;
justify-content: center;
align-items: center;
margin: 10px;
}
<div class="container">
<div class="inner-details">
<h1>Middle 1</h1>
</div>
<div class="inner-details">
<h1>Middle 2</h1>
</div>
</div>
I hope this is your desire output. Please check the code snippets.
* {
box-sizing: border-box;
}
.outer {
width: 50%;
height: 100px;
float: left;
position: relative;
background: pink;
margin: 10px 0;
}
#media only screen and (max-width: 500px) {
.outer {
width: 100%;
}
}
.inner {
position: relative;
width: 100%;
height: 100%;
}
.inner-position {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
<div class="outer">
<div class="inner">
<div class="inner-position">
<p>I should be centered</p>
</div>
</div>
</div>
<div class="outer">
<div class="inner">
<div class="inner-position">
<p>I should be centered</p>
</div>
</div>
</div>
Using the example from the first snippet and wrapping that twice I've managed to get the desired effect, there's still the issue with having to use text-align to align horizontally but this is the closest I can get without using flex or box-sizing: border-box;. If there's a more appropriate way to do this an example would be appreciated.
.wrap {
width: 100%;
display: table;
}
.col {
width: 50%;
float: left;
}
#media only screen and (max-width: 500px) {
.col {
width: 100%;
float: left;
}
}
.outer {a
width: 100%;
height: 100px;
margin-top: 10px;
margin-bottom: 10px;
position: relative;
background: pink;
text-align: center;
}
.inner {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100%;
}
<div class="wrap">
<div class="col">
<div class="outer">
<div class="inner">
<h1>I'm Centered</h1>
</div>
</div>
</div>
<div class="col">
<div class="outer">
<div class="inner">
<h1>I'm Centered Too</h1>
</div>
</div>
</div>
</div>
Here is a content inside: <div class="traki"> </div>
I add following css:
.traki {margin-left: auto !important;
margin-right: auto !important;}
}
And goal is to set inside content center. but it doesn't apply, demo: http://buhehe.de/kalender-2018/
You cannot do an "auto" margin to vertically center an element.
There is a workaround though, you could try this:
.container {
display: table;
height: 100%;
position: absolute;
overflow: hidden;
width: 100%;
}
.helper {
#position: absolute;
#top: 50%;
display: table-cell;
vertical-align: middle;
}
.content {
#position: relative;
#top: -50%;
margin: 0 auto;
width: 200px;
border: 1px solid orange;
}
<div class="container">
<div class="helper">
<div class="content">
<p>stuff</p>
</div>
</div>
</div
In below code how to place text center to the border space just above it as illustrated in the screenshot below "Some Text 1" and "Some Text 2" are in the center to the border space above them.
.Row {
display: table;
width: 100%;
table-layout: fixed;
border-spacing: 10px;
}
.Column {
display: table-cell;
background-color: red;
}
.Column:nth-child(1) {
width:20%;
}
.Column:nth-child(2) {
width:50%;
}
.Column:nth-child(3) {
width:30%;
}
<div class="Row">
<div class="Column">C1</div>
<div class="Column">C2</div>
<div class="Column">C3</div>
</div>
You can achieve that with placing your text elements in the cells, setting them to position: absolute; and push them 50% of their own width out of the cell with transform: translate(50%, 0);.
Of course you'll need proper vendor prefixes to support older browsers.
.Row {
display: table;
width: 100%;
table-layout: fixed;
border-spacing: 10px;
}
.Column {
display: table-cell;
position: relative;
background-color: red;
}
.Column:nth-child(1) {
width:20%;
}
.Column:nth-child(2) {
width:50%;
}
.Column:nth-child(3) {
width:30%;
}
.Column > span {
position: absolute;
right: 0;
top: 1.5em;
transform: translate(50%, 0);
text-align: center;
}
<div class="Row">
<div class="Column">C1<span>Some Text 1</span></div>
<div class="Column">C2<span>Some Text 2</span></div>
<div class="Column">C3</div>
</div>
You could use pseudo-elements to add text and position it with position:absolute and transform: translate()
.Row {
display: table;
width: 100%;
table-layout: fixed;
border-spacing: 10px;
}
.Column {
display: table-cell;
background-color: red;
position: relative;
}
.Column:nth-child(1) {
width: 20%;
}
.Column:nth-child(2) {
width: 50%;
}
.Column:nth-child(3) {
width: 30%;
}
.Column:nth-child(1):after,
.Column:nth-child(2):after {
content: 'Some text 1';
position: absolute;
right: 0;
bottom: 0;
transform: translate(50%, 100%);
text-align: center;
}
.Column:nth-child(2):after {
content: 'Some text 2';
}
<div class="Row">
<div class="Column">C1</div>
<div class="Column">C2</div>
<div class="Column">C3</div>
</div>
Simple answer using table layout to keep it consistent with what your doing:
.Row {
display: table;
width: 100%;
table-layout: fixed;
border-spacing: 10px;
}
.Column {
display: table-cell;
background-color: red;
}
.Column:nth-child(1) {
width:20%;
}
.Column:nth-child(2) {
width:50%;
}
.Column:nth-child(3) {
width:30%;
}
.Row2 {
display: table;
width: 100%;
table-layout: fixed;
border-spacing: 10px;
}
.Column2 {
display: table-cell;
text-align:center;
width:40%;
}
.Column2:nth-child(2) {
width:60%;
}
<div class="Row">
<div class="Column">C1</div>
<div class="Column">C2</div>
<div class="Column">C3</div>
</div>
<div class="Row2">
<div class="Column2">Some Text 1</div>
<div class="Column2">Some Text 2</div>
</div>
Try with new markup with position: absolute;
.Row {
display: table;
width: 100%;
table-layout: fixed;
border-spacing: 10px;
}
.Column {
display: table-cell;
position: relative;
background-color: red;
}
.Column:nth-child(1) {
width:20%;
}
.Column:nth-child(2) {
width:50%;
}
.Column:nth-child(3) {
width:30%;
}
.Column > span {
position: absolute;
right: -45px;
top: 20px;
}
<div class="Row">
<div class="Column">C1<span>Some Text 1</span></div>
<div class="Column">C2<span>Some Text 2</span></div>
<div class="Column">C3</div>
</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 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.