How to fit elements in 100% width without absolute positioning? - html

I'm trying to do a 4-frame design with css, as in this code:
http://jsfiddle.net/7qBKJ/1/
But I don't want to use position:absolute;, and I'm trying to do it like this:
topframe: block;
left,right and centerframes: inline-block;
And I want to ensure there is, say, 200px of width in both rightframe and leftframe, and the remaining parts should be filled by centerframe. How can I manage this without absolute positioning?
I tried this, but it moves the frames up and down, when the screen width decreases :
http://jsfiddle.net/V4vAc/2/
in this fiddle, centerframe aligns with leftframe, since they are both inline-block, with centerframe rule margin-left:0px; but I have no idea how to set centerframe's right to align with rightframe's left, without specifying a width.
So how can I make #centerframe's width equal to screen width - 400 px ?
Thanks !

What you have to do is to put both sidebars first in the flow of the document. Then you float the first sidebar right and the second one left.
<div id="left"></div>
<div id="right"></div>
<div id="center"></div>
/* main.css */
#left {
width: 200px;
height: 100px;
background-color: blue;
float: left;
}
#right {
width: 200px;
height: 100px;
background-color: red;
float: right;
}
#center {
width: auto;
height: 100px;
background-color: green;
margin: 0 200px;
}
http://jsfiddle.net/samgh/JjsFF/
If you want center to be the full width underneath the two sidebars, you can remove the margins. Hope this helps.

Related

Floating div is escaping parent div

I set a width & height to the parent images div. There are two child divs inside of it called image_one and image_two with a set width. The problem is that when I reduce the width of the viewport, the image_two div escapes the parent div and comes under the image_one div. How do I keep this div from escaping? I figured that setting a percentage width would automatically resize the div to stay inside of the parent div. When I set an overflow:hidden, both of the divs disappear.
Here is a link to the code:
http://codepen.io/matosmtz/pen/ZGpNmy
<div class="images">
<div class="image_one">
<p style="background-color:red; text-align:center">Photo</p>
</div>
<div class="image_two">
<p style="background-color:red; text-align:center">Photo</p>
</div>
</div>
.images {
padding: 0;
margin: 0;
width: 100%;
height: 220px;
}
.image_one {
width: 30%;
height: 200px;
position: relative;
background-color: black;
padding: 5px;
float: left;
margin: 5px;
}
.image_two {
width: 30%;
height: 200px;
position: relative;
background-color: black;
padding: 5px;
float: left;
margin: 5px;
}
The .images div is 100% width. This includes the sidebar on your codepen.
The child divs are 30%, but this means 30% of the whole space. So when you reduce the size of the browser, eventually they are big enough to need to slide under one another, because your .sidebar has a fixed width of 200px.
I would suggest having a look at how the Bootstrap CSS works in order to find your fix for this, or straight out using that.

CSS fixed and percentage width with inner floats and centering

I have a responsive header which is quite complex.
The left block is fixed width and the right block is a percentage (100%) I found this great article here on how to do that except I need it the other way around, this example is right block fixed.
http://radiatingstar.com/make-a-layout-with-fluid-and-fixed-size-columns
I did get it working at one point but can't remember how I did it, there should be no scrollbar the outer container should be 100%. The real issue is that in the right block I have 2 inner divs, 1 div should be horizontally centered on the screen not centered in it's div as the fixed left block has pushed it over already.
http://jsfiddle.net/3519a9p0/1/
<div id="container">
<div id=fixed-width>
</div>
<div id=fluid>
<div class="farRight">right icons</div>
<div class="centeredBlock">centered on screen block</div>
</div>
</div>
And the other challenge is the responsive part in that the right icon block as you can see that that is floated to the right should move on top of the centered block as the screen width shrinks.
It would appear that I need to float the centered block too but then it needs to the centered middle of the screen too.
The the fixed width left block could potentially be a float too but it doesn't really matter as after the screen gets to small I switch to completely different layout, it's just the 2 inner divs that I need centered and responsive.
You're a genius if you can solve this!
Cheers!
Here is a working example. I just got rid of the margin and the float. However, while the answer was simple, you should read on below to understand why this worked.
Working Example
Because the left div has a float: left attribute, you can just set the right div to take up 100% of the remaining space. You do not need the negative margin to work the div into its place.
Also, a floated element is taken out of the normal flow of the document, so now you can use margin: 0 auto and as long as the right div has 100% width, it will center across the entire screen.
Update
There were post-question requests made via comments. To solve the issue, I added media queries and removed the float on the right-side div. Also, I had to add extra markup so that the inner divs on the right-hand side could be absolutely positioned properly.
Here is revised CSS. The major changes are:
No need to float the fluid column, add left margin instead
To center a box on the screen, set relative positioning on the container (not the fluid box) and use absolute positioning on the box
As for responsiveness, you can simply remove float, width, height and positioning from elements so that they appear as rows.
/* body margin/padding is reset to get media queries right */
body {
margin: 0;
padding: 0;
}
#container {
position: relative;
height: 100px;
}
#fixed-width {
float: left;
width: 250px;
height: 100%;
background-color: blue;
}
#fluid {
margin-left: 250px;
height: 100%;
background-color: green;
}
.farRight {
float: right;
width: 100px;
height: 40px;
color: white;
background-color: black;
}
.centeredBlock {
position: absolute;
left: 0;
right: 0;
margin: 0 auto;
width: 200px;
height: 40px;
color: white;
background-color: tomato;
}
/* when screen is narrower than 250+200+250 pixels trigger breakpoint 1 */
#media screen and (max-width: 699px) {
.farRight {
float: none;
width: auto;
}
.centeredBlock {
position: static;
width: auto;
}
}
/* when screen is narrower than whatever-you-want pixels trigger breakpoint 2 */
#media screen and (max-width: 499px) {
#container {
height: auto;
}
#fixed-width {
float: none;
width: auto;
height: auto;
}
#fluid {
margin-left: 0;
height: auto;
}
}
<div id="container">
<div id="fixed-width">fixed width</div>
<div id="fluid">
<div class="farRight">right icons</div>
<div class="centeredBlock">centered on screen block</div>
</div>
</div>

