Tips for achieving vertically centered text wrapping around a floating image

Describing it in words is proving to be difficult, so let me just illustrate it for you.

In the following images, the horizontal lines represent the "text", the small box symbolizes the image, and the larger box encompasses the entire webpage. My goal is to make the text behave as shown in the drawings, but I'm struggling to achieve that.

https://i.sstatic.net/jtryI.png

https://i.sstatic.net/Zo5h5.png

I've managed to wrap the text around the image using the float property, and I can vertically center the text using flexbox, grid, or even table. However, I'm facing a challenge when trying to do both simultaneously. Whenever I attempt to vertically center the text using any of these techniques, it seems to run into a clearing bug and treats height:100% as if the image doesn't exist.

https://i.sstatic.net/UmLGq.png

https://i.sstatic.net/yCjSp.png

Answer №1

To accomplish this, you can utilize the float and flex properties. If there is a small amount of text dynamically present, add the .box-flex class to the .box. However, if there is a larger amount of text, there is no need to include the .box-flex class.
In the case of using the flex display, I am employing the order property to shift the image div from the left to the right side.

The code snippet below should provide significant assistance:

*{box-sizing: border-box;}
.box{
    font-family: Arial;
    font-size: 16px;
    line-height: 22px;
    width: 100%;
    max-width: 480px;
    min-height: 100px;
    background-color: rgb(148, 206, 203);
    padding: 15px;
    margin: 15px auto 15px auto;
    color: #333;
    border-radius: 8px;
}
.img{
    width: 100px;
    height: 100px;
    min-width: 100px;
    max-width: 100px;
    max-height: 100px;
    background: coral;
    margin-left: 12px;
    float: right;
    border-radius: 4px;
}
.txt{
    position: relative;
    min-height: inherit;
    padding: 0;
    text-align: justify;
}
.box-flex{
    display: flex;
    justify-content: space-between;
}
.box-flex .img{
    order: 1;
}
.box-flex .txt{
    padding: 0;
    align-self: center;
    min-height: auto;
}
<div class="box box-flex">
    <div class="img"></div>
    <div class="txt">
        If there is a small amount of text (apply .box-flex)
    </div>
</div>
<div class="box">
    <div class="img"></div>
    <div class="txt">
        Lorem ipsum dolor sit amet consectetur adipiscing elit. Unde adipisci soluta beatae minima, dolores atque voluptas, explicabo mollitia, sunt alias nihil doloribus veritatis perferendis quibusdam error exercitationem quia consequuntur eos dignissimos sed veniam? Rerum saepe qui sed corporis voluptatibus soluta quo eaque. Quidem recusandae dolorum nesciunt Rerum saepe qui sed.
    </div>
</div>

Answer №2

CSS Exclusions could potentially provide a solution in this scenario: it offers the ability to wrap text around any element, even those that are absolutely positioned. Unfortunately, this feature is still in the working draft stage and is not yet supported by any browsers.

Here's an example showcasing how CSS Exclusions might be beneficial for your situation. While it uses absolute positioning instead of floating, it doesn't achieve the desired text wrapping effect. The "wrap-flow" rule doesn't seem to work as expected.

.d1 {
    position: relative;
    border: 2px solid cyan;
    min-height:200px;
    margin-bottom: 2em;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 0 1em;
}
.excl {
    position: absolute;
    top: 0;
    right: 0;
    width:100px;
    height:200px;
    background-color: lime;
    wrap-flow: both;
}
<div class="d1">
    <div class="excl"></div>
    <div class="txt">
        <p>
            Lorem ipsum 
        </p>
        <p>
            Nunc eu 
        </p>
    </div>
</div>
<div class="d1">
    <div class="excl"></div>
    <div class="txt">
        <p>
            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse odio massa, tempor auctor posuere et, consequat non velit. Suspendisse potenti. Proin hendrerit eu neque id varius. Morbi consequat dui neque, non fringilla dolor pretium vitae. Pellentesque congue nec justo vel mattis. Fusce lobortis turpis a urna elementum eleifend. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Nulla rhoncus sagittis lacus at dignissim.
        </p>
        <p>
            Nunc eu rhoncus massa. Nam pretium pellentesque faucibus. Mauris fringilla erat et nunc feugiat hendrerit. Nullam bibendum faucibus quam ut...
        </p>
       
       (...) Content continues...

        </p>
    </div>
</div>

In the meantime, one suggestion would be to utilize flexbox when dealing with smaller amounts of text near images, and resort to floating elements when longer blocks of text are present. You may need to compromise on the layout slightly or consider alternative approaches until CSS Exclusions become more widely supported.

Answer №3

To center the text vertically within a div, you can utilize the following CSS properties: position: relative (for the text) top: 50% transform: translateY(-50%)

Alternatively, you could also use the vertical-align property for both image and text: vertical-align: middle

If this solution does not fully address your issue or if something seems to be missing, please do not hesitate to reach out. I am here to help resolve any further concerns.

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

Configuring ng-options with personalized indices

I am a beginner learning AngularJS and I'm working on setting up ng-options with unique indexes. In my tables, I want to use the Primary Key as the index. For example, if the Primary Keys are 1, 3, 4, 5, I only want those indexes in my select. First ...

