How to align elements both in the center and off-centered within a Bootstrap 4 container

Using Bootstrap 4, I have a container-fluid where I placed a logo in the center.

<div class="container-fluid d-flex justify-content-center align-items-center">
  <img src="logo.png">
<div>

Now, I want to add an additional element acting as a button.

<a href="#" class="btn btn-secondary">button</a>

The button should be centered vertically and its horizontal position should be a fixed amount of pixels right from the logo.

https://i.sstatic.net/nqIof.png

How can I achieve this layout using Bootstrap?

Answer №1

This code snippet should solve the issue:

.logo-container a.btn {
  left: calc(100% + 50px);
  white-space: nowrap;
}

/* Additional CSS for reference */
.logo-container {
  min-height: 10rem;
  margin-top: 2rem;
  background-color: #f5f5f5;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container-fluid d-flex justify-content-center align-items-center logo-container">
  <div class="position-relative d-flex align-items-center">
    <img src="https://picsum.photos/150/50">
    <a href="#" class="btn btn-secondary position-absolute">How to buy</a>
  </div>
<div>

In summary, wrapping the logo with position:relative and applying position:absolute to the button will achieve the desired result. Using Bootstrap's flex utility helps center the button vertically.

The button's positioning is adjusted using left: calc(100% + 50px), where you can replace 50px with your preferred fixed pixel amount as mentioned.

A custom class .logo-container is added to the container for targeted styling without affecting other elements. Feel free to customize this class as needed.

If avoiding custom selectors is preferred, an alternative could be:

.container-fluid > .position-relative img + a.btn-secondary.position-absolute {
  left: calc(100% + 50px);
}

While longer, this selector is more specific. Alternatively, adding a unique ID to the button could also be used for targeting purposes.

Answer №2

You can utilize the bootstrap classes : offset-6 along with position-absolute for styling the button.

/* To demonstrate, showing a ruler */
div {
  padding:1em 0;
  background:linear-gradient(to right,transparent 25%, rgba(0,0,0,0.2) 25%,  rgba(0,0,0,0.2) 50%,  transparent 50%, transparent 75%, rgba(0,0,0,0.2) 75%);
 
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid d-flex justify-content-center align-items-center">
  <img src="https://dummyimage.com/180x50/123/cde" />
  <a href="#" class="btn btn-secondary offset-6 position-absolute">button</a>
</div>


Updated based on comment by @tao

If you'd like to position your button 40px from the logo, you can also use margin + position:absolute without additional markup:

a.btn {left:50%;margin-left:130px; /* 40px + half of the logo's width */}

/* For illustration purposes, displaying a ruler */

div {
  padding:1em 0;
  background:repeating-linear-gradient(to left, transparent 0 ,transparent 10px ,tomato 10px ,tomato 20px) center}
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid d-flex justify-content-center align-items-center">
  <img src="https://dummyimage.com/180x50/123/cde"  />
  <a href="#" class="btn btn-secondary  position-absolute">button</a>
</div>


In case the logo's width is unknown, additional markup is necessary for alignment adjustments

a.btn {
  margin-left: 40px;
}


/* For demo, visualizing a ruler */

.d-flex {
  padding: 1em 0;
  background: repeating-linear-gradient(to left, transparent 0, transparent 10px, tomato 10px, tomato 20px) center
}


}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<div class="container-fluid d-flex justify-content-center align-items-center">
  <div class="col p-0"></div>
  <img src="https://dummyimage.com/180x50/123/cde">
  <div class="col p-0"><a href="#" class="btn btn-secondary ">button</a></div>
</div>

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

How can I send a current variable through PHP?

I have a pressing deadline and I need some guidance before moving forward. Can someone please advise if what I'm attempting is feasible or if there's a quicker solution? I encountered some problems with an accordion menu, so as a temporary fix, ...

Issue with importing a file using JQuery causing the click event to not function properly

I have a file named navigation.html located in a directory called IMPORTS <!-- Navigation --> <div class="navbar-inner"> <div class="container"> <button type="button" class="btn btn-navbar" data-toggle="collapse" data-ta ...

Functionality that can be utilized repeatedly

I've been struggling to implement a feature for repeatable blocks in my web form. The issue I'm facing is that when I click the buttons, nothing happens even though they work fine when tested in the console. I've been stuck on this problem f ...

Display/Conceal the UL subelement when selected

I've been struggling to toggle the visibility of some ul checkboxes when their parent is clicked. Despite my best efforts, I just can't seem to make it work! Could someone lend a hand in getting this functionality working? Essentially, I need th ...

Using jQuery to alter an href link's attribute

I am currently attempting to modify the content of a href using jQuery. After researching various solutions on this platform, I came across one that I am trying to use for implementation: How to change the href for a hyperlink using jQuery My current app ...

A single list is utilized by multiple HTML5 selects

If I have 10 fields in a form, each requiring a select option from the year 1950 to 2017, can I create one list from that range and have each select reference it? Or do I need to make ten separate lists for each select? Edit: An example could be the birth ...

What is the method for swapping out the numerical ending of a value three times, increasing the ending, and then looping it with Text Pastry?

I am faced with the task of generating 210 unique checkboxes, each requiring its own distinct name, id, and label for value. These checkboxes are organized in tables, with each table containing 6 checkboxes. So far, I have manually configured 54 checkbox ...

Creating a uniform height for images in a Bootstrap 4 carousel across various sizes and screen dimensions

Here is an example of a carousel provided by w3schools: <div id="demo" class="carousel slide" data-ride="carousel"> <!-- Indicators --> <ul class="carousel-indicators"> <li data-target="#demo" data-slide-to="0" class="active" ...

Steps for adding products to your shopping cart without using a JavaScript framework:

As a newcomer to web development, I took on the challenge of creating an e-commerce website using only HTML, CSS, and vanilla JavaScript (without any frameworks). I've encountered a roadblock while trying to implement the "Add to Cart" functionality ...

Real-time Property Availability Widget

I need assistance with creating a website for a homeowner who wants to rent out their property. The client requires a feature that allows them to log in and easily mark individual days as available or unavailable for rental by choosing specific colors for ...

having trouble with changing the button's background color via toggle

I've been experimenting with toggling the background color of a button, similar to how changing the margin works. For some reason, the margin toggles correctly but the button's color doesn't. <script> let myBtn = document.querySele ...

Updating an Image Using JavaScript

Having trouble changing an image using JavaScript. Followed instructions to use document.getElementbyId("image").src = "image2.jpg", but it's not working. Script is placed at the bottom after the image tag in the HTML. HTML code: ...

Having trouble with Simplehtmldom's innertext function?

While working with simple_html_dom: I encountered the following code snippet: $j = ' <itemBody> <div>films - to watch (Simple Present)<br/> <textEntryInteraction responseIdentifier="RESPONSE_1"/> ...

Sketch a straight path starting from the coordinates x,y at a specified angle and length

Is there a way to draw a line in Javascript starting from a specific x/y position with a given length and angle, without having to define two separate points? I have the x/y origin, angle, and length available. The line should be placed on top of a regula ...

Incorporate user input into Alert Dialog Boxes

Would you be able to assist me in displaying the input value from the "email" field in my alert box? The code seems to be working fine, but I'm having trouble getting the alert box to show the email form value. I decided to use Bootstrap for som ...

Is it possible for CSS hover to have an impact on multiple divs that share the same class name

There are 5 divs on the page, all with the same class name as shown below: Here is the CSS: .test:hover{ color:red; } And here is the HTML: <div class="test"></div> <div class="test"></div> <div class="test"></div> ...

What is the best way to place this dropdown in a fixed position within a parent element with overflow scrolling

I am facing an issue with a dropdown menu inside a parent div. The parent div has a fixed height and is set to overflow: auto. My goal is to have the menu extend beyond the boundaries of the parent when opened, but currently it just increases the height of ...

Activate Google Map marker through an external click

I need help with implementing anchor buttons for different locations that highlight the location details on click. For example, when clicking on "location1" button, I want to highlight location1 on Google Maps. Check out the demo on JSFiddle google.maps.e ...

Is it better to apply the font-family to the body or to the html element?

Is it more appropriate to specify the font-family CSS property on the html element or the body element? html { font-family: sans-serif; } or body { font-family: sans-serif; } I've looked into this issue online, but there doesn't seem to ...

Using PHP to include a class in an input element

Recently, I've been working on a form that involves PHP form validation. In my CSS file, there's a class designated for invalid inputs that I'm trying to add to each one. To see an example, feel free to check out the JSFiddle here. The ".inv ...