I have the following CSS code and I'd like to convert it to LESS
.navbar-nav,
.navbar-nav > li,
.navbar-nav > li > a {
height: 100% !important;
}
And here is the HTML:
<ul class="nav navbar-nav">
<li class="active">Link</li>
<li>Link</li>
<li class="dropdown">
</li>
</ul>
What is the correct or the best way to convert the CSS to LESS?
Option 1 - As Is
You can always keep it as it is. Valid CSS is also valid LESS.
Option 2 - Parent Nesting with Extending
A nested LESS syntax using extend yields the same code:
.navbar-nav {
height: 100% !important;
> li {
&:extend(.navbar-nav);
> a {
&:extend(.navbar-nav);
}
}
}
This may have unwanted side effects if .navbar-nav has other properties associated with it, as all those will be carried over to the nested elements as well.
Option 3 - Like Option 2 but Avoiding other Properties
.navbar-nav {
/* non-extended properties can go here */
* > & {
height: 100% !important;
}
> li {
&:extend(* > .navbar-nav);
> a {
&:extend(* > .navbar-nav);
}
}
}
The output for this would actually be this CSS:
* > .navbar-nav,
.navbar-nav > li,
.navbar-nav > li > a {
height: 100% !important;
}
But * > .navbar-nav will match all the same elements as .navbar-nav would, and at least this allows you to group the three together without getting undesired properties set on the li and a elements that may also need setting in the .navbar-nav element (if such is a problem).
Option 4 - Bogus Class
Setting a bogus class name allows you to also keep separate properties in .navbar-nav, but generates some extra, unused css:
.setHeight {
height: 100% !important;
}
.navbar-nav {
&:extend(.setHeight);
> li {
&:extend(.setHeight);
> a {
&:extend(.setHeight);
}
}
}
CSS Output is this:
.setHeight,
.navbar-nav,
.navbar-nav > li,
.navbar-nav > li > a {
height: 100% !important;
}
(Future) Option 5 - Like Option 4, but No Bogus Class
In the future (current version of LESS as of this writing is 1.5.1), LESS will likely support extending pure mixins, in which case something like this will work so that the bogus class name is not generated:
.setHeight() {
height: 100% !important;
}
.navbar-nav {
&:extend(.setHeight);
> li {
&:extend(.setHeight);
> a {
&:extend(.setHeight);
}
}
}
Conclusion
What is "best" is purely going to be a matter of other factors that only you can determine for your project. In many cases, it may be best to simply keep the code as is (Option 1), but there may be warrant for using another option depending on the rest of your LESS structure and CSS property layout.
I would recommend Lessify: http://leafo.net/lessphp/lessify/. Just type your CSS and get LESS.
You can also check out this Webmasters thread: https://webmasters.stackexchange.com/questions/20369/is-there-a-tool-that-converts-css-to-less-css.
Related
I need to change the padding of my dividers to 0, but can't access them even though they are list items
https://codepen.io/majesticpotatoe-the-bashful/pen/KKwXwBw
.v-breadcrumbs li {
padding: 0px;
}
This CSS above didn't do the trick
This CSS works:
.v-breadcrumbs__divider {
padding: 0 !important;
}
updated codepen
Is it possible to obtain the position: sticky effect on an HTML table build up using just divs and css?
Apparently if I try to add the position: sticky rule to the header, which already contains the display: table-header-group rule, the sticky effects is null.
HTML
<div class="container">
<div class="header-row">
<div class="header>Header</div>
[...]
</div>
<div class="body-row">
<div class="body>Content</div>
[...]
</div>
</div>
CSS
.container {
display: table;
}
.header-row {
display: table-header-group;
position: sticky;
top: 0;
}
.body-row {
display: table-row;
}
.body, .header {
display: table-cell;
}
Live fiddle: https://jsfiddle.net/Lc0rE/9fxobxb0/1/
This question may be a few years old, but I came across the same issue. Especially now that position: sticky is a widely adopted positioning element now.
I got round this by adding a float to the Table header & Table Rows.
.header-row {
display: table-header-group;
position: sticky;
top: 0;
float: left;
}
.body-row {
display: table-row;
float: left;
}
https://jsfiddle.net/58zes8rr/
There may be a cleaner option for this using Flexbox (Arguably not widely supported yet).
I first tried the answer above and added float to my css definition however this blew away all my dynamic column widths and required i specify fixed widths for all columns. Not an option.
As with everyone else, my first instinct was to put sticky on the Row itself but that did not work. It worked PERFECTLY when i added sticky to each cell in the header row.
Fiddle Example
(TLDR)
ON EACH CELL OF YOUR HEADER ROW:
.td.searchResultHeader {
position:sticky;
top:0;
}
When using Sass I would do something global like this (which I got from CSS-tricks btw)
// Variables for MQ's
$mq-mobile-portrait : 320px !default;
$mq-mobile-landscape : 480px !default;
$mq-tablet-portrait : 768px !default;
$mq-tablet-landscape : 1024px !default;
$mq-desktop : 1382px !default;
Then I would create mixins for the media queries like this (I'll only include a few to give you an idea
// Mixins
// Both portrait and landscape
#mixin mobile-only {
#media (max-width : $mq-mobile-landscape) {
#content;
}
}
// Everything up to and including the portrait width of the phone
// Since it's the smallest query it doesn't need a min
#mixin mobile-portrait-only {
#media (max-width : $mq-mobile-portrait) {
#content;
}
}
So Sass has this #content which is great because it means that I don't have to declare the content within the mixin but can do an #include mixinName and it creates the parent wrapper for any CSS properties I need to put into it across different files. I discovered that this worked well for my work flow.
So here's an example of that in a partial .scss file:
section.footer {
height: 90px;
padding: 0 10px;
#include mobile-portrait-only {
padding-top: 10px;
background: $gum;
div.ftrLogo {
display: inline-block;
margin: 0;
height: 70px;
width: 45%;
div.smlLogo {
display: block;
background: url('../images/svg/small-logo2.svg');
width: 106px;
height: 49px;
margin: 0 auto;
padding: 0;
}
p.footer {
font-size: .375em;
color: $white;
text-align: center;
}
}
}
So as you can probably gather the #content allows you to just call an empty media query wrapper anywhere in your files (obviously you have to import all of your partials into one main file) but this is great.
Today I'm using LESS on a project and I like it a lot the problem is I can't seem to find an equivalent solution in LESS-land.
I was reading up on passing rulesets http://lesscss.org/features/#detached-rulesets-feature which looks like it's close to what I want but my brain is not understanding it today; I'm optimistic about tomorrow.
If anyone has tried anything like this or can immediately see the error in my ways; please provide your two cents. I really want to figure it out and thought to ask this gifted community of SO'ers.
Thank you in advance you're a baller!
// Variables for MQ's
#mq-mobile-portrait: 320px;
// Mixins
.mobile-portrait-only(#rules) {
#media (min-width: #mq-mobile-portrait) {
#rules();
}
}
Now you can use the following code:
div {
color: white;
.mobile-portrait-only({
color: white;
width: 100%;
max-width: 500px;
});
}
The above will compile into CSS code as follows:
div {
color: white;
}
#media (min-width: 320px) {
div {
color: white;
width: 100%;
max-width: 500px;
}
}
So detached rules are rules between {} assigned to a variable:
#detached: {};
Detached rules can be used as an argument for a mixin:
.mixin(#detached){}
You as call the above mixin with a detached rule as a parameter:
.mixin({color: red;});
or
#detached: {color: red;} // watch out for the last declaration wins rule for variables
.mixin(#detached);
Inside the mixin you should call the detached rules set to copy its properties and selectors (in fact you don't copy but insert them read for processing):
.mixin(#detached-rules) {
#detached-rules(); // parenthesis are required here
}
Finally for your example your code should look like that shown below:
#gum: url();
#white: white;
// Variables for MQ's
#mq-mobile-portrait: 320px;
// Mixins
.mobile-portrait-only(#rules) {
#media (min-width: #mq-mobile-portrait) {
#rules();
}
}
section.footer {
height: 90px;
padding: 0 10px;
.mobile-portrait-only( {
padding-top: 10px;
background: #gum;
div.ftrLogo {
display: inline-block;
margin: 0;
height: 70px;
width: 45%;
div.smlLogo {
display: block;
background: url('../images/svg/small-logo2.svg');
width: 106px;
height: 49px;
margin: 0 auto;
padding: 0;
}
p.footer {
font-size: .375em;
color: #white;
text-align: center;
}
}
});
}
I hadn't thought of doing it like Bass Jobsen suggested (although I've now seen that his approach is basically how the less docs do it), but I invented a mixin which I think is a bit more flexible. Though they are similar in result, I think the following solution allows for more customization and is easier to implement on the fly.
First I define the different sizes I want to use - to keep it simple, I'll just do two using a 'mobile first approach' (meaning if I don't include a media query, the rules will apply to all sizes and I should only include queries for sizes larger than mobile).
#tablet:~"(min-width:768px)";
#desktop:~"(min-width:1100px)";
Then the mixin:
.respond(#_size;#_rules){
#media #_size {
#_rules();
}
}
And Used Like the following:
.selector {
background:green;
.respond(#tablet,{
color:red;
background:blue;
});
}
And That Outputs:
.selector {
background:green;
}
#media (min-width:768px){
.selector{
color:red;
background:blue
}
}
With only two sizes to remember, it is easy enough just to do it the way Bass Jobsen suggested, but in practice, depending on how fine-grained I want my control to be, I may define up to 8 different media sizes (though I rarely use them all), and my approach above makes the process like calling one function rather than defining 8 different functions ( as I would do were I using the alternate approach ).
Hope this helps someone. It saves me a ton of time.
Is that possible to do with CSS.
I tried this:
#gallery_ul {
display: inline-block;
list-style: none outside none;
margin: auto auto auto auto;
width: 986px;
}
#gallery_ul li {
float:left;
margin:10px;
padding:10px;
text-align:center;
border:1px solid grey;
width:274px;
}
#gallery_ul img {
padding-bottom:5px;
}
If yes then how? Thank you.
You can either do it with CSS-columns or with javascript. I would suggest javascript, unless you don't need to worry too much about browser support/quirks.
See the masonry plugin for the most popular way to do so: http://masonry.desandro.com/
You can. But i think you have to be more specific.
One approximation is to create each block and set "float: left" property, then the squares will organize automatically or you can create three vertical columns and then put the squares inside.
I am having trouble getting a media query to work in bootstrap ( using rails). Below is the media query
#media (min-width: 768px) {
.center.navbar .nav, .center.navbar .nav > li {
display:inline-block;
float:none;
vertical-align:top;
width:100%;
}
.center .dropdown-menu {
display: none;
text-align:left;
}
.center .dropdown.open ul {
display: block;
}
The above media query is overriding all default behaviour no matter what the screen size is.I have received some advice from #baptme (thanks so much) to explain what is happening (which I now understand), basically because the query is using two classes and the default behaviour uses 1 class then the media query overrides. So my question is how do I get the media query to work only when the screen size is below 768px in this example and override the default styles when not
However this is where I get a little confused as when inspecting the elements in Firebug the defaults are as follows
.center.navbar .nav, .center.navbar .nav > li {
display: inline-block;
float: none;
vertical-align: top;
}
.center .dropdown-menu {
text-align: left;
}
Can anyone shed any more light on this, any help appreciated, if you would like to see it in action go to
http://46.32.253.11/
From your example:
This will hide the dropdown, remove the black hover and display the links one under the other aligned on the left:
#media (max-width: 979px) {
.navbar .dropdown-menu {display:none}
.navbar .nav > li a:hover { background-color:transparent}
.center.navbar .nav, .center.navbar .nav > li {display: table;clear:both};
}