I'm working on a small PHP script, I made 2 divs and I used the float to show the two divs in the same line. But I still have a problem with background because the two divs do not have the same height.
This is the css code:
.wrapper{
width:200px;
}
.content{
width:200px;
}
.right{
float:right;
width:100px;
background:yellow;
}
.left{
float:right;
width:100px;
background:red;
}
.clear{
clear:both;
}
And this is the html code:
<div class="wrapper">
<div class="content">
<div class="right">
sdiousoiudosud sdiousoiudosud sdiousoiudosud sdiousoiudosud
</div>
<div class="left">
iuoiu
</div>
<div class="clear">
</div>
</div>
</div>
You should use display: table-cell:
.left, .right{
display: table-cell;
width: 100px;
}
.right{
background:yellow;
}
.left{
background:red;
}
Demo
If you want you can also use display: table-row and display:table, and set all widths. But it isn't necessary. Demo
The "good way" is using display: table-cell, as I explained in my other answer.
But if you want to support old browsers like IE7, you can use the following trick:
.content{
overflow: hidden;
}
.left, .right{
float: left;
width: 100px;
padding-bottom: 10000px;
margin-bottom: -10000px;
}
.right{
background:yellow;
}
.left{
background:red;
}
Where 10000px can be any value greater than the height of the highest element.
Demo
Related
I need to do a website top with some navegations tools.
It is working but I'm not confortable with. I think maybe it is not the right way to do these floating divs on the right.
I need an image on the left and two itens on the right of a full width div.
So I did:
<div id="menu">
<div id="logo">LOGO</div>
<div id="item">Settings</div>
<div id="item">Options</div>
</div>
and
#menu{
position:fixed;
width:100%;
height:50px;
background:#fff;
}
#logo{
float:left;
right:30px;
}
#item{
float:right;
right:30px;
margin-right:10px;
}
Is it ok with float right and everything else or should I change something?
jsfiddle
on #item the right:30px does nothing if you dont specify the postion. Use
#item{
float:right;
position:relative;
right:30px;
}
Flexbox...no need for floats or positioning at all....and the items are in the right order.
#menu {
position: fixed;
width: 100%;
height: 50px;
background: #fff;
display: flex;
}
.logo {
margin-right: auto;
}
.item {
margin-right: 10px;
}
<div id=menu>
<div class="logo">LOGO</div>
<div class="item">Settings</div>
<div class="item">Options</div>
</div>
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;
}
I am trying to make a 3 column website. Left and right columns are small 240px divs attached to the sides. The middle div is a stretchable area where all the elements inside stretch according to the size of the browser.
So far I have it set up like this :
body, html {
height:100%;
}
body {
margin:0;
}
.container {
background:orange;
height:100%;
width:100%;
overflow:hidden;
}
.left {
width:240px;
height:100%;
background:rgba(0,0,0,0.5);
position:absolute;
top:0;
left:0;
}
.middle {
width:100%;
height:100%;
background:orange;
}
.right {
width:240px;
height:100%;
background:rgba(0,0,0,0.5);
position:absolute;
top:0;
right:0;
}
And:
<div class="container">
<div class="left"></div>
<div class="middle">
// all the content
</div>
<div class="right"></div>
</div><!--container-->
How do I make the content in the middle column stay in between the left and right columns? I was thinking to use margin-left and margin-right but I feel it is not a good way of doing it.
Live:
http://codepen.io/daydreamer/pen/0479cc8de929cedc2ac519280a3044aa
If you are supporting modern browsers, I would try using flexbox:
.container {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
}
.container div {
flex-grow: 1;
height: 50px;
}
.side {
max-width: 240px;
min-width: 240px;
background: red;
}
<div class="container">
<div class="side"></div>
<div class="middle">
// all the content
</div>
<div class="side"></div>
</div>
jsfiddle example
Flexbox resource
You don't need to use margin-left, but margin-right would be useful. I would use float: left and get rid of position: absolute on the left sidebar, and use margin-right: 240px and get rid of width: 100% on the middle div.
CSS:
.left {
width:240px;
height:100%;
background:rgba(0,0,0,0.5);
top:0;
left:0;
float:left;
}
.middle {
height:100%;
background:orange;
margin-right: 240px;
}
.right {
width:240px;
height:100%;
background:rgba(0,0,0,0.5);
position:absolute;
top:0;
right:0;
}
Use Twitter Bootstrap to do n-column design and it will save you a lot of work. Right click on inspect the HTML code on the example page I provided and you'll see that all you need to do is set classes to a few div's and it works when you include the bootstrap JS/CSS files.
I've this problem, i want to centralize a div inside a header, the problem is: I've 2 another div(one using float left and another using float right), see below:
<header>
<div class="left">Hello World App Left</div>
<div class="center">Center</div>
<div class="right">Right</div>
</header>
I try to use this css:
header{
width: 100%;
background-color:black;
margin-top:50px;
text-align:center;
}
.left{
float: left;
}
.center{
display: inline-block;
}
.right{
float: right;
}
But this didn't centralize my center div(almost, but didn't because it was affect by the another 2 div's), what options here i've to centralize this div?
See my plnkr.co
Try floating all divs left and setting width:33% to each.
To work off of a previous answer as a possible solution, you could indeed set each to 33%, but to prevent it from scaling, wrap the whole thing in a div. EG:
<div id='wrapper'>
<header>
<div class="left">Hello World App Left</div>
<div class="center">Center</div>
<div class="right">Right</div>
</header>
</div>
css:
header{
width: 100%;
background-color:black;
margin-top:50px;
text-align:center;
}
.left{
float left;
width:33%;
}
.center{
float:left;
width:34%;
}
.right{
float:left;
width:33%;
}
#wrapper{
width:1000px;
}
I have one content div with 960px of width and margin 0 auto (centralized), i want put one div occupying all the space of the left margin, how can i do that?
demo jsBin
#container{
position:relative;
width:300px;/*change this*/
margin:0 auto;
height:200px;
background:#cf5;
padding-left:50%;
margin-left:-150px; /*half the width*/
}
#centered{
width:300px;
height:100%;
position:absolute;
right:0px;
background:#eea;
}
HTML:
<div id="container">
<div id="centered">centered</div>
</div>
HERE: http://jsbin.com/oluwos/3/edit is another way to do it.
Use display: table, like this:
http://jsfiddle.net/yVFzh/
<div class="wrapper">
<div class="left-column"> </div>
<div class="centre"></div>
<div class="right-column"> </div>
</div>
html,
body{
width:100%;
}
.wrapper{
display:table;
width:100%;
}
.wrapper > div{
display: table-cell;
background: blue;
height:400px;
}
.wrapper .centre{
width: 960px;
background: yellow;
}