Bootstrap 3 grid custom class - html

I'm using Bootstrap 3 to design a website, and I would like to ask how could I apply a custom class to an existing col-md-12 for example, so I won't use ids (#cusom-name) ?
should I write my css like
.col-md-12 test {
background: blue;
padding-left: 10px;
}
and my html like:
<div class="col-md-12 test">div content</div>
or should I just stick to something like using a standard col-md-12 and inside it use my custom class with a new div? like
<div class="col-md-12">
<div class="test">
test content
</div>
</div>
I hope it's clear enough... thanks!

In your HTML, load your custom stylesheet file AFTER you load the Bootstrap 3 stylesheet file. Don't ever edit Bootstrap's files, as updating will become difficult.
Then, in your stylesheet just define the class as normal, on its own.
.blue-bg {
background: blue;
padding-left: 10px;
}
Then, as you already have in your HTML, us it like this;
<div class="col-md-12 blue-bg">
div content
</div>
What this will do is apply all the styles from both .col-md-12 (defined by Bootstrap) and .blue-bg (defined by you).
The reason we load your stylesheet last, is for conflicts. If both you and Bootstrap are defining a property. For example, Bootstrap sets the background to red, and then you set it to Blue. Whatever the last stylesheet says, will be obeyed.
In this way, you can define yet another class;
.red-bg {
background: red;
padding-left: 10px;
}
Then use them both as often as you like, however you want. Consider this.
<div class="row">
<div class="col-md-6 blue-bg">
Div with a blue background.
</div>
<div class="col-md-3 red-bg">
Div with a red background.
</div>
<div class="col-md-3 blue-bg">
Another div with a blue background.
</div>
</div>

Just write it like this in your own CSS:
.test {
background: blue;
padding-left: 10px;
}
And like this in the html:
<div class="col-md-12 test">
<p>test content</p>
</div>
Don't change bootstraps css because then it will be harder to update it later on, insteed work with a css you create which overwrites bootstraps rules.

It really depends on what you are styling really. As well as your code style. You could add another modify class to the .col-md-12 class, or nest another class inside that container. There isn't an always or never answer for modifiers. And by modifiers, I mean overrides on BS3's default/core classes.
Also, in your example code, you forgot the period before test. It should be like this if you're going to nest that class inside:
.col-md-12 .text {}
Because .col-md-12 is a grid component, I think it makes sense to nest a div.test inside that component to not muddy up the context of what that element does, or how it behaves. An example that could have unwanted effects would be if you added padding to all .col-md-12 in your app, instead of the one off use of padding. To add the padding in this case, you could nest .test inside of .col-md-12, and add padding to .test (instead of the grid element). In my opinion, you'd have a nice separation of code and it's use. Also, you might be able to use that newly created .test class in other places of your app.
There are a lot of ways to organize your CSS, and keep elements together based on purpose. If you're interested in some reading, you might check out this resource: http://smacss.com/ (among others).

In CSS, you can have properties that will be set for both classes only, but there shouldn't be a space between the class names, as you have. It should be:
.col-md-12.test {
background: lightblue;
padding-left: 10px;
}

you don't need to add additional div, u can have
<div class="col-md-12 test">div content</div>
and define new as well as u can also override bootstrap css for col-md-12 but if u apply directly on it , it will applicable to everwhere where u have used this bootstrap class. so its better to add your custom css on .test e.g
.test {
width: 80%;
padding: 2%;
}
and if your custom css is not overriding bootstraps css u can use !important e.g.
.test {
width: 80% !important;
padding: 2% !important;
}
Defining a rule with the !important 'attribute' discards the normal concerns as regards the 'later' rule overriding the 'earlier' ones.

Related

CSS Best practices in creating and applying classes

Given the following example:
<div class="resource-url-cont clearfix">
<div class="url-icon-action float-left">
<v-btn #click="toggleUrlConsumed(link)" icon x-small color="success">
<v-icon v-html="link.is_consumed ? 'mdi-checkbox-marked-outline' : 'mdi-checkbox-blank-outline'"></v-icon>
</v-btn>
</div>
<div class="url-link float-left"><a :href="link.url" target="_blank">{{ link.name }}</a></div>
<div class="url-icon-action float-left">
<v-btn #click="removeResearchUrlConfirm(link)" icon x-small color="error" class="float-right">
<v-icon>mdi-trash-can-outline</v-icon>
</v-btn>
</div>
</div>
...and the SASS snippet:
.resource-url-cont {
.url-icon-action {
width: 30px;
}
.url-link {
width: 255px;
white-space: break-spaces;
overflow: hidden;
}
}
What would implement best practices for managing the css classes? Given that the class float-left is applied to all 3 divs which contains only a single style: float: left; ...
Would it be better to completely omit the addition of the 2nd class within the html float-left, and apply the style to the existing classes like so...
.resource-url-cont {
.url-icon-action {
float: left;
width: 30px;
}
.url-link {
float: left;
width: 255px;
white-space: break-spaces;
overflow: hidden;
}
}
If I keep the float:left style encapsulated within the existing float-left class, it ultimately reduces the size of the overall compiled styles.
However there is a cost at developer readability while managing the source code, because to me it seems easier to read through the source when the styles for an element are constrained to a single use class.
This is obviously a simple example where it would not make much difference regardless of which technique you used however the differences become more evident when applied consistently throughout a large scale application.
What would be the best standard of practice to implement and why?
Oh man, this is a tricky one as there have been endless amount of articles written about this. Ex. https://css-tricks.com/tailwind-versus-bem/
I think the key thing is: there's no right answer, but just be consistent.
If you decide on keeping most of the styles in your CSS file, and you are worried about the file size, I suggest looking at https://purgecss.com. Ironically, it's created (sponsored?) by Tailwind, who promotes utility based styling. Using this tool, you can reduce your file size. Maybe not as small as using mostly utility classes, but it'll help.
I prefer BEM with some utility classes (per your float example). The hybrid approach has helped me a lot.

Making my Website responsive (in any phone,desktops)

I am new to HTML, CSS I am trying to create a website for my project however one of the requirements is it should be the responsive website.
for some reason, my yellow box becomes smaller and smaller unlike my red and blue box which are big when I clicked smartphone to view, laptop view and different another view it just the yellow box became small and thin. I wanted o to ask for help or advice to make it a responsive website thank you
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
<div class="col-md-4" style="margin-left: 3%" >
<div class="color">Column 1</div>
<div class="color" >Column 2</div>
</div>
<div class="col-md-8">Column 3</div>
</div>
<style type="text/css">
.color:nth-child(1) {
background:red;
height:40%;
margin-top:5%;
margin-left: 2%;
border-radius: 4%;
width: 100%;
}
.color:nth-child(2) {
margin-top: 3%;
border-radius: 4%;
background:blue; height:260px;
}
.col-md-8 {
background:yellow; height:628px;
width: 38%;
margin-left: 15%;
margin-top: 1%;
border-radius: 9%;
}
</style>
I dont know what kind of Website this is gonna be, but try "min-width" instead of the normal "width" in the yellow column
Both the red and blue are actually responsive but because they are set to 100% width it looks like they're not doing anything. Try setting their width to the same width as the yellow and you'll see they do work.
Short answer:
Everything works as expected, due to width:38% css property in col-md-8 override.
Long answer:
First option:
What you would like to do is to create own separate classes so you don't override bootstrap classes directly
Second option:
Bootstrap most probably has all the classes ready for your use-case so try not to write your own css at all and reuse utility classes from bootstrap, please see:
https://getbootstrap.com/docs/4.0/layout/grid/
https://getbootstrap.com/docs/4.1/layout/utilities-for-layout/
https://getbootstrap.com/docs/4.1/utilities/spacing/
Please remember about proper html document layout, for your convenience here's bootstrap starter template:
https://getbootstrap.com/docs/4.0/getting-started/introduction/#starter-template
And to place css in apropriate places within the document:
https://www.w3schools.com/css/css_howto.asp
I advise going through the HTML and CSS courses on the following sites:
https://www.freecodecamp.org/ and https://www.codecademy.com/catalog/language/html-css
They're both free so familiarise yourself with basic HTML and CSS before using bootstrap. These courses also explain how to make a page responsive in the way you want and you'll gain more of an understanding of what you're trying to do and how to do it.

Two CSS classes on one div not working

I have this css from bootstrap.min:
.rew {
margin-right: auto;
margin-left: auto;
margin-top: 20px;
width: 1050px;
}
.rew2 {
margin-right: auto;
margin-left: auto;
margin-top: 20px;
width: auto;
}
And my div like this (I've red examples from question and answer in stackoverflow):
<div class="rew rew2">
content.....
</div>
The (rew2) it's for responsived css, but before that I was wrote the css on my responsive css file, but it's not working the "div tag" always calls css from bootstrap.min css file. So I wrote two classes in the bootstrap.min css file, but not working also. The "div" tag only called the "rew" class and the "rew2" was ignored.
******** The class on responsive css file was deleted and I wrote the class on bootstrapmin css file
The differences it's only on width, if the site opened from desktop it would have 1050px width, and for the responsive (opened from smartphone) it will automatically adjust the template with the smartphone screen as "auto".
*Huft...I'm so confused why it's not working. I need help from you guys.
Thank you,
Best regards,
Kris
Why would you customize bootstraps .css file on your own? Just create your own rules and attach them to your div.
CSS stylings are always used one by one. So if you, for example, include your bootstrap.min.css file before your own styling rules, your own ones would overwrite all bootstrap stylings.
In other words:
First of all include bootstrap.min.css, then your own .css file.
Let's assume you've got this markup
<div class="foo bar"> </div>
You could style it through the 2 classes foo and bar.
.foo {
color: red;
}
.bar {
color: blue;
}
Using this would end up in the blue color, according to the declared order.
Let's even try to be a bit more specific.
You can also overwrite rules by using some more complex selectors.
.foo.bar {
color: black;
}
The above code would overwrite both of the previously defined rules, because they are 'stronger' selectors than a simple single-class selector.
Conclusion
If you want to overwrite bootstraps styling, try to stick to the order. If bootstrap uses some complex selectors and your custom ones won't trigger, try to use a bit more complex ones. Look here to learn more about complex selectors.
A little hint at the end:
Try to avoid !important! A rule, declared as !important, will overwrite all other rules, regardless of whatever you have declared up before.
Don't customize bootstrap.min.css create your own css file, In that you can write your own css as you need.As per you requirement include media query for smartphone in that give width: 100%; for that element.

How to add different CSS to the same HTML tags?

So I want to have a horizontal rule <hr> with a padding of 50px using CSS. However, I already have custom CSS assigned to the <hr> tag which already has padding-bottom:25px; padding-top:100px; padding-left:50px; padding-right:50px; Which is being used to divide the footer from the main content. So how would I achieve having two different CSS styles for the same element?
P.S. I am using Twitter Bootstrap 3
I actually found the answer to what I wanted after all this time.
Basically i did the following:
hr.custom {
margin: 10px;
etc...
}
Then I used:
<hr class="custom">
There are plenty of different ways to add different CSS to different HTML elements. Mainly, we use IDs and classes, read more about IDs and classes on the W3Schools website, I use it all the time. Here is an example of using both the ID and class system to style CSS:
The HTML:
<p class="coolclass">This is the cool class</p>
<p class="redclass">This is the red class</p>
<p id="strangeid">This is the strange ID</p>
The CSS:
.coolclass{
color: blue;
}
.redclass{
color: red;
}
#strangeid{
background-color: green;
color: white;
}
Here is a live example: http://jsfiddle.net/Xanco/8sex7x3p/
New live example with HR stylings: http://jsfiddle.net/Xanco/8sex7x3p/2
Let me make it simple then ..
give it an id and style it accordingly
See it here
Just for this element, you can add a new class to your <hr> like this:
<hr class="actualClass newClass">
with this CSS:
.newClass {
padding: 50px !important;
}

Change specificity by child

I'd like to integrate a theme tag to my elements so they appear in diffrent colours. But since the css selectors have the same css specificity the latest overrides the earlier defined rule.
this is an example that shows my problem:
<div class="red">
<div class="box">This should be red</div>
<div class="yellow">
...
<div class="box">This should be yellow (nested in x levels under the div.yellow)</div>
...
</div>
and here my css
.box { width: 100px; height: 100px; }
.yellow { background-color: yellow; }
.red { background-color: red; }
the box should be listed somewhere, but as soon as it is a sub child of another color definition it should been overwritten.
thanks for any help!
You shouldn't really be doing things this way -- if your theme changes, then suddenly things with class yellow may actually be blue, for example. I would suggest finding a common way of naming things (even if it's just colour1, colour2, colour-highlight...) and then specifying those styles. You can then look into the way your pages are designed and make the rules more specific as necessary (either by using !important or by making the rule more specific, e.g. .colour1 becoming .box .colour1 or div.colour1).
Try:
.box { background-color: inherit; }
See:
http://jsbin.com/imube/edit
I don’t quite see the problem. Here’s what I get with that code:
alt text http://www.pauldwaite.co.uk/images/so/1905834.png
You probably need to use CSS's !important keyword eg:
.yellow { background-color: yellow !important;}