css: how to build centered div with minimum spacing on the left - css

so the past days i tried to achieve the following:
the idea being to have a div (red) that is ultimately centered (using margin:auto;), and on the same level (x-axis) another div that has a fixed size (blue).
on a huge enough display, maximized, it looks great.
now the fun part is when having a smaller screen and/or resizing the window. because of the auto margin, one of the divs overlaps the other:
this is what i want to prevent. (in explanation: red being the menu, blue being the logo)
so the first idea was to shift the red div the needed pixels of the blue div to the right, using padding-left:??px;
but that makes the red div no longer center itself absolutely, but padded ??px to the right. figuratively centered in an extra box (grey).
second idea being to create another (transparent) div on the right of the red div. but that makes the min-width of the whole site become out of bound:
in other words: the scroll bar becomes visible far to early. it's ought to appear just when the window is smaller than the sum of pixels of the red and blue div together. and not, like in img 4, where it appears just when the window is smaller than the sum of pixels of the red div and both divs right and left from it).
so what i want is that:
two divs, not overlapping (even when resizing), the right one at a fixed size, the left one in the center of the window, whithout creating a ghost div, creating blank space on low resolutions.
oh and please no javascript, if possible.
i hope my explanations helped a bit getting my idea.
and i furthermore hope someone with a great idea or with an overlooked feature can help me out.

I take it back... it's marginally possible... with a lot of hackish coding...
http://jsfiddle.net/7myd4/2/
http://jsfiddle.net/7myd4/2/show
There you will find the code and the demo. It involves a wrapper, padding, relative positioning, and a really hackish layout. :P
EDIT:
looking back at this answer from over two years ago... I've come to the conclusion that this answer is terrible.
I've updated it with code samples and a new demo (only thing different is formatting changes and moving inline styles to classes)
HTML
<div class="firstdiv"></div>
<div class="seconddiv">
<div class="innerdiv"></div>
</div>
CSS
body{
padding:10px 0px;
}
.firstdiv {
background-color:#ddd;
position:fixed;
left:0px;
width:200px;
height:200px;
}
.seconddiv {
margin:0 auto;
width:300px;
height:150px;
padding-left:400px;
position:relative;
left:-200px;
}
.innerdiv {
background-color:#ccc;
width:300px;
height:150px;
}
Demo: http://jsfiddle.net/7myd4/55/show
Source: http://jsfiddle.net/7myd4/55/

use Javascript to change the width of the div based on the window width. or use css stacks to check the max-width of the screen and have css for that size.
http://api.jquery.com/width/
http://api.jquery.com/resize/
or check out this stack.
How to dynamically change image/div dimensions based on window size?

Related

Responsive divs issue - one div fixed, second responsive with fixed margin

