How to center wrapper with floating content - css

I have a wrapper wrapping number of large 300 by 300 divs. when the browser is in full screen the wrapper sits in the center. But when I resize the browser the content flows to second the line, but the wrapper takes 100% of the window. For example the browser window width is 800px the content is 4 blocks 300px by 300px so two blocks together takes only 600px and the other two are in the second line. My problem is that wrapper stretches to 800px and the content sits to the left of the screen and i want it in the center (when I use float: left)
When I try the same using display: inline-block and text-align: centre, I can have the content centred in the screen even though the wrapper stretches to 100% of the width. Problem no1. with this solution is that is that if only one block is in the last row than it is centred right in the middle the second problem is mobile browser counts font-size: 0 somehow differently so it is a little misaligned.
simplified fiddle you can see that if the window is small the third orange box falls to second line but is centred. When I use text-align: left than all boxes are against left side leaving significant space on right.
here is the relevant CSS I have on site:
#site-wrapper {
min-height:100%;
text-align: center;
position:relative;
}
#container {
position:relative;
width: auto;
margin: 0 auto;
display: inline-block;
padding-bottom:40px; /* Height of the footer */
}
div#content-wrapper{
padding:5px 0 5px 0;
font-size: 0;
text-align: center;
margin-left: auto;
}
#column1, #column2, #column3 {
margin-right: 5px;
}
#column-last{
margin-right: 5px;
}
div.column {
//float: left;
display: inline-block;
vertical-align: top;
font-size: 0;
}
.item {
background: #0072C6;
width: 20em;
padding: 5px;
height: auto;
margin: 0 0 5px 0;
font-size: 14px;
color: #FFF;
box-shadow: inset 0 0 100px #1082D6;
}

Related

My dividers change size and move about oddly when the window is resized

Below is the main container, side and card containers for my site.
when I resize the window they move and resize themselves and stack on top of each other, I would like them to stay the same size and just have a horizontal scroll bar.
.main {
width: 75%;
}
.side {
background-color: #232323;
border-left: 4px solid #395d9e;
float: left;
width: 16%;
padding: 57px 15px;
height: 100%;
}
.card {
float: left;
background-color: #232323;
padding: 0 15px;
margin: 80px 0 0 20px;
padding: 15px;
width: 82%;
border-radius: 4px;
}
It would help to see your relevant html also, but you have a few things going on here. First, your total for .main, .side and .card total 173%. This might not matter if .card is inside .main or .side.
Next, consider using the css property box-sizing:border-box. In CSS, width is calculated as the sum of (element width + padding + border + margin). Using border-box, the padding and border are considered part of the total width. In other words, for .side, the 16% will include your 15px right and left padding.
Finally, even using border-box, your right and left margins affect total width. If both .card and .side are located inside .main, you could set your right margin for .card as a percent (e.g. margin: 80px 0 0 2%;) and a percentage for the .side element as well.
What is happening here is when you resize your browser window to a small width, the fixed padding and margin can force your elements to float below the others. Assuming .side and .card are inside .main, they have a total declared width of 98% + 80px. You would need (if this is the case) a width of 4000px to contain both .side and .card.
Try using box-sizing:border-box for all 3 elements, and use percentage based margin and possibly padding.

Stop floating div with "fixed" size from floating out of container

I have a <div> container with a certain max-width,
max-width: 300px;
margin: 4em auto;
border: 1px solid black;
that contains two things: 1) text, 2) a floating div with
float: right;
width: 150px;
See my example at http://jsfiddle.net/uXEBR/.
When you reduce the window’s width, the floating <div>, as expected, extends outside the containing <div>, getting beyond its left border. However, I would like it to decrease in width so that it never leaves the outer <div>’s border. In other words, the width specification of the floating element should be conditional on sufficient width of the outer div. Is there a way to achieve this in CSS?
The other option is a media query. Here is an example based on the code you supplied. Keep in mind that .divright element will only shrink as small as the longest word in the div.
>>jsFiddle<<
CSS:
.divout {
max-width: 300px;
margin: 4em auto;
border: 1px solid black;
}
.divright {
float: right;
max-width: 150px;
margin-right: 1.25em;
border: 1px solid black;
}
#media screen and (max-width: 225px) {
.divright {
margin-left: 1.25em;
}
}
Sure:
width: 50%;
max-width: 150px;

I can set images to right side using align right, but not with floats?

