Adjust background image to fit within border in HTML/CSS

I am currently in the process of creating a test webpage to improve my skills in html/css. I'm trying to figure out how to make an image conform to the shape of the border on the page. Despite my efforts, it seems like the image is not perfectly centered within the border. Adjusting the size of the image only exacerbates this issue and causes it to appear more towards the middle of the page rather than within the confines of the border, which is not the desired outcome. I just want the image to fit seamlessly within the border without any parts protruding outside of it. However, achieving this seems to be posing some challenges for me.

Is there a way for me to ensure that the image is accurately centered and completely fills the entire border without any part being left hanging or exceeding the border's boundaries?

#pic {
    float:right;
    transform: rotate(90deg);
}
#bod {
    height:300px;
    width:300px;
    border: 5px ridge blue;
    float:right;
    border-radius: 105px 105px 0px 0px;
    overflow:hidden;
    background-image: url("smile.jpg");
    background-size: 800px 800px;
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-position: center;
}
<div id="bod">
    <div id="pic">
        <img src="http://lorempixel.com/800/500" />
    </div>
</div>

Answer №1

Revise the CSS code for your #bod selector with the following changes:

#bod {
    border-radius: 105px 105px 0px 0px;
    border: 5px ridge blue;
    height: 300px;
    width: 300px;
    float: right;
    overflow: hidden;
    background-image: url("smile.jpg");
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center; 
}

To clarify, I have eliminated the background-attachment attribute and updated the value of the background-size to cover, which is crucial.

Modification

Previously, you had set the image using CSS by specifying background-image as url("smile.jpg") in the #bod styling. Since you are now inserting the image in your HTML with

<img src="http://lorempixel.com/800/500" />
, that line is redundant.

The new image appears off-center, to correct this update your #pic styling as follows:

#pic {
    float: right;
    transform: rotate(90deg);
    transform-origin: 50% 50%;
    height: 100%;
    width: 100%;
}

In the #pic styling, I have included transform-origin, width, and height attributes.

Answer №2

To ensure the proper rotation center, you must confirm that it is at the middle of the div. This can be achieved by following these steps:

#pic {
width:100%;
height: 100%;
transform: rotate(90deg);
}

#pic img{
width: 100%;
height: 100%;
}

https://jsfiddle.net/ebc5yjzu/3/

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

Numerous links were chosen and multiple div elements were displayed on the screen

Currently, I have a setup where selecting one div will show its content. However, I am looking to enhance this by allowing multiple divs to be displayed simultaneously. For example, if 'Div 1' is selected and shown, I want the content of 'Di ...

When using jQuery's .each method, only the final JavaScript object element is added to the divs

I have a unique set of dynamically-created divs, each containing a Title. When a div is clicked, a modal opens (which is cleared upon click), and the Title is displayed again in the modal. My goal is to add the category descriptions into these modals, but ...

Reposition the button alongside the textarea

Seems like this is a common inquiry. I am still learning the ropes when it comes to CSS and JavaScript. I have a textarea with a button beneath it, and I would like the button to stay aligned with the textarea as I resize it! Any suggestions on how I cou ...

I need the PHP code to ensure that only checkboxes can

I keep encountering what seems like a simple issue in my html form with checkbox groups. I am trying to ensure that at least one checkbox is selected from each group, and if not, display an error message using PHP code. However, despite multiple attempts, ...

Updating Angular components manually involves making direct changes to the component code,

Whenever I refresh a component in my Angular application implemented with version 17, the URL 'localhost:4200/(path)' reverts back to 'localhost:4200'. Interestingly, this behavior is consistent across three components except for the Ho ...

Fetching data asynchronously within HTML using AngularJS

I am conducting a quick test to troubleshoot why certain parts of my code are not functioning as anticipated. Within my setup, I have a controller called testCtrl and a service named myService. The goal is to retrieve data from Parse using the service and ...

Create a transparent selection form using Contact Form 7

I'm having trouble making the Options in a Select Drop Down transparent, but it just doesn't seem to work. Even when I try to make it black, it stays black. .wpcf7-select option { background: black !important; } No matter how many times I ...

Tips for removing borders around a textbox with a background image: When incorporating a background image into a

After removing the default borders on a textbox using outline:none;, I noticed that adding a background image caused the border to reappear, and I couldn't get rid of it! How can I solve this issue? Here is the code for the textbox: <input type = ...

Apply the Bootstrap container-fluid class exclusively to extra small devices

Is there a way to utilize the entire width on extra small and small devices (using container-fluid), while only using the container class for other devices? What would be the most effective method for implementing this setup? I attempted using jasnys bo ...

What is the best way to retrieve the value or text from a dropdown menu that has been added to a table row using JQuery?

Within my dynamic table, users can click an "add" button to generate a new row in the table using JQuery. One of the columns in each new row includes a dropdown box with predefined values. How can I retrieve the selected value from this dynamically created ...

The ng-click functionality seems to be malfunctioning when used within the controller in conjunction with ng-bind

After coding, I noticed that my ng-click function is not working. When I inspected the element, I couldn't find ng-click displayed anywhere. Can someone please help me figure out what I'm doing wrong? var app = angular.module('myApp' ...

My Bootstrap navbar seems to be malfunctioning. I'm wondering if there could be an issue with

here is a snippet of the code I'm working on: <nav class="navbar navbar-expand-md navbar-dark bg-dark sticky-top"> <div class="container-fluid"> <a class="navbar-brand" href="#"> <img src="https://i.stack.imgur.com/5XeYV.png" ...

The initial transition in offcanvas on bootstrap 5 is not appearing when a placement is dynamically added

I am currently working on triggering an Offcanvas with JS and making the placement configurable. The issue arises when attempting to dynamically set the offcanvas-end class to the offcanvas element, as it does not transition smoothly the first time it is t ...

angular input elements with no spacing between them

In my project, I have a square component identified by the selector 'app-square': <input type="text"/> I have applied specific styles to this component: input[type=text] { width: 40px; height: 40px; font-size: 15pt; ...

Azure Chatbot that logs conversations in Webchat whenever the user selects 'none of the above' option

Recently, I've delved into the world of web chat services and embarked on a journey to craft a chat bot using pure JavaScript code that can seamlessly integrate into any HTML file. Despite consulting Microsoft's documentation, I find myself in a ...

Select the fourth element in a list using CSS

Is it possible to style the fourth child differently using the ">" selector like so: .thisclass > ul > li > ul > li { color: blue; } I am working with WordPress and trying to avoid creating additional CSS classes. Is there a way to apply s ...

When you hover over the text, the color will shift

I have created two boxes that are blue with white text and change to a different color scheme when hovered over. However, I am experiencing an issue where the text color reverts back to the original color when hovering below the text area of the box. Here ...

Issues with font rendering on Firefox making it difficult to read

Our website's Wordpress theme utilizes the font Montserrat, which is properly displayed in Chrome, IE, and Safari, but is experiencing issues in Firefox. Below is the CSS styling for the font: @font-face { font-family: 'montserratbold'; ...

ErrorType at /create Listing() received an unexpected argument: 'description'

I'm having trouble with my ecommerce site as it's not allowing me to create listings and post them. Below is the code from my views.py: ... (insert code snippet here) ... Here is the excerpt from my models.py: ... (insert code snippet here) . ...

Steps to open an HTML file in a web browser from a remote Linux server

I am facing a challenge where I have an HTML report file stored on a remote server and I need to access it from my local machine using any browser. So far, I have tried the following steps: - I installed httpd on the remote server, started it, and attemp ...