I have two bootstrap containers, one inside header navbar of the page, the second below navbar. Inside each container there is some text. I want text that is in the navbar container to be shifted to the left by 15 px for all responsive breakpoints.
I tried to do this using a negative margin. Here is my code:
<nav class="navbar px-0 navbar-light fixed-top bg-primary">
<div class="container"><div style="margin-left: -15px;">Text1</div></div>
</nav>
<div class="container" style="margin-top: 50px;">Text 2</div>
and JSFiddle example.
This method works, but it has a serious bug. When the width of the browser decreases, text in header navbar starts to go beyond the screen. Is there any way to avoid this? Of course it is clear that I set up margin using #media, but I think this is not quite the right way.
If you use the right way of nesting elements in Bootstrap you will end up with an aligned left side: .row will give you -15px to the left to counter the 15px left padding on .col-xx-xx. What you could do is to add 15px padding to other containers and let the navbar keep the normal bootstrap container max-width... That will keep the default 15px padding on the navbar on mobile/tablet etc.
<nav class="navbar px-0 navbar-light fixed-top bg-primary">
<div class="container">
<div class="row">
<div class="col-sm-12">Text</div>
</div>
</div>
</nav>
<div class="container">
<div class="row">
<div class="col-sm-12">
<div class="container-inner">Text</div>
</div>
</div>
</div>
Codepen Example
You can use navbar-fixed-top instead of fixed-top for navbar section. In Bootstrap 4 adjust margin-left by using default css class ml-1,ml-2,.. and padding pl-1,pl-2 as follows.
Ref Bootstrap 4 spacing
<nav class="navbar navbar-default navbar-fixed-top bg-primary">
<div class="container">
Text 1
</div>
</nav>
<div class="container">
<div class="ml-4 mr-4">
<p>Text 2</p>
</div>
</div>
Try your ans here: https://jsfiddle.net/testdesigner/aq9Laaew/193572/
Related
I recently published a site and I realize that the size of each page of this site is always wider than the size of the screen (Even if my screen has a resolution of 3840 * 2160!) .
I use Boostrap 4 and in the _Layout view, I have by default the meta tag which allows to adapt the size of the page to the used device:
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
I did not touch the bootstrap.css file (at least nothing who's related to the size).
I noticed that if in the _Layout view I remove all which form the top of my page (my menu), the width of the page is correct (but it is obviously not a solution).
So I have a removed one div class each per each to see which one provided the problem but no way, each time the width is always greater than the page.
Do you have an idea?
<div class="bs-docs-section clearfix">
<div class="row m-0">
<div class="col-lg-12">
<div class="bs-component">
<nav class="navbar navbar-expand-lg navbar-light" style="background-color:#C6DA4B">
<div class="container p-0">
<div class="navbar-header">
#Html.ActionLink("Home", "Index", "Home", null, new { #class = "navbar-brand" })
</div>
<div class="collapse navbar-collapse" id="navbar">
//code
</div>
<partial name="_LoginPartial" />
</div>
</nav>
</div>
</div>
</div>
</div>
EDIT
With row m-0, it show like that
give margin: 0; first to row class or m-0, so it not be wider or remove horizontal bar from your page.
for full width add class padding: 0; or p-0 in your container class
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<div class="bs-docs-section">
<div class="row m-0">
<div class="col-lg-12 p-0">
<div class="bs-component">
<nav class="navbar navbar-expand-lg navbar-light" style="background-color:#C6DA4B">
<div class="container p-0">
<div class="navbar-header">
#Html.ActionLink("Home", "Index", "Home", null, new { #class = "navbar-brand" })
</div>
<div class="collapse navbar-collapse" id="navbar">
//code
</div>
<partial name="_LoginPartial">
</div>
</nav>
</div>
</div>
</div>
</div>
I don't know much about bootstrap, but it seems like you need another "container" div around your "row".
<div class="container">
<div class="row">
[etc]
</div>
</div>
Bootstrap row has a default left and right margin with -15px that's why the page shows horizontal scroll bar. To fix this we have a container wrapped to the row like .container or container-fluid (Depends according to the layout).
This question already has answers here:
Bootstrap 4 correct way to use row and col classes [duplicate]
(1 answer)
Bootstrap - do we have to use rows and columns?
(2 answers)
Closed 4 years ago.
I'm writing a page in html using Bootstrap 4.2. I would like to have a horizontal navigation bar at the top of the page, that occupies the whole width of the page, and remains static during navigation.
I tried to use a few divs with the class "container-fluid". The problem is that a margin appears on the left and right side of the bar
Here is an example of what I get in jsfiddle
<div class="container-fluid">
<div class="sticky-top shadow" id="containerMain">
<div class="container-fluid bg-secondary text-light p-1">
<div class="row align-items-center ">
Line number one content here
</div>
</div>
<div class="container-fluid filters bg-light align-items-center">
<div class="row p-1">
Line number two content here
</div>
</div>
</div>
</div>
Does anyone know how to make these unwanted margins disappear? I tried without the "container-fluid" class, but then the margin appears on the right side, with a horizontal scroll that I don't want either.
Thank you very much in advance,
container-fluid has padding-left and padding-right of 15px which is the gap you're seeing. You could overwrite it by adding the class px-0 which is padding of 0 for left and right. And you would then have to overwrite the 15px margins of the row with a mx-0 class.
But if it's a nav that you want, then what you should be using is the nav component of Bootstrap: https://getbootstrap.com/docs/4.1/components/navbar/
Not sure if this is what you're looking for:
Here's the jFiddle
<nav class="fixed-top navbar-light bg-light m-0 p-0">
<div class="container-fluid">
<div class="row text-center text-light bg-secondary p-1">
<div class="col">
Line number 1 content here
</div>
</div>
<div class="row text-center text-secondary bg-light p-1">
<div class="col">
Line number 2 content here
</div>
</div>
</div>
</nav>
I think the bootstrap "row" class adds some margin (15px).
Maybe you can add an own class to make margin: 0. Or delete the row class
I wish that helped!
Thank you.
So I have seen a couple other posts indicating that you have to have a container with h-100 to align items with the align-self class however I am running into problems getting a navbar aligned at the bottom of a div. If I stick the navbar within another container and row it works however then the navbar is not the full width of the main container. Here is what I have:
<div class="container-fluid h-100 body-content">
<nav class="navbar navbar-light bg-light align-self-lg-end">
<a class="navbar-brand" href="#">Navbar</a>
</nav>
<div class="logo_banner">
</div>
<footer>
<p>Footer</p>
</footer>
</div>
Thanks, all help is appreciated.
Try using the class .align-middle.
https://getbootstrap.com/docs/4.0/utilities/vertical-align/
I would like to have some text, then a button, right aligned in a Bootstrap 3
navbar. I can add the corresponding components, but whatever I tried the result is ugly. I never managed to get it right so any help would be appreciated:
<div class="container-fluid">
<div class="row">
<div class="navbar navbar-inverse">
<div class="navbar-header">
<a class="navbar-brand" href="#">Brand</a>
</div>
<div class="nav pull-right">
<p class="navbar-text">text in navbar...</p>
<button class="btn btn-info">then a button</button>
</div>
</div>
</div>
</div>
The text looks ok but the button is stuck in the upper right corner.
Here is a link to bootply
Try adding the following CSS
.navbar .navbar-text{
float: none;
display: inline-block;
}
The default display property of .navbar-text is block, which is forcing it to be in a separate line.
How can I center my div inside a separate div with a class footer navbar-inverse navbar-fixed-bottom in bootstrap? Here's my exact code.
<div class="footer navbar-inverse navbar-fixed-bottom">
<div class="center-block">
<div class="small navbar-text text-center">
First Line<br />
Second Line<br />
Third Line
</div>
</div>
</div>
Image in Fullscreen:
But if I resize the width of my browser, the position is fine.
.navbar-text was floating to the left.
Fixed it for ya :)