I am a web front-end developer (newbie).
Hypothetically, if I am writing code for a web page using Twitter Bootstrap and want a responsive sidebar, I can do something like this:
<div class="col-xs-12 col-md-3">...</div>
Let's say, in the interest of separation of concerns, I would like the design people to decide how many columns wide the sidebar should be on each screen width.
Wouldn't it be better to do something like this:
<div class="sidebar">...</div>
and have the designer do something like this:
sidebar = col-xs-12 col-md-3
somewhere in the CSS?
Is this possible? Are there tools that will allow this? Am I way off base?
This is possible, with some help from a CSS preprocessor like the following:
Sass
.sidebar {
#extends .col-xs-12;
#extends .col-md-3;
}
Less
.sidebar {
&:extend(.col-xs-12);
&:extend(.col-md-3);
}
Hope this helps!
You should use a preprocesor to compile your CSS, and create semantic class from unit classes.
For example in Sass:
.sidebar {
#extends .col-xs-12;
#extends .col-md-3;
}
You can download Bootstrap in Sass on the offical website.
You can read the article "Using Sass To Semantically #extend Bootstrap", it can help you achieve what you want.
You propose instead of determining it in the html with a class on div like this:
<div class="col-xs-12 col-md-3">...</div>
determine it in the css with something like this:
.sidebar {
width: 100%;
#media (min-width: $screen-sm-min) and (max-width: $screen-sm-max) {
width: 50%;
}
#media (min-width: $screen-md-min) {
width: 33%;
}
}
In any case you have to edit something: either html file or css file. Consider your project, to know which one is easier.
I would suggest to put columns into the html (it would be kind of default case)
<div class="sidebar col-xs-12 col-md-3">...</div>
and then, if needed, override it for specific pages in css with something like this:
.page-order .sidebar {
width: 33%;
}
Related
I know this may seem like a very simple question, but please understand I know very little about CSS.
Essentially, I have a responsive website with two columns. I want to add an ad on the right for Desktop, and one of the left for Mobile. So far so good, I only have to copy and adapt the code they provide on their website, and this did work for mobile, hiding the ad:
<style type="text/css">
.adslot_1 { display:inline-block; width: 320px; height: 50px; }
#media (max-width: 400px) { .adslot_1 { display: none; } }
</style>
<ins class="adsbygoogle adslot_1"
data-ad-client="ca-pub-1234"
data-ad-slot="5678"></ins>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
However, how can I adapt this for desktop? I assumed all I had was to change the max-width: 400px to min-width, but it doesn't seem to be working at all... any ideas on why, and how can I fix it?
In many cases scripts that insert content dynamically tend to modify the element's inline CSS properties and that usually overrides the style definitions, because of specificity. You can learn more on specificity in CSS on MDN.
What I would recommend you to do is to add !important to your style definition to see if that does hide the element. Your style should look something like: (I added some extra info to the #media query.
.adslot_1 {
display:inline-block;
width: 320px;
height: 50px;
}
#media only screen and (max-width: 400px) {
.adslot_1 {
display: none !important;
}
}
What you could also do is open your browser's inspect tools (developer tools) and inspect that specific element to see which styles are being applied and also check that based on media query.
I have the follow html in Angular2.
<div class="col-xs-12 col-lg-8" >
<p style="font-size: 30px">
{{ teacher.personalInfo.name }}<br/>{{ teacher.personalInfo.surname }}
</p>
</div>
In my view, the text is aligned at the left (as I wanted). How can I say that when is for col-xs-12 it has to be centred?
Thank you.
The best approach for this would be to create a specific class for you container and only use media queries to modify the text position on mobile.
Here's the general idea following the BEM CSS naming convention:
<style type="text/css">
.thing {
... some styles
}
.thing__title {
text-align: center;
}
// tablets start at 768px width
#media (max-width: 767px) {
.thing {
... some mobile styles
}
.thing__title {
text-align: left;
}
}
</style>
<div class="thing col-xs-12 col-lg-8">
<p class="thing__title">... some text</p>
</div>
No need to increase the loading time of your site by adding jQuery to add styles to an element.
Bad idea to target modifier classes from component libraries. Especially your grid as you might removing that or the class name could be deprecated in later versions leaving your site vulnerable.
Can you use jquery?
$('.col-xs-12').css('text-align','center');
There is a good explanation of Bootstrap 3 and 4 Media Queries here at Bootstrap 3 breakpoints and media queries.
Bootstrap provides a great deal of flexibility to your project, but from minute details such as text justification between breakpoints, you will need to add a media query to your own CSS and apply the styles as desired.
So you might try something like this:
<div class="teacher-info col-xs-12 col-lg-8" >
<p class="ta-xs-left" style="font-size: 30px">
{{ teacher.personalInfo.name }}<br/>{{ teacher.personalInfo.surname }}
</p>
</div>
<style>
// Default to center the paragraph to center
.teacher-info p {
text-align:center;
}
// Large devices (desktops, 992px and up)
#media (min-width: 992px) {
// When the screen is larger than a tablet, left align the text
.ta-xs-left {
text-align:left;
}
}
</style>
Edit
In line with martinsoender's answer, I agree you shouldn't target modifier classes, and should add your own classes. This edit is to show how I would do that.
Essentially, I would add a class to the parent to denote what holds (teacher-info), then give the element I want to modify a class. In this case I create a class that looks similar to a bootstrap class. ta-xs-left ({text-align}-{Xtra-Small}-{Alignment}), then it can be reused wherever you need it.
So I'm fairly new to coding and I set a task for myself, that being to recreate my WordPress site from the ground up using HTML, CSS and javaScript, now as I have looked for resources online for the best method ongoing about making a responsive navigation bar, of course, I came across an example on W3Schools.
Now the question I have is what is the best way to go about Adjoining Classes, my CSSLint picks it up as bad practice(At least that's what I'm taking away from it) so I'm met with a conundrum whether to stick with them and just make it incompatible for IE 6 (I believe) or to just learn the use the better standard.
#media screen and (max-width: 600px) {
.cMainNav.responsive {
position: relative;
}
.cMainNav.responsive a.icon {
position: absolute;
right: 0;
top: 0;
}
.cMainNav.responsive a {
float: none;
display: block;
text-align: left;
}
}
I think you're misunderstanding. Adjoined classes are not a standard. This is just one of the many parts of css specificity.
If you have an element with two classes:
<div class="square red"></div>
<div class="square green"></div>
You can target their combination like so:
.square.red {}
CSSLint may be warning you that making really long, complex selectors like this is less than ideal. You never want to end up with something like this:
.square.red.fixed.flex.button{}
If you need really specific targeting, you're better off assigning an id or a specific class altogether.
<div id="loginModal"></div>
In general, all of these are just tools at your disposal. Read more about specificity and keep in mind that there's really no "wrong" option here, but any of these options can be abused.
I currently have a CSS with rules which are duplicates in both .small and the media query for #a, because I want the same behaviour when browser width is smaller than 600px and when a button is clicked.
In my real case, the rules are very long, and I would like to avoid to duplicate them. How to deduplicate such a CSS?
$('#b').click(function(){ $('#a').addClass('small'); });
#a { background-color: green; }
.small { background-color: yellow !important; /* many other rules */ }
#media (max-width: 600px) {
#a { background-color: yellow !important; /* many other rules */ }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="a">Hello</div>
<input id="b" type="button" value="Change" />
I don't think there's a way to do this in pure CSS, this is one of the things you can solve with precompilers like SASS where you can inject CSS rules anywhere you want with variables or mixins for example
I can think of Javascript solution to your problem.
window.onresize = function(event){
if(window.innerWidth < 600px){
// add the class to the element
}else{
// remove the class
}
}
I don't think you can achive what you want in any other way without using precompiler.
You'd have to use a CSS pre-processor to do this.
Otherwise you'd have to use jQuery to toggle some other class based on viewport width or assign the .small styles to #a as well, then make a media query to undo it all when you don't want it to look like that.
Are there known issues with Web Essentials and Bootstrap mix-ins? I am getting incorrect CSS (repeatable -- see my code below).
I am working with the bootstrap.less package and using Web Essentials to compile the less file to css. (VS 2013 + Update 1/Latest web essentials)
So, if I create a simple page like so, everything works:
that is, the 2 divs stack horizontally, until my browser window gets too small, then they stack vertically.
<div class="container">
<!-- this row uses no LESS-->
<div class="row">
<div class="col-sm-12 col-md-6">
<span>Div1</span>
</div>
<div class="col-sm-12 col-md-6">
<span>Div2</span>
</div>
</div>
</div>
Now, if I create this little block of LESS
#import (reference) 'bootstrap/bootstrap.less';
.div-container {
.make-md-column(6);
.make-sm-column(12);
}
And then change my code like this:
<div class="container">
<!-- this row uses the bootstap LESS mixin (css compiled with Web Essentials)-->
<div class="row">
<div class="div-container">
<span>Div1</span>
</div>
<div class="div-container">
<span>Div2</span>
</div>
</div>
</div>
It stops working -- specifically, the divs stack vertically, even on a medium or large width.
Now, I think that the LESS that I put in is exactly equivalent to the bare classes that I was using in my first snippet.
I think that the problem is that WebEssentials/Visual Studio is not compiling the LESS correctly.
I believe that is the problem, becuase if I look at the generated css, there are no media queries in it, but the bootstrap mixin file (mixins.less to be specific) specifies a media query for hte .make-xx-col mixins.
I'm probably not the first person to hit this, so I must just be doing something wrong. Is there a workaround? Have I mis-configured something? Is there some other LESS compilation solution that I should use instead of Web Essentials?
Reorder the mixins so the compiled css output doesn't override the selectors with larger min widths.
Less:
.div-container {
.make-sm-column(12);
.make-md-column(6);
}
CSS:
.div-container {
position: relative;
min-height: 1px;
padding-left: 15px;
padding-right: 15px;
}
#media (min-width: 768px) {
.div-container {
float: left;
width: 100%;
}
}
#media (min-width: 992px) {
.div-container {
float: left;
width: 50%;
}
}
#import (reference) 'bootstrap/bootstrap.less';
.div-container {
.make-md-column(6);
.make-sm-column(12);
}
I think this is a simple file import problem..Try Using
#import '../../../bootstrap/bootstrap.less';
Or Modify your code using '../../../' ..
This is an impotent thing...
Note :-
If you are using Web Essential ..Then you should import the files like this.Otherwise the .Less to .CSS conversion will not work..
#import "../../../content/bootstrap/variables.less";
#import "../../../content/bootstrap/mixins.less";
#import "../../../content/bootstrap/utilities.less";