How to center this container of unknown size? - html

I have the following CSS/Markup
http://jsfiddle.net/3SNm8/
.res-tile-wrap {padding-bottom:25px;}
.res-tile
{
border:solid 1px #CCC;
width:36px; height:34px; margin: 25px 0 0 25px; float:left; position:relative;
}
<div class="res-tile-wrap">
<div class="res-tile">1</div>
<div class="res-tile">2</div>
<div class="res-tile">...etc</div>
</div>
res-tile-wrap will contain an unknown number of tile divs. Is there a way to center res-tile-wrap or give the appearance that it is centered given that I do not want to give it a specific width because I want as many tiles per row as the screen will allow.

For the inner divs you can use display:inline-block instead of float:left and as inner content is behaving like an inline element you can center it in the container res-tile-wrap with text-align:center :
res-tile-wrap {
padding-bottom:25px;
text-align:center;
}
.res-tile{
border:solid 1px #CCC;
width:36px; height:34px; margin: 25px 0 0 25px;
display:inline-block;
}
Example with 4 inner divs
Example with 11 inner divs

.res-tile-wrap{text-align:center}
.res-tile
{
border:solid 1px #CCC;
width:36px; height:34px; margin: 25px 0 0 25px;
display:inline-block;
}
You don't need position:relative or float at all. Make tiles display: inline-block, so they will behave like "text" and text-align:center the container.

While #Noyulysses answer works well for centering along the X-axis (and I'm assuming that's what your question was asking about) it's nearly impossible to both vertically and horizontally center an element with an unspecified width and height without using JavaScript.
You can center something in the exact center of the screen with something like this:
jQuery.fn.center = function ()
{
this.css("position","fixed");
this.css("top", ($(window).height() / 2) - (this.outerHeight() / 2));
this.css("left", ($(window).width() / 2) - (this.outerWidth() / 2));
return this;
}
$('.downBig').center();
$(window).resize(function(){
$('.downBig').center();
});
I got this function from another SO answer somewhere... I can't find it at the moment but when I do I'll link to it.
DEMO

To center a div of an unknown width simply apply display: table to it, with margin: 0 auto.
DEMO http://jsfiddle.net/3SNm8/8/
.res-tile-wrap {
padding-bottom:25px;
display: table;
margin: 0 auto;
}

You have 2 easy ways :
1) display:table : DEMO
html{
display:table;
height:100%;
margin:auto;
}
body {
display:table-cell;
vertical-align:middle;
}
2) display:flex; : DEMO
html, body{
display:flex;
flex-direction:column;
height:100%;
align-items:center;
}
body > div {
margin:auto;
}

.res-tile-wrap
{
width: 100%;
margin-left:auto;
margin-right:auto;
text-align: center;
}
hand type: untested

Another option http://jsfiddle.net/3SNm8/12/
body {
text-align:center;
}
.res-tile-wrap {
padding-bottom:25px;
margin:0 auto;
display:inline-block;
}

Related

remove gap between div?

I have a question, I tried to search on google but unluckily I didn't find any answer...
So i want to remove gap between DIVs.
I have a small PHP application which shows users images in a div with some other info, DIV width is 250px and DIV height is auto because I don't know the length of content and I also didn't know the number of the DIV because i fetch data from MYSQL.
Here is a screen-shoot of my web page:
And my code is:
<style>
#det
{
width:250;
height:auto;
max-height:400px;
border-bottom:3px solid darkred;
background-color:white;
float:left;
padding:0px;
margin:0 auto;
border-radius:5px;
}
HTML
<div id='det'>
<img src='$user_image'><p>some text</p>
</div>
I want to remove gap between DIV and show it how a magazine-style look like. Example: masonry.desandro.com. Currently i used float:left but it's not working properly and maybe because I didn't set height. Is there anyway to solve this without set div height?
Thanks
Use flex fluid layout for showing variable height elements properly.
.container {
display: flex;
flex-wrap: wrap;
}
.item {
width: 179px;
flex-grow: 0;
flex-shrink: 0;
}
https://jsfiddle.net/w4pfxk2x/
You should use display:inline-block; instead of float:left; and your problem will be fixed.
.det{
width:250;
height:auto;
max-height:400px;
border-bottom:3px solid darkred;
background-color:white;
display:inline-block;
padding:0px;
margin:0 auto;
border-radius:5px;
vertical-align:top
}
https://jsfiddle.net/IA7medd/haqkxkmb/

