Prevent collision between div and img with CSS

I am facing an issue with a figure containing an image that floats to the right, along with a div element.
Here is my HTML structure:

<div>
<figure id="fig">
<img src="img.jpg"></img>
<figcaption>Caption</figcaption>
</figure>
<div id="inner">
Blah Blah Blah
</div>
</div>

The CSS for my div element is as follows:

#inner{
text-align: center;
width: 70%;
margin: auto;
padding: 1% 5%;
}

For the figure element, I have the following CSS:

#fig{
width:162px;
height:277px;
margin:auto 7px auto 7px;
float:right;
}

And for the img element, the CSS is:

img{
width:162px;
height:277px;
}

The issue arises when the div is positioned below the figure. The problem occurs because the div's width is set to 70% and it does not adjust when the figure overlaps. I need the div to take up 70% of the space, excluding the area covered by the figure.

Answer №1

Modify your #inner div by removing the width attribute and adding a margin to the right side with the total width of the figure element (including margin, padding, width, etc.). This will cause the figure to align with the right margin of the #inner container. Remember that DIVs are block-level elements by default and occupy 100% width.

http://jsfiddle.net/kacH7/

CSS

#fig {
     width: 162px;
     height: 277px;
     float: right;
     margin: auto 7px;
}
#inner {
     text-align: center;
     padding: 1% 5%;
     margin-right: 176px;
     background-color: red; /* for demonstration purposes only */
}
img {
     width: 162px;
     height: 277px;
}

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

Having trouble with the gradient hover effect on the Tailwind Button in dark mode?

In my nextjs project with Tailwind CSS, I am encountering an issue while trying to apply specific properties to a button using globals.css. The problem lies with the dark:hover properties not working as expected. .coloredButton { @apply bg-gradient-to- ...

"Please note that the function of the enter key to navigate between form fields is

When I use the enter key to move between form fields, it's not working as expected: The cursor doesn't move to another field in the form when I press the enter key. Removing the submit button allows the enter key to work properly. The issue se ...

What steps can I take to ensure that Bootstrap components are compatible with my mobile device?

[![This is the result I see when accessing my page on a mobile device <head> <title>Login Page</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=devic ...

Using jQuery Chosen to showcase numerous select fields simultaneously within a webpage

I have implemented a search feature using in a dropdown list for user convenience. On the post/create page, I have added 2 tabs: Link & Text Both tabs include a "subreddit_id" field that utilizes $(selector).chosen();. Interestingly, the jQuery chos ...

Having trouble with my phonegap app not allowing me to open any

I've just embarked on creating my first phonegap application using an HTML5 mobile website template. After deploying it, I noticed that the external links are not working within the app. Clicking on a link doesn't seem to do anything. To addres ...

Best practices for blocking the interface in AngularJS during data loading phase

Within my form, there are numerous fields where the data is fetched from a server, including dependent data lists. During this data loading process, I would like to freeze the interface by displaying a progress bar. This can be achieved by overlaying a lay ...

Utilizing jQuery event handlers on fresh elements within a Knockout.js array

I am working with a Knockout.js viewmodel that contains a list of products, which are retrieved using an ASP.NET Web API call: // Defining the viewmodel var myViewModel = { products = ko.observableArray([]); }; // Applying knockout bindings ko.applyB ...

I am looking to provide customized edits to particular h1 tags

Is it possible to apply custom formatting to an h1 tag using CSS for a specific section of the webpage? For example, instead of just using: h1{ } I am looking to do something like this: .specific.h1{ } Any suggestions on how to ...

Is it necessary to always include all styles for jQuery UI separately in my project?

While reading various articles, I have noticed a common suggestion to explicitly bundle each jQueryUI .css file, such as the answer provided on How to add jQueryUI library in MVC 5 project?. However, upon examining the .css files within the Content/themes/ ...

Is there a way to rearrange the order of columns in Bootstrap 4?

Presenting a straightforward scenario: <div class="row"> <div class="col-md-12 col-lg-6 col-xl-7">A</div> <div class="col-md-12 col-lg-6 col-xl-5">B</div> </div> In essence, if md A B I wish for it to be like ...

Modify the input value when using the get_search_form() function in Wordpress

After experimenting with the latest get_search_form() function in WordPress, I've encountered an issue where I can't figure out how to remove the text label from the search submit button. Does anyone have any suggestions or solutions for this pr ...

Click events registered using JQuery's .on() method are not functioning

After clicking on the link, I want to add HTML content following the element that has the class jstore-js-detailLink. However, my current method doesn't appear to be functioning: <a href="" class="jstore-js-detailLink">hello</a> <div ...

Query database using two dropdown menus and display the results in a table using PHP

I am facing an issue with two dropdowns on my webpage. The second dropdown's options are based on the selection made in the first dropdown. Now, I need to use the selected values from both dropdowns to query a database and display the results in an HT ...

Send the td value to a PHP script with the help of JavaScript

I have an HTML table that displays records from a database. Below is a screenshot of my table There is a button in a TD (i.e column 5,7,9), when I click the button I want to perform a function to display a popup box with an HTML table. Before that, I wa ...

When would using <style> be more appropriate than using a css file?

Is it necessary to use the style element when you can simply write in the CSS file? Are there any circumstances where using 'style' is more beneficial than the CSS file? ...

Is there a way to align this text to the center and eliminate its default height?

Recently, I revisited my journey of learning HTML and CSS, only to encounter some new challenges that I hadn't faced before. https://i.sstatic.net/PhKVs.png In particular, I'm grappling with a peculiar issue revolving around a button in my code: ...

Dynamically adjust the height of a parent div to match its child div's height upon clicking using JavaScript

Within my div element (divChatContainer), there is a top div (divChatContainerTop) containing a "span" element. When this "span" element is clicked, I want the container div to resize to the height of the "divChatContainerTop" (essentially minimizing the c ...

Maximizing efficiency in website reloading and development with Sinatra using Ruby along with Rack and Haml

In my efforts to build a Sinatra website, I have gone from using Shotgun to now utilizing Rerun for reloading my Thin server whenever I make file edits. However, the development cycle is proving to be quite time-consuming. Even small changes to CSS, JavaS ...

Encountering difficulties triggering the click event in a JavaScript file

Here is the example of HTML code: <input type="button" id="abc" name="TechSupport_PartsOrder" value="Open Editor" /> This is the jQuery Code: $('#abc').click(function () { alert('x'); }); But when I move this jQuery code to a ...

Is there a way to prevent Mac users from using the back and refresh buttons on their browser?

There seems to be a common trend of disabling the back button or refresh button on Windows using JS or Jquery to prevent F5 from working. Can this same method be applied to CMD+R on Mac computers? ...