Align two divs vertically within a button

Is there anyone out there who can assist me in vertically aligning 2 divs inside a button element? Here are the properties:

  • The height of the button is known
  • The height of the orange div is unknown
  • One of the divs contains text (orange div) with either 1 or 2 lines, but the number of lines is unknown

.

Here is the HTML code :

<button style="width: calc(125px * 1); height: calc(30px + 24px); margin-top: 0;margin-bottom: 0;">
    <div id='bt_icon'></div>
    <div id='bt_text'>Fermer</div>
</button>

CSS :

button {
    display: inline-block;
    border: 1px solid rgba(0, 0, 0, 0.5);
    border-radius: 3px;
    padding-left: 5px;
    padding-right: 5px;
    color: black;
    font-family:'Tahoma', 'Arial', 'Helvetica', sans-serif;
    font-size: 13px;
    cursor: pointer;
    background: linear-gradient(to bottom right, #EDDDB5, #D5C59D);
    box-shadow: 2px 2px 0 rgba(255, 255, 255, 0.3) inset, -1px -1px 0 rgba(0, 0, 0, 0.3) inset, 2px 2px 8px rgba(0, 0, 0, 0.3);
}

#bt_icon {
    float: left;
    width: 34px;
    height: 34px;
    background-color: green;
}

#bt_text {
    float: right;
    width: calc(100% - 10px - 34px);
    background-color: orange;
}

And here's the fiddle for reference:

https://jsfiddle.net/15xceg8p/1/

The green square will contain an icon and is properly centered vertically, however, the orange rectangle containing text is currently not aligned vertically.

Answer №1

Kindly revise the code provided below

button {
    display: inline-block;
    border: 1px solid rgba(0, 0, 0, 0.5);
    border-radius: 3px;
    padding-left: 5px;
    padding-right: 5px;
    color: black;
    font-family:'Tahoma', 'Arial', 'Helvetica', sans-serif;
    font-size: 13px;
    cursor: pointer;
    background: linear-gradient(to bottom right, #EDDDB5, #D5C59D);
    box-shadow: 2px 2px 0 rgba(255, 255, 255, 0.3) inset, -1px -1px 0 rgba(0, 0, 0, 0.3) inset, 2px 2px 8px rgba(0, 0, 0, 0.3);
}

#bt_icon {
    width: 34px;
    height: 34px;
    background-color: green;
    display: inline-block;
    vertical-align: middle;
}

#bt_text {
    display: inline-block;
    vertical-align: middle;
    width: calc(100% - 10px - 34px);
    background-color: orange;
}
<button style="width: calc(125px * 1); height: calc(30px + 24px); margin-top: 0;margin-bottom: 0;">
    <div id='bt_icon'></div>
    <div id='bt_text'>Close</div>
</button>

Answer №2

Experiment with the following styles and eliminate all CSS styling from the button:

button{display: inline-block;}
button>span{display: inline-block;vertical-align: middle;}

Additionally, get rid of the float property from both spans. https://jsfiddle.net/t698pL8j/

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 behind the jQuery .after() method inserting a row that is cramming itself into one cell?

I'm attempting to dynamically add cells to the right side of a table using jQuery when a row is clicked. I have successfully implemented inserting a new row on click, which also toggles its visibility with CSS. However, I am encountering an issue whe ...

Press here to unveil the solution

UPDATED with MORE CODE Included Unfortunately, our web designer suffered a stroke and is unable to assist me in finalizing some design elements. It appears that I will need to figure things out on my own. I have been using Typepad for a website that incl ...

How can I make sure that an asp:content or div element remains visible at the top when scrolling?

Within my asp:content tag, there is a div element: <asp:Content ID="btnContent" ContentPlaceHolderID="ButtonContent" runat="server"> <div id="dButtons" runat="server" visible="false"> Some CONTENT </div> </asp:Content> As ...

Is there a way to efficiently install web assets such as bootstrap, jquery, and font-awesome without relying on a content delivery network

Is there a way to automatically install jquery, bootstrap, and font-awesome without going through npm and manually moving the files to the correct folders? Are there any programs that can update these libraries and place them in the appropriate directories ...

