I'm pretty new to Sass and I want to depending on certain condition use one css class or another...
Basically if my #media has size #media width:320px I would like to use bootstrap's col-xs-12 and #media has size width:480px then bootstrap's col-xs-6. I've tried to accomplish that by #extend these two classes within #media, but It wont compile.
There is a way to declare a variable and use it in classname?
Something like that:
$my-class: none;
#media (min-width:320px) {
$my-class: col-xs-12;
}
#media (min-width:480px) {
$my-class: col-xs-6;
}
And in my html:
<div class="row">
<div class="col $my-class"><!--Replace with the value of this variable.-->
</div>
</div>
I've read some material of Sass, but like a said, I'm very, very new to it.
I don't know if I'm asking correctly or even if make sense...
Your syntax is a bit off for SASS, but the root of the problem, is that you need to extend or include bootstraps classes. The col-xx-xx classes are actual media queries, so you are essentially placing a media query within a media query.
You want to do this for your SASS:
.row {
.col {
#extend .col-xs-12;
#extend .col-sm-6;
}
}
You don't need the special class for the .col unless you need to apply styles to elements with that class only.
So, your SASS could look like this:
.row {
.your-class {
#extend .col-xs-12;
#extend .col-sm-6;
}
}
Your HMTL
<div class="row">
<div class="col your-class"></div>
</div>
As the other answer posted, SASS doesn't directly affect your HTML and cannot read variables in your HTML. You simply need to use classes like normal CSS as SASS just gets processed to CSS.
As a note! For this to work you must include bootstrap in your SASS file so the preprocessor has access to the variables:
Your SASS file (at the top):
#include 'bootstrap.scss' <-- or wherever your bootstrap SASS files live
Hopefully this helps.
Sass can't access or modify HTML markup. It can only compile to a static CSS file.
Using Bootstrap's grid, you should be able to include column classes for each breakpoint, e.g. col-xs-12 col-sm-6 to get 12 columns # the xs breakpoint and 6 columns # the sm breakpoint.
Alternatively, if you're using Boostrap v4 (still in alpha), you can use #include make-col(6) within your media queries to have a little more control and keep your markup classes more semantic.
Related
Let I have 2 classes named class1 and class2. Also I have an element with id="responsive_element". What I want is to assign class1 to this element when screen size is below 768px and class2 otherwise.
I can do this in Less like:
#media screen and (max-width:768px){
#responsive_element{
.class1()
}
}
#media screen and (min-width:769px){
#responsive_element{
.class2()
}
}
Is there any "CSS only" way to achieve this?
Edit: I think I couldn't explain my question clear enough. I am already able to do this by compiling less, but the size of css file grows for the long class definitions and using them too much. I want to handle it with simply changing class of the element .
Except for the .class1() and .class2() calls, your Less code already is CSS. Specifically, the #media queries that apply styling based on screen size aren't Less-specific. So, for example, the following is pure CSS:
#media screen and (max-width:768px){
#responsive_element{
color: blue;
}
}
#media screen and (min-width:769px){
#responsive_element{
color: red;
}
}
If you want to convert your Less into CSS, all you need to do is copy the CSS code from .class1 and .class2 into the place of the .class1() and .class2() calls. In fact, since Less is implemented as a converter to CSS, you can just use the online converter at LESS2CSS to do the conversion for you.
If you're asking if there's a way in plain CSS to write these queries so that they use an existing .class1 and .class2 style definition without copying, then I believe the answer is no. The main reason Less was invented was because CSS doesn't support this kind of reuse of styling information.
If you mean, is there a way to use CSS alone to change the class of an element in response to changing screen sizes (literally, adding or removing new classnames to the element's HTML "class" attribute so CSS for different classes will take effect on that element), then the answer is simply "no, you cannot do this with CSS alone".
(Less can't do this either. Your original Less code doesn't change the class attribute of the element, as you can plainly see by looking at the generated CSS. It just uses classes as a handy trick to name sections of shareable CSS.)
The only way to change the element's class in the manner you seem to want is to make changes to the DOM, which you can obviously do via JavaScript but not with CSS alone.
If I have an element as such (using Bootstrap css):
<div class="col-sm-6 col-md-3 mydiv">
some text here
</div>
I would like to be able to do something like this (example in SCSS):
.mydiv {
&:not(.col-sm-6) {
font-weight:bold;
}
&:not(.col-md-3) {
text-transform:uppercase;
}
}
So the col-sm-* and col-lg-* take care of resizing the container according to their respective #media queries, but I would also like to apply a certain style when one of them is active vs. the other.
The example code obviously doesn't work (since the div always has the .col-sm-6 and .col-md-3 classes).
I know i can create my own #media queries, or customize bootstrap.css media queries and add .mydiv to them, but i have several modules and would like to keep their respective CSS (SCSS) separate without having to also create multiple #media queries in each of them.
Is it possible to hide element on devices smaller than tablets (col-sm-* & col-xs-*) without using 2 bootstrap classes visible-md visible-lg on the element with bootstrap 3's predefined classes:
<div class="col-sm-8 visible-md visible-lg"> ..</div>
Thanks
dkj
As per the documentation the visible (and hidden) classes only show for that media query size, if you wish to extend the classes to allow you to do this with a single class you can but this would require you to have a basic understanding on media queries and css3 (sass or less if you use a preprocessor).
You could just use the hidden-xsclass to hide it below the col-sm-* query range as #Vijay Maheriya said in the comments.
If you only have one (or a few) element(s) you want to hide/show selectively, it's probably easiest/best to just use both classes. If you are doing this with several elements and prefer to keep your markup cleaner, you can easily just create your own class for this. For instance:
.hidden-sm-xs {
display: none;
}
#media screen and (min-width: 992px) {
.hidden-sm-xs {
display: block;
}
}
If you've customized the Bootstrap breakpoints, note that you will need to adjust 992px to match your custom breakpoint. As others have noted, you could just override one of Bootstrap's existing classes, but I prefer to just create a new one...It's the same amount of CSS, and you never know when you might actually want hidden-xs to hide only on the smallest screens like it was intended. But if you've overridden it, you'd have a lot of refactoring to do.
Yeah, forget about the classes and just do:
.your-selector {
display: none;
}
#media(min-width: 992px) {
.your-selector {
display: block;
}
}
Use the classes for more specific cases when realy needed.
I am very new to sass and i am trying to convert a bootstrap layout to bootstrap-sass so that i can have multiple "brands" use the same code-behind with different layouts.
I've managed to use variations of the following to have two versions with different grid structures:
.someclassname {
#include make-sm-column(6);
}
is there an equivalent for hidden-xs (perhaps something like #include make-hidden(xs)?
The idea being one site might have a div hidden on mobile but the other site would want the same div visible.
You can do the following
.someclass {
#extend .hidden-xs;
}
I am developing a Wordpress theme with the help of bootstrap so I am manually applying cases on all content images like this:
<img src="images/logo_03.png" class="img-responsive">
Is there any way to apply the same class properties automatically? I don't want to query for this purpose. I am sure bootstrap has a way to solve my problem, so let me know any solution with CSS.
You can use the LESS mixins directly in your theme.
If you want all images to be responsive you can say:
//in your theme.less file
img {
.img-responsive();
}
Will give you this in your theme.css file:
img {
//all Bootrap CSS properties that make your image responsive
//including surrounding media queries
}
However, this is not recommended because it applies to all <img> tags.
A more professional option would be to make your class like:
//in your theme.less file
.theme-img {
.img-responsive();
//additional theme-specific styling
border: 1px solid blue;
}
and apply it to your images:
<img class="theme-img" src="..." />
Update:
unlike the other answers that suggest using jQuery, this solution doesn't need any scripting and it works in old browsers (eg. IE). Besides it will work for any <img> tag that is inserted into the document even after the jQuery code is run. If you decide to go with Javascript, however, I recommend using document.querySelectAll() because it doesn't need jQuery and runs slightly faster.
Should be easy enough to add the class based on the element attribute, see below:
<script type="text/javascript">
$(document).ready(function() {
$("img").addClass("img-responsive");
});
</script>
The best way is to use the declarations provided by bootstrap for the class .img-responsive in your CSS.
For instance, you can set all the images of your website with the content of that class:
img {
display: block;
max-width: 100%;
height: auto;}
and that's it.
All your images will have the content of the class .img-responsive without the need of specify it.
If you want to add the img-responsive class to post thumbnail image in WordPress you can add like this
the_post_thumbnail('thumbnail', array('class' => 'img-responsive'));
If you want to add to another image in content you can add img-responsive class to those image with jQuery just add this to your javascript file
jQuery(document).ready(function( $ ) {
/*add Class to Element*/
$('.wp-post-image').addClass('"img-responsive');
});