#sponsors {
float: right;
display: inline;
width: 728px;
height: 100px;
margin: 60px 11px 0;
}
<div id="sponsors">
<img src="images/sponsors/1.png">
<img src="images/sponsors/2.png">
</div>
I can't move images to the right side of div with this, but div align="right" works.
How can I set images to right side using css3?
Change the display to block and add text-align:right
#sponsors {
float: right;
display: block; /* or remove this line, as block is default for div */
width: 728px;
height: 100px;
margin: 60px 11px 0;
text-align: right;
}
Display inline doesn't make much sense on elements with a width and a height sepcified. I assume you want the browser to respect your width and height so display should be block, or be removed completely as it is a div element which implies display:block by default. Then you want the elements inside the div to align to the right, which you do by applying text-align.
Try this:
#sponsors a img {
float:right;
}
Currently you are floating the sponsors div to the right, instead of the images inside the sponsors div. Target the images to float them, and it should work for you.
The images themselves need to be floated or their parent element needs to have its text alignment modified.
Floated: http://jsfiddle.net/MAz4Q/1/
#sponsors {
width: 728px;
height: 100px;
margin: 60px 11px 0;
}
#sponsors img {
float: right;
}
Aligned: http://jsfiddle.net/MAz4Q/2/
#sponsors {
width: 728px;
height: 100px;
margin: 60px 11px 0;
text-align: right;
}

CSS - How to prevent horizontal scrolling?

This question probably has a simple solution.
I've designed a website with two columns side by side. Everything is fixed (menu bar and left column) with the exception of the right column.
This is intentional as I only want the right column to scroll has it will hold the readable content for the page. So everything is great, right?
Not exactly, the left column is floated left, and the right column is also floated with a larger enough left margin to allow to to sit properly in the page on load.
However when the screen is too small horizontally, the user can scroll left and right with moves the second column all around and even under my fixed first column. That is what I want to prevent.
How can I get the second column to scroll vertically but not move horizontally?
Here's a snipet of the css:
#main-content {float: left; margin: 100px 0 0 0; background: rgba(128,127,128,0.9); padding: 15px 25px 15px 15px; width: 500px; -moz-border-radius: 20px; -webkit-border-radius: 20px; border-radius: 20px;}
#button-glue {float: left; position: fixed; padding: 0 25px 15px 0px; width: 525px;}
#button{
float:right; margin: 5px -20px 0 0;
}
#button a {
background:url(../images/button.png)
no-repeat;
display:block; /* Necessary, since A is not a block element */
width: 167px;
height: 58px;
}
#button a:hover {
background:url(../images/buttonhover.png) no-repeat;
width:167px;
height:58px;
}
.right {float:right; margin: 0 0 5px 25px;}
#secondary-content {float: right; margin: 100px 0 15px 569px; background: rgba(128,127,128,0.9); padding: 20px; background: rgba(128,127,128,0.9); width:405px; -moz-border-radius: 20px; -webkit-border-radius: 20px; border-radius: 20px;}
Thank you!
overflow-x:hidden
that will not allow scroll bars on an element and hide anything hanging over.
I hope I understood your question right way, but why do you may not need to use float.
Float is to push an element to the left or right, and I think it's very handy but for your solution you don't need it. Instead you can use on your secondary-content div position: absolute. Instead of using margins it's easier to use top, left. So if you want to have your secondary-content div in the right place you can use:
position: absolute;
top: 100px;
left: 569px;
I suggest you do the same with the other elements and use margins for creating space around your elements.

CSS/XHTML problem with horizontally and vertically centered and floated div

I have a problem with floating divs. Here is my website: http://www.wokarts.com/index.php?option=com_gallery&controller=images&parent=6
I don't know why on some rows there are 4 images and on others only one.
Here is code responsible for it:
#gallery-images {margin:0 0 50px 0; display: table}
div.image-item {
float:left; display: table; text-align: center; position: relative;
width:180px; height: 150px;
margin:10px 0 30px 40px;
vertical-align: middle; text-align: center;
}
div.image-item a {display: table-cell; vertical-align: middle;}
div.image-item img {margin:auto; border:1px solid #d3d4d4;}
#gallery-image {margin:0 0 30px 0 auto; text-align: center; display: block; width:970px; padding-bottom: 80px;}
div.photo-item {margin: 0 auto; width:850px; display: block; text-align: center;}
div.photo-item a.img {margin:0 auto; text-align: center; }
Try display:inline-block;.
div.image-item { display: -moz-inline-block; display:inline-block; zoom:1; *display:inline; vertical-align:top; width:180px; margin:10px 0 30px 40px; vertical-align: middle; text-align: center; }
Change the display on div.image-item to inline-block and I would modify to have the image center within that div.
When you left float elements like that, as soon as they no longer fit on one line, the next floating element will drop down only as far as is necessary until it does fit.
The 5th image, for example, "gets stuck" against the bottom of your 3rd image instead of moving all the way back to the left edge because the 4th image isn't as high.
For the 10th image this doesn't happen, because the 9th image is higher than all the other images left of it, so as soon as the 10th image has dropped below the edge of the 9th image, it clears the entire row.
You'll see what I mean more clearly if you replace the margin by padding and add a border to .image-item.
To force the first item of each row down to the start of the next line, you should either not use floats, wrap every row of 4 items in a containing div, or use the magical powers of CSS3 to clear the floats at the start of each row:
.image-item:nth-child(4n+1) { clear:left }

Resources