Fixed width div on right, fill remaining width div on left [duplicate] - html

This question already has answers here:
2 column div layout: right column with fixed width, left fluid
(8 answers)
Closed 8 years ago.
Im searching for a way to have 2 divs as columns where div on right has a fixed width and div on left fill remaining space.
Does anyone happen to know if this can be done?
My attempt (renders block2 underneath block1):
<style>
.block1 {
width: auto;
height: 200px;
background-color: green;
}
.block2 {
float: right;
height: 200px;
width: 200px;
background-color: red;
}
</style>
<div class="block1">test1</div>
<div class="block2">test2</div>

You can do it like this:
HTML:
<div class="right">right</div>
<div class="left">left</div>
CSS:
.left{
background:red;
}
.right{
float:right;
width:200px;
background:green
}
Check this live example http://jsfiddle.net/QHTeS/2/

Float Both of the elements left:
<style>
.block1 {
width: auto;
height: 200px;
float: left;
background-color: green;
}
.block2 {
float: left;
height: 200px;
width: 200px;
background-color: red;
}
</style>
<div class="block1">test1</div>
<div class="block2">test2</div>
You should wrap them in a container as well to prevent messing up the rest of your layout. :)
http://jsfiddle.net/tcFjN/
That was wrong!
Use display: table; on parent and display: table-cell; on children:
<div id="wrapper">
<div class="block1">test1</div>
<div class="block2">test2</div>
</div>
#wrapper
{
display: table;
width: 100%;
}
.block1 {
width: auto;
height: 200px;
display: table-cell;
background-color: green;
}
.block2 {
display: table-cell;
height: 200px;
width: 200px;
background-color: red;
}
http://jsfiddle.net/tcFjN/1/

This is my solution without floats. The only caveat is that I need to use a wrapper. So, if the desired HTML is
parent (has a border, margin, padding,...)
left (fixed width)
right (variable width, fill the entire space)
I must rewrite it as
parent (has a border, margin, padding,...)
wrapper (has no styling)
left (fixed width)
right (variable eidthm, fill the entire space)
My HTML is
<div style="border:1px solid black; background:red; margin:10px; padding:10px;" >
<div style="">
<div style="display:table-cell; padding:10px; min-width:100px; max-width:100px;background:green;">Left</div>
<div style="display:table-cell; padding:10px; width:100%; background:yellow;">Main content</div>
</div>
</div>
The main points here are:
No use display:table because then we can not set the border
The use of min-width, max-width
The use of width:100%

Check this jsfiddle
Start out with a container <div> (#container) that holds both the left and right <div>s. Float one <div> to the right and give it a specific width (320px in my example). Then give the other <div> an absolute position starting at the absolute left (0px) and ending at the left edge of the <div> on the right (320px).
If you adjust the width of #container, the right <div> will remain fixed at 320px while the left <div> will expand to fill whatever the remaining area is.

Related

How do I set the offset of a div from the top of an outer div to be the same as the offset from the side of the outer div using only css?

So I made a mockup of what I want to do so it's easier to understand. I have an outer div that takes up 33% of the page and an inner div that has a width of 50% (of the outer div). The outer div has a "text-align: center;" style so the space on each side of the inner div is 25% the width of the outer div. I want to offset the inner div from the top of the page so it is the same distance from the top as from the sides. Most of the responses to similar questions advise using jquery, but I would prefer to use a css-only solution if there is one. How can i do this?
I apologize if this is a basic question. I am inexperienced with frontend and I couldn't find any way of setting CSS attributes equal to the values of other attributes.
Any help would be appreciated!
The CSS:
.side{
background-color: #ECEFF1;
height: 100%;
width: 33%;
text-align: center;
}
.profpic{
width: 50%;
border-radius: 50%;
position: relative;
}
The Html:
<div class="side mdl-shadow--4dp" >
<image class="profpic mdl-shadow--4dp" src="/profpic.jpg"></image>
</div>
<div class="content">
</div>
This can be achieved simply by just putting these two CSS rules: padding: 25% and width: 50%. That will center the image equally from the left, top, and right. Here is the code I used:
HTML
<div id="side">
<image src="http://placehold.it/120x120&text=image1" id="box">
</div>
CSS
#side {
width: 33.33%;
height: 1000px;
background-color: #474747;
}
#box {
width: 50%;
padding: 25%;
}
Then a JSFIDDLE if you would like.
EDIT
To be a bit more question relevant, this is what the OP's CSS would be:
.side{
background-color: #ECEFF1;
height: 100%;
width: 33%;
}
.profpic{
width: 50%;
border-radius: 50%;
padding: 25%;
}
As of CSS3 You can use the units vw and vh to achieve this.
vh refers to the height of the viewport, vw to its width.
Your div is taking 33% of width, while the inner box 50% of that. Therefore, your margin on the left and right will be approx 25% of the outer divs width.
So, what you need as a top-margin is 33% * 25% = 8.25% of the vw unit:
<div id="outer">
<div id="inner">
lorem ipsum
</div>
</div>
#outer{
background-color:red;
width:33vw;
margin:0;
padding-top:1px;
height:100vh;
}
#inner{
background-color:blue;
width:50%;
margin: 0 auto;
margin-top:8.25vw; // this is 25% of 33% width
padding-top:1px;
}
http://jsfiddle.net/xaLc4zd2/
Resize the windows and see, how the inner div retains its relative position.
This should serve as a reference for your need. The key is to apply box-sizing to the 33% container, and apply 25% margin to the element inside the container.
<div class="box">
<div class="innerbox"></div>
</div>
<div class="box">
<div class="innerbox"></div>
</div>
<div class="box">
<div class="innerbox"></div>
</div>
.box{
width: 33%;
min-height:400px;
background-color: #777;
display: inline-block;
box-sizing: border-box;
}
.innerbox{
width:50%;
margin:25%;
min-height:200px;
background-color: #444;
}
See this codepen example

