Achieving vertical alignment of an unordered list and button within a fixed-width div

I have a 300px wide div that includes an unordered list of icons and a button that need to be vertically centered.

What would the HTML/CSS look like to achieve the following:

A) Float the unordered list to the left and center it vertically B) Place the button to the right of the unordered list, also vertically centered?

HTML:

 <div class="hb-left">
 <ul>
 <li><img src="cat.jpg"></li>
 <li><img src="dog.jpg"></li>
<li><img src="mouse.jpg"></li>
</ul>
<button>Demo</button>
</div>

CSS:

.hb-left {
 width: 300px;
height: 50px;
}

.hb-left Ul {
float: left;
display: inline-block;
list-style-type: none;
margin: 0 auto;
}

.hb-left button {
height: 40px;
float: left;
display: inline-block;
margin: 0 auto;
}

Answer №1

If your button and ul have been styled with display: inline-block, you can easily align them vertically using the CSS property vertical-align.

.someClass {
     display: inline-block;
     vertical-align: middle;
}

To learn more about the usage of vertical-align, check out this link: https://developer.mozilla.org/en-US/docs/Web/CSS/vertical-align

The vertical-align CSS property specifies how to vertically align an inline or table-cell box.

When elements are set to inline-block, you generally won't need to use float as they should naturally fit next to each other as long as the parent container is wide enough. It's also recommended to give a fixed width to your inline elements so they can properly fit inside a 300px parent container.

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

Bootstrap allows for word wrapping on either one or three lines

I am currently utilizing Bootstrap 3 for my website, and I have a sentence composed of three parts at a certain point on the page. For example, it might read "Bud Spencer walks with Terence Hill." where X="Bud Spencer", Y="walks with", Z="Terence Hill." T ...

Implementing Content-Security-Policy with React and Material-UI v4 for secure styling requirements

Utilizing a React UI with material-ui version 4, the web server is embedded within a JVM application during Production. Following npm build for the UI, the bundle is deployed alongside the server side code. An essential requirement involves implementing C ...

JavaScript radio button returning value as undefined issueThe problem with radio buttons

http://jsfiddle.net/jngpjbjm/ Take a look at the provided fiddle link. I'm having an issue where the radio button value is showing up as undefined. I can't figure out why this is happening. Any assistance would be greatly appreciated. <input ...

What is the best way to format an image within an <a> element?

Below is a code snippet that includes a tags, with one containing an image. <div class="container"> <a href="#">Not styled</a> <a href="#"><img src="image.png"></a> </div> If I specifically want to style ...

Get rid of the upgrade button feature from TinyMCE editor

Recently, I started using tinymce in inline mode and it's a new tool for me. I have managed to adjust the text field input to my liking with just a few editing buttons. However, I am stuck trying to figure out how to get rid of the "Upgrade" button fr ...

Obtaining the radio button value from the form in the view.py file

I am encountering a challenge in extracting the radio button value within my view.py file. Despite successfully retrieving all values, I am unable to obtain the radio button value. Various attempts have been made utilizing both POST and GET methods but no ...

The issue is that only the first checkbox within ngFor is being toggled on and off each time a different checkbox is clicked

When I have checkboxes inside a ngFor loop and click on any one of them, only the first one gets checked and unchecked, updating only the status of the first checkbox. Here is the HTML template: <div *ngFor="let num of count" class="m-b-20"> & ...

How to Preserve Previous Value in a jQuery Change Event for an <input> Element?

Today I spent some time searching for a simple solution, but unfortunately, I have not found one yet. Essentially, what I am trying to do is capture a change in an input element while also being able to retrieve the previous value. Below is a basic exampl ...

What could be the reason for the mouseleave event not functioning properly?

I have a JSFiddle that is working perfectly. Check out my Fiddle here. In my code, I am trying to make a div change colors when hovered over and then back to its original color when the mouse moves out. It works fine on JSFiddle but not locally. When I r ...

Attempting to duplicate Codepen's code onto my local machine

Trying to figure out how to make this work locally after finding it on codepen https://codepen.io/oxla/pen/awmMYY Seems like everything works except for the functionality part. I've included the JS File and the latest Jquery in my code. <head&g ...

Issue with Safari causing footer to sporadically appear over overlay

I am encountering a problem with my webpage that has a sticky footer, scrolling content, and an overlay that appears after a few seconds. The issue is specific to Safari on certain devices like iPhone 12-15, where the footer and part of the scrolling conte ...

Building a dynamic mega menu using Bootstrap

Looking to enhance our ERP application with a mega menu feature. Does anyone have a link to a free Bootstrap mega menu template? I've searched on Google but haven't been able to locate the right one. ...

Top strategies for efficiently managing the loading of extensive PHP pages using Jquery, Ajax, HTML, and other tools

Hey there, I hope you're doing well in this new year. I've been working on a project that creates a "league table" using a large amount of data, similar to those seen in sports like football. The backend is built in PHP to process the data and d ...

Create a design where two columns can be scrolled independently and extend to fit the height of the viewport

I have successfully created a JS Fiddle that demonstrates exactly what I am looking for: http://jsfiddle.net/j7ay4qd8/5/ This Fiddle features two columns that can be scrolled separately, and different sections of the left column can be easily scrolled to ...

Background image in Bootstrap not covering entire web page

I am facing an issue where the background image is confined within the constraints of the login panel, and I'm unsure why. My goal is for the image to span the full width and height of the browser window. Any assistance on this matter would be greatly ...

Display a div in a specific color depending on whether the value is positive or negative, and

I created a counter but I want to change the color of the id="clicks" based on whether the value is positive or negative, and also add text accordingly. var clicks = 0; function clickplus() { clicks += 1; document.getE ...

Tips for Omitting Children <li> from CSS Design

I'm currently working in SharePoint Designer and facing an issue with custom bullets in my list. My goal is to apply a custom bullet on the parent li elements (Desc 1 and Desc 2), but unfortunately, it's also being applied to all the children li ...

What is the method to establish a set width for dropdown options in Bootstrap's selectpicker?

Here is some code snippet <select class="selectpicker form-control" data-live-search="true" id="subject_teacher_drop_down"> <option>English</option> <option>Methodology of social science with special reference to ec ...

Tips on linking a condition-reaction to document.querySelector

I am struggling to connect the condition-reactions to the input id of passid. I am unsure where to place the document.querySelector() method in order to link the indexed conditions correctly. Below is the code snippet: <!doctype html> <html> ...

Dynamic jQuery dropdown menu with nested or multi-level options, allowing for changing options within the same field based on selection

I am looking to implement a unique dropdown feature that combines chained fields with navigation for select field options. The concept involves dynamically loading options from Ajax/php, where the user can select an option and then navigate back to previou ...