I'm trying to understand the case with responsive divs but I've got one problem.
As long as whole stuff is about 100% div's width it's not a big deal. But I find it difficult to add some additional features.
Here's my tiny fiddle:
[https://jsfiddle.net/yqh31d7v/][1]
The effect I'd like to gain is to fix sidebar's width (unless I define it to not) and shrink only blue div maintaing fixed margin-left at 20px.
I was trying to set width with % but it is not holding correct margin-left all the time.
So no matter what the browser width is I want to keep 300px sidebar width 20px margin left and keep blue div responsive from 0 to 700px.
P.S. I've checked some previous topics including ie. this fiddle
[http://jsfiddle.net/FXk4x/10/][2]
But adding left:20px; or margin-left:20px doesn't work.
Thanks in advance.
You currently have this css at the bottom:
#media screen and (max-width: 850px) {
#sidebar {
float:left;
margin-left:0px;
width:300px;
}
}
This is telling the browser that if the window is 850px or less then apply these rules (margin-left:0px;). So your css is working, but your viewing in the wrong screen widths to see the margin-left:20px; occur. Check these two examples:
Your exact code but in full screen (you will see the 20px margin if your screen width is higher than 850px):
https://jsfiddle.net/yqh31d7v/embedded/result/
Using a max width of 50px for the media query:
https://jsfiddle.net/yqh31d7v/1/
Ok, I have figured it out.
If you want to mix fixed and responsive floating divs you need to change position of fixed one to absolute.
I'ce updated the previous fiddle, it's more or less like this:
[https://jsfiddle.net/yqh31d7v/][1]

How to get background to have all 4 margins

If I want to have my background be separated into 4 equal-sized divs, that isn't hard for me to accomplish. What's puzzling, though, is that only the top and left margins appear, and I cannot get the right specification to have margins on the right and bottom appear.
.bg {
height:100%;
width:100%;
position:fixed;
background-color:blue;
}
With the code above, you can see that there is a top and left margin only.
The way I figured out is to just set the height and the width to slightly under 100%. Thanks for the other feedback.

Image does not resize in smaller browser

I am trying to place a image on the bottom right corner of my page and have it always be there. I have managed to do this but I have a problem when making the browser window smaller (which also is a problem while looking at it in different advices with smaller screens). When I make the browser smaller the image stays in the same size, and eventually overlaps over the other items (like my menu for example). Is there anyway to make that the image automatically becomes smaller together with the browser?
The css code I have used to place the image on the bottom right corner looks like this:
display:block;
float:right;
bottom:0;
right:0;
width:340px;
The image is a png image placed inside a div in my html.
I hope someone can understand what I mean and help me with this!
You can give the image a percentage for a width instead of a pixel width. It will then resize when you resize the window. Try setting width: 20% as a starting point and see what happens.
You can try this
<html>
<style>
img{
position:absolute;
bottom:0;
right:0;
max-width: 100%;
height: auto;
width: auto;
}
</style>
<body>
<img src="slide2.jpg"/>
</body>
</html>
resizing browser also resizes image, and is always at bottom.
Using % values for the width and height of the image should work.
Don't use floating for this case, your positioning should be absolute and don't set pixel-based sizes. If you want to set its size, you can do it by percentage, however, maybe the best solution would be to have separate themes for separate screen sizes.

CSS3 Box, How to strech the image inside a div with box properties

Yes! I did searched for related issues as it seems to be an easy question! However the solutions given on other topics do not solve my issue!
Im using an area filled with divs (CSS3 Box). Multiple rows with multiple divs
Each div gets his own picture that needs to stretch to fit the width and height of the div! (no matter what resolution).
Currently only the width stretch works. Height stretching seems to stretch it over all rows.
Fiddle to show you guys http://jsfiddle.net/h2Ya8/32/
Any solutions?
I'm not sure if it's what you're looking for, but adding
.product {
position:relative;
and
.product img{
width:100%;
height:100%;
position:absolute;
bottom:0; right:0;
}
seems to force the image to width/height of the container.
Check the demo.
Do you need to preserve the ratio?

Footer's background pattern gets cuttoff when window is resized

So most of this site so far uses auto centering (the container and nav have margin-left/right:auto) and things seem to go all well and dandy except for the footer.
When I resize the size of the window everything is filled nicely except when I scroll horizontally the footer seems to be cut off on the right side.I've read that this may be a browser bug. Though it occurs in IE and chrome and firefox so it could just be sloppy coding (I am a big newb).
Here is the css:
#footer {
background-image:url(../Images/footer_bg.jpg);
color: white;
height:300px;
padding-top:20px;
}
/*I have 4 headings with Ps that I want to display horizontally side by side*/
#footerContent{
min-width:1000px;
}
/*So I tried floating <li> inside <ul> and limiting its width, which worked fine */
#footerContent ul{
width:1000px;
margin-left:auto;
margin-right:auto;
}
#footerContent li {float:left; width:250px; }
Just to reiterate it works fine when the browser is full screened or resized. But after you resize and you use the horizantal scrollbar to scroll all the way right then the background image is cut off.
I've tried width:100%, min-width, width:1000px; but none of those seemed to work.
http://postimage.org/image/3so264fnb/
Regarding your comment about Stackoverflow being similar
(at least as of 4-29-2012)
The issue on stackoverflow seems to be that the footer contains another div element, footerwrap, that has a width: 960px set to it, but footer itself has no width setting. A div is basically designed to simply "group" block level content. It is a common misconception that a div expands with it's content. Actually, a div expands to its parent if an explicit width is set on a parent. If there is none, then it fits the browser window. This is what you (and stackoverflow) is experiencing.
To get the div to relate to the content width, you must either:
Explicitly set the width or min-width of the container. So, if stackoverflow set a min-width: 990px (the 960px of the footerwrap + the padding of 15px on each side) on the footer that wraps footerwrap, then its problem is solved.
Set the container div to float, as a floated element wraps its content.
Take a look at this example fiddle. Note the first two div's experience the same issue you are seeing. If you shrink or expand the size of the iframe window in the fiddle, the first two div's will contract or enlarge with it, but still leave blank space on the horizontal scroll. The third and fourth div's have had my fixes above applied. The fifth div is to show the fact that the inner div, if not defined in width, will expand to the width of a container that has an explicit width set.
As a side note, it may work (I have not tested in many browsers, but FF 11 worked) to actually just add a float: left to the body element in those cases where the body does not have a set width. As this example shows, it seems to be effective in causing the first two div's to behave just like the 3rd and 4th divs.
I hope this helps.
Original Answer
It is a little unclear what can be done because there is some information lacking. Here are some things to look for:
Is your background-image wide enough (or can it / should it have a background-repeat: repeat-x applied to make it wider if needed)?
Does your footer width (1000px) match your upper content width? If footer is constrained narrower than what the upper content area (or header, etc.) is allowed to be, then it's background will not align.
That's the best I can do without seeing more of your html and css for the page, and not knowing the size of the image and your intention for how it is to function.

Resources