How to align two divs in CSS when they are not always both present

Currently working on CSS styling for a store. I have a div that aligns the buy button to the left, and Prev and View Next images to the right.

The issue arises when the "buy" button is occasionally not present due to PHP functionality.

When the buy button is absent, the images need to be centered to avoid empty space on the left side where the buy button was positioned.

Initially, I considered using margin:0px auto, but realized it would require a constant width setting, correct?

What seemed like a simple task at first has become quite challenging.

Link to fiddle

Simplified version for clarity

I believe I might be overlooking something basic that I can't seem to pinpoint.

HTML:

<div id="comprarbtn">
    <div id="wrappcomprarbtn">
        <input class="comprarbtn commonButton" type="button" value="Buy Now" id="buynowlogin">
        <div id="naviminicc"> <a href="javascript:void(0);" class="prevImg"><img src="images/navmini_01.png" class="navmini1"></a>

            <img src="images/navmini_02.png" class="navmini2" rel="#mies1"> <a href="javascript:void(0);" class="nextImg"> <img src="images/navmini_03.png" class="navmini3"></a>

        </div>
    </div>
</div>

CSS:

.comprarbtn { width:175px; 
  line-height:51px;  
  background-image:url(image.jpg); 
  border:0px; 
  font-size:12px; 
  padding-left:10px; 
  cursor:pointer; 
  overflow:hidden; 
  text-indent:0px; 
  z-index:10; 
}

#comprarbtn { 
  float:left; 
  position:absolute; 
  width:321px; 
  text-align:center;
  height:51px;  
  z-index:1000;  
  display:table-cell; 
  background-color:#f4f4f4;
}

#wrappcomprarbtn { margin:0px auto;}

#naviminicc { width:145px; float:right;}
#naviminicc a { margin:0px; padding:0px; }
.navmini1 {  cursor:pointer; margin:0px; }
.navmini2 {cursor:pointer; margin:0px; }
.navmini3 {cursor:pointer; margin:0px; }

#navmini { width:135px; max-width:135px;}

Answer №1

I'm not entirely certain about the CSS and HTML code you shared, but to achieve your desired outcome:

  • Set a fixed width for the wrapping div that can accommodate both the button and images
  • Apply margin: 0 auto to center it and use text-align: center
  • Ensure the inner contents have display: inline property

CSS:

.wrapper {
    width: 200px; /* Ensure it's wide enough for everything */
    text-align: center;
    margin: 0 auto;
}

.wrapper .buttons {
    display: inline;
}

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

What is the reason for initializing I with the length of the response?

I am attempting to generate a table using an AJAX JSON response, but the for loop sets i to the maximum value right away. $.ajax(settings).done(function (response) { console.log(response); var i = ""; td = document.createElement('td'); t ...

Ways to modify the input field value in my form based on the current page context

I am currently developing a website for a sports event organization company using Angular, HTML/CSS. The focus of this website is on the triathlon sport and it consists of several stages. On the home page, there are five image tags representing each stage ...

Can the navbar brand be centered in Bootstrap 4 while also aligning some text to the left of the brand?

Just dipping my toes into website coding and Bootstrap is a whole new territory for me. I'm struggling with centering the brand in the navbar while keeping the text aligned to the left of it. So far, every attempt results in both elements being center ...

Designating the selected button as "active"

Consider the following HTML snippet: <div class="wp-editor-tabs"><button type="button" id="bbp_topic_content-tmce" class="wp-switch-editor switch-tmce" data-wp-editor-id="bbp_topic_content">Visual</button> <button type="button" id="bb ...

The functionality of Bootstrap toggle ceases to operate properly following an AJAX content update

I am currently using AJAX load to fetch some content on my webpage. I am working with Bootstrap 3 and Bootstrap toggle. When the content is loaded, the Bootstrap 3 content functions properly (the panel-primary panel is clearly visible). However, the Bootst ...

Steer clear of using inline styling when designing with Mui V5

I firmly believe that separating styling from code enhances the clarity and cleanliness of the code. Personally, I have always viewed using inline styling (style={{}}) as a bad practice. In Mui V4, it was simple - I would create a styles file and import i ...

PHP updating data in a MySQL database

I've been using easyPHP and encountering issues with updating records in my database. Whenever I try to update, the HTML form displays <?php echo $btitle; ?> and <?php echo $bauthor; ?> instead of updating the data. However, it does print ...

Problem uploading images to server and database

I'm currently experiencing difficulties with photo uploads on my website. The issue arises at the $fileName = basename($_FILES["file"]["name"]); line where I receive the error message "Please select a file to upload". There seems to be no database in ...

Guidelines for choosing an Element Id within an attribute tag using jQuery

Check Output and -->question. . I am a beginner in utilizing Bootstrap and JQuery for programming. I am interested in learning how to identify the "id" of an input element that is nested within another element's attribute. In my code, I am using ...

Guide on retrieving a file through an HTTP request with restricted cross-origin access restrictions

Having some issues with downloading files from specific links, such as . My goal is to automate the download process for these redirected files. The challenge I'm facing is that trying to access the link directly through a web browser just gives me a ...

Error: Unable to locate the reference

Having trouble with my JavaScript code not recognizing the linked .js files I added. I initially linked them through CodePen, but manual references don't seem to be working. Attempted suggestions from this page Why does jQuery or a DOM method such as ...

Tips for changing the <title> in an AngularJS one-page application

I am working on a single-page AngularJS application. The index.html file is structured as follows: <html ng-app="myApp" ng-controller="MyCtrl as myctrl"> <head> <link rel="stylesheet" href="my-style-sheet.css"> <title>{{ ...

What is the best way to vertically align a container beneath a navbar without triggering the appearance of a scrollbar?

I'm in the process of developing an application using Bootstrap 4. The app features a navigation bar and a container located beneath it. Below you'll find the structure of the application: <body> <div id="root"> ...

Verify if there are DOM elements located within a DIV container, execute the functions associated with those elements sequentially

I am in the process of creating a game using HTML, CSS, and JavaScript. My focus right now is on manipulating DOM elements without relying on the canvas tag. The goal is to develop a pseudo graphical programming language, similar to the environment provide ...

The button on the Cookie Policy navigation window is no longer operational on the ASP.NET Core MVC website utilizing Bootstrap version 4.3.1

After integrating Bootstrap 4 into my ASP MVC Core web project and converting some HTML code to a Cookie Policy window, I noticed that the accept button is no longer functioning. Although the button is visible and clickable, it does not respond. In additi ...

How can I center two images using a hover effect in WordPress?

When the mouse hovers over an image, it changes to another image. Everything works smoothly except for one issue - the image is not centered. Below is the code I am currently using: HTML: <figure> <a> <img class="hover" src="na ...

How to make an image expand to fit the screen when clicked using HTML and CSS

Is there a way to make the images on this page grow to screen size when clicked, rather than extending past the boundaries of the screen? Currently, the images enlarge to twice their original size on click. How can I ensure that they expand responsively to ...

Tips for customizing the appearance of Angular Material select drop down overlay

In my project, there is an angular component named currency-selector with its dedicated css file defining the styling as shown below: .currency-select { position: absolute; top: 5px; right: 80px; z-index: 1000000; color: #DD642A; f ...

The external embedded CSS seems to be missing from index.html after the Docker build process

My Vue/Webpack application includes an embedded CSS link in my index.html file that references an external source. Here is an example snippet: <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" con ...

How can I link an HTML anchor to a calendar?

Is there a way to create a universal link that generates a calendar event using HTML? For example, we can create an email link with <a href="mailto:<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ff9e9b9b8d9a8c8cbf9a879e928f9 ...