Putting a button in center

I seem to have a difficulty in putting the button i made in the center of the screen. Here's the source code.
View Products
Here are the styles
/*header button styles*/
a.header-button {
display: inline-block;
background-color:#
}
a.header-button:hover {
color: #e8ddc8;
text-decoration: underline;
}
.header-button {
background-color:#031634;
color:#e8ddc8;
padding: 10px 5px 10px 5px;
border-radius: 5px;
text-align:center;
}
You see, even though I put text-align:center in the .header-button, the button does not position itself at the center. (See image below)
Can anyone help me?
Try this. Working Fiddle
a.header-button {
display: block;
background-color:#;
margin:0 auto;
width:100px;
}
One solution: http://jsfiddle.net/xzY5j/
css:
.header-button {
background-color:#031634;
color:#e8ddc8;
padding: 10px 5px 10px 5px;
border-radius: 5px;
}
.container {
text-align:center;
width: 100%;
padding: 50px;
background-color:#ccc;
}
}
HTML
<div class="container">
View Products
</div>
Other suggestion is:
Binita Tamang solution she wrote it before I had a chance so I will let her get the credit for it :)
You should give the container or the parent element text-align:center
jsfiddle
HTML
<div class="parent">
View Products
</div>
CSS:
.parent{
text-align:center;
}
If you want to put a button or any element on middle of the screen,then use position absolute an give the top 50% and left 50% now,give margin just half of its width and height in minus :
Something like this :
CSS
button{
position:absolute;
top:50%;
left:50%;
width:100px;
height:30px;
margin:-15px 0 0 -50px;
}
And if only middle from left then :
button{
position:absolute;
top:some_top_in_px;
left:50%;
width:100px;
height:30px;
margin:0px 0 0 -50px;
}
Fiddle DEMO

How to split page into 4 equal parts using two container divs?

I am designing a webpage that needs to be split into 4 equal DIVs. This would be easy if I didn't also need to overlap text onto two of these DIVs. So, I have decided the best route would be to stack two container DIVs on top of each other, each with a width of 100% and height of 50%. Then, I would split these into two DIV classes, each with a height of 100% width of 50%, thus giving me 2 DIVs per container DIV, which are 2 in number.
My current CSS:
#collectionsTop {
width: 100%;
height: 50%;
overflow:hidden;
margin: 0 auto;
}
.topRight {
background-color:red;
width:50%;
height:100%;
float:left;
clear:both;
display:inline-block;
overflow:hidden;
}
.topLeft {
background-color:blue;
width:50%;
height:100%;
float:left;
clear:both;
display:inline-block;
overflow:hidden;
}
#collectionsBottom {
width: 100%;
height: 50%;
overflow:hidden;
margin: 0 auto;
}
.bottomRight {
background-color:yellow;
width:50%;
height:100%;
float:left;
clear:both;
display:inline-block;
overflow:hidden;
}
.bottomLeft {
background-color:green;
width:50%;
height:100%;
float:left;
clear:both;
display:inline-block;
overflow:hidden;
}
And my HTML:
<div id="collectionsTop">
<div class="topRight"><img src="http://www.solomovies.ch/uploads/blog/lorem-ipsum-1440x900-text-on.jpg"></div>
<div class="topLeft"><img src="http://www.solomovies.ch/uploads/blog/lorem-ipsum-1440x900-text-on.jpg"></div>
</div>
<div id="collectionsBottom">
<div class="bottomRight"><img src="http://www.solomovies.ch/uploads/blog/lorem-ipsum-1440x900-text-on.jpg"></div>
<div class="bottomLeft"><img src="http://www.solomovies.ch/uploads/blog/lorem-ipsum-1440x900-text-on.jpg"></div>
</div>
Apparently, none of the above works in any capacity at all, displaying the images in their full resolution, not floated, and in no way limited by their parent DIVs. I have no idea why. Please help.
You have placed - clear:both in the css of topLeft , topRight elements
idea => clear:both; - No floating elements allowed on the left or the right side of a specified element ,
hence in your case also similar thing is happening,
check this fiddle : http://jsfiddle.net/4q4Jz/
update:
now check the fiddle.. demo
remove all the `clear:both;
and try it.
`

