CSS float and responsive behaviour - css

Here is my template:
<div id="block1">text</div>
<div id="block2">
<span>content of variable size</span>
</div>
and some basic CSS
#block1 {
float:left;
}
#block2 {
float:right;
}
#block2 span {
}
When reducing the width, how could I make it behave so that, once the two divs cannot fit the page inline, the second div will go below the first (rather than be right floated anymore)?
NOTE: I would like to avoid using media queries.
This responsive theme CSS would be used for multiple sites with content of different sizes.
JSFiddle
In this current JSFiddle, The second div is on the right hand-side. It is fine to me.
If possible without media queries, i would like to design css so that once the second div goes below , the span content is not at the right-hand side

If you mean "I want div2 to go below, but aligned left this time", it's not possible as this behaviour is not predictable using CSS only.
There's no CSS-way to know when it goes below, so impossible to change the floating attribute at this moment.
You could achieve this using Javascript or jQuery. Logic would be:
on ( window resize ) {
if ( div1_width + div2_width > container_width ) {
Change div2 CSS.
}
}
Of course I would suggest to use media queries too.

You can set min-width on the divs. Then, when the line is too small, the one on the right will drop down. However, it will still be floated which may cause issues. That's where media queries come into play to fix such things.

Too many media queries would not make for a pretty responsive design, not to mention they would be a headache.
Anyway, you would have to use at least one media query to achieve a truly responsive design, the simplest example is below:
<div id="block1">text</div>
<div id="block2"> <span>content of variable size</span>
</div>
* {
padding: 0;
margin: 0;
}
#block1 {
float:left;
height: 200px;
background: yellow;
width: 49.5%;
margin-right: .5%;
}
#block2 {
float:right;
height: 200px;
background: tomato;
width: 49.5%;
margin-left: .5%;
}
#block2 span {
}
#media only screen and (max-width : 768px) {
#block1 {
float:none;
width: 100%;
}
#block2 {
float:none;
width: 100%;
}
}
Fiddle here.
If you want to have a look at something more practical, a good starting point is here(its an example of an accordion changing layouts depending on screen size, using media queries).

Related

Foundation Flex grid float single element to the left and multiple elements to the right

I have following HTML:
<div>
<div class="elemA"></div>
<div class="elemB"></div>
<div class="elemC"></div>
</div>
I would like to achieve following result on medium breakpoint:
Is there any way to position elements like that, without wrapping B and C into additional parent-container?
Such solution is not an option as element A should be positioned in between B and C on small breakpoint:
It can be easily achieved with regular foundation grid by adding float left and float right styles, however it stops working with flex-grid...
Foundation Float grid is not able to do that (and probably no other flex grids do either). They are simply not designed for such usage. Most FE frameworks provide other grids based on Flex and other techniques, which may or may not give a way to do it.
However once your project uses the Flex grid there's little help in that.
A possible solution is to use custom CSS with floats and source ordering. The only issue is for this, if the height of "B" + "C" is less then the height of "A", you have to know/set the height of your "A" div, because the outer one would only grow to fit "B" and "C" and can cause "A" to overflow other elements coming after the outer div.
/* Core of layout */
.wrapper {
position: relative;
}
#media screen and (min-width: 40em) {
.elemB {
width: 50%;
float: right;
}
.elemA {
top: 0;
position: absolute;
width: 50%;
clear: both;
}
.elemC {
width: 50%;
float: right;
clear: both;
}
}
/* If the height of B + C is less than height of A, unfortunatelly we need to know the height of A */
.elemA {
height: 120px; /* must be known */
}
.wrapper {
min-height: 120px; /* this must be set to the same as the height of A :( */
}
/* Nothing important below this line, only appearance for the example */
.wrapper {
background-color: #bbb;
}
.wrapper div {
color: white;
text-align: center;
}
.elemB {
background-color: #3a598e;
height: 20px; /* simulate some content */
}
.elemA {
background-color: #618745;
}
.elemC {
background-color: #515658;
height: 80px; /* simulate some content */
}
<div class="wrapper">
<div class="elemB">B</div>
<div class="elemA">A</div>
<div class="elemC">C</div>
</div>
This will work regardless of your grid.
Indeed it is not ideal if you'd like to use the breakpoints exactly as defined by your grid, but in fact if you compile your CSS files from the Foundation sources, you can use the media query mixins in your Sass.
If you on the other hand use pre-compiled Foundation CSS, than the breakpoints are fixed and you can simply use the same on your custom CSS. For example to use 1 col layout only on small and two columns above, use #media screen and (min-width: 40em) as in my example above. You can find the media queries of the default breakpoints in the last part of this chapter.

