Twitter Bootstrap Remove Margin of the Span - css

I am using Twitter Bootstrap. And i have used span8 and span 4 in a row. Is there anyway to remove the leading margin-left:20px from the first span of the row without needing to over ride it manually ?

That 20px margin you see on your #mainContent area is due to the setup of the bootstrap grid, which uses a container of 940px, it is supposed to be removed by the .row container with a margin-left:-20px property. In your setup, your content area is working just the way it was designed too, but your top pageHeader and mainNav sections are not appropriately inserted into the grid, you just have divs inside the .row top sections that are not being contained within the proper containers of the grid.
To fix this you can just insert all of your pageHeader and mainNav elements inside a .span12 container and everything should stack accordingly.
Fixed markup
<header class="row" id="pageHeader">
<div class="span12">
<div id="logo">Logo</div>
<div id="userDetails">userDetails</div>
</div>
</header>
<nav id="mainNav" class="row">
<div class="span12">
<ul>
<li>Home</li>
<li>Dashboard</li>
<li>Blog</li>
<li>Idea Exchange</li>
</ul>
</div>
</nav>
Also, quick tip, you can switch your mainNav background color to the proper grid container of .span12 simply by targeting it like so:
nav#mainNav .span12 {
background: url("../images/nav_bar_bg.png") repeat-x scroll 0 0 transparent;
height: 45px;
overflow: hidden;
}

you can add a class in your css with an !important:
example:
.no_margin{
margin:0px !important;
}
and add that class to your html when required.
(sorry for my bad english xD)

there is also small less utility at
http://getkickstrap.com/extras/#single-view
called flushspan

By using "row" or "row-fluid" class as parent of your span class
<div class="row">
<div class="span3">
<ul>
<li>Home</li>
<li>Dashboard</li>
<li>Blog</li>
<li>Idea Exchange</li>
</ul>
</div>
</div>

Related

Can I use container tag inside a container-fluid tag?

I am currently designing a website and need to know if I can use container tag inside a container-fluid tag? Is it a good design approach to use that.
You can do this if you want, though it is generally not necessary, as their primary purpose is to be an external wrapper. The main thing to be aware of is the 15px padding on either side of a Bootstrap container, as well as their varying max-width media queries.
When nested, a child .container will not fill the width of the screen at any point.
Bootstrap Containers overview.
Bootstrap.css Source Code. Open this, ctrl + f and search .container, .container-fluid, and the sm, md, lg, and xl container sizes to see how they are styled.
Open the following code snippet example in Full Page mode and re-size to see their effects within .container-fluid, without .container-fluid, and with several .container divs nested within eachother.
.container-fluid {
background:pink
}
.container, .container-sm, .container-md, .container-lg, .container-xl {
border: 1px solid black;
background: lightblue;
}
.not-a-bootstrap-container {
background: orange;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<div class="container-fluid">
<h2>
Within container-fluid
</h2>
<div class="container">
.container
</div>
<div class="container-sm">
.container-sm
</div>
<div class="container-md">
.container-md
</div>
<div class="container-lg">
.container-lg
</div>
<div class="container-xl">
.container-xl
</div>
</div>
<div class="not-a-bootstrap-container">
<h2>
Without container-fluid
</h2>
<div class="container">
.container
</div>
<div class="container-sm">
.container-sm
</div>
<div class="container-md">
.container-md
</div>
<div class="container-lg">
.container-lg
</div>
<div class="container-xl">
.container-xl
</div>
</div>
<div class="container-fluid">
<h2>
Multiple nested containers (within Container-Fluid)
</h2>
<div class="container">
<div class="container">
<div class="container">
Three .container divs nested within eachother in a .container-fluid
</div>
</div>
</div>
</div>
Yes, you can use the container tag inside of a container-fluid tag. It all depends on the needs. Let say you have a scenario where you want to make the navbar of full screen width and page content to be in a single centered container.
There is nothing like bad approach in this.

Decreasing the White Space Once the Media Query Changes

Im using bootstrap grid but Im with some doubts.
I want to have a header where i have a logo at left some links at right. And below this header I want to have a menu that occupies the same with of the content above.
I have this in this example: https://jsfiddle.net/v529b4mz/1/, and it is working. But there are 2 issues:
For example if the size of the screen is 1920px I have some white margins around content and if the screen is resized the space around decreases and there is some point that the media querie changes and when the media querie change there are again more white space around, but i Would like to not have too much space. So, I would like to have less space around when the media queries change so the content can occupy more space.
Do you know how to do this?
For example if I want to have this menu with background-color green in this example: https://jsfiddle.net/v529b4mz/1/ in all media queries except for the extra small media query where I want to hide this menu and show a mobile one. How to do that in a way that its not necessary decrease too much the font size or the padding of the list items, or both but instead utilize the white space around so the list items appear legible up to the small media query?
And, there are some margins between the list elements, do you know why?
Note: The header and section elements are just to give a full width background color to the bootstrap containers.
HTML:
<header style=background:yellow;>
<div class="container">
<div class="row">
<div class="col-2" style="background-color: orange">
Logo
</div>
<div class="col-10 d-flex justify-content-end" style="background-color: pink; ">
Links
</div>
</div>
</div>
</header>
<section style="background:pink;">
<div class="container">
<div class="row" style="background-color: green">
<div class="col-12">
<ul>
<li>Item1</li>
<li>Item2</li>
<li>Item3</li>
<li>Item4</li>
<li>Item5</li>
<li>Item6</li>
<li>Item7</li>
</ul>
</div>
</div>
</div>
</section>
CSS:
*{
box-sizing:border-box;
}
ul{
display: flex;
align-items: center;
list-style:none;
justify-content:space-between;
}
ul li{
padding:20px;
border:1px solid gray;
}
You're using .container which creates a fixed width grid between 2 given breakpoints.
You can replace it by .container-fluid which is... well fluid :)
Note that you'll have to set manually some padding on the section parent: can be a fixed padding in rem, em or px or proportional to the width of the viewport with vw unit (100vw = the width of the viewport) or a mix of them depending of the resolution.
Relevant documentation (Bootstrap 4.0 as seen in your fiddle)
Containers provide a means to center your site’s contents. Use .container for fixed width or .container-fluid for full width.
Updated Fiddle
<section style="background:pink;">
<div class="container-fuild">
<div class="row" style="background-color: green">
<div class="col-12">
<ul>
<li>Item1</li>
<li>Item2</li>
<li>Item3</li>
<li>Item4</li>
<li>Item5</li>
<li>Item6</li>
<li>Item7</li>
</ul>
</div>
</div>
</div>
</section>

