Floating multiple elements in a container - css

I got this:
Demo.
And I would like this result:
I got this far:
Demo.

There are 2 ways you can do this in a flexible way. Both remove the need for floats (and clearing floats) and neither requires JavaScript to achieve.
The first is to use flexbox. The advantage here is that if there isn't enough room for the elements to fit comfortably on one row, they'll adapt. The down side is that IE10 is the first version of IE to support it. As a fallback, you'll need to use display: inline-block on each of the elements with flex applied to them.
http://jsfiddle.net/n4Yzc/7/
.always-visible {
display: flex; /* prefixed attribute: -moz-flex, -webkit-flex */
align-items: stretch; /* prefixed property: -moz-align-items, -webkit-align-items */
flex-flow: row wrap; /* prefixed property */
}
.toggle-me {
background-color: green;
flex: 1 1 5em; /* prefixed property */
}
.name-mail {
background-color: red;
flex: 1 1 8em; /* prefixed property */
}
.description {
background-color: yellow;
flex: 2 1 50%; /* prefixed property */
}
.favourite-food {
background-color: orange;
}
The second is to use the display: table properties. This solution is well supported (IE8+), but it ends up looking squished on narrower devices.
http://jsfiddle.net/n4Yzc/5/
.always-visible {
display: table;
}
.toggle-me {
background-color: green;
display: table-cell;
}
.name-mail {
background-color: red;
display: table-cell;
}
.description {
background-color: yellow;
display: table-cell;
}
.favourite-food {
background-color: orange;
}

adjust height of toggle-me, name-mail and description class like the following
.toggle-me {
background-color: green;
float:left;
height:150px;
}
.name-mail {
background-colo
r: red;
float:left;
height:150px;
}
.description {
background-color: yellow;
height:150px;
}
.favourite-food {
background-color: orange;
}

See the fiddle for desired output: http://jsfiddle.net/n4Yzc/2/
.toggle-me {
background-color: green;
float:left;
height:100px;
}
.name-mail {
background-color: red;
float:left;
height:100px;
}
.description {
background-color: yellow;
float:left;
width:67%;
}
.favourite-food {
background-color: orange;
}
Updated fiddle: http://jsfiddle.net/n4Yzc/6/
Width variable width of description div on basis of toggle-me and name-mail divs.

Related

Sass how to use ampersand within :not() pseudo class to mean not having a parent class?

