Looking for assistance on positioning an image of an icon both vertically and horizontally within a div that has a border radius. Can anyone help with

I am attempting to center an image within each of the four divs below. I have experimented with using the image as a background, but due to the border radius, it does not appear quite right. To provide context, I have included a fiddle. HTML:

<div id="gold" class="divSquare">1</div>
<div id="lblue" class="divSquare">2</div>
<div style='clear:both'></div>
<div id="redb" class="divSquare">3</div>
<div id="dblue" class="divSquare">4</div>

CSS:

#gold{
background-color:#F3B500;
border-radius: 100px 100px 5px 100px;
transition: all .2s ease-in-out; }

#gold:hover{
opacity: 0.8;
transform: scale(1.2);
border:none;
margin:8px;
}
#lblue{
background-color:#0E43C6;
transition: all .2s ease-in-out;
border-radius: 100px 100px 100px 5px;  }
#lblue:hover{
opacity: 0.8;
transform: scale(1.2);
margin:8px;}
#redb{
background-color:#7E0202;
transition: all .2s ease-in-out;
border-radius: 100px 5px 100px 100px; 
}
#redb:hover{
opacity: 0.5;
transform: scale(1.2);
margin:8px;}
#dblue{
background-color:#041871;
transition: all .2s ease-in-out;
border-radius: 5px 100px 100px 100px;
}
#dblue:hover{
opacity: 0.5;
transform: scale(1.2);
margin:8px;}
.divSquare{
width:100px; height:100px; margin:2px;float:left;
text-align: center;
vertical-align: middle;
line-height: 150px;}

http://jsfiddle.net/gerryboy/osaqnzoL/

Answer №1

To achieve content centering within your divs, consider using flexbox as a solution (given it aligns with your compatibility needs):

.contentBox {
  display: flex;
  justify-content: center;
  align-items: center;
}

View the Demo

Answer №2

From what I gather, you are looking to utilize the background-image CSS property to place an image on a div element and then increase both the size of the div and the image upon hovering over the div. Please confirm if my interpretation is correct.

Answer №3

Unless there is a strict requirement for the image to be part of the content, I would opt for using a background image.

* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
body {
  padding: 3em;
}
#gold {
  background-color: #F3B500;
  background-image: url(http://lorempixel.com/output/abstract-q-c-25-25-2.jpg);
  background-repeat: no-repeat;
  background-position: center center;
  border-radius: 100px 100px 5px 100px;
  transition: all .2s ease-in-out;
  transform-origin: bottom right;
}
#gold:hover {
  opacity: 0.8;
  transform: scale(1.2);
  border: none;
}
#lblue {
  background-color: #0E43C6;
  transition: all .2s ease-in-out;
  border-radius: 100px 100px 100px 5px;
  transform-origin: bottom left;
}
#lblue:hover {
  opacity: 0.8;
  transform: scale(1.2);
}
#redb {
  background-color: #7E0202;
  transition: all .2s ease-in-out;
  border-radius: 100px 5px 100px 100px;
  transform-origin: top right;
}
#redb:hover {
  opacity: 0.5;
  transform: scale(1.2);
}
#dblue {
  background-color: #041871;
  transition: all .2s ease-in-out;
  border-radius: 5px 100px 100px 100px;
  transform-origin: top left;
}
#dblue:hover {
  opacity: 0.5;
  transform: scale(1.2);
}
.divSquare {
  width: 100px;
  height: 100px;
  margin: 2px;
  float: left;
  text-align: center;
  vertical-align: middle;
  line-height: 100px;
  color: white;
}
<div id="gold" class="divSquare">1</div>
<div id="lblue" class="divSquare">2</div>
<div style='clear:both'></div>
<div id="redb" class="divSquare">3</div>
<div id="dblue" class="divSquare">4</div>

Answer №4

One way to tackle vertical alignment is by utilizing the position and margin top properties set at 50%, alongside a transform: translateY of -50% for perfect vertical alignment.

position: relative;
top: 50%;
transform: translateY(-50%);

Check out this demo showcasing some CSS improvements.

Another approach, demonstrated in this showcase, uses images as background covering various shapes. It might need adjustments but can definitely be achieved. Don't forget to apply the following:

.divSquare{
    overflow: hidden;
}

This will help mask out any unwanted image bleeding around the edges.

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

When viewing a webpage on a small browser, the content will automatically expand to fill the

Attempting to utilize responsive CSS media queries to hide the sidebar unless the screen is large or a tablet big enough in landscape mode. It appears to be functioning properly while resizing the browser, but at a certain size, it occupies the entire scre ...

"Exploring the magic of product galleries: A behind-the-scenes look at how websites create images

