CSS positioning of two horizontal elements - html

How do I align foo to left and bar link to right?
<td>
<span>foo</span>
<span>bar</span>
</td>

You should use float to achieve that
Demo
td span:first-of-type {
float: left;
}
td span:last-of-type {
float: right;
}
Note that the pseudo I am using will target the first and the last span respectively, but if you are looking to support legacy versions of IE or other browsers, this will fail, using unique class for each span is recommended instead.
The only reason am using pseudo here is it saves me to declare classes, this is a huge advantage if you want to do this for each td
Also, I assume that you are not well informed for float or you must have tried to do so, if that's the case, you can refer my answer here for detailed information over float and also refer this, which isn't necessary for this scenario because you are using float in td except the floated elements, but if you have any elements or text apart from these two elements in the same td than make sure you clear the floating elements.

td > span:first-child {
float: left;
}
td > span:last-child {
float: right;
}
DEMO: http://jsfiddle.net/Px4un/

You can use inline styles, like this:
<td>
<span style="float: left;">foo</span>
<span style="float: right">bar</span>
</td>
Or plain CSS:
<style>
.left {
float: left;
}
.right {
float: right;
}
</style>
<td>
<span class="left">foo</span>
<span class="right">bar</span>
</td>
Please, don't use pseudo elements, like :first-child or :first-of-type, because it will be not cross-browser.

Related

Is there an equivalent of the page-break-after attribute

I know that page-break-after is used in printing html documents, but is there a browser equivalent?
Like I have this html:
<div class="c1">
<div>item1</div>
<div>value11</div>
<div>value12</div>
<div>item2</div>
<div>value21</div>
<div>value22</div>
</div>
and I want to see something like
item1 value11 value12
item2 value21 value22
The css:
.c1 > div{
display:inline-block
}
.c1 > div:nth-child(3n){
????page-break-after
}
I need a page break after every third div (:nth-child(3n)), but I can't figure out how to add it. Is there a way to achieve it (without adding new elements)?
Asuming you cannot alter the DOM (which I would advise), you can fix it with floats and clears:
.c1 > div { float: left; margin-right: 10px; }
.c1 > div:nth-of-type(3n+1) { clear: left; }
So you're floating the divs, but deny the 4th one to have a element at it's left, and thus forcing it on another line.
Fiddle example

How to add CSS if element has more than one child?

I have td tags and a several div inside td:
<td>
<div class='test'></div>
<div class='test'></div>
</td>
<td>
<div class='test'></div>
</td>
I want to add margin-bottom to div if there are more than one in the td. How can I do this with the css?
You can't directly 'count' total numbers of elements in CSS, so there's no way to only apply the class if there's 2 or more divs (you'd need JavaScript for that).
But a possible workaround is to apply the class to all divs in the td...
td > div {
margin-bottom: 10px;
}
... and then override/disable it with a different style when there's only one element. That indirectly lets you add the style when there's 2+ more child elements.
td > div:only-child {
margin-bottom: 0px;
}
Alternatively you can apply to every div after the first one, if that happens to work for your situation.
td > div:not(:first-child) {
margin-bottom: 10px;
}
Edit: Or as Itay says in the comment, use a sibling selector
td > div + div {
margin-bottom: 10px;
}
td > div:not(:only-child) { margin-bottom: 10px; }
Well actually you can do this with css using the nth-last-child selector
FIDDLE
So if your markup was like this:
<table>
<td>
<div class='test'>test</div>
<div class='test'>test</div>
</td>
</table>
<hr />
<table>
<td>
<div class='test'>test</div>
</td>
</table>
CSS
div:nth-last-child(n+2) ~ div:last-child{
margin-bottom: 40px;
}
... the above css will style the last div element only if there exists a container that has at least 2 child divs
Just to see how this works better - here's another example fiddle
did a nice little combo with the accepted answer
only applies style to the first child when its NOT the only child.. so when there is more than 1
td > div:not(:only-child):first-child { }
CSS-Has has limited browser support:
but here you go:
td:not(:has(div:first-child:last-child))
bonus:
td:not(:has(div:only-child))
i think there is no way to add the 10px margin to each div inside a td without the use of css3.
so a solution would be to use javascript and check if there are more than 1 div's inside the td and then if yes add a special class.
css
.myMarginClass div{
margin-bottom:10px;
}
js
var td=document.getElementsByTagName('td'),
l=td.length;
while(l--){
if(td[l].getElementsByTagName('div').length>1){
td[l].className='myMarginClass';
}
}
else for modern browsers the proper solution is the :only-child
proposed by #mikel
If there are other kinds of elements inside the td, you can still select the only div with this :only-of-type
<td>
<div class='test'></div>
<div class='test'></div>
</td>
<td>
<div class='test'></div>
<p class='test'></p>
</td>
CSS
td > div:only-of-type{
margin-bottom:10px;
}

why can't I get the button to form inline?