Make second div appear above first, without absolute position or changing html

My page is split into 3 slices, as shown in this JFiddle.
In my full source code, I have media queries to help manage sizing between mobile and desktop. When someone accesses the site on mobile mode, Logo should appear at the top, and Items should appear below it. (I set display: none on my picture div to hide it)
Problem:
I can't change the positioning of the divs in HTML, or it'll disturb my current 3 slice layout. Absolute positioning is not an option, since most of my site is already dynamically sized, and I wouldn't want absolute positioning to interfere on a resolution I haven't tested on. This means calculating the margin sizes would be out of the question aswell.
So, absolute positioning is not allowed, nor is changing the orders of the divs. The result I'm looking for would be similar to this, exception without repositioning the divs.
My question is not about media queries, or how to size for mobile using media queries. I am only asking about how to get the layout I want with the restrictions in place (no absolute positing, no calculating margins, no changing div order).
Other questions I looked at:
Reposition div above preceding element - First answer suggests repositioning divs, which I cannot do. Second answer relies on calculating the position, which could interfere with other dynamically sizing elements.
Move The First Div Appear Under the Second One in CSS - Suggests I use absolute positioning, which I cannot do
Flexbox layout is your friend here. display: flex can be used to interchange the elements position on the layout.
#container { display:flex; flex-direction: column; text-align:center;}
#items { order: 2 }
#logo { order: 1 }
#picture { display: none; }
<div id="container">
<div id="items">Items</div>
<div id="logo">Logo</div>
<div id="picture">Picture</div>
</div>
display: flex works only in modern browsers. Check caniuse.
A test on my android mobile shows it working on Firefox and Chrome, but not on the stock Android browser.
I tried to solve the solution using transform: translateY property in percentage value.
Note: This works if and only if the two containers have same height. or if the height is already known, then you can set the transform: translateY value according to the height.
CSS
#media (max-width: 700px) {
#container > div {
width: auto;
display: block;
float: none;
}
#container #picture {
display: none;
}
#logo {
transform: translateY(-100%);
}
#items {
transform: translateY(100%);
}
}
Working Fiddle
Probably the easiest is if you play with minus margins. Note that the below sizes (width and side margins) may need to be adjusted to your specific needs.
#container * {
width: 95vw;
text-align: center;
}
#items {
width: 50%; /* #picture is hidden so we split the screen into 2 */
float: left;
margin-top:30px; /* has to be smaller than the absolute of #logo */
margin-left:25%; /* half of the element's width */
}
#logo {
width: 50%; /* #picture is hidden so we split the screen into 2 */
float: right;
margin-top:-40px; /* its absolute has to be greater than the one of #items */
margin-right:25%; /* half of the element's width */
}
#picture {
width: 33%;
float: right;
display:none; /* Hiding #picture as you said you would */
}
<div id="container">
<div id="items">Items</div>
<div id="logo">Logo</div>
<div id="picture">Picture</div>
</div>

How do I make two columns--one flexible--that wrap when necessary?

I need two columns, basically blocks side-by-side, that wrap when necessary for a responsive design.
The issue that I'm running into is that the first column/block is statically sized, but the second column/block needs to fill the remaining width. However, they should still wrap when necessary.
Say the left-most block has a static width of 200px, while the right-most fills the remaining width, BUT with a min-width of 300px. That way it should wrap (the second block placed below the first block instead of on the right side) when necessary.
I've tried a variety of methods to no avail--floating the left block, using absolute position, etc., but I can't get the results I'm looking for.
Hopefully it's possibly using CSS alone, and not using a CSS3 media query to show/hide two different versions. Or resorting to JS... :P
Did you want something like this
HTML
<div class="outer">
<div class="leftBar">Test</div>
<div class="rightCnt"></div>
</div>
CSS
* {margin: 0}
.leftBar {
width: 200px;
min-height: 600px;
float: left;
background: red;
}
.rightCnt {
margin-left: 200px;
min-height: 600px;
background: yellow;
}
#media (max-width : 500px) {
.leftBar {
float: none;
width: auto;
min-height: 200px;
}
.rightCnt {
margin-left: 0;
}
}

