I have a main div (#homeGallery), in which i have a span(.imgClass) that is used to load one of a list of images.
I need the image to be centered not only vertically but horizontally in the div.
So far I have this code.
#homeGallery > .imgClass{
margin:auto;
position: relative;
top: 0;
bottom: 0;
display: block;
left: 0;
right: 0;
}
and
#homeGallery > .imgClass > img {
margin:auto;
float:center;
max-width:60%;
max-height:99%;
border: 2px solid;
}
Any help would be appreciated
This is a jewel I found recently. Use position: absolute with a top, left, bottom and right. You can center your span horizontally and vertically.
HTML:
<div class="wrapper">
<span class="image"></span>
</div>
CSS:
.wrapper {
width:400px;
height: 400px;
position: relative;
background-color: #afafaf;
}
.wrapper .image {
position: absolute;
top: 25%;
left: 25%;
right: 25%;
bottom: 25%;
background-color: #000;
}
http://jsfiddle.net/QTDrm/
You can try this code:-
#homeGallery > .imgClass > img {
position:absolute;
left:0;
right:0;
top:0;
bottom:0;
margin:auto;
max-width:100%;
max-height:100%;
overflow:auto;
}
You can try the following:
#homeGallery > .imgClass > img {
margin:0px auto;
display:block;
max-width:60%;
max-height:99%;
border: 2px solid;
}
Here's a Fiddle
#homeGallery .imgClass {
position: relative;
width: 200px;
height: 200px;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -100px;
}
If you dont know the image width & height than you could use jQuery solution
$(function() {
var imgW = $('.imgClass').outerWidth(),
imgH = $('.imgClass').outerHeight();
$('.imgClass').css({ marginLeft: - imgW / 2 + 'px', marginTop: - imgH / 2 + 'px' });
});
and this CSS
#homeGallery .imgClass {
position: relative;
top: 50%;
left: 50%;
}
This is my prefered method:
HTML
<div id="homeGallery">
<span class="imgClass">
<span class="fakeImg">You can use whatever img you want here</span>
</span>
</div>
CSS
#homeGallery{
height: 400px;
border: 1px solid #333;
text-align: center;
}
#homeGallery:before{
content: '';
height: 100%;
vertical-align: middle;
display: inline-block;
}
.imgClass{
display: inline-block;
vertical-align: middle;
text-align: left;
background-color: blue;
}
jsfiddle here.
The good side is that this is 100% css-based vertical alignment. You don't have to worry about screen size or DOM size change.
The cons is that it doesn't work on IE7 or lower.
If you want to both vertically and horizontally center an element, you should have a look at this approach:
jsFiddle
It works in all current browsers and IE8+.
HTML
<div>
<span class="element"></span> <!-- This can be any element -->
</div>
CSS
html, body {
width: 100%;
height: 100%;
display: table;
}
body > div {
display: table-cell;
vertical-align: middle;
}
body > div > .element {
display: block;
margin: 0px auto;
}
In your specific case with an img inside a span inside a div, I would solve it this way using the approach I have outlined above: jsFiddle
Note that I had to change some CSS classes to get it to work nicely with the image inside the span. I have set text-align: center on the div and display: inline-block; on the span. Below I have inserted the full classes which I have had to change to make it work for your situation.
CSS
body > div {
display: table-cell;
vertical-align: middle;
text-align: center;
}
body > div > .element {
display: inline-block;
}
Related
which is the style to apply on a div to make component (button) centered in the botton without knowing the size of the remaining space that will take the div and the size of the button because this style will be generic.
I used this style but it didn't work for me:
<div location="buttonLayout" style="display:flex; justify-content:center; align-items:flex-end ;"></div>
The button is centered but not placed in the bottom of the remainig space of the parent div.
You could use absolute positioning to get button at the bottom middle:
.parent {
background: gold;
position: relative;
width: 300px;
height: 400px;
box-sizing: border-box;
padding-bottom: 48px; // Padding + button height
}
.parent button {
background: grey;
border: none;
height: 32px;
color: #fff;
position: absolute;
bottom: 8px;
left: 50%;
transform: translateX(-50%);
}
<div class="parent">
<button>Button any size</button>
</div>
Just add bottom: 0; to the button.
If you have the following HTML:
<div id="flexItem" location="buttonLayout">
<button id="bottomButton" type="button">Click Me!</button>
</div>
You can use this CSS:
html, body{
height: 100%;
width: 100%;
}
body{
margin: 0;
}
#flexItem{
height: 100%;
width: 100%;
display: flex;
justify-content:center;
align-items:flex-end ;
}
#bottomButton{
bottom: 0;
}
JSFiddle.
Remember not to put inline CSS.
try this
.mybutton {
position: absolute;
bottom: 0px;
margin-left: auto;
margin-right: auto;
}
For this to work the parent container also has to have a position setting other than the default (static), for example position: relative;
I try to align a div at the bottom of a parent div. I tried the following:
JS Fiddle
I thought it would work with:
.bottom-menu {
position: absolute;
bottom: 0;
height: 50px;
background-color: yellow;
}
But as you can see in the fiddle it does not work. How can i do it?
You need to give a width to it as well. also you should use position: relative on the parent (the default is static). See updated fiddle: http://jsfiddle.net/wuf0m41z/1/
You could also specify right and left properties, and set them to 0, instead of setting width exclusively:
HTML:
<div class="container">
<div class="top-menu"></div>
<div class="bottom-menu"></div>
</div>
CSS:
.container {
height: 400px;
background-color: green;
position:relative;
}
.top-menu, .bottom-menu {
height: 50px;
background-color: yellow;
}
.bottom-menu {
position: absolute;
bottom: 0;
right:0;
left:0;
}
JSFiddle
just add the position:relative; to your container and don't forgetto add a width:100%; to your bottom menu.
here is an example:
http://jsfiddle.net/leojavier/wuf0m41z/10/
Try it !!!
.container {
height: 400px;
position: relative;
background-color: green;}
.top-menu {
height: 50px;
background-color: yellow;}
.bottom-menu {
height: 50px;
background-color: yellow;
position:absolute;
bottom:0;
width:100%;}
Your code is correct now just have to add width.
.bottom-menu {
position: absolute;
bottom: 0;
height: 50px;
background-color: yellow;
width:100%;
}
I have two divs that I am trying to stack over each other but the one I want on top is not showing. I want the blue background div to lay on top of the red background div. Any advice? The reason why I want to overlay the blue div is because the container is a centered grid and I want the red div to be the background for the first half of the page.
JSFIDDLE
CSS
.buddy {
width: 50%;
height: 629px;
display: inline-block;
position: relative;
background: red;
}
.buddy-content {
position: absolute;
top: -629px;
z-index: 10;
background: blue;
}
.container {
max-width: 1000px;
margin: 0 auto;
overflow:hidden;
position:relative;
padding: 0 10px;}
You have made the second div absolute so you don't need to give the negative value for top. The second div is hiding because you top -629px; Try making the top:0 and see. And also for your current code. Remove the overflow hidden and put z-index like this:
.buddy {
width: 50%;
height: 629px;
display: inline-block;
position: relative;
z-index:9;
background: red;
}
.buddy-content {
position: absolute;
top: -629px;
z-index: 10;
background: blue;
}
.container {
max-width: 1000px;
margin: 0 auto;
width:200px;
height:200px;
position:relative;
padding: 0 10px;
}
.buddy {
width: 50%;
height: 629px;
display: inline-block;
position: absolute;
background: red;
}
.buddy-content {
position: absolute;
z-index: 10;
background: blue;
}
<div class="buddy BlueGradient">
</div>
<div class="container">
<div class="buddy-content">
ROGER
</div>
</div>
https://jsfiddle.net/kt77cp3e/6/
just add z-index : higher to the div that you want to show on top and set z-index low to the other one ..
ant one thing your code is working good just you need to remove " top : -629px;"
that thing is not allowing blue div to be on top just it is showing at the -629 px position..!!!!
If you can update your code like this, it may solve the issue:
Demo:https://jsfiddle.net/kt77cp3e/7/
CSS:
html, body {
height:100%;
width:100%:
}
.container {
width:50%;
height:100%;
background:red;
position:relative;
}
.container>div {
position:relative;
left:0;
right:0;
}
.container>div:first-child {
top:0;
height:50%;
background:blue
}
.container>div:last-child {
bottom:0;
height:50%;
background:green
}
HTML:
<div class="container">
<div></div>
<div></div>
</div>
Update: Considering the latest updated code, I think you should remove overflow:hidden from the container styles. That should do the trick
You should set the dimension on the .container div.
CSS:
.container {
position:relative;
width:100px; //You may modify these values
height:100px
}
Demo: https://jsfiddle.net/kt77cp3e/1/
.buddy { width: 50%; height: 629px; display: inline-block; position: relative; background: red;}
.buddy-content { position: absolute; top: 0px; z-index: 10; background: blue; }
.container {max-width: 1000px; margin: 0 auto; overflow:hidden; position:relative; padding: 0 10px; position: relative;}
<div class="container">
<div class="buddy BlueGradient">
<div class="buddy-content">ROGER</div>
</div>
</div>
This brings the text "Roger" with blue background on top of the red background
I am trying to center an image inside a div in the exact middle - horizontally and vertically, but this is not working for me:
#col_as_table
{
float:left;
position:relative;
width:14%;
min-height:100px;
height:auto;
display: table;
display: table-cell;
text-align: center;
vertical-align: middle;
}
HTML
<div id="col_as_table"><img src="test.png"></div>
Give this a shot. You'll need an extra div.
Fiddle!
HTML:
<div class="divTable">
<div class="innerDivTable">
<img src="test.png"/>
</div>
</div>
CSS:
.divTable {
width: 200px; /* purely for demonstration */
height: 200px; /* purely for demonstration */
display: table;
border: 1px solid black;
}
.innerDivTable {
display: table-cell;
vertical-align: middle;
text-align: center;
}
Fiddle: http://jsfiddle.net/4mYj3/1/
CSS:
#col_as_table
{
position:relative;
margin: auto;
text-align: center;
vertical-align: middle;
display: table;
height:500px;
}
img{
position: absolute;
top: 50%;
left: 0;
bottom: 0;
right: 0;
}
I wrote a Pen some time ago for the same purpose. It shows 3 methods on how you can center an image vertically as well as horizontally.
I'd personally use absolute positioning or table cell depending upon the situation.
#div3{
display:table-cell;
vertical-align:middle;
}
#div1 img{
height:100px;
position:absolute;
top:50%;
left:50%;
-webkit-transform:translateY(-50%) translateX(-50%);
}
Here's a fiddle:
http://jsfiddle.net/C4DC7/
There are many ways to accomplish this. My example expands on the idea that you can afford your images to expand the parent div element.
How to make a button be at the bottom of div and at the center of it at the same time?
.Center {
width: 200px;
height: 200px;
background: #0088cc;
margin: auto;
padding: 2%;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
.btn-bot {
position: absolute;
bottom: 0px;
}
<div class=Center align='center'>
<button class='btn-bot'>Bottom button</button>
</div>
Give the parent element…
display: table; text-align: center;
…and its child element…
display: table-cell; vertical-align: bottom;
This way you do not need to know the width of the child element in case it changes, thus requiring no negative margins or workarounds.
For example write like this if you have position absolute:
.btn-bot{
position:absolute;
margin-left:-50px;
left:50%;
width:100px;
bottom:0px;
}
Note: give margin-left half of the width of the button.
JSFiddle demo
Does the button need to be absolutely positioned? If so, you could specify a width and then a negative left margin:
.btn-bot{
position: absolute;
left: 50%;
width: 100px;
margin-left: -50px;
bottom:0px;
}
fiddle
You can use display: table-cell and vertical-align: bottom to achieve this, although you need a container div set to display: table.
HTML
<div class="boxContainer">
<div class="box">
<button>Bottom button</button>
</div>
</div>
CSS
.boxContainer {
display: table;
}
.box {
width: 200px;
height: 200px;
background: #0088cc;
padding: 2%;
display: table-cell;
text-align: center;
vertical-align: bottom;
}
Demo
One way of doing this would be to give it an absolute width and then a margin half of that:
width: 250px;
margin-left: -125px;
Another way would be to give the div the following line-height and remove all styles from the button:
line-height: 398px;
I know this has been answered a long time ago, but I couldn't use display:table on the parent container so I had to find another way around.
It turned out that this worked just fine:
.container{
position:absolute;
}
.button{
position:absolute;
bottom: 5px;
left: 0%;
right: 0%;
text-align: center;
}
Edit: This works if your button is a <div>, not for an actual <button>
I used table-row to combine text and button in one container:
#out{
display:table;
position:absolute;
}
#out div#textContainer{
display:table-row;
}
#out div#text{
display:table-cell;
vertical-align:top;
}
#out div#buttonContainer{
display:table-row;
text-align:center;
}
#out div#button{
display:table-cell;
vertical-align:bottom;
}
CodePen
This is what worked for me and I think is what several people were trying to get at.
Just use a full width wrapper div with the button centered inside it.
<div class="outer">
<div class="inner-button-container">
The Button
</div>
</div>
.outer{
position:relative;
height:200px;
width:500px;
background-color:lightgreen;
}
.inner-button-container{
background-color:grey;
position:absolute;
bottom:0;
right:0;
left:0;
text-align:center;
}
a{
margin:auto;
background-color:white;
}
Result:
Fiddle
You can use align-items: flex-end like so:
html,
body {
height: 100%;
}
.flex-container {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.center {
width: 200px;
height: 200px;
background: #0088cc;
display: flex;
align-items: flex-end;
}
.btn-bot {
margin: 0 auto;
display: block;
}
<div class="flex-container">
<div class="center">
<button class="btn-bot">Bottom button</button>
</div>
</div>