I have divs sorted 1, 2, 3 ... as html layout
HTML:
<div class="bigdiv">
<div class="div1">div 1</div>
<div class="div2">div 2</div>
<div class="div3">div 3</div>
</div>
CSS:
.div1 {
float: left;
}
.div2 {
float: left;
}
.div3 {
float: left;
}
How can I reorder divs to this layout with CSS
<div class="bigdiv">
<div class="div3">div 3</div>
<br>
<div class="div1">div 1</div>
<div class="div2">div 2</div>
</div>
by CSS (float, :after, :before)
My try:
.div3 {
float: right;
}
.div3:before {
content: '\A\A';
white-space: pre;
}
Thank you in advance.
You can do this with the CSS Flexible Box Layout Module
The ‘order’ property controls the order in which flex items appear
within their flex container, by assigning them to ordinal groups.
A flex container will lay out its content starting from the lowest
numbered ordinal group and going up. Items with the same ordinal group
are laid out in the order they appear in the source document. This
also affects the painting order [CSS21], exactly as if the elements
were reordered in the document. (W3.org)
FIDDLE
CSS (Without browser specifics)
.bigdiv {
display: flex;
flex-direction: row;
}
.div1 {
order: 2;
}
.div2 {
order: 3;
}
.div3 {
order: 1;
}
You could try to play around the display property and use this style:
CSS
.bigdiv { display:table; }
.div1, .div2 { float: left; }
.div3 { display: table-header-group; }
Live example: http://jsbin.com/axekof/2/edit
Note the display:table; applied to the main wrapper and the display property applied to the .div3. The example should work even on IE8 (didn't tested)
As a sidenote, for .div3 you may use both display: table-caption and display: table-header-group, but there's a slight difference: with the first property, the parent element wraps only .div1 and .div2 while, with the latter, the container wraps all the children elements (try to apply a border to the .bigdiv so you can clearly see the difference)
HTML:
<div class="bigdiv">
<div class="div1">div 1</div>
<div class="div2">div 2</div>
<div class="div3">div 3</div>
</div>
CSS:
.bigdiv {
display:table;
width:100%;
}
.div1 {
display: table-row-group;
}
.div2 {
display: table-footer-group;
}
.div3 {
display: table-header-group;
}
The result will be:
div 3
div 1
div 2
And for those pesky early versions of Internet Explorer... Source
Old browsers IE6 and IE7 do not support CSS properties of display: table family.
Also, IE8 has a dynamic rendering bug that appears in some cases: if a block to move contains preudo-table elements (display: table*) (this is the only buggy case noticed currently), some pseudo-table cells (such cells as well as cells count are different each time the page is reloaded) may disappear randomly when the page is initially rendered.
So, for IE8 and lower, we can override CSS rules that make blocks tably, and additionally move blocks to required positions in DOM tree of HTML document with JavaScript instead of CSS:
/**
* Reorders sibling elements in DOM tree according to specified order.
* #param {Array} elems Sibling elements in desired block order.
*/
function reorderElements(elems) {
var count = elems.length;
if (!count) {
return;
}
var parent = elems[0].parentNode;
for (var i = count - 1; i >= 0; i--) {
parent.insertBefore(elems[i], parent.firstChild);
}
}
// If IE8 or lower
if (document.all && !document.addEventListener) {
var blocks = [
document.getElementById('div3'),
document.getElementById('div2'),
document.getElementById('div1')
];
reorderElements(blocks);
}
You can't do that with just CSS as I see you are manipulating the DOM. However if you would still want to achieve that you would need to use position:relative for bigdiv and position:absolute for .div1 .div2 and .div3, which are actually the children of .bigdiv. You would then need to use left, top, right, and bottom properties to style them as needed.
Related
I'd like to specify a CSS rule (using SASS/SCSS) for a specific div, located as follows,
<div class="parent-div">
<div class="first-child has-this-class">
......
</div>
<div class="second-child">
.....
</div>
I need to write a specific CSS rule to "second-child", when the first child has the class "has-this-class".
I tried to use SCSS as this, but it didn't work.
.parent-div{
.first-child{
&.has-this-class + .second-child{
//Write the styles here
}
}
}
you can try this
.parent-div{
.first-child:has(.second-child){
//Write the styles here
}
}
but :has() selector isn't supported by all browsers look at can I use
This code will affect both the first and second child
.parent-div {
.first-child {
&.has-this-class,
+ .second-child {
width: 200px;
height: 200px;
background-color: crimson;
}
}
}
I have two inline-block elements on my header bar. The right side element is floated right, but when screen gets so narrow that it gets wrapped to another line, I'd like to center it instead.
EDIT: The first element content is user-submitted, so I don't know its width in advance.
Below I've included javascript-based solution of the behavior I'm looking for.
Is this possible to achieve by using only CSS?
$(window).bind("resize", function() {
var totalWidth = $('.container').width(),
firstWidth = $('.first').width(),
secondWidth = $('.second').width();
if (totalWidth > firstWidth + secondWidth) {
$(".container").addClass('one-line');
$(".container").removeClass('two-lines');
} else {
$(".container").addClass('two-lines');
$(".container").removeClass('one-line');
}
});
.container div {
display: inline-block;
}
.container.one-line .second-container {
float: right;
}
.container.two-lines .second-container {
display: block;
text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container one-line">
<div class="first">One and two lines layout: align left</div>
<div class="second-container">
<div class="second">One lne layout: align right. Two line layout: align center.</div>
</div>
</div>
/
I am building a website using singularity.gs which I am fairly new to.
I am having trouble giving a div a background-color, this is my html structure:
http://oi44.tinypic.com/205xt1i.jpg , the "green" part is my about div.
<div class="about">
<div class="photo">
<img class="photoBorder" src="images/foto_jeroen.png" alt=""/>
</div>
<div class="text">
<h1>Hello I'm Jeroen Druwé</h1>
<p>...</p>
</div>
</div>
To achieve this affect is had to set a height for the div:
#include breakpoint(70em) {
.about {
height: 340px; //This property will set the height of the div
}
.about .photo {
padding: 1em;
#include grid-span(2, 4);
}
.about .text {
padding-top: 7em;
padding-left: 1em;
display: inline;
#include grid-span(4, 6);
}}
If I remove the "height:340px" no background will be drawn:
http://oi39.tinypic.com/2s16ezl.jpg (only my thin borderline)
Is there a way to let the div wrap its height around its content (.photo,.text)?
Note: if i remove #include grid-span for .photo and .text the background shows but I do not want to lose the singularity functionality
Thanks in advance!
Don't span the container.
The problem you experience happens because Singularity columns are floated, and floated elements are taken out of the flow. This means that the container does not "know" about your columns any more, so it behaves like an empty element.
There's a property called clear that positions an element below any nearby floated element. If you create an extra element inside the container after all your columns, the clear: both; rule applied to it will push it below the floated columns, effectively stretching the container as high as columns are:
<div class="about">
<div class="photo">
<img class="photoBorder" src="images/foto_jeroen.png" alt=""/>
</div>
<div class="text">
<h1>Hello I'm Jeroen Druwé</h1>
<p>...</p>
</div>
<div class=clear></div>
</div>
.clear { clear: both; }
But don't add an extra element, that's not semantic. Instead, use the :after pseudo element that appears at the end of an element's contents. Using :after is just like creating a blank element at the end of element's contents.
.about {
&:after {
content: ''; // This is required for the pseudo-element to exist
display: block; // Default display is inline, have to change that for `clear` to work.
clear: both; // Yay, magic!
}
}
This technique is called "clearfix".
This can be done even simpler with the multi-purpose Toolkit extension from Team Sass, the authors of Singularity:
#import 'toolkit';
.about { #extend %toolkit-micro; }
The %toolkit-micro extendable has some additional rules that makes the clearfix trick work in older browsers. There's also the %clearfix-legacy extendable that works even in ancient browsers.
I fixed it.
Forgot to add an #include grid-span(12, 1); for my .about
I have two selectors to play with to achieve this design:
I have tried almost everything but I just cant seem to get the text to float right next to the big letters
Here is the code:
Jsbin
html:
<div class="processlinks-section-template">
<div class="processlinks-section-item" data-letter="H">
<div class="processlinks-section-item-title">
Haftonbladet.se
</div>
<div class="processlinks-section-item-title">
Hteabagz.com
</div>
</div>
<div class="processlinks-section-item" data-letter="C">
<div class="processlinks-section-item-title">
Cftonbladet.se
</div>
<div class="processlinks-section-item-title">
Cteabagz.com
</div>
</div>
</div>
CSS:
[data-letter] {
margin:7px;
background:#ef8;
}
[data-letter]:before {
content:attr(data-letter);
font-size:36px;
margin:7px;
}
.processlinks-section-template
{
width: 270px;
height: 100%;
}
}
.processlinks-section-item-title
{
margin-top:5px;
}
.processlinks-section-item-title a
{
color:black;
}
.processlinks-section-item-title a:visited
{
color:black;
}
.processlinks-section-item-title a:hover
{
color:#0084c9;
}
Any kind of help is appreciated
Note: I have a javascript that appends stuff so I rather just stay with these two selectors.
If there is one item it seems to ruin the design and I think thats the problem.
Take a look: jsbin.com/UHiZUJU/9/edit
Float both the letter and link to left and add clearfix with it.
Updated jsfiddle
Add float: left to the :before psuedo-element that contains the letter, and clear: left to the section container:
[data-letter]:before {
content:attr(data-letter);
font-size:36px;
margin:7px;
display:inline-block;
}
.processlinks-section-item {
clear:left;
}
Updated JSBin
Currently your :before psuedo-element is display: block by default in the absence of another display declaration, which means it automatically fills 100% the width of its parent and functions like it has a line break after it (as compared to inline elements).
Floating a block element means it only fills the width it needs rather than its usual behavior of filling the full width and also removes the implicit presence of a line break. The clear: left on the container just ensures the float is reset for each section.
To make it like in your image change your margin:auto 7px;
Trying to change a div background color when hover over another div. But I can't get it to worked. Been seing aroud her now, but can't find a similair question.
<style type="text/css">
#main {
width: 960px;
height: 600px;
margin: auto;
position: relative;
background: red;
}
#trykk {
width: 100px;
height: 100px;
background-color: yellow;
}
#trykk:hover #main {
background-color: green;
}
</style>
<div id="main">
<div id="trykk">
</div>
</div>
Thats the code I've been using. The only problem is that I'm not allowed to use javascript. So is there any way I can change background color on div #main when I hover over div #trykk?
A demo related to Rodik's answer, as he said you cannot change select parent using a child hence you cannot change the style of parent element, but if you want you can change your markup, as you said you cannot use javascript but if you can change the markup than it will go like this
Demo1
HTML
<div id="main">Main</div>
<div id="trykk">Trykk</div>
CSS
#main:hover + #trykk {
background-color: green;
}
Or if you want to nest your div's as you are doing right now, just change the selector like this
Demo2
HTML
<div id="main">Main
<div id="trykk">Trykk</div>
</div>
CSS
#main:hover > #trykk {
background-color: green;
}
CSS selection only works one way, from parent to child.
A child's state, hence, cannot affect it's parent's state.
A javascript mouseover event will be needed to implement this type of functionality.
with jquery you could do this:
$(function(){
$("#trykk").hover(function(){
$("#main").toggleClass("greenBackground");
});
});