At first I have to that I read a lot of tutorials and still I don't know what am I doing wrong...
I would like to use 4 divs inline. In those divs I would like to put: text, image, text, text. And I would like the middle text to set automatically to the maximum width.
I wrote a simple code, just to show my problem:
<div>
<div style="float: right; width: 100px; background-color: red">right</div>
<div style="margin-right: 100px; background-color: blue">
<div style="width: 100px; background-color: green">left</div>
<div style="width: 100px; margin-left: 100px; background-color: pink">
<img src="../zdjecia_przedmiotow/1_small.jpg" />
</div>
<div style="margin-left: 200px; background-color: green">center</div>
</div>
</div>
And it looks like this:
I would like to do it using divs!
what am I missing?
First you need to float those divs inside so they align next to each other. Then you can use calc() to make the last container take the rest of the width;
FLOAT EXAMPLE
OR
You can use display: table on the parent and set the children to display: table-cell like so:
TABLE EXAMPLE
Also I restructured this a bit since there was some unnecessary elements/styles in there:
HTML
<div class="wrapper">
<div class="box box1">left</div>
<div class="box box2">
<img src="../zdjecia_przedmiotow/1_small.jpg" />
</div>
<div class="box box3">center</div>
<div class="box box4">right</div>
</div>
CSS
.wrapper{
overflow:hidden;
width:100%;
}
.box{
float: left;
}
.box1{
width: 100px;
background-color: green;
}
.box2{
width: 100px;
background-color: pink;
}
.box3{
background-color: green;
width:calc(100% - 300px);
}
.box4{
width:100px;
background-color: blue;
}
CLEANER FIDDLE
I simplified the HTML structure a bit and used floats. With the CSS left inline:
<div style="background-color:blue;">
<div style="width: 100px; background-color: green; float:left;">left</div>
<img src="../zdjecia_przedmiotow/1_small.jpg" style="width: 100px; background-color: pink; float:left;" />
<div style="background-color: green; width:calc(100% - 300px); float:left;">center</div>
<div style="width: 100px; background-color: red; float:right;">right</div>
</div>
After CSS is out:
.box{background-color:blue}
.left{width: 100px; background-color: green; float:left;}
.fill{background-color: pink; width:calc(100% - 300px);}
.right{width: 100px; background-color: red; float:right;}
<div class="box">
<div class="left">left</div>
<img class="left" src="../zdjecia_przedmiotow/1_small.jpg"/>
<div class="left fill">center</div>
<div style="right">right</div>
</div>
Fiddle
Related
My code structure looks like this
<div style="height: 100px;
Width: 200px;"> <!-- Container -->
<div style="float: left;
height: 50px;
Width: 100px;
background-color: red;">
</div>
<div style="float: left;
height: 50px;
Width: 100px;
background-color: blue;">
</div>
<div style="float: right;
height: 50px;
Width: 100px;
background-color: green;">
</div>
</div>
But the right position of elements should look like this:
┌──────┬──────┐
│ red │green │
├──────┼──────┘
│ blue │
└──────┘
I cannot change or add any additional code, the only way is with CSS.
How should I float the divs to be in the right order as I mentioned above?
Edit: My code doesn't and can't contain div with clear.
you dont need floating for that. disable all floating using !important to override the inline styles, and then use :nth-of-type() to select the green div and position it absolutely with right and top equal 0;
div {
position: relative;
}
div > div{
float: none !important;
}
div > div:nth-of-type(3) {
position: absolute;
right: 0;
top:0;
}
<div style="height: 100px; Width: 200px;">
<!-- Container -->
<div style="float:left; height: 50px; Width:100px; background-color:red;">
</div>
<div style="float:left; height: 50px; Width:100px; background-color:blue;">
</div>
<div style="float:right; height: 50px; Width:100px; background-color:green;">
</div>
</div>
You can use clear: left on the blue box to push it down and then use negative margin on the green box to push it up.
<div style="height: 100px; Width: 200px;">
<!-- Container -->
<div style="float:left;height: 50px;
width:100px; background-color:red;">
</div>
<div style="float:left;clear:left;
height: 50px; Width:100px; background-color:blue;">
</div>
<div style="float:left; height:50px;
width:100px; background-color:green;margin-top:-50px;">
</div>
</div>
Well this is more like a puzzle instead of a legit question but here goes.
With the proper use of margins and positions in addition to assigning null to clear property one can accomplish your scenario.
<div style="height: 100px; Width: 200px;">
<!-- Container -->
<div style="float:left; height: 50px; Width:100px; background-color:red;"></div>
<div style="float: right; height: 50px; margin-top: 50px;Width:100px; background-color:blue;position: absolute;"></div>
<div style="clear: none;"></div>
<div style=" height: 50px; margin-left: 100px;margin-bottom: 50px;Width:100px; background-color:green;"></div>
</div>
</div>
Keeping the same HTML structure, you could select the divs in CSS using :nth-child(N). In this case you'd just need to update the blue (2) and green (4) boxes, and the one with the clear:both style (3):
div > div:nth-child(2) {
margin-top: 50px;
}
div > div:nth-child(3) {
display: none;
}
div > div:nth-child(4) {
margin-top: -100px;
}
<div style="height: 100px; Width: 200px;">
<!-- Container -->
<div style="float:left; height: 50px; Width:100px; background-color:red;">
</div>
<div style="float:left; height: 50px; Width:100px; background-color:blue;">
</div>
<div style="clear:both;"></div>
<div style="float:right; height: 50px; Width:100px; background-color:green;">
</div>
</div>
Notice that this will work for this particular example. It would be ideal if the container div had an id and use that instead of div >.
For a more generic solution that would work independently of the height of the boxes, you could use transform:translate() like this:
div > div:nth-child(2) {
transform:translate(0%, 100%);
}
div > div:nth-child(3) {
display:none;
}
div > div:nth-child(4) {
transform:translate(0%, -100%);
}
As you can see on this JSFiddle: http://jsfiddle.net/eekhjv3n/1/
I have some HTML code that looks like this:
<div class="container">
<div class="element"> </div>
<div class="element"> </div>
<div class="element"> </div>
<div class="element"> </div>
</div>
And I want to display it in a two-column layout, where each element is displayed directly underneath the one above. I've made a JSFiddle to show my current progress, but I can't figure out how to remove the white gaps between the elements. Is it at all possible, or do i need to change the HTML (I'd rather not)?
An easy way would be to wrap each column items into separate divs. Your .box and .one, .two, .three css declarations are interfering.
[http://jsfiddle.net/grLyvomy/][1]
You could use a seperate div for each column (in your case two).
.container{
border: 1px black solid;
width: 320px;
}
.clear{
clear: both;
}
.leftColumn{
width: 50%;
float: left;
}
.rightColumn{
width: 50%;
float: right;
}
.box:nth-child(2n+1){
background: green;
border-bottom: 1px solid red;
}
.box:nth-child(2n){
background: red;
border-bottom: 1px solid green;
}
.one{ height: 50px; }
.two { height: 80px; }
<div class="container">
<div class="leftColumn">
<div class="box one">first</div>
<div class="box two">second</div>
<div class="box three">third</div>
</div>
<div class="rightColumn">
<div class="box else">first</div>
<div class="box two">second</div>
<div class="box three">third</div>
<div class="box four">fourth</div>
<div class="box one">last</div>
</div>
<div class="clear"></div>
</div>
http://jsfiddle.net/nvmcxjpL/8/
Try to change one and three divs height sum same as two by changing three div height 20px to 30px
.three {
float: left;
width: 50%;
height: 30px;
}
I'm trying to make this kind of responsive design with CSS.
It basically should be 600px width when there's enough space to show whole length.
When not, it folds, then right partial come appears on the bottom of left partial.
I'm struggling how to archive this.
This is DEMO that I could go this far
http://jsfiddle.net/a7Fkj/5/
HTML
<div class="table_row">
<div class="left_partial">
<div class="StoreName">Walmart Store</div>
<div class="Location">Located in California</div>
</div>
<div class="right_partial">
<div class="store_icon"><img src="https://twimg0-a.akamaihd.net/profile_images/616833885/walmart_logo_youtube.jpg"><div>
<div class="person1">John Smith<div>
<div class="person2">Mike Tailor<div>
<div class="person3">Jessica Swan<div>
</div>
</div>
CSS
div.table_row{
min-width: 300px;
margin-bottom: 30px;
}
div.left_partial{
width: 300px;
}
div.right_partial{
width: 300px;
}
div.StoreName{
background-color: #000000;
color: #FFFFFF;
}
div.Location{
}
div.store_icon{
width: 60px;
height: 60px;
}
div.person1{
}
div.person2{
}
div.person3{
}
You don't need media queries for this design- you can use inline-block to collapse the layout when the browser is resized.
HTML:
<div class="table_row">
<div class="left_partial">
<div class="StoreName">Walmart Store</div>
<div class="Location">Located in California</div>
</div>
<div class="right_partial">
<div class="store_icon"><img src="https://twimg0-a.akamaihd.net/profile_images/616833885/walmart_logo_youtube.jpg" /></div>
<div class="people">
<div class="person">John Smith</div>
<div class="person">Mike Tailor</div>
<div class="person">Jessica Swan</div>
</div>
</div>
</div>
CSS:
div.table_row{
min-width: 300px;
margin-bottom: 30px;
}
div.left_partial{
width: 300px;
display:inline-block;
vertical-align:top;
}
div.right_partial{
width: 300px;
display:inline-block;
vertical-align:top;
margin-bottom:30px;
}
div.StoreName{
background-color: #000000;
color: #FFFFFF;
}
div.Location{
}
div.store_icon{
width: 60px;
height: 60px;
display:inline-block;
vertical-align:top;
}
div.store_icon img{
width:100%;
height:100%;
}
div.people{
display:inline-block;
vertical-align:top;
height:60px;
width:234px;
}
jsfiddle: http://jsfiddle.net/kmMEM/
This will collapse the design when the browser is resized- I added a 30px bottom margin to your right_partial to ensure the list stacks correctly.
what i have is 3 divs, 1 for left 1 for center and 1 for right
what i need is 3 columns - the left is always there, the center as well but it's width should be adaptive if the right is there or not
<html>
<head>
</head>
<body>
<div style="width:460px; padding:0px 20px;">
<div style="float:left; background: red; width:100px;">
red
</div>
<div style="float:left; background: yellow; max-width:400px">
yellow
</div>
<div style="float:left; background: green; width:100px;">
green
</div>
</div>
</body>
</html>
what am i'm doing wrong?
The best I could come up with, in order to avoid JavaScript solutions and to use CSS and HTML only, is to use class-names for the columns, and to re-order your HTML in order that the right-most column is first in the html:
<div class="wrap" style="width:500px">
<div class="col right">Right Column</div>
<div class="col left">Left column</div>
<div class="col middle">Middle column</div>
</div>
<div class="wrap" style="width:500px">
<div class="col left">Left column 2</div>
<div class="col middle">Middle column 2</div>
</div>
With the CSS adjacent-sibling selector, firstSibling + secondSibling, this can be used to amend the width of the middle column:
.wrap {
width: 500px;
margin: 1em auto;
overflow: hidden;
}
.left,
.right {
width: 100px;
background-color: #ffa;
}
.middle {
width: 400px;
background-color: #f90;
}
.left,
.middle {
float: left;
}
.right {
float: right;
}
div.right + div.left + div.middle {
width: 300px;
}
JS Fiddle demo.
If you float the .middle column right, instead of left as in the previous example, then you can simplify the adjacent-sibling selector, and the HTML is, effectively, visually reversed (which is slightly easier to understand/work with than the above example wherein the two columns come first, in reverse order, and then the middle column comes at the end), giving:
<div class="wrap" style="width:500px">
<div class="col right">Right Column</div>
<div class="col middle">Middle column</div>
<div class="col left">Left column</div>
</div>
<div class="wrap" style="width:500px">
<div class="col left">Left column 2</div>
<div class="col middle">Middle column 2</div>
</div>
And the CSS:
.wrap {
width: 500px;
margin: 1em auto;
overflow: hidden;
}
.left,
.right {
width: 100px;
background-color: #ffa;
}
.middle {
width: 400px;
background-color: #f90;
}
.left {
float: left;
}
.right,
.middle {
float: right;
}
div.right + div.middle {
width: 300px;
}
JS Fiddle demo.
References:
Adjacent sibling selector, at the W3.org.
Width includes borders, you have to take that into consideration. If you decrease the width of the center element a bit, the right will not wrap.
I don't however see anything in your code which would handle the center width being "adaptive".
Not sure exactly what you're trying to do, but this might work. If you can give a little more detail about what it is supposed to do, it would help, but this seems to do what you want. If the right column is set to display:none then the center still goes all the way over.
<html>
<head>
</head>
<body>
<div style="width:480px; padding:0px 20px;">
<div style="float:left; background: red; width:100px;">
red
</div>
<div style="float:right; background: green; width:100px;>
green
</div>
<div style="background: yellow; width:100%;">
yellow
</div>
</div>
</body>
</html>
I have an issue with floating divs. I have a container st to fixed width, and I have child elements inside that which are all div elements. What I want is that, I need two elements to be shown in a row. The code that I wrote is as follows.
CSS
#container
{
width: 400px;
}
.item1
{
width: 180px;
height: 200px;
border: 1px solid red;
float: left;
margin: 10px 0 0 10px;
}
.item2
{
width: 180px;
height: 250px;
border: 1px solid red;
float: left;
margin: 10px 0 0 10px;
}
HTML
<div id="container">
<div class="item1">1</div>
<div class="item1">2</div>
<div class="item1">3</div>
<div class="item1">4</div>
<div class="item1">5</div>
<div class="item1">6</div>
<div class="item1">7</div>
<div class="item1">8</div>
<div class="item1">9</div>
</div>
This can be viewed at Demo1
But what I want is like this result. The only thing is that the height of the individual items can be different.
Hope I have made everything clear.
Thanks in advance
Additional clarification
The content elements will be generated dynamically in server and will be passed to the client.
Also the order should be like 1,2,3,4,...
The only thing is that in a row there should be two items and the first one should be aligned to the left side of the container.
You can't accomplish that with CSS only, but there is a jQuery plugin to do the trick. It's called jQuery Masonry, give it a try
You need a second wrapper:
<div id="container">
<div class="wrapper"><div class="item1">1</div></div>
<div class="wrapper"><div class="item1">2</div></div>
...
</div>
Float the wrapper and give it a fixed size. The items inside can have their own height.
I prefer using lists for this type of thing. Better HTML semantics.
<div class="container">
<ul>
<li><div class="item1">1</div></li>
<li><div class="item2">2</div></li>
</ul>
</div>
style:
.container ul {
width:400px;
}
.container li {
float:left;
height:200px;
width:180px;
}
If you want each pair of items to be in a row, and you have control over the dynamic generation of the content, see my edits to your fiddle here
To summarize:
Markup -
<div id="container">
<div class="itemrow">
<div class="item1">1</div>
<div class="item1">2</div>
</div>
<div class="itemrow">
<div class="item2">3</div>
<div class="item1">4</div>
</div>
<div class="itemrow">
<div class="item2">5</div>
<div class="item1">6</div>
</div>
<div class="itemrow">
<div class="item1">7</div>
<div class="item2">8</div>
</div>
<div class="itemrow">
<div class="item1">9</div>
</div>
</div>
CSS -
#container
{
width: 400px;
}
.itemrow
{
float: left;
clear: both;
}
.item1
{
width: 180px;
height: 200px;
border: 1px solid red;
float: left;
margin: 10px 0 0 10px;
}
.item2
{
width: 190px;
height: 250px;
border: 1px solid red;
float: left;
margin: 10px 0 0 10px;
}
Edit: Just read your above comment about having to edit the server side logic for rendering. Obviously this will only work if you can control that.
you're specifying item2 to be 10 pixels wider than item1 so I'm not clear on what you're trying to do....