<style>
.holder{
width:3in;
height:4in;
background:aqua;
}
.orangeBox{
float:left;
background:orange;
width:1in;
height:1.5in;
}
.yellowBox{
float:left;
background:yellow;
width:1in;
height:1in;
}
</style>
<div class="holder">
<div class="orangeBox">R</div>
<div class="yellowBox">1</div>
<div class="yellowBox">2</div>
<div class="yellowBox">3</div>
<div class="yellowBox">4</div>
<div class="yellowBox">5</div>
<div class="yellowBox">6</div>
<div class="yellowBox">7</div>
<div class="yellowBox">8</div>
<div class="yellowBox">9</div>
<div class="yellowBox">10</div>
<div class="yellowBox">11</div>
</div>
What I'm trying to achieve is to have the individual boxes float left (which they're doing) but also to stay on the same line.
In the example here I want box 5 to NOT appear under box R, but rather to automatically wrap to the next full line.
I know this can be achieved via float:right, but that would reverse all the numbers, making everything backwards.
https://jsfiddle.net/o4eem3za/
You can clear every other element after the 6th element:
Updated Example
.yellowBox:nth-child(2n + 6) {
clear: left;
}
Alternatively, depending on the desired results, you could also just clear the 6th element as well:
Updated Example
.yellowBox:nth-child(6) {
clear: left;
}
Shouldn't you just make both boxes orange and yellow same height?
Right now you have orangebox 1.5in and yellowbox 1in heigh. Set 1.5 for yellowbox also and it will fix your issue.
.yellowBox{
float:left;
background:yellow;
width:1in;
height:1.5in; /* changed this from 1in to 1.5in */
}
Fiddle: https://jsfiddle.net/Munja/o4eem3za/1/
If you are looking for a grid pattern, where you have distinct rows, I'd recommend display:table and display:table-cell. (EDIT: discard the display:table stuff, if you're floating R right - unnecessary)
<style>
.holder{
width:3in;
height:4in;
background:aqua;
}
.orangeBox{
float:right;
background:orange;
width:1in;
height:1.5in;
margin-bottom:.5in;
}
.yellowBox{
float:left;
background:yellow;
width:1in;
height:1in;
}
</style>
<div class="holder">
<div class="orangeBox">R</div>
<div class="yellowBox">1</div>
<div class="yellowBox">2</div>
<div class="yellowBox">3</div>
<div class="yellowBox">4</div>
<div class="yellowBox">5</div>
<div class="yellowBox">6</div>
<div class="yellowBox">7</div>
<div class="yellowBox">8</div>
<div class="yellowBox">9</div>
<div class="yellowBox">10</div>
<div class="yellowBox">11</div>
</div>
EDIT: This still uses floats, but it leverages the special display attributes of table cells. Change the width of the .holder to 4 and you will see it functions as you'd expect.
EDIT v2: Changed orangeBox to float right, added .5 margin to bottom, removed display:table stuff (unnecessary if floating per poster's comment)
With display: inline-block you can achieve it
https://jsfiddle.net/o4eem3za/4/
display:inline-block
Related
Here is a prototype of what I am trying to implement
Here is what I currently have : JsFiddle
I am trying to get the picture of the guy on the laptop to align correctly with and to the right of the paragraph components - Business Traveller, Office Supply Purchases, etc...
What I've tried is using Align attribute, changing my img src code to
<img id="laptop" align="middle" src="zoom-39988392-3.JPG" height = "90" width ="90" />
but that didn't have any effect.
I also tried Float but that messed up my margins and the organization of my left components.
Is there a way I can do this without floating?
See the fiddle
The HTML and CSS that i've used is as follows. Used float:left
HTML
<div class="container">
<div id="choices">
<p class="choice">Business Traveller</p>
<p class="choice">Office Supply Purchases</p>
<p class="choice">Stay at home parent</p>
<p class="choice">Entertainment</p>
<p class="choice">Profile 6</p>
</div>
<div class="image"></div>
</div>
CSS
html, body, .container {
height:100%;
}
#choices {
width:30%;
float:left;
}
.choice {
margin-top:0px;
margin-left:20px;
text-align:center;
width:100%;
background-image: url("http://i.imgur.com/H43sVoi.png");
padding-top:15px;
padding-bottom:15px;
}
.image {
height:100%;
width:65%;
background-color:red;
float:left;
}
You will have to work with the height and width of each divs. I just made it roughly.
You have to create two columns. 1 column for the menu and the second column for the image. If you do this, you wont have trouble floating.
I'm having a bit of trouble getting a unordered list of items to change its parents size.
I have something like this
HTML
<div class="content">
<div id="product-range">
<ul>
<div class="product-container">sup</div>
<div class="product-container">sup</div>
<div class="product-container">sup</div>
<div class="product-container">sup</div>
<div class="product-container">sup</div>
</ul>
</div>
</div>
CSS
.content {
position:relative;
background:white;
max-width:1299px;
margin: 0 auto;
padding:0;
}
#product-range {
position:relative;
margin-left:25px;
margin-right:15px;
background:blue;
}
.product-container {
position:relative;
width:398px;
height:500px;
float:left;
margin-right:20px;
margin-top:15px;
background:green;
}
Which results in
http://jsfiddle.net/qfUTq/
The parent collapses into nothing. The only way I've been able to get around this is to add an empty div with a clear:both like this:
http://jsfiddle.net/eE6XQ/
Is there a proper way to go about doing this? I've tried to add that same property to all the other classes or ID and it doesn't produce the same effect.
On #product-range
play with overflow property :auto,scroll,...;
i guess overflow:auto cant solve your problem but its not the best way to do so.
Apply overflow: hidden to #product-range and it will wrap around the elements.
I'm currently updating a pretty old website (last update was around 2001), and have agreed to use HTML5 and CSS3.
For the general design, I'm working on a very clean white and gray tones style, with many paddings and margins. My problem resides in the home page: I'd like to have a 3-column centered layout. But where to start? I've tried some floating, but in vain.
Am I doing this right ?
HTML:
<div class="colwrapper">
<div class="ltcol"></div>
<div class="ctcol"></div>
<div class="rtcol"></div>
</div>
CSS:
.colwrapper { width:1020px; }
.ltcol, .ctcol, .rtcol { width:300px; margin:0 10px; padding:10px; }
.ltcol { float:left; }
.ctcol { margin-left:340px; }
.rtcol { float:right; }
your css should be like this:
.ltcol, .ctcol { float:left; }
.rtcol { float:right; }
The purpose of the CSS float property is, generally speaking, to push a block-level element to the left or right, taking it out of the flow in relation to other block elements. This allows naturally-flowing content to wrap around the floated element. This concept is similar to what you see every day in print literature, where photos and other graphic elements are aligned to one side while other content (usually text) flows naturally around the left- or right-aligned element.
For More details you must have to read this intresting article.
See This Demo: http://jsfiddle.net/akhurshid/YRWLV/
Your HTML is very clean - this is a great step forward.
You need to add a float: left to all the columns. To ensure the float is cancelled after your columns, it is best to add a clear div after the floated columns.
HTML:
<div class="colwrapper">
<div class="ltcol">Column 1</div>
<div class="ctcol">Column 2</div>
<div class="rtcol">Column 3</div>
<div class="clear"></div>
</div>
CSS:
.colwrapper { width:1020px; }
.ltcol, .ctcol, .rtcol { width:300px; margin:0 10px; padding:10px; background-color: #efefef }
.ltcol { float:left; }
.ctcol { float:left; }
.rtcol { float:left; }
.clear { clear: left; }
So you add css3 tag for this questio so I suggest you to make this with css3 column layout:
More info
for example
HTML
<div class="colwrapper">
<div>text</div>
</div>
CSS
.colwrapper div
{
-moz-column-count:3; /* Firefox */
-webkit-column-count:3; /* Safari and Chrome */
column-count:3;
}
It does not work on IE.
Use one of these tried and tested implementations instead of rolling out your own. In addition to the fact that you'll be getting tested and working code, you'll add responsiveness to your site with almost zero effort.
http://cssgrid.net/
http://960.gs/
http://framelessgrid.com/
http://goldengridsystem.com/
and lots more if you google..
could also use Flexbox property for this now as well so you don't need to worry about floats or clearfix's.
main{
/* .colwrapper{ */
display: flex;
flex-flow: row;
justify-content: center;
}
main > section{
/* .ltcol,.ctcol,.rtcol{ */
display:flex;
flex-flow:column;
align-items:center;
padding:10px; padding:.625rem;
}
main > section:nth-child(2){
/* .ctcol{ */
margin:0 20px; margin:0 1.25rem;
}
http://caniuse.com/flexbox shows the support for it isn't quite as far along as you would probably like, however, there are ways to improve support by mixing old versions of the syntax with the new http://css-tricks.com/using-flexbox/ has a great write up on it from Chris Coyier if you want to play with this for a next project (this post is fairly old). You can also get more details at http://html5please.com/#flexbox
Also, if you're using HTML5 I'd probably go with sections over divs for a more semantic structure, so a comparison would look something like this:
<main>
<section></section><!-- or <nav></nav> -->
<section></section><!-- or <article></article> -->
<section></section><!-- or <aside></aside> -->
</main>
instead of...
<div class="colwrapper">
<div class="ltcol"></div>
<div class="ctcol"></div>
<div class="rtcol"></div>
</div>
Just try putting the rtcol div beofre le ltcol div.
<div class="colwrapper">
<div class="rtcol">X</div>
<div class="ltcol">X</div>
<div class="ctcol">X</div>
</div>
http://jsfiddle.net/EDjpy/
Given this simplified snippet :
<html>
<body>
<div>
<div style='text-align:center;'>asdfaskjdfakjsd</div>
<div style='float:right'>
<input type='submit' value='asdf' />
</div>
</div>
</body>
</html>
The button floats to the right, but below the text(On the next line). I know I can realign it using relative positioning, but is there a correct way of having both on the same line.
Even better if adding the button on the right would not effect the centre align of the text. ie it does not get pushed to the left.
You can switch the order of the two divs:
<div style='float:right'>
<input type='submit' value='asdf' />
</div>
<div>asdfaskjdfakjsd</div>
As long as you don't mind them being in reverse order.
Here's a fiddle to demonstrate this effect. The fourth example shows the divs reversed.
I apologize for jumping around. I noticed that even with the reversed divs, the text didn't appear completely centered.
Here is yet another solution (5th example): http://jsfiddle.net/tracyfu/zYzqr/
#method5 {
position: relative;
}
#method5 .submit {
position: absolute;
right: 0;
top: 0;
}
The only problem with this is that if you're not careful, or your text is dynamic, it could collide with the absolutely positioned submit.
I misunderstood your question the first time. You should add float:left; on your initial div and also make sure to add clear:both; to the div below them. If you want the text to be centered, you need to have a width on the initial div.
HTML:
<div id="container">
<div id="content">
asdfaskjdfakjsd
</div>
<div id="containerButton">
<input type='submit' value='asdf' />
</div>
</div>
<div class="clear">asdfaskjdfakjsd</div>
CSS:
#container {
width:300px;
}
#content {
float:left;
text-align:center;
width:90%;
}
#containerButton {
text-align:right;
}
.clear {
clear:both;
}
Live DEMO
The standard approach is to use the "clearfix" hack. CSS:
/* For modern browsers */
.cf:before,
.cf:after {
content:"";
display:table;
}
.cf:after {
clear:both;
}
/* For IE 6/7 (trigger hasLayout) */
.cf {
*zoom:1;
}
Credit Nicolas Gallagher. Then wrap your line in a cf element:
<div class="cf">
<span>Button text</span>
<div style='float:right'>
<input type='submit' value='asdf' />
</div>
</div>
Button text is changed to a span, or you could leave it as a div and float it left. cf is used to give block properties to a set of elements that do not naturally exhibit them. Without it, ensuing content will not be cleared.
DEMO
Morning,
I have the following HTML:
<div id="sah_padding">
<div id="sah_holder">
<div id="sah_name">Hello [agent_name]</div>
<div id="sah_logout">✖</div>
</div>
You are working with [customer_name]
</div>
and I have the following CSS:
#sah_padding{
padding:10px 10px 10px 10px;
}
#sah_holder{
padding-bottom:10px;
clear:both;
}
#sah_name{
float:left;
width:50%;
}
#sah_logout{
text-align:right;
}
#logout_link{
color:#fff;
text-decoration:none;
font-weight:bold;
}
However my login link and Hello message aren't aligning correctly, the logout link is a few pixels below the hello message and I need them to be on the same horizontal line. What am I doing wrong?
if you set line-height :1 to #logout_link element, it should correct the alignment
(of course feel free to choose a different value to adjust it)
Give float to your #sah_logout also. Write like this:
#sah_logout{
float: left;
text-align:right;
}
Check this http://jsfiddle.net/QUNT2/
Check the line-height of your elements!!!
You need to set a float to #sah_logout as well:
#sah_logout{
float: left;
text-align:right;
}
You can even make it say, float: right. It's entirely your choice. Doing only a text-align: right will modify how the inner contents of the container behave, not how the div behaves within the flow.
Also, you might have a few problems with the parent div not wrapping correctly around the children divs (as all children now have float properties), so you might need to add another div, with clear: both set in its style:
<div id="sah_holder">
<div id="sah_name">Hello [agent_name]</div>
<div id="sah_logout">✖</div>
<div style="clear: both"></div>
</div>
Instead of wasting time with block level elements why don't you simply use inline elements for both the "Hello" text and the logout link? That's what inline elements are supposed to do - stay in line with each other.
<div id="sah_padding">
<div id="sah_holder">
<span id="sah_name">Hello [agent_name]</span>✖
</div>
<span>You are working with [customer_name]</span>
</div>
Its is highly unlikely that you would need any of the CSS code you previously used for elements inside sah_holder unless you want to style them differently.
check it your updated html :-
<div id="sah_padding">
<div id="sah_holder">
<div id="sah_name">Hello [agent_name]</div>
<div id="sah_logout">You are working with [customer_name]</div>
</div>
</div>
your updated css:-
#sah_padding{
padding:10px 10px 10px 10px;
}
#sah_holder{
padding-bottom:10px;
border:1px solid red;
overflow:hidden;
}
#sah_name{
float:left;
width:50%;
}
#sah_logout{
float:right;
}
.logout_link{
color:black;
text-decoration:none;
font-weight:bold;
}
or you can see the demo:- http://jsfiddle.net/WnecH/11/