I need add a condition "not parent class" to the code but it is not working.
Below is what I've tried:
.display-download-icon {
&:not(.media-2 &){ // if it does not has a parent with .media-2 class
&:hover {
.image-overlay {
display: block;
opacity: 1;
z-index: 2;
}
}
}
}
But it translates into the CSS below:
.display-download-icon:not(.media-2 .display-download-icon):hover .image-overlay {
display: block;
opacity: 1;
z-index: 2;
}
The HTML structure is:
<div class="media-2">
<div class="display-download-icon">
<div class="image-overlay"></div>
</div>
</div>
Can you help?
Thanks
To begin with, we have to get some things straight. You probably misunderstood this article with the somewhat unfortunate title. Although CSS has a child selector, it lacks the possibility to select parents. As SASS is only a more convenient way to write CSS, SASS cannot have such a thing either. This said, let's move on to your question.
If I understand you correctly, you want to have the hover effect on the image-overlay element only if it is not contained in an element with the class media-2.
If that's the case, then you will have to change your approach.
First, define the normal behaviour, i.e the one that is expected for all the elements except the ones with the .media-2 class. Then define a new rule for the .media-2 class children.
SASS
.display-download-icon {
display: block; /* Just for the demo */
height: 100px; /* Just for the demo */
width: 100%; /* Just for the demo */
border: 1px solid black; /* Just for the demo */
margin-bottom: 10px; /* Just for the demo */
> .image-overlay {
display: block;
opacity: 1;
z-index: 2;
height: 100%; /* Just for the demo */
width: 100%; /* Just for the demo */
&:hover {
background: red; /* Just for the demo */
}
}
}
.media-2 .display-download-icon > .image-overlay:hover {
background: none; /* Just for the demo */
}
Snippet
Check this snippet for the functionality
.display-download-icon {
display: block;
height: 100px;
width: 100%;
border: 1px solid black;
margin-bottom: 10px;
}
.display-download-icon > .image-overlay {
display: block;
opacity: 1;
z-index: 2;
height: 100%;
width: 100%;
}
.display-download-icon > .image-overlay:hover {
background: red;
}
.media-2 .display-download-icon > .image-overlay:hover {
background: none;
}
<div class="media-1">
<div class="display-download-icon">
<div class="image-overlay"></div>
</div>
</div>
<div class="media-2">
<div class="display-download-icon">
<div class="image-overlay"></div>
</div>
</div>
<div class="media-3">
<div class="display-download-icon">
<div class="image-overlay"></div>
</div>
</div>
I hope that you get the idea.
All the "&" does is act as an alias for the preceding parent selector, which is why you are getting the compiled output you are (because of the "&" after the .media-2 classname) . You just need to remove the last "&" and it will compile fine.
.display-download-icon {
&:not(.media-2){ // removed the "&"
&:hover {
.image-overlay {
display: block;
opacity: 1;
z-index: 2;
}
}
}
}
The above with compile to
.display-download-icon:not(.media-2):hover .image-overlay {
display: block;
opacity: 1;
z-index: 2;
}
But you can also remove some of that nesting as well to make it more readable
.display-download-icon:not(.media-2):hover {
.image-overlay {
display: block;
opacity: 1;
z-index: 2;
}
}
Which will compile to the same output.
Sadly, there's no such thing as a parent selector in CSS, so this isn't really a Sass ampersand issue as much as a core behaviour of CSS.
You'll need to write a dedicated rule like this:
/* base icon styling */
.display-download-icon {
...
}
/* separate rule for parent case */
div:not(.media-2) {
.display-download-icon {
&:hover {
.image-overlay {
display: block;
opacity: 1;
z-index: 2;
}
}
}
}

#mixin select a specific placeholder by using argument

I'm trying to use a mixin with an argument and inside a placeholder.
The idea is to use one line of code to select a specific placeholder in a class.
Actually, I don't know if there is another better way to do that, maybe with a function or other.
I'm learning Sass, so I'm trying to experiment.
HTML
<div class='wrap'>
<div class='box'></div>
<div class='box'></div>
</div>
SCSS
// VAR
$size-xs: 30px;
$size-s: 50px;
$size-m: 70px;
$color-1: orange;
$color-2: rgb(34,139,86);
#mixin box($val) {
%box-one {
width: $size-s;
height: $size-s;
background: $color-1;
margin: auto;
border-radius: 6px;
}
%box-two {
width: $size-s;
height: $size-s;
background: $color-2;
margin: auto;
border-radius: 6px;
}
.box {
#extend #{$val} !optional;
}
}
.wrap {
width: 100%;
height: 100px;
background: #f5f5f5;
display: flex;
justify-content: space-between;
#include box(box-one);
}
thank you!
Currently, your code is not working because your forgot to put a % before #{$val}:
.box {
#extend %#{$val} !optional;
}
Anyhow, it's not a good idea to put placeholder selectors inside a mixin because every time the mixin is called, you are creating new selectors. It means that for example if you add:
.randomSelector {
#include box(box-one);
}
You will get:
.wrap .box { ... }
.randomSelector .box { ... }
Instead of:
.randomSelector .box, .wrap .box { ... }
So I would recommend you to externalize %box-one and %box-two.
One last thing, if the only difference between these two classes is the background property, maybe using a single class regrouping the commons properties would be a better optimization:
.box {
width: $size-s;
height: $size-s;
margin: auto;
border-radius: 6px;
}
%box-one {
background: $color-1;
}
%box-two {
background: $color-2;
}
#mixin box($val) {
.box {
#extend %box-#{$val} !optional;
}
}
.wrap {
width: 100%;
height: 100px;
background: #f5f5f5;
display: flex;
justify-content: space-between;
#include box(one);
}
If you have more box styles you can even dynamically create the placeholders:
$boxStyles: (
one: $color-1,
two: $color-2
);
#each $box, $style in $boxStyles {
%box-#{$box} {
background: $style;
}
}

