Having trouble with aligning HTML elements with Bootstrap v4 framework

In the header, I have a container for the logo and another one for the login form. I want the logo to be on the left and the login form to be positioned at the top right corner. However, when I use float-left for the logo container and float-right for the login form, they don't align properly. I've made sure that the containers are large enough to fit in, but they still do not align as expected.

Below is the code snippet:

<header class="bg-inverse text-white col-sm-12" style="border: 1px solid blue">

  <div id="logo" class="col-sm-8" style="border: 1px solid red">
    <h1 class="font-weight-bold">Somelogo</h1>
  </div>

  <div id="login-form" class="col-sm-3">
    <form class="" style="border: 1px solid blue">
      <div class="form-group mb-0">
        <label for="username">Username</label>
        <input type="text" class="form-control form-control-sm" id="username">
      </div>

      <div class="form-group mb-1">
        <label class="pt-1 mb-0" for="Password" style="border: 1px solid blue">Password</label>
        <input type="password" class="form-control form-control-sm" id="password">
      </div>

      <button type="submit" class="btn btn-primary col-sm-5 btn-sm">register</button>
      <button type="submit" class="btn btn-primary col-sm-5 btn-sm">login</button>

    </form>
  </div>

</header>

This is how it looks without setting a position Picture 1

When I set float:left to the div with ID logo and float:right to the div with ID login-form, it looks like this Picture 2

And when only setting the div with ID login-form to float-right, it appears like this Picture 3

PS: I'm attempting to use only Bootstrap 4. Please explain why this alignment issue is happening.

Answer №1

One common mistake beginners make is not using the Bootstrap grid correctly.

The key with Bootstrap rows and columns is that they are meant to work in conjunction with each other.

Remember: You cannot nest a Bootstrap column directly inside another Bootstrap column. This will cause issues, as seen here.

Here's the solution:

When nesting content inside a Bootstrap column, always start by placing a div (or another appropriate HTML element) with the class .row inside that column. Then, place a div with the class .col within that row div.

Incorrect way:

<div class="container">
    <div class="row">
        <div class="col">
            <div class="col"></div>
        </div>
    </div>
</div>

Correct way:

<div class="container">
    <div class="row">
        <div class="col">
            <div class="row">
                <div class="col"></div>
            </div>
        </div>
    </div>
</div>

Alternatively, this approach would also be suitable:

<div class="container">
    <div class="row">
        <div class="col">
        </div>
    </div>
    <div class="row">
        <div class="col">
        </div>
    </div>
</div>

I believe the following layout is what you intended to create:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZ... 
...
... omitted for brevity
...
...
</script>

Note:

In the code snippet above, I included the .container-fluid class and removed the .text-white class.

P.S. Here's an alternative version which I think might be more suitable depending on your requirements:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZ... 
...
... omitted for brevity
...
...
</script>

Answer №2

If you're looking for a solution, consider utilizing the flex utilities of Bootstrap 4:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet"/>
<header class="bg-dark text-white col-sm-12 d-flex justify-content-between align-items-center" style="border: 1px solid blue">
  <div id="logo" class="col-sm-8" style="border:1px solid red">
    <h1 class="font-weight-bold">Somelogo</h1>
  </div>
  <div id="login-form" class="col-sm-3">
    <form style="border: 1px solid blue">
      <div class="form-group mb-0">
        <label for="username">Username</label>
        <input type="text" class="form-control form-control-sm" id="username">
      </div>
      <div class="form-group mb-1">
        <label class="pt-1 mb-0" for="Password">Password</label>
        <input type="password" class="form-control form-control-sm" id="password">
      </div>
      <button type="submit" class="btn btn-primary col-sm-5 btn-sm">register</button>
      <button type="submit" class="btn btn-primary col-sm-5 btn-sm">login</button>
    </form>
  </div>
</header>

Alternatively, you can use the .container and .row classes for layout structuring:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet"/>
<header class="container bg-dark text-white col-sm-12" style="border: 1px solid blue">
  <div class="row">
    <div id="logo" class="col-sm-8 align-self-center mr-auto" style="border:1px solid red">
      <h1 class="font-weight-bold">Somelogo</h1>
    </div>
    <div id="login-form" class="col-sm-3">
      <form style="border: 1px solid blue">
        <div class="form-group mb-0">
          <label for="username">Username</label>
          <input type="text" class="form-control form-control-sm" id="username">
        </div>
        <div class="form-group mb-1">
          <label class="pt-1 mb-0" for="Password">Password</label>
          <input type="password" class="form-control form-control-sm" id="password">
        </div>
        <button type="submit" class="btn btn-primary col-sm-5 btn-sm">register</button>
        <button type="submit" class="btn btn-primary col-sm-5 btn-sm">login</button>
      </form>
    </div>
  </div>
</header>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to customize my visual studio code to automatically insert an indented space between curly brackets in CSS when I press enter?

Whenever I try to open curly brackets in CSS and press enter, it simply moves to the next line without adding an indentation as shown below: body { } Despite going through the settings and experimenting with different options, I haven't been able to ...