Have you ever wondered how this website creates the customization effect in its gallery? When you adjust the color, stones, or other attributes of the ring, it appears as though a new image is being generated. (click Customize) Do you believe they have a ...

Center the radio buttons regardless of the length of the text

My dilemma lies with 3 radio buttons and their corresponding labels - while 2 of them are aligned perfectly beneath each other due to their matching character lengths, the middle one extends further making it look misaligned. Check out this fiddle. Below ...

What is the best way to update the color of an fa icon when an error message is displayed using PHP

I am facing an issue with my form where error messages are displayed on certain input fields. The error messages show up correctly, but I want to change the color of the "fa" icon in the field when its corresponding error message appears. My goal is to ma ...

What could be the reason for the CSS property of margin-right not being applied in the HTML code?

New Code: table { border-radius: 3px; margin-top: 5px; margin-left: 5px; margin-right: 5px; } <div class="table-responsive-md"> <table class="table table-striped table-bordered table-hover table-sm text-light"> <caption> ...

Tips for converting inline CSS to stand-alone attributes without any cost

I am attempting to modify an au generated HTML code, for example: <div class="intro editable" id="field_text">text <strong>text</strong> text <span style="text-decoration: underline;">text</span> <span style="color: #ff00 ...

A guide on implementing input tags through instant search in HTML using AngularJS

As someone new to Angular JS, I am working on adding input tags by clicking from instant search using Angular JS. I came across a helpful example of instant search in action through this demo that Google provided. Now, I want to implement the ability to a ...

Designing a unique CSS border for custom list item bullets

I'm currently exploring the possibility of implementing a CSS border around an image that I have imported as a custom bullet for my list items: ul { list-style: none; margin-right: 0; padding-left: 0; list-style-position: inside; } ul > ...

Summernote information embedded with HTML elements

I just started using the summernote text editor and I'm trying to figure out how to extract the content from the textarea without all the HTML tags. This is what I have in my code: <textarea class="summernote" id="summernote" ng-model="blog.c ...

Is there a way to display the button only when the user is able to enter an email in the input field?

I have a form for users to update their profile and a button to delete their account. I want the delete account button to only be visible if the user enters their email. $(document).ready(function() { var user_email = $('input[name="emp_email"]&a ...

Ways to apply CSS styles dynamically within a v-for loop

Despite searching through the VUE official website and various other sources, I have not been able to find a satisfactory solution. I am reaching out for assistance, thank you in advance. <div class="tag-item" :ref="`tagItem_ ...

Enhance your website's user experience by implementing Bootstrap breadcrumb styling integrated

I am a beginner with bootstrap and currently trying to customize the breadcrumb styling. I attempted the following code which resulted in the desired inline layout. <div class="BreadCrumbs"> <a href="www.yahoo.com" >Yahoo</a&g ...

Creating a dropdown menu in Bootstrap 4 using JSON information

I am trying to create a dynamic drop-down menu using an input field with a drop-down button inside a form. Currently, I am attempting to populate the drop-down menu with static JSON data. However, I am encountering issues with getting it to function proper ...

What is the most efficient method for applying multiple classNames to elements in Next.js?

Could you provide me with a list of all methods, both with and without packages? Also, I'm interested in knowing why one method is considered better than the others. ...

Tips for enhancing webpage load times by optimizing CSS and jQuery file handling

As a beginner in HTML and CSS, I am facing slow loading times when testing my pages on a local server. This makes me concerned about the potential loading time on the internet. Currently, I have all the files included in the <head> tag at the top of ...

Problem with React-Bootstrap CSS styling

After importing Bootstrap CSS, I noticed that extra CSS is being applied to the navbar, as seen in the image below. Although Bootstrap cards are being used in the code, the issue is specifically with the Navbar. Interestingly, the problem disappears when I ...

Where should I place my elements to ensure proper positioning in the Code Generator?

Hey everyone, I've been working on developing a code generator similar to Sketch2Code and I've hit a roadblock with my JSON structure. I'm trying to create an HTML page using the myData example provided with a predefined HTML structure. Thin ...

Include a carbon copy when generating a script for an email

I am working on a form that sends emails, and I need to include a cc email address in the recipient list. Can someone guide me on how to modify the line below to achieve this? Thank you. mail("<a href="/cdn-cgi/l/email-protection" class="__cf_email__ ...

Unable to display imported image in React application

I have a component where I want to display an image: import React from 'react' import background from '../media/content-background.jpg' function ContentPage(){ return( <div id="content-page"> < ...

The alignment of the label button within the form-group is being set to the top

While using Bootstrap 4, I encountered an issue with aligning the save button in a form group because there was no label at the same level. Despite trying to follow an example from here, I still had to hard code margin-top to prevent the button from bein ...