How to put a div to the right of .container in Bootstrap?

Basically, I need to put a back-to-top button at the right side of the footer.
Something like this:
What I get is this:
You can see that there is a blank space between footer and the end of viewport, that space is the height the back-to-top button, if I remove the button the blank space is removed too.
I'm using bootstrap so my html code is similar to:
<footer class="container-fluid">
<div class="container">
<div class="content1>CONTENT 1</div>
<div class="content2>CONTENT 2</div>
</div>
<div class="back-to-top>TOP</div>
</footer>
You can see an example in Bootply. You can see that the footer has to be 20px height (min-height: 20px) but instead it is 40px.
I think that my problem will be solved if I can put the .back-to-top div beside the .container div.
How can I get this?
You can use helper class pull-right and move TOP link before container:
<footer class="container-fluid">
<div class="back-to-top pull-right">TOP</div>
<div class="container">
<div class="content1>CONTENT 1</div>
<div class="content2>CONTENT 2</div>
</div>
</footer>
You need to remove your CSS bloc:
.back-to-top {
float: right;
position: relative;
top: -20px;
}
Doc: http://getbootstrap.com/css/#helper-classes-floats
Having a min-height proxy doesn't mean you footer is going to be 20px. That just mean its height won't be smaller than that. If you want your height to be 20px, use height property. If for some reason you want it to be variable, you can look to the max-height property.
For your "back-to-top" button, here is my suggestion :
http://jsfiddle.net/Bladepianist/38ne021p/
HTML
<footer class="container-fluid navbar-inverse">
<div class="row">
<div class="col-xs-6">CONTENT 1</div>
<div class="col-xs-5">CONTENT 2</div>
<div class="col-xs-1 text-right" id="back-to-top">TOP</div>
</div>
</footer>
CSS
.container-fluid {
color: white;
}
Basically, I change your "back-tot-top" class to an ID in my model but you're free to adapt it to your liking.
Using the col-system and the text-positions classes, you can achieve the same rendering as you show in your question. That way, the back-to-top button is part of the footer.
Hope that's helping ;).

How do i make sure that a div reserves the remaining horizontal space?

