I'm trying to create a responsive grid with a defined pattern, like this:
right now i have working part of it here:
grid demo
But I can't put all the columns in the right place, the big box on the right side never has 2 boxes on its left.
This is the code for the container:
<div class="container">
<div class="box">
<p>box1 BIG</p>
</div>
<div class="box">
<p>box2</p>
</div>
<div class="box">
<p>box3</p>
</div>
<div class="box">
<p>box4</p>
</div>
<div class="box">
<p>box5</p>
</div>
<div class="box">
<p>box6 BIG</p>
</div>
<div class="box">
<p>box1 BIG</p>
</div>
<div class="box">
<p>box2</p>
</div>
<div class="box">
<p>box3</p>
</div>
<div class="box">
<p>box4</p>
</div>
<div class="box">
<p>box5</p>
</div>
<div class="box">
<p>box6 BIG</p>
</div>
</div>
and this is the css:
.container {
max-width: 1000px;
margin: 0 auto;
background-color: #5B83AD;
}
.box {
background-color: #5B83AD;
width: 50%;
float: left;
height: 200px;
}
.box:nth-child(6n+1){
background-color: #444444;
height: 400px;
}
.box:nth-child(6n){
background-color: #992277;
height: 400px;
}
#media screen and (max-width: 620px) {
.box {
clear: both;
width: 100%;
}
}
#media screen and (min-width: 621px) {
.box {
clear: none;
width: 50%;
}
}
I want the boxes to keep always the same layout:
1 big - 2 half height
2 half height - 1 big
...
And i need it to work on IE8 too
Is there a way to achieve this layout(it has to be responsive and if i remove a box the layout has to re-adapt)?
They are out of order, but maybe you'll accept it I'm not sure. Box 4 is the big one not 6 which makes it a little strange but the design still follows the pattern.
Sorry if this isn't what you wanted.
http://jsfiddle.net/UN2DH/3/
The only major change is this from :nth-child(6n) to nth-child(6n+4) and the added float right to that rule.
.box:nth-child(6n+4){
background-color: #992277;
height: 400px;
float: right;
}
HTML
<div class="container">
<div class="box">
<p>box1 BIG</p>
</div>
<div class="box">
<p>box2</p>
</div>
<div class="box">
<p>box3</p>
</div>
<div class="box">
<p>box4 BIG</p>
</div>
<div class="box">
<p>box5</p>
</div>
<div class="box">
<p>box6</p>
</div>
<div class="box">
<p>box1 BIG</p>
</div>
<div class="box">
<p>box2</p>
</div>
<div class="box">
<p>box3</p>
</div>
<div class="box">
<p>box4 BIG</p>
</div>
<div class="box">
<p>box5</p>
</div>
<div class="box">
<p>box6</p>
</div>
</div>
CSS
.container {
max-width: 1000px;
margin: 0 auto;
background-color: #5B83AD;
}
.box {
background-color: #5B83AD;
width: 50%;
float: left;
height: 200px;
}
.box:nth-child(6n+1){
background-color: #444444;
height: 400px;
}
.box:nth-child(6n+4){
background-color: #992277;
height: 400px;
float: right;
}
#media screen and (max-width: 620px) {
.box {
clear: both;
width: 100%;
}
}
#media screen and (min-width: 621px) {
.box {
clear: none;
width: 50%;
}
}
EDIT:
I forget you don't really need the float:left on the .box:nth-child(6n+5), .box:nth-child(6n+6) rule so I removed it above. (so just a heads up if you used that code. It's not necessary because the .box rule already had a float: left) I also updated the jsfiddle above. Heres the old JSfiddle previously its CSS, and the CSS above, had this rule.
.box:nth-child(6n+5), .box:nth-child(6n+6){
float: left;
}
EDIT:
Okay so I just had to have a little fun. :)
I added the following to a new JSFiddle. To make it look like your picture. I wasn't sure how many lines of text you were going to have, but the vertical centering will only work if it's one line of text so keep that in mind. If you want it centered otherwise you'll have to use some other method, like this, and if you know the height and the width of the div or image you want to center here is a great method that will work. Just make sure your parent div is position: relative., if you're using the code from that 'great method', or else this will center this in your whole browser window or the closest element with positioning of fixed, absolute, or relative to it. (more about positioning here.)
I added most of the styles at the bottom of the style sheet just to seperate the old from the new so you could tell what's different, but first I'll show you what styles I added to the existing code. I changed two background colors for .box:nth-child(6n+1) I added background-color: #676767; and for .box:nth-child(6n+4) I added background-color: #CDCDCD;.
Here are the changes I made at the bottom of the stylesheet. (if you decide to go with this you can merge the style rules together.)
.box {
text-align: center;
box-sizing: border-box;
line-height: 200px;
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
color: #fff;
}
.box:nth-child(3n+1) {
line-height: 400px;
}
.box p {
margin: 0;
}
.box:nth-child(3n+2) {
background-color: #7ACDC8;
}
.box:nth-child(3n+3) {
background-color: #3CB878;
}
Yeah, I know now my answer is just ridiculously long...
Anyways, hope you like it. :) If not that's okay too.
Maybe this is what you want. Demo.
.box:nth-child(1),.box:nth-child(7n){
background-color: red;
height:200px;
}
.box:nth-child(2n){
background-color: green;
height:100px;
}
.box:nth-child(3n){
background-color: yellow;
height:100px;
}
.box:nth-child(5),.box:nth-child(11){
background-color:grey;
height:200px;
float:right;
}
You just need to play around with css3 :nth-child() selector.
Related
I'm new to both HTML and CSS. Currently, I'm trying to find out how to arrange a div with four textboxes, which includes a heading, paragraph and an img located to the left. The entire thing should look like this:
Starting by selecting a div bottompane, since this goes on the bottom part of the page. Giving it a width and height which will be inherited to the following divs.
As you can see, this looks far from what it is supposed to be. Right now, I don't know how to approach further from this step. I'm struggling with the syntax and knowledge about CSS.
Can anyone lend me a hand with this? Code written below:
.bottompane {
width: 100%; /* defines overall width of bottompane */
height: 300px; /* defines overall height of bottompane */
background-color: silver;
position: relative;
}
.row {
text-align: center;
}
.features-text {
width: 50%; /* 50% width of the parent directory bottompane */
height: 120px;
display: inline-block;
}
.text-upperright {
width: 50%;
}
.text-upperleft {
width: 50%;
}
.text-bottomleft {
width: 50%;
}
.text-bottomright {
width: 50%;
}
#cat {
float: right;
}
#world {
float: left;
}
#swim {
float: right;
}
#columns {
float: left;
}
h1 {
}
p {
}
<div class="bottompane">
<div class="row">
<div class="features-text">
<div class="text-upperleft">
<h1>Best-in-class Features</h1>
<p>Nobody likes this stuff better than us, you can bet your life on that.</p>
</div>
<img src="cat.svg" alt="Cat Picture">
</div>
<div class="features-text">
<div class="text-upperright">
<h1>Reliable Service</h1>
<p>You can count on us to help you whenever you need it. We're talking round the clock service.</p>
</div>
<img src="world.svg" alt="World Map">
</div>
</div>
<div class="row">
<div class="features-text">
<div class="text-bottomleft">
<h1>An Acquired Taste</h1>
<p>It may take a little while for you to warm up to us but once you do you will never want to switch.</p>
</div>
<img src="swim.svg" alt="Person swimming">
</div>
<div class="features-text">
<div class="text-bottomright">
<h1>No Limits</h1>
<p>There are absolutely no limits. We do not throttle. We do not cap.</p>
</div>
<img src="columns.svg" alt="Greek/Roman columns">
</div>
</div>
</div>
I hope this help!
<html>
<style>
.bottompane {
width: 100%; /* defines overall width of bottompane */
height: 300px; /* defines overall height of bottompane */
background-color: silver;
position: relative;
}
.row {
text-align: center;
}
.features-text {
width: 50%; /* 50% width of the parent directory bottompane */
height: 120px;
display: inline-block;
position:relative;
float:left;
}
.text-upperright {
width: 50%;
}
.text-upperleft {
width: 50%;
}
.text-bottomleft {
width: 50%;
}
.text-bottomright {
width: 50%;
}
.imageClass{
float:left;
width: 50px;
}
#cat {
float: right;
}
#world {
float: left;
}
#swim {
float: right;
}
#columns {
float: left;
}
h2 {
}
p {
margin-left:35%;
text-align:left;
}
</style>
<div class="bottompane">
<div class="row">
<div class="features-text">
<div class="text-upperleft">
<img src="cat.svg" alt="Cat Picture" class="imageClass">
<h2>Best-in-class Features</h2>
<p>Nobody likes this stuff better than us, you can bet your life on that.</p>
</div>
</div>
<div class="features-text">
<div class="text-upperright">
<img src="world.svg" alt="World Map" class="imageClass">
<h2>Reliable Service</h2>
<p>You can count on us to help you whenever you need it. We're talking round the clock service.</p>
</div>
</div>
</div>
<div class="row">
<div class="features-text">
<div class="text-bottomleft">
<img src="swim.svg" alt="Person swimming" class="imageClass">
<h2>An Acquired Taste</h2>
<p>It may take a little while for you to warm up to us but once you do you will never want to switch.</p>
</div>
</div>
<div class="features-text">
<div class="text-bottomright">
<img src="columns.svg" alt="Greek/Roman columns" class="imageClass">
<h2>No Limits</h2>
<p>There are absolutely no limits. We do not throttle. We do not cap.</p>
</div>
</div>
</div>
</div>
</html>
If you don't mind grids you can do it this way, it's much shorter:
<div class="features-text">
<div><img src="swim.svg" alt="Person swimming"></div>
<div>
<h1>An Acquired Taste</h1>
<p>It may take a little while...</p>
</div>
</div>
.bottompane {
width: 100%; /* defines overall width of bottompane */
height: 300px; /* defines overall height of bottompane */
background-color: silver;
position: relative;
display: grid;
grid-template-columns: 50% 50%;
}
.features-text {
width: 90%;
height: 120px;
display: grid;
grid-template-columns: 20px auto;
}
And set padding and so on accordingly. And remove row div, of course.
The following markup and CSS causes weird behaviour in Chrome:
#parent {
background: yellow;
height: 120px;
margin-bottom: 20px;
width: 100px;
word-wrap: break-word;
}
#box {
background: red;
float: left;
height: 100px;
width: 100%;
}
<div id="parent">
<div id="box"></div>
<div>OIL</div>
</div>
<div id="parent">
<div id="box"></div>
<div>OWL</div>
</div>
Fiddle: https://jsfiddle.net/7sq3ybxr/
The upper example (containing the word OIL), causes word breaks even though there is literally no room to the right. The lower one doesn't. I'm assuming it's got something to do with the character width. In other browsers, the word is always displayed below the box, as expected.
Does anybody know what causes this behaviour or have an idea for a workaround? The size, float and word-wrap properties must stay, however.
Edit: Oh, by the way, writing the markup like this seems to fix it, but it's not something I have control over (imagine user input via tinyMCE):
<div id="parent">
<div id="box"></div>
<div>
OIL
</div>
</div>
This appears to be a bug in Chrome.
In Chrome 50.0.2661.102 m the expected result was displayed
In Chrome 51.0.2704.103 m the undesired result is displayed
What can be done to avoid this issue?
I imagine that this bug will be fixed in time, but in the meantime you could use letter-spacing to increase the amount of space taken by the letters and force the expected behaviour.
.parent {
background: yellow;
height: 120px;
margin-bottom: 20px;
width: 100px;
word-wrap: break-word;
}
.box {
background: red;
float: left;
height: 100px;
width: 100%;
}
.word {
letter-spacing: 1.8px;
}
<div class="parent">
<div class="box"></div>
<div class="word">OIL</div>
</div>
<div class="parent">
<div class="box"></div>
<div class="word">OWL</div>
</div>
JS Fiddle
Floating elements in css causes errors like this. In previous versions of Internet explorer, we used to see problems like this all the time.
If I float one element, and want others stay intact, I usually use "clear:both" on the next element so that that element and the elements after that would not be effected.
#parent {
background: yellow;
height: 120px;
margin-bottom: 20px;
width: 100px;
word-wrap: break-word;
}
#box {
background: red;
height: 100px;
float:left;
width: 100%;
}
.box-2 {
clear:both;
}
<div id="parent">
<div id="box"></div>
<div class="box-2">OIL</div>
</div>
<div id="parent">
<div id="box"></div>
<div>
OIL
</div>
</div>
Floating elements are out-of-flow, so they overlap following blocks, unless they establish a block formatting context. That shouldn't be a problem, because line boxes are reduced, so the text should be pushed below the float in a full-width line box.
However, for some reason, this layout confuses Chrome. To fix this, you can establish a block formatting context with display: inline-block.
#parent {
background: yellow;
height: 120px;
margin-bottom: 20px;
width: 100px;
word-wrap: break-word;
}
#box {
background: red;
float: left;
height: 100px;
width: 100%;
}
#box + div {
display: inline-block;
}
<div id="parent">
<div id="box"></div>
<div>OIL</div>
</div>
<div id="parent">
<div id="box"></div>
<div>OWL</div>
</div>
Give a space character after the OIL or any unbreakable word. This might fix the error. Since word-wrap is breaking all the alphabets in the word.
#parent {
background: yellow;
height: 120px;
margin-bottom: 20px;
width: 100px;
word-wrap: break-word;
}
#box {
background: red;
float: left;
height: 100px;
width: 100%;
}
<div id="parent">
<div id="box"></div>
<div>OIL </div>
</div>
<div id="parent">
<div id="box"></div>
<div>OWL</div>
</div>
I have a responsive website with max-width set to 1000px, but I need to fit background picture that will overlap one of the divs and also place full page-width bottom borders to other divs.
The code i have is like this:
.container {
width: 100%;
max-width: 1000px;
}
.logotest {
background-color: #03b9e5;
height: 50px;
}
.navtest {
background-color: #e4ed00;
height: 25px;
}
.socialtest {
background-color: #ab801a;
height: 25px;
}
.main {
height: 750px;
background: url(background.jpg) no-repeat top center;
margin: auto;
}
.line {
border-bottom: 1px solid black;
}
.container:after {
clear: both;
content: ".";
display: block;
height: 0;
visibility: hidden;
}
<body>
<div class="container" id="first">
<div class="logotest">
</div>
<div class="socialtest">
</div>
<div class="navtest">
</div>
</div>
<div class="line"></div>
<div class="main line" id="second">
</div><div class="container">
<div id="third">
</div>
</div>
</body>
I get the first div with correct width and bottom border going across the full page width, second div has got the background picture showing, but the max-width of 1000px does no longer apply. The bottom border is shown correctly (dividing second and third div) and the third div has got the correct max-width applied again.
What am I doing wrong/not doing to get the max-width for the second div?
YOUR SOLUTION
If the browser support of background-size property is good enough for you, you can use background-size: cover;. Check here or here to see browser support.
Here is the code snippet to show how it works. Be sure to position your background-image to center center if you want it to always be centered.
.container {
width: 100%;
max-width: 300px;
margin: 0 auto;
}
.line {
border-bottom: 1px solid black;
}
.logotest {
background-color: #03b9e5;
height: 50px;
}
.navtest {
background-color: #e4ed00;
height: 25px;
}
.socialtest {
background-color: #ab801a;
height: 25px;
}
.main {
height: 250px;
background: url(http://lorempixel.com/250/250) no-repeat center center;
background-size: cover; /* This does the magic */
}
.container:after {
clear: both;
content: ".";
display: block;
height: 0;
visibility: hidden;
}
<body>
<div class="container" id="first">
<div class="logotest">
</div>
<div class="socialtest">
</div>
<div class="navtest">
</div>
</div>
<div class="line"></div>
<div class="main" id="second">
<div class="container">Put your content in here.</div>
</div>
<div class="line"></div>
<div class="container">
<div id="third">
</div>
</div>
<div class="line"></div>
</body>
LAST (BUT NOT LEAST)
You might want to check this great article about the state of responsive images in web design, that will help you if you are going into responsive web design: Responsive images done right.
I didn't find an answer for this specific case of mine, so I decided to ask a new question. I want to have 2 DIVs on the left side of the page (with a fixed width) and a single DIV on the right side, occupying the rest of the page width. Also the single DIV on the right should have its independent height (when its height is increased it shouldn't affect the height or position of the DIVs on the left). Something like this is what I want:
This is the HTML code:
<body>
<div class="div1">Div1</div>
<div class="div3">Div3</div>
<div class="div2">Div2</div>
</body>
This is the CSS I have right now:
div.div1 {
float: left;
height: 400px;
margin-right: 10px;
width: 200px;
}
div.div3 {
height: 425px;
overflow: hidden;
}
div.div2 {
clear: left;
float: left;
height: 15px;
margin-top: 10px;
}
The only problem is that Div2 top position is affected by the height of Div3 and I get something like this:
Try this:
<html>
<head>
<style>
div.div1 {
float: left;
height: 400px;
margin-right: 10px;
width: 200px;
background-color: blue;
}
div.div2 {
clear: left;
float: left;
height: 15px;
width: 200px;
margin-top: 10px;
background-color: red;
}
div.div3 {
height: 425px;
overflow: hidden;
background-color: green;
}
</style>
</head>
<body>
<div class="div1">Div1</div>
<div class="div2">Div2</div>
<div class="div3">Div3</div>
</body>
</html>
Once I re-ordered the Divs and added a width for Div 2 it works fine
https://jsfiddle.net/6g7qx26b/
This also works if you replace the css height properties with min-height properties, allowing for greater flexibility. Widths may also be specified in percentages
now you can use the right content with overflow:hidden and not conflicting with the left divs.
Check this:
http://jsfiddle.net/6UyTr/1/
div.left-content { margin-right: 10px; overflow: hidden; width: 200px; float: left; }
Check it on http://jsfiddle.net/cz2fP/
<div style="float:left;">
<div class="div1">Div1</div>
<div class="div2">Div2</div>
</div>
<div class="div3">Div3</div>
Grouping the left div element by another div element.
div.div1 {
height: 400px;
margin-right: 10px;
width: 200px;
background: red;
float: left;
}
div.div3 {
height: 15px;
margin-top: 10px;
margin-right: 10px;
background: green;
clear: both;
width: 200px;
}
div.div2 {
height: 425px;
overflow: hidden;
background: blue;
float: left;
width: 200px;
}
<div style="float:left;">
<div class="div1">Div1</div>
<div class="div2">Div2</div>
</div>
<div class="div3">Div3</div>
And see this link http://jsfiddle.net/bipin_kumar/cz2fP/3/
<style>
div.left{
float: left;
}
.main{
width : 100%;
}
.clear{
clear : both;
}
div.div1, div.div2 {
margin-right: 10px;
width: 200px;
background: red;
}
div.div1 {
height: 400px;
}
</style>
<body>
<div class="main">
<div class="left">
<div class="div1">Div1</div>
<div class="div2">Div2</div>
</div>
<div class="div3">Div3</div>
<div class="clear"></div>
</div>
</body>
http://jsfiddle.net/rkpatel/qd6Af/1/
I needed something similar, just mirrored (1 div left, 2 divs right) and I couldn't work it out. A few Google searches later, I found a website which easily allows you to create a grid, assign number of rows/columns to differently named divs and it even gives you the HTML/CSS code to just copy and paste it. I didn't know about this and wasted a good hour on trying various other ways, so if you didn't know about this website yet, here it is.
Sorry for replying to such an old thread, I just want to help people.
Try this
<body>
<div class="left">
<div class="div1">Div1</div>
<div class="div2">Div2</div>
</div>
<div class="div3">Div3</div>
</body>
DEMO
<div class="main">
<div class="div1">
<div class="div2"></div>
<div class=="div3"></div>
</div>
<div class="div4"></div>
</div>
and in css use min-height property
.div1 {
float:left;
}
.div4 {
float:right;
}
.main {
min-height:200px;
}
I have simple structure with container and inside boxes:
<div id="container">
<div class="block"></div>
// more blocks
<div class="block"></div>
</div>
What I would like to achieve is to center boxes inside this container but to pack them as much as possible in a one line. The same I can do using JS: http://jsfiddle.net/JhxSd/ but I would like to avoid that, and use only CSS. Is that possible?
#media queries
Use a set of #media queries to define different layouts for the grid based on the current screen size. The only part of the layout that needs to vary is the width of the grid wrapper.
For all practical purposes, this is the only CSS solution available at present. For an explanation of why #media queries are appropriate, and why other available CSS options won't work, see this answer.
JSFiddle Demo
The above demo has #media queries for screen sizes up to 1200px wide (more can be added as needed), and does not use JavaScript. The rendered width of #container is always 75% (not counting the border), and the grid is centered within #container.
Note: This solution requires adding a wrapper div around the blocks. In each #media query, the width of the wrapper is just enough to fit the number of columns appropriate for the current screen size. The fixed wrapper width is what allows the grid as a whole to be centered within #container. If editing the static HTML isn't an option, the wrapper div can be added when the page loads using jQuery.
HTML
<div id="container">
<div class="grid-wrapper">
<div class="block"></div>
...
</div>
</div>
CSS
#container {
width: 75%;
...
}
.grid-wrapper {
margin: 0 auto;
width: 70px; /* Default: 1 column */
}
#media (min-width: 200px) {
.grid-wrapper {width: 140px;} /* 2 columns */
}
#media (min-width: 290px) {
.grid-wrapper {width: 210px;} /* 3 columns */
}
...
I hope this will do the trick:
http://jsfiddle.net/CnjZR/1/
<div id="container">
<div id="wrap">
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
<div class="block"></div>
</div>
CSS:
#container {
width: 100%;
overflow: hidden;
background: red;
text-align: center;
}
.block {
width: 20px;
height: 20px;
background: blue;
float: left;
margin: 5px;
}
#wrap {
background: green;
overflow: hidden;
display: inline-block;
}
Not too sure if you where looking for something like 'flex-justify' , I added in the demo a turn around based on inline-boxes behavior and text-align values.
edit : point cleared: text-align:center ; is it.
http://jsfiddle.net/JhxSd/10/
The point is you should not use float, but display.
Float is not friendly with centering , nor vertical nor horizontal, since it is not standing in the natural flow of the document.
#container {
width: 75%;
border: 1px solid;
text-align:center;
overflow:hidden;
padding:1em 1em 0;
box-sizing:border-box;
float:left;
}
#container .block {
width: 50px;
height: 50px;
background-color: blue;
display:inline-block;
margin: 10px;
}
I think, everything you have almost done already.
#container {
width: 75%;
border: 1px solid;
float: left;
}
#container .block {
width: 50px;
height: 50px;
background-color: blue;
float: left;
margin: 10px;
overflow: hidden;
}
http://jsfiddle.net/JhxSd/3/
Try this:
#container {
width: 75%;
border: 1px solid;
float: left;
overflow: hidden;
text-align: center;
}
#container .block {
display: inline-block;
width: 50px;
height: 50px;
background-color: blue;
margin: 10px;
}
If you truly need everything left-aligned then I think you're out of luck with just CSS.
You can use the text-align:justify for the container and use the display:inline-block for the div.block. but you need add some placeholder tag at the last.Like this:
HTML
<div class="wrapper">
<div class="block">1</div>
<div class="block">2</div>
<div class="block">3</div>
<div class="block">4</div>
<div class="block">5</div>
<div class="block">6</div>
<div class="block">7</div>
<div class="block">8</div>
<div class="block">9</div>
<div class="block">10</div>
<div class="block">11</div>
<div class="block">12</div>
<div class="block">13</div>
<div class="block">14</div>
<div class="block">15</div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>
CSS
.wrapper {
width: 75%;
border: 1px solid;
font-size: 0.1px;
text-align: justify;
}
.wrapper:after {
content:"";
display:inline-block;
width: 100%;
}
.wrapper div{
font-size: 16px;
display:inline-block;
*display: inline;
zoom:1;
color: #fff;
background-color:blue;
width: 50px;
height: 50px;
margin: 10px;
}
.wrapper .placeholder {
width: 50px;
height: 0px;
background:none;
}
Please view the demo. A detailed tutorial, please click here.