When I place two different divs side by side with a sidebar, and have only margin to the right on the first one (that is on the left). And the right one don't have one.
How do I create a good structure so I don't have to manually add style="margin:0" to the right HTML element?
To demonstrate this I have created this illustration:
You can use nth-child(odd) to target every odd item in the order of your items.
Since you didn't provide any actual code example, I have put together a structure I felt appropriate for such a design.
Here's an example I knocked up: http://codepen.io/michaelpumo/pen/OMgEKp
In the CodePen, I am using Jade for HTML and SCSS for the CSS. I recommend using them, but if you want the compiled code, it's below.
HTML
<div class="grid">
<div class="grid__left">
<div class="grid__hero"></div>
<div class="grid__item"></div>
<div class="grid__item"></div>
</div>
<div class="grid__right">
<div class="grid__sidebar"></div>
</div>
</div>
CSS
* {
box-sizing: border-box;
}
body {
padding-top: 10px;
}
.grid {
width: 600px;
overflow: hidden;
margin: 0 auto;
}
.grid__left, .grid__right {
float: left;
}
.grid__left {
width: 400px;
}
.grid__right {
width: 200px;
padding-left: 10px;
}
.grid__hero, .grid__item, .grid__sidebar {
background: #000;
}
.grid__hero {
width: 100%;
height: 200px;
margin: 0 0 10px 0;
}
.grid__hero {
width: 100%;
height: 200px;
margin: 0 0 10px 0;
}
.grid__item {
float: left;
clear: none;
width: 195px;
height: 200px;
margin: 0 10px 10px 0;
}
.grid__item:nth-child(odd) {
margin-right: 0;
}
.grid__sidebar {
width: 100%;
height: 410px;
}
Hope it makes sense. The real meat of the matter is on the .grid__item divs, that have a margin right, but this being removed for every odd one, which results in the effect I think you're after.
In any case, it looks like you're after a grid system. There are many out there; most popular being Bootstrap. I strongly recommend using one of them: http://getbootstrap.com
Personally, I use a SCSS based grid called Neat: http://neat.bourbon.io/
Related
I know it's a simple problem but I couldn't put words on it in my researches, so here's a picture of what I want:
Basically, I have a list of features which I want to display that way, which could be seen as a vertical list where rows are overlapping. I tried using :nth-of-type(2n) css selector to alternate left and right alignments, which is currently working, and make the overlapping by setting vertical negative margins, but this makes all items move up, so there's no overlapping.
Also, since I want the design to be responsive, it should just display as a centered vertical list on mobile.
What's the better way to achieve that?
NB: Don't worry about what's inside the gray boxes, it doesn't serve the purpose of this question.
Code:
.feature {
width: 50%;
text-align: center;
margin: 0 auto;
}
.feature:nth-of-type(2n + 1) {
margin-left: 0;
margin-right: auto;
}
.feature:nth-of-type(2n) {
margin-left: auto;
margin-right: 0;
/*margin-top: -60px*/
}
You can use translation to translate the right boxes to the bottom or the left one to the top
.container {
display: flex;
flex-wrap: wrap;
padding-bottom: 50px;
}
.feature {
flex: 0 1 45%;
margin: 5px 2%;
text-align: center;
min-height: 100px;
border: 2px solid;
box-sizing: border-box;
}
.feature:nth-of-type(2n) {
transform: translateY(50px);
}
<div class="container">
<div class="feature"></div>
<div class="feature"></div>
<div class="feature"></div>
<div class="feature"></div>
</div>
I have made some changes to the code of #Temani Afif, I just made it a bit more like how you wanted, By adding some more styling.
Also I have added the mobile view for this
Just add this CSS,
.feature:nth-of-type(2n) {
transform: translateY(100px);
}
.feature:nth-of-type(1n) {
margin-bottom: 100px;
}
For Mobile Responsiveness
#media(max-width:600px){
.feature{
flex:100%;
}
.feature:nth-of-type(2n) {
transform: translateY(0);
}
.feature:nth-of-type(1n) {
margin-bottom: 0;
}
}
Credits : #Temani Afif
Index code
<div id="pageMiddle">
<div id="midleft">This is a test to make sire everything is ok. This text will be removed once the test is complete and everything is working fine.</div>
<div id="midmid"><?php include_once("modules/twitter_twr.php"); ?></div>
<div id="midright"><?php include_once("modules/random_profiles.php"); ?></div>
CSS
#pageMiddle {
width: 1000px;
margin: 0px auto;
height: 900px;
}
#pageMiddle > #midleft {
width: 200 px;
margin: 0px;
float: left;
}
#pageMiddle > #midmid {
width: 600px;
margin: 0px;
display: inline;
}
#pageMiddle > #midright {
width: 200px;
margin: 0px;
float: right;
}
I have tried many ways to do this. I just can't seem to get my head around it at all. I am trying to get it so all divs sit nicely next to each other so I able to fill them with content for the index page basically. Thanks for your time to look at it and I hope you can correct my mistakes as I suck at CSS.
DEMO
Actually this is all you need:
#pageMiddle {
width: 1000px;
margin: 0px auto;
height: 900px;
}
#pageMiddle > div{
float: left;
}
than you can just set your widths:
#midleft, #midright {
width: 200px;
}
#midmid {
width: 600px;
}
Note there's no need to use #pageMiddle > #midleft cause ID is already unique-per-page.
The issue here is display:inline does not allow you to use the width attribute in CSS.
This should fix it with the current code you have now:
#pageMiddle {
width: 1000px;
margin: 0px auto;
height: 900px;
}
#midleft {
width: 200px;
margin: 0px;
display: block;
float: left;
}
#midmid {
width: 600px;
margin: 0px;
display: block;
float: left;
}
#midright {
width: 200px;
margin: 0px;
display: block;
float: left;
}
Also, since you're using floats, it's generally best to have your float divs lay above non-float divs (if you're trying to achieve a single row). Your HTML syntax should be as follows:
<div id="pageMiddle">
<div id="midleft">This is a test to make sire everything is ok. This text will be removed once the test is complete and everything is working fine.</div>
<div id="midmid"><?php include_once("modules/twitter_twr.php"); ?></div>
<div id="midright"><?php include_once("modules/random_profiles.php"); ?></div>
</div>
Here's the fiddle (added backgrounds so you can see the distinction between divs): http://jsfiddle.net/g732A/1/
Note that this isn't the best way to write columns, but it does achieve what you're trying to do.
I am trying to convert a three-column web-page layout from HTML tables to CSS, but there is one characteristic which I have so far been unable to replicate.
The HTML solution allows a photograph to be placed by php into a column to the right of the main text, but in the absence of a photograph, that column collapses, and the main text content extends up to a final right-hand 'spacing' column to maintain a right-hand margin.
So far, the only way I have found of achieving the three-column array in CSS is to use a container of fixed width, and to use fixed width styles for the div tags defining the columns, together with floats. The basis of the HTML code is:
<div class="container">
<div class="title_strip"><img src="headline_text.png"></div>
<div class="hdr_img_space"><img src="leaf_header.jpg"></div>
<div class="lh_col"><p align="center">ADMINISTRATION<br/>[WEBMASTER]</p></div>
<div class="main_content"><p>This is the location for the main administrative page
content, and will hopefully be able to contain all the necessary text, even if it
over-runs</p></div>
<div class="rh_image"><img class="rh" src="photo.jpg"></div>
<br style="clear: both"></div>
and the core CSS styling is:
body {
margin: 0px;
padding: 0px;
background-color: #200542;
}
p {color: #CCCCCC; font-family: arial, sans-serif; font-size: 14pt; line-height:1.4em
}
.container {
width: 1000px;
min-height: 600px;
max-height: 2200px;
margin: 8px auto 0 auto;
background: #333333;
padding: 0px;
}
.main_content {
float: left;
min-height: 600px;
max-height: 2000px;
margin-left: 0px;
width: 516px;
padding-top: 20px;
background: #333333;
}
.lh_col {
float: left;
width: 200px;
min-height: 600px;
max-height: 2000px;
padding-top: 20px;
padding-left: 20px;
padding-right: 24px;
margin: 0;
background: #333333;
}
.rh_image {
width: 200px;
min-height: 620px;
max-height: 2000px;
float: right;
padding-top 20px;
padding-left: 20px;
padding-right: 20px;
background: #333333;
}
img {
display: block;
}
.rh {
margin-top: 40px;
}
Because the columns are fixed width, the removal of the image tag leaves a wide gap to the right of the main text, and none of the suggestions I have found in web searches shows a satisfactory solution - generally the collapse of the right-hand column leaves the text layout in the centre column unchanged, despite the apparent availability of more space.
I want to avoid solutions which would give problems with browser compatibility - is there any way of achieving what I want in CSS, or shall I have to persist with the now deprecated use of the HTML table tag?
Peter N.
If I understand correctly, what you need is if there is no image in the right column, your main content should be without "gap", or complete layout should look like 2-column page, if so try this:
first thing is to move your
<div class="rh_image"> before
<div class="main_content">
next just adjust css like this:
.main_content {
min-height: 600px;
max-height: 2000px;
margin-left: 0px;
padding-top: 20px;
background: #333333;
}
it should do the trick
Sorry I just saw your fiddle
here's the link with the complete code: http://jsfiddle.net/darkosss/3deMj/
I am trying to do a hide / reveal using javascript and css and my divs are stacking rather than lining up side by side. i have set a width and floats... i cant figure out what's going on. any help is greatly appreciated.
#container {
width: 760px;
margin: 20px auto;
padding: 30px;
background-color: #000;
border-width: 0px;
color: #fff;
}
#1a {
width: 300px;
float: left;
margin:10px
background-color: #000;
}
#1b {
width: 400px;
margin: 10px;
background-color: #000;
}
and the html:
<div id="container">
<div id="1b" class="hidden">
Module Details:
My First Page
</div>
<div id="1a">
01
</div>
i've been messing with it a lot, and now the second div is in the middle of the first... so here is a link if that's helpful too:
http://www.amandasmithsf.com/m14_SMITH_demo/test.html
Update
Inspecting your CSS in Firebug, I noticed that it wasn't being applied, then I saw why -- HTML IDs should not start with a number (or at least, not if you want them to work the CSS # selector; it turns out that in HTML5 they decided to start allowing IDs beginning with numbers, but you'll have to use a different strategy to select them with CSS: http://benfrain.com/when-and-where-you-can-use-numbers-in-id-and-class-names/).
Starting the IDs with letters instead of numbers made it work:
<style>
.hidden {display:none}
#container {
width: 760px;
margin: 20px auto;
padding: 30px;
background-color: #000;
border-width: 0px;
color: #fff;
}
#b1 {
float: left;
width: 300px;
margin:10px;
background-color: #000;
}
#a1 {
float:left;
width: 10px;
margin: 10px;
background-color: #000;
}
</style>
<script>
function unhide(id) {
document.getElementById(id).style.display = 'block';
}
</script>
<div id="container">
<div id="b1" class="hidden">
Module Details:
My First Page
</div>
<div id="a1">
01
</div>
</div>
Original Answer
I think you just need to add float: left; to #1b.
Or, if for some reason you really only wanted to assign float: left; to one of them, it would need to be #1b - the floated element needs to come before the non-floated element next to which you want it to display.
We have 3 columns (someone would want more maybe)
Lets say I want 30px distance between them.
That would mean I need to create 3 different styles :
First column : margin-right:15px
Center column : margin-right:15px;margin-left:15px;
Last column : margin-left:15px;
Maybe its not quite complicated but its quite no-comfortable, especially when needed for some wordpress etc. where end-user may not have a HTML background.
Here is a fiddle.
Is it possible to achieve this in a simple manner ?
You could have
HTML
<div class="margin-right">
<div class="margin-left margin-right">
<div class="margin-left">
CSS
.margin-left { margin-left: 15px }
.margin-right { margin-right: 15px }
there's an easier solution ( jsFiddle ) that does not require you to use 2 classes which is:
.col-gutter {
padding-right: 20px;
}
.col-gutter:last-of-type {
padding-right: 0;
}
you can just set the gutter size to whatever size you need using one single class
Update ( IE8 )
if you'd want to support at least IE8 you could use this instead:
.col-gutter {
padding-left: 20px;
}
.col-gutter:first-child {
padding-left: 0;
}
You could always use the following method (for 3 column layout):
.onethird {
width: 30%;
margin-left: 5%;
float: left;
}
.onethird:first-child {
float: left;
margin-left: 0;
}
.onethird.third {
float: right;
}
Just wrap it in a container, and make sure you clear the floats.
Here is an update to your fiddle : http://jsfiddle.net/kFeFj/23/
Try negative margins on a container element
http://jsfiddle.net/5JZGt/ (this demo shows multiple boxes)
Some HTML:
<div class="container">
<div class="child">
</div>
<div class="child">
</div>
<div class="child">
</div>
</div>
The CSS:
.container {
margin: -10px 0 0 -10px;
}
.container .child {
width: 100px;
height: 100px;
background: red;
margin: 10px 0 0 10px;
float: left;
}
You can do away with the .child class and just reference the div or whatever element you're using.