I am trying to create my own website from scratch, now i ran into a problem considering the HTML/CSS bit.
I am trying to create this standard "header, navigation, content" layout where the header is at the top, the navigation is at the left and the content starts below the header and to the right of the navigation
I use the following piece of code:
<div id="head">
<img src="..." id="logo" style="float:left">
</div>
<div id="nav">
{some elements}
</div>
<div id="content">
{some elements}
</div>
But as soon as the "style='float:left'" is added to my code, the "content" and "nav" DIV automatically moves to the right of the "head" DIV, is it possible somehow to make the "head" DIV "reserve" the remaining space, so that the "content" and "nav" DIV wont move up to the right of it, but stay below?
This problem is called "Collapsed Parent". To prevent this you must clear float.
Add this CSS to your style sheet:
.clearfix:after {
content: ".";
display: block;
height: 0;
clear: both;
visibility: hidden;
}
And add the class "clearfix" to your header, like
<div id="head" class="clearfix">
<img src="..." id="logo" style="float:left">
</div>
For more information on clearing floats you can check out this url - http://www.tutorialrepublic.com/css-tutorial/css-alignment.php
<div style="width:250px;">
<div id="head" style="width:100%; border:1px solid red;">
<img src="..." id="logo"> Header-Logo
</div>
<div id="nav" style="float:left;width:47%;border:1px solid blue;">
Navigation-Left
</div>
<div id="content" style="float:right; width:50%;border:1px solid blue;">
Content-Right
</div>
</div>
-I think the reason is "float:left" inside tag id="head". Because you want to put the header in the top of website so no need to use "float:left" for it.
-You can use "float:left" for tag id="nav" and "float:right" for tag id="content"
Here is the result: http://jsfiddle.net/4JgA4/119/
=> No need to notice to all my information inside some tags (Just for decorations :D)
Clearing the floats will do.
You need to add a class to your div and in your css as below:
.clear
{
clear:both;
}
Add overflow:hidden to the #head element. I don't know why it works, but it ensures that all floated elements are contained inside the parent.

How to center a div with Bootstrap2?

http://twitter.github.com/bootstrap/scaffolding.html
I tried like all combinations:
<div class="row">
<div class="span7 offset5"> box </div>
</div>
or
<div class="container">
<div class="row">
<div class="span7 offset5"> box </div>
</div>
</div>
changed span and offset numbers...
But I cant get a simple box perfectly centered on a page :(
I just want a 6-column-wide box centered...
edit:
did it with
<div class="container">
<div class="row" id="login-container">
<div class="span8 offset2">
box
</div>
</div>
</div>
But the box is too wide, is there any way I can do it with span7 ?
span7 offset2 gives extra padding to the left span7 offset3 extra padding to the right...
Bootstrap's spans are floated to the left. All it takes to center them is override this behavior. I do this by adding this to my stylesheet:
.center {
float: none;
margin-left: auto;
margin-right: auto;
}
If you have this class defined, just add it to the span and you're good to go.
<div class="span7 center"> box </div>
Note that this custom center class must be defined after the bootstrap css. You could use !important but that isn't recommended.
besides shrinking the div itself to the size you want, by reducing span size like so... class="span6 offset3", class="span4 offset4", etc... something as simple as style="text-align: center" on the div could have the effect you're looking for
you can't use span7 with any set offset and get the span centered on the page (Because total spans = 12)
Bootstrap3 has the .center-block class that you can use. It is defined as
.center-block {
display: block;
margin-left: auto;
margin-right: auto;
}
Documentation here.
If you want to go full-bootstrap (and not the auto left/right way) you need a pattern that will fit within 12 columns e.g. 2 blanks, 8 content, 2 blanks. That's what this setup will do.
It only covers the -md- variants, I tend to snap it to full size for small by adding col-xs-12
<div class="container">
<div class="col-md-8 col-md-offset-2">
box
</div>
</div>
Sounds like you just wanted to center align a single container.
The bootstrap framework might be overcomplicating that one example, you could have just had a standalone div with your own styling, something like:
<div class="login-container">
<!-- Your Login Form -->
</div>
and style:
.login-container {
margin: 0 auto;
width: 400px; /* Whatever exact width you are looking for (not bound by preset bootstrap widths) */
}
That should work fine if you are nested somewhere within a bootstrap .container div.
add the class centercontents
/** Center the contents of the element **/
.centercontents {
text-align: center !important;
}
#ZuhaibAli code kind of work for me but I changed it a little bit:
I created a new class in css
.center {
float: none;
margin-left: auto;
margin-right: auto;
}
then the div become
<div class="center col-md-6"></div>
I added col-md-6 for the width of the div itself which in this situation meant the div is half the size, there are 1 -12 col md in bootstrap.
Follow this guidance https://getbootstrap.com/docs/3.3/css/
Use .center-block
.center-block {
display: block;
margin-left: auto;
margin-right: auto;
}
wrap the div in a parent div with class row then add style margin:0 auto; to the div
<div class="row">
<div style="margin: 0 auto;">center</div>
</div>

Resources