How to vertically and horizontally align image in div? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Image center align vertically and horizontally
I'm uploading images to a php file so I can resize them to a certain width or height, which ever is smaller. The image resizer I'm using is a class from phpclasses.org and it seems to be working pretty good. The problem is, not all images are the same size. Some are wide and narrow while some are almost square. So this is causing all the images to position on the top of my div and not the middle. My thought is to have CSS perfectly center the images so the different sizes look decent when viewed, but I can get them to center horizontally but not vertically. Here's a screenshot of what I"m trying to center:
As you can see the guns are aligned on top of the div? I can't use a hard coded figure to push it center since other images will have different heights. I need some way of determining the size of the image and figuring it from there.
Here's the code for the div's that the images are in:
#product {
float:left;
margin:5px;
width:200px;
height:200px;
border:1px solid #999;
}
#product-image {
margin:2px auto;
width:194px;
height:145px;
border:1px solid #999;
text-align:center;
}
Thanks for having a look.
#product
{
width: 200px;
height: 200px;
line-height: 200px;
text-align: center;
}
#product img
{
vertical-align: middle;
max-height: 200px;
max-width: 200px;
}
Max-Height / Max-Width is used to handle images that may be too large.
For dynamic vertical alignment where you cant use position absolute i suggest using javascript for an absolute fix across all situations but you can use several techniques seen in the following Guide for vertical alignment
$(document).ready(function(){
var image = ​$('#product-image')​​​​​​​​​;
var container = $(image).parent();
var margin = (container.height() - image.height()) / 2;
image.css('margin-top', margin);
});
with the following CSS
#product
{
float:left;
margin:5px;
width:200px;
height:200px;
border:1px solid #999;
text-align:center;
}
#product-image
{
margin:2px auto;
width:194px;
height:145px;
border:1px solid #999;
}​
<div id="product">
<div id="product-Inner">
<img id="product-image" src="imageURL" />
<div>
<div>
style css
#product {
float:left;
display: table;
box-sizing: border-box;
margin:5px;
width:200px;
height:200px;
border:1px solid #999;
text-align:center;
}
#product-Inner {
display: table-cell;
vertical-align: middle;
text-align:center;
}
#product-image {
box-sizing: border-box;
max-height: 100%;
max-width: 100%;
border:1px solid #999;
}
you can easily get your desired results through display:table-cell & vertical-align:middle;
CSS
#parent {
width:200px;
height:200px;
display:table;
background-color: black;
float:left;
margin:5px;
}
#parent-image {
display:table-cell;
vertical-align:middle;
text-align:center;
}
DEMO

creating two floating divs side by side with 100% height

I'm try to create a side by side div that have 100% height, i manage to get the first div working but the second one is cause problem, been trying to do this for the last 3 hours.
#mainWrapper{
width: 900px;
margin:0px auto;
background:#fff;
}
/*leftColumn */
.leftColumn {
float:left;
width:250px;
height:100%;
background:#fafafa;
border-left:solid 1px #dedede;
position:fixed;
top:0px;
}
/* Content */
.mainContent {
float: left;
width: 650px;
height:100%;
background:#fff;
margin-left:252px;
padding-bottom: 50px;
}
example of how it's supposed to look like
http://i49.tinypic.com/ycef7.jpg
how it looks like at the moment.(tried everything dunno how to fix it)
http://i49.tinypic.com/2ryk5eo.png
Rather than giving explicit height to both the inner divs floated left, you should use overflow:hidden; on parent div, e-g:
#mainWrapper{
width: 900px;
margin:0px auto;
background:#fff;
overflow:hidden;
}
Add
html, body {
height: 100%;
}
(http://jsfiddle.net/zYWjJ/2/)