The alignment of my text appears to be off

I am facing an issue with the second <ul> on my webpage, which contains more content compared to the first one. In this second <ul>, the last line is appearing floated to the left and does not appear below the penultimate line as expected. Li ...

Fading CSS Hover Popup Display and Hide

I am looking to create a CSS-only popup for an image that will appear when a user hovers over the image with their mouse. After researching on various platforms, such as this thread, I have developed the following solution: a.tooltip span { width: 250 ...

Challenges arising from the use of 'position: absolute' in managing relationships between grandparents, parents, and children

Within my React project, all code is enclosed within the root div (grandparent). In my Project.js component, there is a separate div (parent) solely responsible for the background color. This component is then displayed in App.js without any additional d ...

Overlaying Div Concealed by Background Video Filter

Whenever we add a filter to our background video, the div overlay ends up moving behind the video and becoming hidden. To see an example of this issue, check out this fiddle showcasing the background video with the text overlay: https://jsfiddle.net/dyrepk ...

The symbol for expanding and collapsing sections in the Bootstrap accordion is not dynamically updating

I'm facing an issue with my bootstrap accordions. I have two accordions and each one has an inner accordion. The first one is set to be opened by default. However, when I click on the second accordion, the icon of the first one does not update to a pl ...

Automatic scrolling feature in JavaFX

I am currently working on developing a chat box using JavaFX. My goal is to implement an auto-scroll feature that will scroll down the page when it gets filled up. However, I am facing some issues with this functionality. import javafx.application.Applica ...

Gradually move down until you reach 100%

When I attempt to click a button and scroll, I can easily achieve this with HTML (option 1). However, the scrolling motion is abrupt and I would prefer to have more control over the speed of the scroll. I then experimented with jQuery (Option1) but I am u ...

jQuery allows you to hide a div when the mouse leaves it

HTML <div class="container"> <div class="section" id="block1">Section-1</div> <div class="section" id="block2">Section-2</div> <div class="section" id="block3">Section-3</div> <div class="sectio ...

Replace current CSS classes with new classes provided by the API

One of the challenges I'm facing involves overriding existing classes that I've defined in an Angular component's SCSS file. In this unique scenario, different styles will be retrieved from an API, each with a class name associated with the ...

How to target a child or element in jQuery with a specific attribute value?

Imagine having multiple unordered lists with different items, where one or more have the class 'active' <ul id='list'> <li class='row active'></li> <li attrA='something active'></li> ...

What is the best method for determining the current value of an On/Off switch with labels for both states?

How can I retrieve the current value of an On/Off switch that is labeled as On and Off? Below is the HTML code for this particular switch: <div class="make-switch has-switch"> <div class="switch-on switch-animate"> <input type="checkbox" d ...

The Node application seems to be having trouble locating the JavaScript files that are referenced in

I am in the process of creating a basic chat application using node and socket.io. I'm following a tutorial that can be found at this link: http://socket.io/get-started/chat/ The problem I am encountering is that the tutorial includes some javascript ...

Strange spacing issues with inline-block elements and struggling to vertically center content within them

I feel like I'm losing my mind trying to figure this out... any help would be greatly appreciated. There are three divs on the page that need to sit on a single line. They must be square with rounded corners, so setting width and height is necessary ...

Steps to apply styles to an iframe element directly from its inner HTML code

Looking for help with this line of code: <iframe src="http://www.google.com" name="google" style="Z-INDEX: 0; MARGIN-TOP: -1px; WIDTH: 100%; HEIGHT: 1070px"> <html> <head></head> <body> < ...

Display a div using jQuery when it reaches halfway the height of another div

I have a code snippet that hides the .wp-filter when reaching the bottom of the .wpgb-layout jQuery(window).bind('scroll', function() { if(jQuery(window).scrollTop() >= jQuery('.wpgb-layout').offset().top + jQuery('.w ...

Vertical alignment of image on the left column

I'm attempting to create a setup with a small icon on the left and a text field on the right. Here's what I've accomplished so far on the left side... https://i.sstatic.net/g2dov.png The issue I'm facing is that when I have multiple ...