I am struggling to understand how guarded expressions (from a different topic I am led to believe that these are the LESS equivilant of if functions) can be used to set individual properties. Although the below works it seems a little over the top, however it does allow me to do what I need to do. Is this the best way? If not what would be considered so?
.cover() {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.cover(#property, #value) when (#property = "top") {
position: fixed;
top: #value;
right: 0;
bottom: 0;
left: 0;
}
.cover(#property, #value) when (#property = "bottom") {
position: fixed;
top: 0;
right: 0;
bottom: #value;
left: 0;
}
.cover(#property, #value) when (#property = "left") {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: #value;
}
.cover(#property, #value) when (#property = "right") {
position: fixed;
top: 0;
right: #value;
bottom: 0;
left: 0;
}
This lets my type for example...
#cover{
.cover("left", 55px);
}
which renders as
#cover{
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 55px;
}
If you use LESS version >= 1.6, you can use dynamic property names, which would reduce your code to this:
.cover(#property: left, #value: 0) {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
#{property}: #value;
}
#cover {
.cover(left, 55px);
}
In addition to the #mingos answer, an optimized impl. (i.e. with no redudant properties in output) could look like:
.cover(#property: top, #value: 0) {
position: fixed;
top: #t;
right: #r;
bottom: #b;
left: #l;
.-(#property);
.-(...) {#t: 0; #r: 0; #b: 0; #l: 0}
.-(top) {#t: #value}
.-(right) {#r: #value}
.-(bottom) {#b: #value}
.-(left) {#l: #value}
}
// usage:
.a {.cover(left, 55px)}
.b {.cover(right, 33px)}
Related
I have a react app and I am trying to write a css rule like the following using styled components:
.react-draggable-transparent-selection .preview-wrapper:after {
content: '';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
filter: blur(4px);
}
I am trying this but it does not seem to work:
const PreviewContainer = styled.div`
position: relative;
height: 100%;
flex-grow: 1;
& .react-draggable-transparent-selection {
&:after {
content: '';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
backdrop-filter: blur(5px);
}
}
`;
the .react-draggable-transparent-selection class and .preview-wrapper are not on the same element. How can I write this using styled components?
Ok, I am pretty sure that styled components do not support applying a style based on an ancestor having a conditional css class based on this issue. I have opened my own github issue with styled components here, but in the mean time I solved this using state in react and passing as a prop like this:
const PreviewContainer = styled.div`
position: relative;
height: 100%;
flex-grow: 1;
${({ isDragging }) => isDragging &&
css`
&:after {
content: '';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
backdrop-filter: blur(5px);
}
`
}
`;
Is the element with the class react-draggable-transparent-selection a child of the PreviewContainer? Something like the below?
<PreviewContainer>
...
<div className="react-draggable-transparent-selection">
</div>
...
</PreviewContainer>
If it is, this is how you would style it using Styled Components:
const PreviewContainer = styled.div`
position: relative;
height: 100%;
flex-grow: 1;
& .react-draggable-transparent-selection {
&:after {
content: '';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
backdrop-filter: blur(5px);
}
}
`;
I have created a code for a page following Stackoverflow answers. It works very well, but I'd like to optimize it, which I do not know since I'm not a programmer. What is the right way?
#black:before {
content: "";
position: fixed;
top: 0;
right: 0; left: 0; bottom: 0; background: none;
z-index: -2;}
#red:before {
content: "";
position: fixed;
top: 0;
right: 0; left: 0; bottom: 0; background: none;
z-index: -2;}
#black:target::before {background: #ACAA92;}
#red:target::before {background: #ACAA92;}
#black:hover .text{display:block;}
#com:hover .text{display:block;}
All selectors which should share the same properties and values can simply be comma separated. You can write them all on one line though a more preferred style is to put each one its own line to aid readability:
#black:target::before, #red:target::before { background: #ACAA92; }
#black:hover .text,
#com:hover .text {
display:block;
}
https://developer.mozilla.org/en-US/docs/Learn/CSS/Introduction_to_CSS/Combinators_and_multiple_selectors#Groups_of_selectors_on_one_rule
I often use similarity less , for example:
.position{
position:absolute;
right:10px;
top:20px;
}
So I want to use a mixin :
.position(#absolute:absolute;#top:0;#right:0;#bottom:0;#left:0){
position: #absolute;
top:#top;
right: #right;
bottom:#bottom;
left:#left;
}
But sometimes I don't need right, but the function always inject right into my code.
So what I should do if I want to have the follow code:
.position(#absolute:absolute;#top:12px;#left:12px;#bottom:12px);
//without 'right'
.position{
position: absolute;
top:12px;
bottom:12px;
left:12px;
}
Thanks!
You can rewrite your mixin like this (it doesn't have any required parameters):
.position(
#position: absolute;
#top: false;
#right: false;
#bottom: false;
#left: false
){
position: #position;
& when not (#top = false) {
top: #top;
}
& when not (#right = false) {
right: #right;
}
& when not (#bottom = false) {
bottom: #bottom;
}
& when not (#left = false) {
left: #left;
}
}
Now you can set only those params you really need:
a {
.position(
#top: 20px,
#left: 0px
);
}
b {
.position(
#bottom: -50px,
#right: 0
);
}
Css output:
a {
position: absolute;
top: 20px;
left: 0px;
}
b {
position: absolute;
right: 0;
bottom: -50px;
}
I have a small .png file that repeats to form a background image. I need to set the body height of my page to 100% in order to use min-height property on my content wrapper. However, trying to use the background-image in conjunction with height:100% results in the image getting cut off when the page is scrolled. See picture to elaborate what I mean:
Background on top
But when scrolling it is cut off
How do I get the background image to repeat over the whole page, even after the user scrolls down? Here is the css:
body {
background-color:#9AAEBF;
overflow-y:scroll;
height:100%;
}
html:after {
background-image: url('http://www.example.com/img/background.png');
opacity:0.4;
content: "";
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
z-index: -1;
}
Thanks for your ideas.
EDIT:
This is the image i need repeated:
Here is a fiddle:
http://jsfiddle.net/Nick_B/x2h3g/
try this
html:after {
background-image: url('http://www.example.com/img/background.png');
background-repeat:repeat;
opacity:0.4;
content: "";
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
z-index: -1;
}
Now used to background-size
As like this
body {
background-color:#9AAEBF;
overflow-y:scroll;
height:100%;
}
html:after {
background-image: url('http://i.stack.imgur.com/iuzZU.png');
background-size:100%;
opacity:0.4;
content: "";
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
z-index: -1;
}
Demo Full page
Live Demo
you can achieve your desired result through give the backgroun-size:100% in your html:after
CSS
html:after {
background-image: url('http://i.stack.imgur.com/iuzZU.png');
background-size:100%;
opacity:0.4;
content: "";
top: 0;
left: 0;
bottom: 0;
right: 0;
position: absolute;
z-index: -1;
}
I can't get my images to go side my side (below share and follow) on this page:
http://toddheymandirector.com/REEL/index_newlook_gallery2222.html
The suspect code:
#gallery ul div {
min-width:26.6%;
margin:0;
float:left
background-color:#595959;
}
Any ideas?
find this block into your css file:
img.a {
left: 0;
position: absolute;
top: 0;
z-index: 10;
}
and replace the position to relative:
img.a {
left: 0;
position: relative;
top: 0;
z-index: 10;
}