bootstrap css classes hierarchy - html

I would like to learn how to build my css file using bootstrap elements. It's pretty tricky to try to select a unique element when all have pretty much the same classes so beside using id's how can I select a unique element from a structure like this one?
<div class="row">
<div class="col-lg-12">
<div class="row">
<div class="col-md-2">CREATED</div>
<div class="col-md-2">MODIFIED</div>
<div class="col-md-2">USER STATUS</div>
<div class="col-md-2">STATUS</div>
<div class="col-md-2">MODEL</div>
</div>
<div class="row">
<div class="col-md-2">26/02/2018</div>
<div class="col-md-2">26/02/2018</div>
<div class="col-md-2">quote requested</div>
<div class="col-md-2">
<select class="form-control"></select>
<button class="btn btn-default">SAVE</button>
</div>
<div class="col-md-2">
<button class="btn btn-default">DOWNLOAD FILE</button>
</div>
<div class="col-md-2">
<button class="btn btn-default">SELECT</button>
</div>
</div>
</div>
</div>
Let's assume I want the DOWNLOAD FILE button to have this property: width:50% . How can I select that particular button without giving him an id?

Solution 1:
Use css selector instead of IDs.
Example:
.row .col-lg-12 .row:nth-child(2) .col-md-2:nth-child(5) button.btn.btn-default {
width: 50%;
background-color: red;
color: #ffffff;
}
More: W3School.
Here is the solution(1) of your question on Codepen.
Solution 2:
Use a custom css class for the button(s).
Example:
.btn-custom {
width: 50%;
background-color: red;
color: #ffffff;
}
Here is the solution(2) of your question on Codepen.
Hope this helps.

Related

How to center text on button in different screens in bootstrap4 or css

I have a problem with button text. when I resize, its text remains no more in center on some screen sizes as in these images.
In iPad, it is good
In iPhone, it is ugly, not centered position
Html
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<div #fade class="img-wrapper">
<img [src]="imgPath" class="img-fluid">
<div class="img-overlay">
<button routerLink="/test"
class="btn btn-success fill-container testButton">
TEST
</button>
</div>
</div>
</div>
</div>
CSS
.img-wrapper {
position: relative;
}
.img-overlay {
position: absolute;
bottom: 31%;
left: 13.5%;
width: 34%;
height: 4%;
background-color: red;
}
.testButton {
font-size: 3vw;
line-height: 20px;
}
.fill-container {
width: 100%;
height: 100%;
}
Please let me know what should I make right to do it right.
Regards
The solution is simple: If you don't try to prevent the native Bootstrap 4 classes from working by applying custom css that breaks them, then adding the text-center class is all you need.
Here's a code snippet that shows this class perfectly doing its job:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<div #fade class="img-wrapper">
<img [src]="imgPath" class="img-fluid">
<div class="img-overlay text-center">
<button routerLink="/test"
class="btn btn-success fill-container testButton">
TEST
</button>
</div>
</div>
</div>
</div>
</div>
However, if you start adding css hacks that break Bootstrap, then, of course, you'll need even more css hacks to fix the problems caused by the original css hacks.
In this case, I recommend going back to square one, removing all of your custom css and using native Bootstrap 4 classes to accomplish what you need because native Bootstrap 4 classes can do almost everything you'll ever need in terms of layout. No need for any css hacks.

CSS Nth-Child 3n hierarchy structure

With how my HTML is structure I am having hard time selecting 3n child. It doesn't seem even notice the 3n selector of class heroLetter, but if I use the 1n child selector the code notices the class, but it also selects every div. I am not sure how to call the 3n child selector with this structure of classes I have made.
Code:
.heroLetter {
float: left;
width: 48%;
margin-top: 150px;
text-align: center;
font-size: 600px;
color: #f5543a;
position: relative;
}
.windowWrapper .section .heroLetter:nth-child(3n) {
float: left;
width: 48%;
margin-top: 150px;
text-align: center;
font-size: 200px;
color: #f5543a;
position: relative;
}
<div class="wrapper">
<div id="section1" class="windowWrapper">
<div class="section group">
<h1 class="introH1">
<span class="Grand">GRAND</span>
<span class="Stand">STAND</span>
</h1>
<p class="introP">A new font.</p>
scroll down
</div>
</div>
<div id="section2" class="windowWrapper">
<div class="section group">
<div class="col span_6_of_12">
<h1>STORY</h1>
<p>Grandstand invokes</p>
</div>
<div class="heroLetter">
G
</div>
</div>
</div>
<div id="section3" class="windowWrapper">
<div class="section group">
<div class="col span_6_of_12">
<h1>PROCESS</h1>
<p>Grandstand invokes</p>
</div>
<div class="heroLetter">
S
</div>
</div>
</div>
<div id="section4" class="windowWrapper form">
<div class="section group">
<div class="col span_6_of_12">
<h1>BEAM</h1>
<p>Grandstand invokes</p>
</div>
<div class="heroLetter">
<div class="circle"></div>
a
</div>
</div>
</div>
</div>
</div>
The nth-child refers to children of the same parent only.
You could adjust your code to refer to the outer most common element.
Some css like this should help you:
.windowWrapper:nth-child(4n) .section .heroLetter {}
In this case it's the 4th windowWrapper since it contains your 3rd heroLetter