Selenium cannot find iFrames

I've come across various suggestions about transitioning to iframes such as driver.switchTo().frame("test frame"); and driver.switchTo().defaultContent(); Yet, I'm having trouble navigating to MenuFrame 4 in the following code. I am utilizing Ja ...

Getting the full referrer URL can be achieved by using various methods depending

I am currently working with ASP.Net. My goal is to retrieve the complete referrer URL when visitors arrive at my website from: https://www.google.co.il/webhp?sourceid=chrome-instant&rlz=1C1CHEU_iwIL457IL457&ion=1&espv=2&ie=UTF-8#q=%D7%90 ...

How to dynamically alter a PHP variable using a JavaScript button click on the existing webpage

I've spent the last hour scouring stackoverflow for answers to my problem, but trying out solutions has only resulted in errors - perhaps because I'm attempting to implement it on the same page. I've experimented with saving the value to a ...

Struggling to achieve the desired vertical text alignment in a React Native layout

I am in need of assistance with a follow-up question related to my issue. I have a specific layout that I would like to use as a component, but I am struggling to make it more square-shaped using CSS in React Native. Can someone provide guidance on how to ...

What is the best way to apply CSS max-left or min-left positioning to an absolute element?

I currently have an element with the CSS properties: position:absolute; left: 70%; Is there a way to restrict this element from moving more than 900px from the left side? Similar to max-width but for positioning? ...

Is it necessary for all elements of a web application to exist as React components?

It's been more than 2 years since I last worked with React, and revisiting my old code has left me feeling a bit confused. If I were to create an Instagram clone, would it be best to use HTML templates and inject JavaScript like this: <div id="rea ...

Perfectly aligning css tabs both vertically and horizontally with dead centering technique

Can you help me figure this out? Currently, the tabs in the HTML and CSS provided below are positioned close to the top of the screen. How can I adjust the markup so that the tabs are dead-centered (both vertically and horizontally) instead? Check out th ...

Creating a link with a POST method and without using a new line

I am attempting to generate a square matrix and send data using the post method. I have discovered a solution involving JavaScript and form submission, but each time a new form is created, it results in a new line being added. Alternatively, if I move th ...

Using preventDefault and stopPropagation will prevent altering the CSS properties of a div

I recently made the switch from using onclick on a div to touchstart in order to increase the speed of clicking on a touch screen. However, I have encountered an issue where the CSS property that changes the background color is no longer functioning. Below ...

Adding an attribute to the last item of an Angular / NGX Bootstrap component

I am working with a dynamic list that utilizes NGX Bootstrap Dropdowns to showcase 4 sets of dropdown lists. However, the Dropdowns in the final list are getting obscured off-page. Thankfully, NGX Bootstrap offers a solution in the form of a dropUp option ...

Why does the styling of the inner Span adhere to the style of the outer Span?

How can I adjust the opacity of the color "blue" to 1 in the code snippet below? <!DOCTYPE html> <html> <body> <p>My mom's eyes are <span style="color:blue;font-weight:bold;opacity:0"> <span style="color:blue;fo ...

What is the best way to horizontally align a group of list elements while also displaying them side by side?

I've been working on creating a Sign Up page and here is the code I have so far: <div class="form"> <ul class="tab-group"> <li class="tabsignup"><a href="/signup.html">Sign Up</a ...

Ways to arrange Bootstrap 4 navigation bar links side by side and align them to the right

I am facing a challenge in designing a navigation bar where the brand logo is on the left, while the navigation links are placed on the right. Currently, the links appear stacked on top of each other instead of side by side: <link href="https://maxcd ...

Unable to display the content

Why isn't the text expanding when I click "see more"? Thanks XHTML: <div id="wrap"> <h1>Show/Hide Content</h1> <p> This code snippet demonstrates how to create a show/hide container with links, div eleme ...

Interacting with click events on SVG elements without a bounding box

Within my SVG (target) element, there are multiple SVG (descendent) elements. This SVG container is nested deep within the SVG (root) on the webpage. The issue I am encountering is that when I register an event on the SVG (target), it only triggers if yo ...

Customize dropdown item colors in React using a color picker

I am currently utilizing a react color picker to create a personalized 16 color gradient. The process involves selecting a color from a dropdown menu and then using the color picker to make adjustments. This action updates an array that dictates the stylin ...

Mastering the Art of Defining SVG Gradients in a Page-wide CSS File

On my HTML page, I have implemented a JavaScript code that dynamically generates SVG elements and inserts them into the HTML page. Currently, all the styling attributes such as fill colors are specified in the CSS of the entire webpage. This approach allo ...

Troubleshooting: Images not appearing on HTML pages

I've recently started learning HTML and can't figure out why my code isn't working. I am trying to add an image but it only appears as a blue box with a question mark in it. Here is my code: <head> <title>Welcome</title> ...

Organizing an Ordered List of Items into Alternating Columns Using HTML

I am in the process of developing a responsive HTML design to showcase an array of organized data. For smaller screens, the layout will consist of a single column displaying items sequentially. However, on larger screens, the design should adapt to featur ...