Switch out either even or odd divs within a container

I need help figuring out how to alternate the background colors of divs within a container. I want the first one to have a red background, the second to have blue, the third red again, and so on... Here is the code I currently have: <div>...</di ...

Use Angular and JavaScript to fetch HTML from a mySQL database and dynamically render it on a webpage

I'm currently utilizing Angular for the front end and Node for the back end. The data is retrieved from a MySql database where it's manually stored in text format with HTML tags. For example: <ul> <li>item1</li> <li> ...

Angular 13: Problems arise with custom theme in Angular Material version 13

I've set up a custom theme palette for my project that works perfectly with version ^12.2.13 of Angular Material, but not with ^13.2.3. Here's the SCSS code for my custom theming: my-custom-theme.scss @import '~@angular/material/theming&apo ...

Creating a Smooth Curve with CSS

Recently, I've decided to create a unique clock design inspired by the one showcased here. However, instead of using images like they did, I would like to implement ARC. Is it possible to create an arc using only CSS? For instance, if I wanted to make ...

Trouble arises when trying to define the style for an element generated through the Document

I'm having trouble setting the style for a 'tr' element created using DOM within JavaScript. Here's the code snippet: var tr = document.createElement("tr"); tr.onmouseover=function(){this.style.backgroundColor='#fbf9e0';}; ...

Align Text with Icon Using FontAwesome

Hey, I'm having a bit of trouble with something simple and it's driving me crazy. All I want to do is center the text vertically next to a FontAwesome icon. However, no matter what I try, I just can't seem to get it to work. I've looke ...

Unable to load circles on page refresh with amCharts Maps

I am experiencing an issue with the functionality of my amCharts maps. Whenever I refresh the page, the circles that are supposed to be displayed disappear. However, when I zoom in, the circles reappear. This problem has me puzzled, and I'm not sure ...

Tips on how to update the styling of an active link

http://jsfiddle.net/G8djC/2/ Looking to create a tabbed area where content changes based on the tab clicked. The Javascript function switches the link class to active upon clicking. However, struggling to change the color of the active tab beyond the firs ...

Creating a responsive design for a centered image with absolute positioning

I'm trying to align an image at the bottom of a div while also centering it using the following CSS: HTML: <div class="myDiv"> <img src="http://bit.ly/1gHcL8T" class="imageCenter" /> </div> CSS: .myDiv { width: 80%; height: ...

Sending numerous HTML forms using a sole submit button through AJAX

On my website, I have a page named publish.php where users can input details for each image they upload. Each image has its own form, but only one form is displayed at a time in a tabbed layout when the user clicks on the corresponding thumbnail. I want to ...

The issue with the ng-click property not triggering arises when it is assigned to a button that is generated dynamically

I need to construct a table dynamically based on the results returned from SQL. In this scenario, populating the table with the start time, end time, and user is straightforward: var StartTime = document.createElement('td'); var EndTime = docume ...

What techniques can I implement using JavaScript or CSS to create a slightly transparent effect on my text?

I am currently developing a website that features changing background images. I am looking to have the text on the site mimic the color of the backgrounds, but not so much that it becomes difficult to read. How can I make the text transparent in this man ...

Two media queries are combining the same declaration, with one targeting iPads and the other targeting desktop devices

In my code, I am utilizing two media queries within a bulleted list li tag. The issue arises when both queries, clearing every nth-child(2n+1) and nth-child(3n+1), are active on the same page in Chrome's device views. This causes the grid layout to ap ...

How can I capture a screenshot of the div displaying pictures uploaded by the user on the frontend?

Take a look at this code snippet. It allows users to upload images, move them around, resize, and rotate them once uploaded. $(function() { var inputLocalFont = $("#user_file"); inputLocalFont.change(previewImages); function previewImages() { ...

Is it possible to embed HTML code within JavaScript strings?

Is my approach to inserting HTML into a JavaScript string correct? In particular, I want to insert an HTML link into a div tag using JavaScript. Here is the HTML: <div id="mydivtag"></div> And here is the JavaScript code: document.g ...

variance in efficiency when retrieving a DOM element

While this may not make much of a difference for smaller, simpler DOM elements, the performance impact could be significant when dealing with larger, more complex ones. Is there a noticeable performance gap between storing an element in a variable and cons ...

Material UI Paper component does not extend to full viewport height

My React component includes a Material UI <Paper> component that acts as a background. This <Paper> is nested inside a <ThemeProvider>, which in turn is nested within a <div>, and then further nested inside the <body>. I have ...

Having trouble with SCSS in a jsfiddle? It could be due to a pesky cross-browser bug

Can someone help me with a positioning problem I'm having? I have a span element below an image inside an li tag, but for some reason the styles are not being applied. You can check out the example in the link below: https://jsfiddle.net/ymm8xpb8/16/ ...

What is the best way to increase the spacing between buttons in a Bootstrap navigation bar?

Can anyone guide me on how to add space between each button in this Navigation Bar? I want them to be separated, similar to this example. I am using bootstrap 5. <nav class="navbar navbar-expand-lg navbar-light bg-light shadow-sm bg-body"&g ...