How to set the title in the middle of the page?

I have a title with a button on the right, but I met two problem:
I want the title in the centre of the page;
I want the button and title are in the same horizontal line.
This is my code:
<div class="container">
<div class="row">
<div class="btn btn-primary pull-right">This is a long button</div>
<h1 class="text-center">This is a long title</h1>
</div>
</div>
This is my demo.
Please help me, thanks.
Use this code to attain the desire result:
HTML:
<div class="container">
<div class="row">
<div class="col-xs-2"></div>
<div>
<h1 class="col-xs-8 text-center">This is a long title</h1>
</div>
<div class="btn btn-primary col-xs-2 pull-right mar">Follow</div>
</div>
</div>
CSS:
.mar{
margin-top: 20px;
}
you can use the tags < center > < /center >
example:
<center>long title</center>
Just add this data in a new CSS file or in an existing CSS file.
#bt {
width: 100%;
}
.text-center {
width: 100%;
}
Also add an ID to the button as "bt".
Just zero the margin-top on the h1
.text-center {
margin-top: 0;
}
At least in your code-pen that did the trick.
In the future, in situations like this, always use the inspector on your browser to see the box-model of the element, helps a lot.
If you need more info on the box-model: http://www.w3schools.com/css/css_boxmodel.asp
Make Your Div Width as 100% and make use of Text-align Property.
<h2 style="text-align: center">XYZ</h2>
Then adjust your buttton with margin-top property.
Like this:-
<div class="container">
<div class="row">
<div class="col-xs-2"></div>
<div>
<h1 class="col-xs-8 text-center" **style="margin-top: 0"**>This is a long title</h1>
</div>
<div class="btn btn-primary col-xs-2 pull-right">Follow</div>
</div>
</div>

Two divs, each within two divs - how can I change the stacking order using CSS (for responsive)?

Preface: I cannot change the HTML.
To better explain my question, I have provided an illustration below. Essentially, I have two rows of divs - the first row has content, and the second row would have a button beneath the content.
I want to make the page responsive, so that the div with a button always is below its corresponding div with content, but the extra container divs are proving a challenge. Any ideas?
What I have (above) and what I want (below).
Here's the HTML code:
<div class="choice-section">
<div id="choice_1" class="choice odd" style="margin-top: 242px;">
<div class="content">asdf</div>
</div>
<div id="choice_2" class="choice even" style="margin-top: 322px;">
<div class="content">asdf</div>
</div>
</div>
<div>
<div class="vote-choice odd">
<a class="vote btn" href="javascript:void(null)" choice="1">Vote</a>
</div>
<div class="vote-choice even">
<a class="vote btn" href="javascript:void(null)" choice="2">Vote</a>
</div>
</div>
I would strongly reccomend looking at Bootstrap's grid and column system with which this can easily be achieved. This framework will make responsive design a breeze for you.
You can see an example of something pretty similar to what you are trying to achieve in a Plunker I very quickly put together
CSS:
.blue {
background-color:blue;
height:200px;
}
.red {
background-color:red;
height:200px;
}
.purple {
background-color:purple;
height:200px;
}
.green {
background-color:green;
height:200px;
} here
HTML:
<div class="container">
<div class="row">
<div class="col-sm-3">
<div class="red">
</div>
</div>
<div class="col-sm-3">
<div class="blue">
</div>
</div>
<div class="col-sm-3">
<div class="purple">
</div>
</div>
<div class="col-sm-3">
<div class="green">
</div>
</div>
</div>

Expanding the classes - CSS/HTML convention

I have the simple table generated by js and I'd like to change some properties of columns and rows like background color, width statically. I can extend the row and col classes directly in CSS file or do it in html.
Example (CSS approach):
HTML:
<div class="table">
<div class="row">
<div class="col">
<div class="row">
<div class="foo"></div>
<div class="col"></div>
</div>
</div>
</div>
</div>
CSS:
...
.col, .foo {
display: table-row;
height: 20px; }
.foo { background: red; width: 100px; }
...
Example (html approach):
HTML:
<div class="table">
<div class="row">
<div class="col">
<div class="row">
<div class="col foo"></div>
<div class="col"></div>
</div>
</div>
</div>
</div>
CSS:
...
.col {
display: table-row;
height: 20px; }
.foo { background: red; width: 100px; }
...
In my opinion the second approach is more convenient for dynamically changing elements rather then static ones. However the first one obscures the structure of the html which can cause some problems with understanding the javascript. My question is which approach would be better in this case?