I want the btn next to the string. I can't figure it out even using CSS inline
<span class="subscribe_button"> <h3>Books</h3> <%= render 'follow_form' %></span>
CSS:
.subscribe_button {
display: inline;
}
You have some invalid HTML here.
A block level element cannot be within an inline one, this is basic HTML knowledge.
What I suggest you do is wrap both elements in a div and use float: left;
<div class="wrap">
<h3>Books</h3>
<span class="subscribe_button"> unsubscribe</span>
</div>
CSS:
.wrap
{
width: 300px;
}
.wrap h3,
.wrap span
{
float: left;
}
.wrap span
{
margin-left: 10px/*your value*/;
}
I also suggest you go read up on HTML rules, what is allowed where and why they are or are not allowed.
http://jsfiddle.net/Kyle_Sevenoaks/zJUZs/
The Books part is (also) a block (due to <h1>), so you need to set it to inline as well (as shown in the comment of limelights), otherwise your button will still be pushed to the next line.
Try adding this to your CSS
.subscribe_button h3 {
float: left;
}
If you float an element it means other elements after it will wrap onto the same line as it (as long as theyre width does not make them too wide).
Span is inline element and h3 is block element. Inline elements should be inside block elements. Have you tried to validate your html code? http://validator.w3.org/
try:
display: inline-block;
Try following code
.subscribe_button h3{
display: inline;
}
use float:left for both h3 and button
I think you can do this with this code:
.subscribe_button > * {
display: inline;
}
'>' is a child selector and * matches to all element.
Yo can read more about CSS2 selectors: CSS2 Selectors

how to properly align multiple label-text box pairs (with and without floats)?

I wrote the following :
<ul>
<li>
<span class="filter">
<label>Name: </label><input type="text"/>
</span>
</li>
<li>
<span class="filter">
<label>Id: </label><input type="text"/>
</span>
</li>
</ul>​
with the CSS as :
span.filter{
width: 50px;
}
input{
text-align: right;
}
label{
text-align: left;
}
ul{
list-style: none;
}​
But the result is as shown in this fiddle. Some people suggest using floats, but why is this not working ?
It's not necessary to use float. There are many ways to make it work:
Eg.
label{
display: inline-block;
width: 200px;
text-align: left;
}
http://jsfiddle.net/krL7z/7/
If you want to achieve this effect using floats, set the display property of your labels to block, declare their width, and float them left. Then float your inputs left and us clear:left on the label elements so that they don't wind up all on one line.
so:
label{
display:block;
width:50px; /*arbitrarily determined */
float:left;
clear:left;
}
input{
float:left;
}
See the result in this fiddle.
You can also do this using display:inline-block (and hence less code) as indicated in xiaoyi's answer.
Another way is to use table cells, which (while some consider them semantically appropriate to forms) are not something you generally want to rely on for layout.
If you are in a situation where you know you are able to limit the browsers you support (Explorer higher than IE7) and all the others Firefox, Chrome, Opera, Safari ... then you can use the display:table and display:table-cell to layout the cells in table fashion - without the actual table mark-up.
span.filter
{
display:table;
}
span.filter label, span.filter input
{
display:table-cell;
}

elegant CSS positioning for spacing

Suppose I have the following HTML:
<div id="Wrapper">
<div class="MyClass"></div>
<div class="MyClass"></div>
<div class="MyClass"></div>
<div class="MyClass"></div>
<div class="MyClass"></div>
</div>
and the following CSS:
.MyClass {
float: left;
margin-right: 5px;
}
All these elements are going to be positioned on one line with a space in between of 5px. The problem is that there will also be a space of 5px at the end. I want to have Wrapper really wrap the .MyClass divs so that there's no space on the edge.
Now I can think of several ways of doing this:
with jquery, set the right margin of the last element to 0.
with CSS create a new class - .MyClassForLastElement with marin-right set to 0.
creating a negative right-margin of -5px for .Wrapper.
I was wondering if there's an elegant and clever way of doing it.
Not sure if there is a perfect solution, I use to do that:
.MyClass {
float: left;
margin-left: 5px;
}
.MyClass:first-child {
margin-left: 0;
}
I do it with with first-child since it is supported in IE6-7 while last-child is not.
If you don't want the last child to have a margin-right use the last-child psuedo-selector.
.MyClass:last-child {
margin-right: 0px;
}
The following rules would provide the desired effect. First element will have no margin, but effectively any consecutive element would have margin-left:5px;.
.MyClass {
float: left;
margin: 0;
}
.MyClass + .MyClass {
margin-left: 5px;
}
Well supported across browsers, IE7+
the + adjacent selector matches an element that is a next sibling of another element, in the example above it's a .MyClass element following another .MyClass element
selectors as like this one
.MyClass + .MyClass {
margin-left: 5px;
}
More info http://www.w3.org/TR/CSS2/selector.html#adjacent-selectors