div push down at center it right away

How do i push my content down once i do (ctrl + mouse scroll) to zoom in , and the content center it self right away.
So i have #div left #div right both beside each other, and once it zooms, #div left will push down #div right ,and #div right will center it self right away.
Here is my jsfiddle sorry please copy and paste http://jsfiddle.net/Tedeee/bBpEm/
HTML
<div id="top">
<br />
hi
</div>
<div id="outer">
<div id="right">right</div>
<div id="left">left</div>
</div>
CSS
div {
outline: red 1px solid
}
#right {
width: 200px;
height: 200px;
float:left;
background-color:red;
}
#left {
width: 200px;
height: 200px;
float:left;
background-color:grey;
}
#outer {
float:left;
clear:both;
text-align: center;
}
Sample website
Try look at the home-section https://coderwall.com/welcome
As you can see once my div got pushed down the # right div wont move to center.
and also i need to center it once it is pushed down.
How do i do this, i really need help. Thanks.
Try displaying your divs as inline-block.
Updated JSFiddle
This comes with some caveats. inline-block elements will have whitespace surrounding them, and there are fixes for that. My preferred method is to add margin-right: -0.36em; to the inline-block elements. There are other fixes you can look up too.
Basic structure is as follows:
HTML
<div id="top"> <br />hi</div>
<div id="outer">
<div id="right">right</div>
<div id="left">left</div>
</div>
CSS
div { outline: red 1px solid }
#outer {
text-align: center;
}
#right {
width: 200px;
height: 200px;
display: inline-block;
background-color: red;
}
#left {
width: 200px;
height: 200px;
display: inline-block;
background-color:grey;
}

(CSS) how I could make the div takes all available space in width without a specific value but there is another div in the same place with a specific

I have this issue I can't find a solution for it:
I have 3 divs, two of them are located inside the third.
The div which contains the others has a percentage width.
The second one which is inside the first, doesn't have a specific width and is floated to the left.
The third which is also inside the first does have a specific width and is floated to the right.
The question is how would I make the second div take as much width as possible??
Because it fits the contents as default.
<div id="a">
<div id="b">
</div>
<div id="c">
</div>
</div>
<style>
#a{
width: 80%;
}
#b{
width: ??;
float:left;
}
#c{
width: 50px;
float:right;
}
</style>
arrange your divs like this
<div id="a">
<div id="c">456</div>
<div id="b">123</div>
</div>
and remove the float from #b
#b{
background-color:#06F;
}
check the jsFiddle file
Working jsFiddle Demo
You should put your fixed element before the other one:
<div id="a">
<div id="c">
FIXED ELEMENT
</div>
<div id="b">
FLEXIBLE ELEMENT
</div>
</div>
And in CSS:
#a {
width: 80%;
background: green;
overflow: hidden;
}
#c {
width: 50px;
float: right;
background: yellow;
}
#b {
margin-right: 50px;
background: pink;
}
Floats aren't a great choice for layout purposes, since that's not really what it was designed for. If all you're looking for is to have 2 elements side-by-side and not the other aspects of float, I recommend the table* display properties instead:
http://cssdeck.com/labs/tbarj05i
#a{
width: 80%;
display: table;
}
#b{
display: table-cell;
}
#c{
width: 50px;
display: table-cell;
}
I would suggest giving #C a percentage value instead of pixels, or find out the total width and set it to that minus 50px.
Also did you try width:100%?
width: 100% for B is his container's width, hope this illustrates:
<html>
<head>
<style>
div{ border: solid 1px #ccc;}
#a{
width: 80%;
}
#b{
width: 100%;
float:left;
}
#c{
width: 50px;
float:right;
}
</style></head><body>
<div id="a">
<div id="b">DIV B
</div>
<div id="c">DIV C
</div>
</div>
</body>
</html>

