Two inner DIVs side by side and one in center - css

There are two situations:
1. Two divs A & B in a container side by side
2. One div A in the same container in the center
Here is an example: http://jsfiddle.net/lvil/toj9w9zz/
.container_one, .container_two {width:200px; height:100px; background-color:red;}
.container_one .inner_a {width:100px; background-color:green; float:left;}
.container_one .inner_b {width:100px; background-color:blue; float:right;}
.container_two .inner_a{width:100px; margin:0 auto; background-color:green;}
<div class="container_one">
<div class="inner_a">
a
</div>
<div class="inner_b">
b
</div>
</div>
<br>
<div class="container_two">
<div class="inner_a">
a
</div>
</div>
Let's say the containers have the same class(not like in the example).
The number of inner DIV always changes (1 or 2).
Is it possible to make css fit both situations?
I have tried many things but could not achieve this.

Try inline-block, keep this inner_a rules for both divs and remove floats.
.container_one, .container_two {
background-color: #ff0000;
height: 100px;
text-align: center;
width: 200px;
}
.container_two .inner_a {
background-color: #008000;
display: inline-block;
width: 100px;
}

The following code is the best and least:
.inner_a {
background-color: green;
display: table;
margin: 0 auto;
width: 100px;
}
.container_one .inner_a {
float:left;
}
.container_one, .container_two {
width:200px;
height:100px;
background-color:red;
}
.container_one .inner_b {
width:100px;
background-color:blue;
float:right;
}

What you want to do is use display:inline-block instead of floating and then target the divs with class .inner_a when they are the last child of the parent, like so:
.container_one>div{
display:inline-block;
width:100px;
}
.inner_a{
background-color:green;
}
.inner_a:last-child{
margin:0 auto;
}
.inner_b{
background-color:blue;
}

Related

vertical-align:bottom WITH float:left

how could i do vertical-align:bottom for some divs that have float:left?
you can see the source here: http://jsfiddle.net/Lb1su4w2/
this is what i have: (every color is a different div)
this is what i want to have:
Vertical align only works with inline block elements, floated elements ignore the vertical align property.
Update the box class with the following:
.box {
display: inline-block;
vertical-align: bottom;
width:80px;
}
I would make them all inline block elements and remove the whitespace with one of these techniques.
Updated fiddle: http://jsfiddle.net/9rcnLb8n/
Alternatively you could use flexbox with the align-self: flex-end; property.
HTML:
<div id='wrapper'>
<div id='a' class='box'>aa</div>
<div id='b' class='box'>bb</div>
<div id='c' class='box'>cc</div>
<div id='d' class='box'>dd</div>
</div>
CSS:
.box {
width:80px;
vertical-align: bottom;
display: inline-block;
}
#a {
background-color:red;
height:200px;
}
#b {
background-color:green;
height:100px;
}
#c {
background-color:yellow;
height:150px;
}
#d {
background-color:blue;
height:300px;
}
#wrapper {
border: 1px solid pink;
display: table;
}
In this case don't use:
float: left;
Instead use:
display: inline-block;
Check out my fiddle:
http://jsfiddle.net/Lb1su4w2/6/

CSS: all sub-div elements on one line with centerder 2nd element

i am attempting to align 3 sub-div elements in one line, first element with left floating, third - with right and 2nd - centered.
Below my draft. What I have missed?
http://jsfiddle.net/yDzL9/
In my real e-shop the problem is in aligning of 2nd div which contains input (button), but in jsfiddle's example the problem with the third div.
Use display: inline-block for all sub elements and set white-space: nowrap; property to parent div.
Example-
HTML:
<div id="container">
<div class="wishlist">
</div>
<div class="cart">
</div>
<div class="compare">
</div>
</div>
Method 1 (Without Floated div):
CSS:
#container {
height:26px;
width:300px;
background-color:#009900;
white-space: nowrap;
}
#container div{
display: inline-block;
width:100px;
height:26px;
}
Working Example
Method 2 (With Floated div)
CSS:
#container {
height:26px;
width:300px;
background-color:#009900;
white-space: nowrap;
text-align: center;
}
#container div{
display: inline-block;
width:50px;
height:26px;
}
.wishlist{
background-color:blue;
float: left;
}
.cart{
background-color:black;
}
.compare{
background-color:red;
float: right;
}
Working Example
Using the display: flex css rule it will work as you want it to!
HTML:
<div id="container">
<div class="wishlist"></div>
<div class="cart"></div>
<div class="compare"></div>
</div>
CSS:
body {
margin: 0;
padding: 0;
}
#container {
height:26px;
width:300px;
padding: 10px;
display: flex;
background-color:green;
}
#container > div {
height: 90%;
width: 90px;
}
.wishlist {
float: left;
background-color:blue;
}
.cart {
margin: 0 auto;
background-color:yellow;
}
.compare {
float: right;
background-color:red;
}
As shown in this jsfiddle, and here is another one.

How to center 2 divs next to each other with a specific width