How to target an after of an element with two classes with SCSS?

I know that when you want to target a div with two classes with SCSS, it should be done like this:
HTML:
<div class="item active">...</div>
SCSS:
.item {
&.active {
/* enter code here */
}
}
But when I want to target an element's after, what then? As in with CSS:
.item.active:after {
/* enter code here */
}
Thanks!
Well you can do it in a few ways
a. This you should use if you want to add some styles to the .active class also.
.item {
background: red;
height: 100px;
width: 100px;
&.active {
&:after{
content: "aaa";
}
}
}
or
b. This you should use if you want just to add some styles to the :after pseudo-element if item has class active
.item {
background: red;
height: 100px;
width: 100px;
&.active:after{
content: "aaa";
}
}
see jsFiddle
Try:
.item {
&.active {
&:after {
/* enter code here */
}
}
}
.item {
&.active {
/* enter code here */
&:after{
/* enter code here */
}
}
}

Using the same values with different classes in different #media without duplicating

I have CSS like that:
.class1 {
display: block;
color: red;
}
.class2 {
display: block;
color: blue
}
#media(max-width:800px) {
.class1-mobile {
display: block;
color: red;
}
.class2-mobile {
display: block;
color: blue
}
}
#media(min-width:800px) {
.class1-desktop {
display: block;
color: red;
}
.class2-desktop {
display: block;
color: blue
}
}
All the properties and their values are the same and the only difference is in class names and media queries. So I'd like to know if there is a way not to duplicate them.
here is a simplified way of doing your media queries using non-mobile first approach (max-width)
.class {
display: block;
/* optional because div is already block element*/
}
.class1 {
color: blue
}
.class2 {
color: red
}
#media(max-width:800px) {
.class1 {
color: red;
}
.class2 {
color: blue
}
}
<div class="class class1">red</div>
<div class="class class2">blue</div>
Use one class.
You can use the same class on multiple elements and You can use multiple
classes on the same element.

CSS Syntax (very basic)

I would like to do the following thing and I am wondering about the best way to go about.
I have a div which is each 20% of the container's width (5 blocks). I would like to give a different background color to each block but only using one CSS class. What is the best way to do it?
In the past, I used to create 5 different classes where only the bg color is different (as everything else is the same - 20% width and same height) but I think there is a better way to do it.
Is it possible to create a class in the CSS that handles the different bg colors for each container?
I am not very sure if it will work, but but can try this - have a same class for all the divs (as you are already having - say, the class is 'myDiv'). Then In css -
.myDiv:nth-child(1){
background-color: red;
}
.myDiv:nth-child(2){
background-color: blue;
}
.myDiv:nth-child(3){
background-color: yellow;
}
and so on..
Hope this helps :)
See http://www.w3.org/TR/css3-selectors/
in this link see css3 selector for li,div and ...
tr:nth-child(2n+1) /* represents every odd row of an HTML table */
tr:nth-child(odd) /* same */
tr:nth-child(2n+0) /* represents every even row of an HTML table */
tr:nth-child(even) /* same */
/* Alternate paragraph colours in CSS */
p:nth-child(4n+1) { color: navy; }
p:nth-child(4n+2) { color: green; }
p:nth-child(4n+3) { color: maroon; }
p:nth-child(4n+4) { color: purple; }
/* Alternate division enter code herecolours in CSS */
.mydiv:nth-child(1) { color: navy; }
.mydiv:nth-child(2) { color: green; }
.mydiv:nth-child(3) { color: maroon; }
.mydiv:nth-child(4) { color: purple; }
Play around with :nth-child or adjacent sibling selectors
Like so
.parent { width: 100%; }
.parent > div { width: 20%; float: left; }
.parent > div:nth-child(1) { background-color: black; }
.parent > div:nth-child(2) { background-color: blue; }
.parent > div:nth-child(3) { background-color: purple; }
.parent > div:nth-child(4) { background-color: orange; }
.parent > div:nth-child(5) { background-color: yellow; }
Example here

Resources