Aligning the icon within the div and adding a gap between each div

I'm fairly new to web design, specifically dealing with html and css, and I'm attempting to create something similar to the design in the following image:

My struggle lies in centering the fa-envelope-o icon both horizontally and vertically. I've tried the following code:

#envelopeDiv{
    background-color: red;
    width:62px;
    height:42px;
    line-height:42px;
    text-align:center;
    vertical-align:middle;
    display:inline-block;
}

#disableAllDiv .big-icon{
    font-size: 25px;
    line-height: inherit;
}

#disableAllDiv{
    background-color: #4caf50;
}

#disableAllButton{
    margin-top: 10px;
    margin-bottom: 10px;
}

Unfortunately, this code didn't yield the desired outcome. Could anyone offer sample code or a more professional approach? Even tips on how to research would be greatly appreciated.

Answer №1

As stated by @Michael_B in the previous comment, utilizing flexbox is considered a contemporary approach to address this issue.

While horizontal centering can be achieved easily with margin: auto, vertical alignment proves to be more challenging. As an alternative to CSS, I will suggest a non-CSS method.

Personally, I prefer a direct JavaScript (and jQuery) solution for achieving this, using absolute positioning and dynamically adjusting the top and left properties to half the dimensions of the container minus half of the image's dimensions.

$("img").css({
  "top": $("div").height() / 2 - $("img").height() / 2,
  "left": $("div").width() / 2 - $("img").width() / 2,
"position": "absolute"
});
div {
  width: 500px;
  height: 500px;
  background-color: #aaf;
  position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <img src="http://images.clipartpanda.com/envelope-with-letter-clipart-envelope-md.png" />
</div>

For a demonstration, you can view a live example on JSFiddle.net.


UPDATE

Upon reflection, if you have predetermined the dimensions of your container and image, achieving the desired outcome becomes simpler by applying specific padding to the container.

Take a look at this illustrative example on JSFiddle.net.

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 best approach to creating a fully rounded capsule using CSS?

I've been having trouble creating a complete capsule in CSS and I'm not achieving the expected outcome. Can anyone assist me in resolving this issue? I am new to CSS. Below is my code: <style> #myStyle { position: relat ...

Stopping the WordPress gallery at the end without looping using Elementor

Is there a way to make my gallery stop at the end of the last picture without having to add any jQuery code? I inserted this gallery using elementor widgets and would appreciate your help on this matter. Additional information: Upon clicking on an image, ...

How can I ensure that the size of the Dropdown Menu Item matches its parent in both Bootstrap and CSS styles?

I am working on a navigation bar that has a dropdown menu which appears on hover. I want the size of the dropdown menu items to match the size of the parent element. Here is an image for reference: https://i.stack.imgur.com/oNGmZ.png Currently, the "One ...

Creating Eye-Catching Images by Incorporating Image Overlays Using Just One Image

I'm facing a bit of a challenge here. I need to figure out how to overlay an image onto another image using just a single image tag. Specifically, I want to add a resize icon to the bottom right corner of an image to let users know they can resize it ...

Encoding a JSON representation of an HTML list where all children are displayed at each parent item

Here is the structured list that I am currently working with: function convert( name_ref ) { name_ref = name_ref + " > ol > li"; var mylist = []; $(name_ref).each(function () { if ($(this).find('> ol > li').length){ myl ...

Troubleshooting Problems with CSS `transform` in JQuery UI's `Slide` Transition

While implementing JQueryUI's slide transition on an element with a CSS transform, I encountered an issue where the top half of the element gets hidden during the animation. Is there a way to tweak either my JQueryUI animation or CSS to avoid this pro ...

Instructions on transforming a XAML grid in WPF into a JSON array

Currently, I am utilizing a XAML WPF grid in my project and I am looking to transform the structure of the grid into JSON format. Do you have any suggestions or ideas on how this can be achieved? For reference, here is an example: <GridBinding> ...

The inner HTML functionality in Angular 2 seems to be malfunctioning when dealing with HTML tags

I am facing an issue with an array that includes displayName with HTML tags: this.topicsList = [ {id: "173", name: "Discussion1", displayName: "Discussion1", status: 1}, {id: "174", name: "discussion123", displayName: "discussion123", status: 1}, {id: "19 ...

Div element obstructing the ability to view markers

Issue with a positioned DIV over Google Maps: The DIV is causing accessibility problems for the map, taking up the entire width when it should stop at the green square borders. Interaction with the map is limited to the very bottom after the green square. ...

JsPlumb: Incorrect endpoint drawn if the source `div` is a child of a `div` with `position:absolute`

My current setup involves two blue div elements connected by jsPlumb. You can view the setup here: https://jsfiddle.net/b6phv6dk/1/ The source div is nested within a third black div that is positioned 100px from the top using position: absolute;. It appe ...

What is the reason behind this HTML/CSS/jQuery code functioning exclusively in CodePen?

I have encountered an issue where this code functions properly in JSFiddle, but not when run locally in Chrome or Firefox. I suspect there may be an error in how the CSS or JavaScript files are being linked. In the Firefox console, I am receiving an error ...

Switching CSS styles - seeking a smoother integration

I have successfully implemented a JS CSS switcher, which works brilliantly. However, I would like it to function more seamlessly. When opening a new page on the site, the default CSS style sometimes flickers briefly before the selected CSS style is reappli ...

Implementing jQuery form.js to dynamically update image src upon success

I am currently utilizing jquery.form.js to submit a form via AJAX and retrieve a response. My PHP page is configured to output the image URL in the format "images/thumbs/071112130957.jpg" Below is my jQuery code: $("#imageform").ajaxForm( { target: &ap ...

Learn the process of sending information to a MySQL table using a select tag in PHP

I am attempting to use a select tag to submit data to a MySQL table using PHP. I have been successful in inserting data using the text type with the following code: <td> <label class="control-label">Image Type </label> </td> &l ...

My live website is plagued by an annoying horizontal scroll bar that doesn't appear in the local host version

I am struggling to remove the unwanted horizontal scroll bar on my live website, although it is not visible on my local host... <-- this is my website, built using Vite + ReactJs. I attempted to use overflow: hidden; and max-width: 100% on the body t ...

Issue encountered while trying to play sound on PhoneGap

Greetings! I am attempting to integrate sound into various buttons and components of a game. I found this code on the PhoneGap website which works perfectly fine during testing: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "h ...

Manually adjusting the aria-expanded attribute to "true" does not result in opening a new tab

After a certain condition is met, I want to open another tab. Below is my bootstrap navbar code along with the jQuery script. <ul class="nav navbar-default nav-justified" role="tablist" id="navbar"> <li role="presentation" class="active"><a ...

Use PHP to highlight the row that has been selected in an HTML table

I am currently working on a project where I have styled multiple HTML tables on my website using alternating gray and white bands. Now, my goal is to highlight the selected row in a darker shade of gray. I have experimented with various solutions, and the ...

Manipulating SVG filter attributes with CSS: A comprehensive guide

In my quest to master CSS animation, I found myself needing to manipulate the values of SVG filter attributes. While attempting to reference a CSS variable within the specularConstant attribute, I encountered an error from the browser. Is it feasible to ...

Improving Vue Component on Navigation

I am facing an issue with my navbar where I have two versions. Version 1 features a white text color, while Version 2 has black text. The need for two versions arises due to some pages having a white background, hence the requirement for black text. Bot ...