I know this is a drawn out question but with all the examples I could find on the net for some reason I cannot replicate.
I need 2 div boxes that are 300px; wide to be next to each other and be centered in the middle.
I have the following code
<div class="container">
<div class="box1"></div>
<div class="box2"></div>
</div>
<style>
.container {
width:100%;
text-align:center";
}
.box1 {
float:left;
width:300px;
}
.box2 {
float:right;
width:300px;
}
</style>
For whatever reason I can get the boxes next to each other but it stays on the left side of the screen instead of the center. I just need them centered
Example: http://jsbin.com/ubanuf/12/edit (not mine)
It is cause you have float: left. There are several ways to do that, you can add display: inline-block; or add margin: 0 auto;
.box1 {
float:left;
width:300px;
display: inline-block;
}
.box2 {
float:right;
width:300px;
display: inline-block;
}
Change your CSS as follow
.container {
width:100%;
text-align:center;
}
.box1, .box2 {
display: inline-block;
width:300px;
}
inline-block elements will be align with the text, if you set the text-align: center for the container div.
you have extra (") double quote at your CSS text-align: center";.
Here is an other way (there are plenty indeed!):
.container {
margin: 0 auto;
width: 100px;
}
.box1 {
width:50%;
display: inline-block;
}
.box2 {
float: right;
width:50%;
display: inline-block;
}
There is redundant space between the two child DIVs if you use inline-block, here is my solution.
container {
width:100%;
text-align:center;
font-size: 0; // hack the space
}
.container div{
font-size: 16px; // hack "hack the space"
}
.box1 {
display:inline-block;
width:300px;
}
.box2 {
display:inline-block;
width:300px;
}
you does not need to do any thing..just give box1 and box2 width in "%" or give container width in "Pixel". That will solve your problem
Like This
<style>
.container {
width:100%; //or change it to 600px
text-align:center";
}
.box1 {
float:left;
width:300px; //or change it to 50%
}
.box2 {
float:right;
width:300px; //or change it to 50%
}
</style>
Try to keep everything in "%" or "Pixel"
Thanks

Two divs full and fixed width [duplicate]

This question already has answers here:
DIVs Arrangement - HTML CSS
(3 answers)
Closed 9 years ago.
I am trying to make two divs in the main container side by side, with bottom div should adapt to the width of the rest of the space.
<div id="container">
<div id="left">fixed width</div>
<div id="right">rest of space width</div>
</div>
#container {
float:left;
width:100%;
}
#left, #right {
padding:50px;
background: #ccc;
vertical-align: bottom;
display: inline-block;
/* ie6/7 */
*display: inline;
zoom: 1;
}
#right {
padding:20px;
background:#000;
color:#fff;
}
I'm stuck at this stage http://jsfiddle.net/Z9qW3/7/
#container
{
overflow-x:hidden
}
#left
{
width:20%;
float:left;
position:fixed;
border:1px solid red
}
#right
{
width:80%;
float:right;
position:absolute;
left:20%;
bottom:0;
border:1px solid red
}
Try This Code:
Demo
css
#left
{
width:20%;float:left;position:fixed;left:0px;
border:1px solid yellow;
}
#right
{
width:80%;float:right;
border:1px solid red;
}
html
<div id="container">
<div id="left">fixed width</div>
<div id="right">rest of space width</div>
</div>
Hope this would help...
#container {
width: 100%;
}
#left {
padding: 50px;
background: #ccc;
vertical-align: bottom;
display: inline-block;
/* ie6/7 */
*display: inline;
zoom: 1;
float:left;
}
#right {
padding-top: 20px;
padding-bottom: 20px;
background:#000;
color:#fff;
width: 79%;
float: right;
}
What i can get from your question is you want a fixed div on left side and the other div should take the remaining space on right and on the bottom of left div. I think a better approach would be to make a single big div and then float a fixed div on left and then you can use the remaining portion to position individual elements with css positioning property

Inline-block elements within a fixed width container with overflow:hidden get pushed down

See my fiddle here: http://jsfiddle.net/Waterstraal/bMfbH/2/
The HTML:
<div class="row">
<div class="actionPanel"></div><div class="resultPanel"></div>
</div>
The CSS:
.row {
width:500px;
height: 50px;
overflow:hidden;
border:1px solid #ccc;
}
.resultPanel {
display:inline-block;
width:450px;
height: 50px;
background: #ddd;
}
.actionPanel {
display:inline-block;
width:50px;
height: 50px;
background:#eee;
}
I want to "slide" the resultpanel to the right (so it's still on the same level as the actionPanel), but instead it gets pushed down out of view.
The width of the actionPanel is being made bigger in javascript so that the total width of the two elements are bigger than the width of the parent element.
Does anyone know how I can achieve the effect i'm after? I've tried to use floating elements, but that had the same result. I also tried to use a table element, to no effect.
Thank you in advance.
Add white-space: nowrap to the container of the inline-block elements, in this case .row.
http://jsfiddle.net/thirtydot/bMfbH/3/
Change css:
body {
padding:10px;
}
.row {
width:500px;
height: 50px;
overflow:hidden;
border:1px solid #ccc;
}
.resultPanel {
height: 50px;
background: #ddd;
}
.actionPanel {
float:left;
width:50px;
height: 50px;
background:#eee;
}

Resources