Width: 100% not filling up screen on mobile devices

I'm currently trying to optimize a Wordpress site for mobile devices, but I'm struggling with getting the footer of the site to cooperate. The site is here:
http://whitehallrow.com/
When loaded on mobile, the width of the body shrinks in accordance with the screen size and wraps all the contained text within it. However, the footer keeps its width, which I understand is because the width is hard-coded to look good on a computer screen. I've made a media query in the CSS that targets devices with screens 500 pixels wide or smaller, in order to get the footer to resize to the width of the body. Here is a snippet of my CSS that I've been tweaking:
#media screen and (max-width: 500px) {
#customfooter{
width:100%;
}
}
For whatever reason, this is not working - it still shows the footer as being much wider than the body. I've tried max-width:100%, width:auto; max-width:auto, and none of them work.
How do I achieve this without hard-coding anything?
Change your CSS from
#teakfooter {
width: 100%;
}
#verybottom {
width: 100%;
}
add a class so this gets higher priority
.page #teakfooter {
width: 100%;
}
.page #verybottom {
width: 100%;
}
I tried it out using Firebug and it seems to be working well like this.
Edit: After going over a few more things in the comments, I noticed a couple of things causing the footer to not fill out.
.site {
padding: 0 1.71429rem;
}
This is causing #customer footer to have padding on both sides.
#teakfooter {
margin-left: -40px;
}
This is causing #teakfooter to have whitespace on the right side.
also in firebug you can check METRICS (in right column you have Computed Styles, Styles, Metrics, etc.). In METRICS you will see that around your body there is a padding: 24px;
Solution:
body {
padding: 0;
}

css responsive theme image break layout

Hi and thanks for reading, am building this site http://myspacioclub.com and am using a wordpress responsive theme, and I got this image "bannerfb" with class "banner" that was asked for the customer. So inside the space for the logo I create a new div to put the banner and added this properties to the div of the banner:
.banner {
position:relative;
top:-170px;
left:450px;
}
but as the theme is responsive, when i make windows smaller like the size of tablet or cellphone the layout breaks, can someone help me?
How could I fix the theme that only use the banner properties when the window is in a bigger resolution, or any similar solution but the idea is to keep the banner with those properties without been affected by the smaller size.
You can achieve this different ways, but one way is following: First wrap your logo and banner in a div
<div class="wrap">
<div class="logo">
<a href="">
<img src="http://myspacioclub.com/wp-content/uploads/2013/04/Myspacioclub.png"/>
</a>
</div>
<div class="banner">
<img src="http://myspacioclub.com/wp-content/uploads/2013/04/bannerfb.jpg"/>
</div>
</div>
Then add following CSS:
.wrap {
position: relative;
width: 100%;
}
.logo {
width: 50%;
float: left;
}
.banner {
width: 50%;
float: right;
}
.banner img, .logo img {
width: 100%;
height: 100%;
}
You can see working example in here. Also, I have to point out, that at least at the moment you are using more than 7000px width image in your banner. This is NOT what you should do. You banner, at least in with my screen, is 700px wide. DO NOT ever use bigger images than you need. It shows 700px wide image, but you still have to load the 7000px one. Convert to smaller size! If you necessarily need bigger image for big screens, you could use javascript or css #media tag to load different image for different screen size. For that you have to set your banner image as background not as <img> and then do something like this in CSS:
#media only screen and (min-width: 35em){
/* Style adjustments for viewports that meet the condition */
.banner { background: url(path/to/image); }
}
You can set many steps like this. Just add another one, change the min-width and load different image to background.
So in your page you have to do following in CSS:
#media (min-width: 1320px){
.span8 { width:1178px; }
}
.name-logo, .banner { width: 50%; }
.banner img { width: 100%; height: 100% }
.name-logo img { width: auto; height: auto; }
.name-logo { float: left; }
.banner { float: right; }
Trick with responsive layout is to use percentage values not fixed pixel ones and do not use negative margins if possible.

Resources