Floating box to the right

I'm currently creating a website, which has a centered box with text and and such.
Now, i also want a box floating on the right, with a little gap from my main box.
I'll leave a picture here, where the red box i drew is the floating box i want to make.
Btw. the blue box is just a censored box i didn't want on the picture.
So my question for you is, how do i make a floating box like that?
I've tried a couple of times with different methods.
in the CSS, i've made a box and gave it the property float:right;
But when i do that, it just turns out like this
Any help will be greatly appreciated
DEMO
You can keep an element center align by defining its width then using margin: 0 auto; technique. this will make sure your center div is in center then you can use position: absolute to create the other box on offset position.
HTML:
<div class="main-wrapper">
<div class="main">This is in center position.</div>
<div class="side">This is in offset position.</div>
</div>
CSS:
body {
background: #333;
color: #fff;
}
.main-wrapper {
position: relative;
margin: 0 auto;
}
.main, .main-wrapper {
width: 500px;
}
.main {
border: 1px solid #f00;
min-height: 500px;
}
.side {
width: 200px;
border: 1px solid #f00;
min-height: 200px;
position: absolute;
top: 60px;
right: -300px;
}
.main, .side {
text-align: center;
padding: 10px;
}
My best guess is that you have a <div> with a float: right; in the end. Keep it in the first code. So that it gets floated correctly. I would code this way:
<div class="right">Right</div>
<div class="main">
Main Contents
</div>
CSS would be:
.right {float: right; width: 20%;}
.main {margin: auto; width: 60%;}
Preview:
Fiddle: http://jsfiddle.net/praveenscience/8WHyp/
U can have main container display : inline-block
width of each sub container as width : 30%;
and width of the floating box which is inside 3rd sub container as
width : 100%;
In case u dont need first div,
put some margin for the 2nd container
ex .. margin-left : 300px;
and in case u dont want ur floating box width to be 100% of the 3rd container, u can adjust it too

Float-left disabled padding

Two divs are side by side, one is floating left with a width of 25%, the other just has a width of 75%. But when padding is applied on the right hand div, the padding doesn't work properly.
Here is a JSfiddle example:
http://jsfiddle.net/88upt/
<div id="top">
</div>
<div id="middle">
</div>
<div id="bottom">
</div>
CSS
#top {
float: left;
background-color: green;
width: 25%;
height: 100%;
}
#middle {
background-color: blue;
padding: 30px;
min-height: 30%;
}
#bottom {
background-color: red;
min-height: 70%;
}
Can someone explain to me why this is happening?
Thanks
Floating something is kind of like making it's position absolute. It will hover on top of it's neighboring containers. Add a margin-left equal to the width of the floated element to make the container the correct width.
http://jsfiddle.net/88upt/4/
#middle {
background-color: blue;
padding: 30px;
min-height: 30%;
margin-left:25%
}
EDIT Elaborating a bit more.
The floated element pushes the content of the sibling elements over. It will not push the left side of the content's element over. The padding is there it's just hidden by the floating element.
Add overflow = "auto" in the #middle.
#middle {
background-color: blue;
padding: 30px;
min-height: 30%;
overflow: auto;
}
In this way, you don't need to know the width of floating element.
Width doesn't factor in padding.
Source: http://www.w3schools.com/css/css_boxmodel.asp
The width only applies to content, not padding, border, or margin.
You can find more information here.

Float two DIVs by width in px and percentage

How to float two DIVs side by side as the width of one is defined in pixel and the other should fill the available width in the parent DIV? The point is that the content of second DIV may be blank; thus, I cannot leave its width empty.
.div1 {
float:left;
display:block;
width:200px
}
.div2 {
float:left;
display:block;
width: [... 100% - 200px ...]
}
Simple - float the first div and give only margin equal to first div width to second div (a div is a block element so it already has a 100% width - just for the sake of validation you can add width:auto):
.div1 {
float: left;
width: 200px;
height: 100px;
background: violet;
}
.div2 {
width: auto;
margin-left: 200px;
height: 100px;
background: yellow;
}
There's a fiddle here: http://jsfiddle.net/gFyY3/
If you don't have to use float, you can also use the inline-block display-type. Saves you from clearing the floats etc.
<div style="width: 250px; display: inline-block; vertical-align: top;">
Some content <br> More <br> More
</div>
<div style="display: inline-block;">
Some more content <br> More <br> More <br> More <br> More <br> More <br> More
</div>
There's a fiddle here: http://jsfiddle.net/Neograph734/tbqpy/
You could set div1 to position absolute, that it lays over div2, then you give div2 a width of 100% and all child-elements of div2 a margin left of 200px.
If you're sure your second div will always be blank, I suggest you to use the Clear property like this example:
div{
float: left;
width: 200px;
}
div.clear{
clear: both;
}
Then you won't need a second div.
But if you want to fill the second with content I'd rather suggest you to set both div with a percentage value like:
div.first{
float: left;
max-width: 20%;
}
div.second{
float: left;
max-width: 80%;
}
Hope this help.
Aymeric.