Make Div on right side fill out all available space

I want to create two divs beside each other, however I want the one on the left side to be 300px, and the right one to take up the remaining amount on the screen. How would that be possible? Thanks!
The most straight-forward (and I would say correct) way is to use display: table:
#wrapper {
display: table;
width: 100%;
}
#left, #right {
display: table-cell;
color: white;
}
#left {
background: blue;
width: 300px;
}
#right {
background: red;
}
<section id="wrapper">
<aside id="left">Left 300px</aside>
<div id="right">Right the rest</div>
</section>
http://jsfiddle.net/YbLZE/1/
Try resizing the bottom right frame.
Updated with HTML5 elements section and aside, which you should use if you have an HTML5 doctype. I have to remember to use those...
This is the working example:
http://jsfiddle.net/tnm62/
Explenation:
1. Place both elements in one container.
2. Position your left element absolute, set its width to 300px.
3. Set left margin to your right element to 300px.
One solution is to float: left; the left div that's 300px wide, and then apply overflow: hidden; to your right div. Here's the basic outline:
HTML:
<div class = "left">
Glee is awesome!
</div>
<div class = "right">
Glee is awesome!
</div>
CSS:
.left {
width: 300px;
float: left;
}
.right {
overflow: hidden;
}
And a little demo: little link.
Here's something for newer browsers (not IE):
CSS:
#container {
display: box;
}
#left {
width: 400px;
}
#right {
box-flex: 1;
}
​HTML:
<div id="container">
<div id="left">Left</div>
<div id="right">Right</div>
</div>​
Demo: http://jsfiddle.net/N5zhH/1/
This should be sufficient:
<div style="overflow: hidden;">
<div style="width: 300px; float: left;"></div>
<div style="margin-left: 300px;"></div>
</div>
overflow: hidden will stretch the container div to accommodate the tallest child element
float: left floats the element left (doh!)
width: 300px and margin-left: 300px together assures that if the right column is taller than left it will not flow below the left floated div; it will maintain a 300x gap from the left edge of container div
Tip: change to margin-left: 320px to add a 20px gutter
Here is a nice little DEMO

Let div fill up space next to centered container

I have a webpage containing a centered container with content and I want to display a logo next to it.
The layout is as following: div - container. Where the container is centered and the div lef of the container needs to fill out the width left on the screen.
html, body {
height: 100%;
width: 100%;
}
#container {
width: 800px;
margin-left: auto;
margin-right: auto;
min-height: 100%;
}
<div id="container">
</div>
<div id="lef">
</div>
A jsfiddle with this code is available on http://jsfiddle.net/7QJQn/
This is the option that comes closed
http://jsfiddle.net/7QJQn/4/
I think that the best solution for doing something like this is just using javascript / jQuery.
Depending on which browsers you wish to support, you could use calc().
Basically, you want 50% of the viewport width (50vw) minus half of width of #container (so you're measuring from the center of your #container and you use half of all the values) - I'm assuming that you're OK with absolute positioning #lef to the viewport to keep it to the right?
CSS (fiddle here):
#lef {
background-color:yellow;
width:calc(50vw - 100px);
height:20px;
position:absolute;
right:0;
top:0;
}
Add this to your css:
#lef{
float:left
}
And change the order of the divs in the html, like this:
<div id="lef"></div>
<div id="container"></div>
First of all, you should wrap your markup in a wrapper div so elements stay tight.
I made some changes, take a look:
<div id="wrapper">
<div id="lef">
</div>
<div id="container">
</div>
</div>
​
And the css:
html, body {
height: 100%;
width: 100%;
}
#wrapper{
width: 360px;
}
#container {
width: 200px;
margin-left: auto;
margin-right: auto;
height: 100px;
background-color:red;
}
#lef {
background-color:yellow;
width: 160px;;
height:100px;
float: left;
}​
Example
If using flexbox is an option, you can do this with the flex-grow property:
With the following markup
<div class="main-row">
<div class="filler"></div>
<div class="row-content">Fixed width centered div</div>
<div class="filler"></div>
</div>
you need to set flex-grow: 1 on the filler divs. See this fiddle.