Floating image along with accompanying text; CSS styles have not been incorporated

As I work on developing my web application and utilize a bootstrap template, I encountered a situation where I needed to include an inline image with text, with the image floating to the left and the text positioned to the right.

The paragraph element in the html was contained within a div. To achieve the desired layout, I researched and came across suggestions to add an img property to the CSS rule of the parent div. Below is the structure in which I attempted to insert the image:

<div class="container">
  <div class="row">
    <div class="col-lg-8 col-md-10 mx-auto">
      <p style='float:right;' > <img src={{ url_for('static',filename='img/ankulGupta.tiff') }}>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Saepe nostrum ullam eveniet pariatur voluptates odit, fuga atque ea nobis sit soluta odio, adipisci quas excepturi maxime quae totam ducimus consectetur? Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eius praesentium recusandae illo eaque architecto error, repellendus iusto reprehenderit, doloribus, minus sunt. Numquam at quae voluptatum in officia voluptas voluptatibus, minus! Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
    </div>
  </div>
</div>

While attempting to add the float rule to the image, I encountered some difficulties. Despite defining the relevant CSS rules within the parent div, the image layout didn't respond as expected. Eventually, I resorted to directly adding style rules to the img tag to achieve the desired effect:

<img style='float:left; margin-right:10px; margin-top:8.5px; width: 210px; height:194px' src={{ url_for('static',filename='img/img.jpg') }}>

It seems the CSS rules didn't apply properly, prompting me to seek clarification on what might have caused the issue. If you have insights or suggestions on how to better structure the CSS rules for the image, as a beginner in web development, I would greatly appreciate any guidance.

Answer №1

There is a problem in the code snippet row>img. In CSS, the > symbol is used to select direct children of a parent element. However, in this case, img is not a direct child but a grandchild. To fix this, update your code to the following:

.row img{
   display: inline-block;
   float: left;
 }

You also mentioned that the above code is not working. In that case, make sure to apply all remaining styles to properly format the inline img tag. Here is an example:

.row img{
   margin-right:10px; margin-top:8.5px; width: 210px; height:194px;
   float: left;
 }

Code Snippet:

.row {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
  margin-right: -15px;
  margin-left: -15px;
}

.row img{
  margin-right:10px; margin-top:8.5px; width: 210px; height:194px;
  float: left;
}
<div class="container">
  <div class="row">
    <div class="col-lg-8 col-md-10 mx-auto">
      <p style='float:right;' > <img src='img/ankulGupta.tiff'>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Saepe nostrum ullam eveniet pariatur voluptates odit, fuga atque ea nobis sit soluta odio, adipisci quas excepturi maxime quae totam ducimus consectetur? Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eius praesentium recusandae illo eaque architecto error, repellendus iusto reprehenderit, doloribus, minus sunt. Numquam at quae voluptatum in officia voluptas voluptatibus, minus! Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
    </div>
  </div>
</div>

Answer №2

The reason why the .row>img selector only works if img is a direct child of the row container in the DOM tree structure is because of the specificity of the CSS selector. To resolve this issue, it's recommended to use a more generic approach like the following:

.row img {
  display: inline-block;
  float: left;
}

Alternatively, if you want a more specific styling solution, you can apply a class selector to the images:

.image-style {
  display: inline-block;
  float: left;
}

<img class="image-style" src={{ url_for('static',filename='img/img.jpg') }}>

Answer №3

In order to ensure the CSS is properly applied to the image, it is necessary to adjust your CSS rule as follows:

.row > div > p > img {                                                          
  display: inline-block;
  float: left;
}

The reason your rule is not working is because the img tag is not a direct child of the .row element.

Answer №4

.row img{
  display: inline-block;
  float: left;
}

Modify to the following format:

.row img{
  display: inline-block;
  float: left;
}

Trust this adjustment proves beneficial to you.

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

Selecting elements using XPath

My goal is to target an i element using the following XPath: //div[contains(text(), "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="32565356564153565341725e5d5d1c515d5f">[email protected]</a>")]//parent//i[contains(@c ...

The floated div is disrupting the flow due to its margin

Two divs are causing some alignment issues: one floated left and the other floated right. The margin on the right div is pushing it down, causing the left div to appear lower than desired. I want both divs to be aligned at the top of the page. HTML: < ...

Issue with Vue2: encountering an error with the view-router component when attempting to utilize the <router-view> tag

Currently delving into Vue2 for the first time and facing hurdles with routes. Ever since I inserted <router-view></router-view> in index.html, an exception has been popping up: [Vue warn]: Failed to mount component: template or render functi ...

Having difficulty stripping HTML tags from the response JSON

Hello, I am new to AngularJS and facing an issue with parsing JSON data. The JSON response contains HTML format data, with tags like <, BR, etc. When checking the response in a browser, it appears fine; however, on devices such as tablets or mobile phon ...

Tips for removing the Y-Axis entirely from a CanavasJS chart

I'm working with a canvasJS chart and my goal is to completely hide the Y-Axis. I've managed to remove the Y-Axis line and title in this JSFiddle example. I came across these properties to hide the Y-Axis title and line, but I'm struggling t ...

Showing a JSON file in an HTML page

I've been attempting to showcase a local JSON file on an HTML page. I stumbled upon some information online, but it's causing me quite a bit of confusion. Here is the JSON file I have: { "activities": [ { "name": "C:&bs ...

What is the best way to restrict a link to only a specific div element and not a Bootstrap

My HTML conundrum involves a div styled with the class .win_box: This particular div is nested within a Bootstrap column, and the link associated with it extends throughout the entirety of said column. However, I want to restrict user access to this link ...

Animated collapse with margin effect in Material-UI

I have been utilizing the MUI-Collapse component to display collapsible content within a list. I am looking to add vertical spacing between the Collapse components in the list, but I do not want the spacing to remain when the Collapse is collapsed. I am ha ...

Having trouble making the swing effect trigger on mouseover but not on page load

I am trying to achieve a swinging effect on mouseover only, not on page load. Below is the JS code I have: (function swing() { var ang = 20, dAng = 10, ddAng = .5, dir = 1, box = document.getElementById("box"); (function setAng(ang){ ...

Styling the pseudo element ::part() on an ion-modal can be customized based on certain conditions

Looking for a solution regarding an ion-modal with specific CSS settings? I previously had the following CSS: ion-modal::part(content) { width: 300px; height: 480px; } Now, I need to adjust the height based on conditions: if A, the height should be lo ...

How to store various check-box selections as separate entries using PHP

I need help saving the results of multiple check-boxes as separate records. Unfortunately, my code is not working as expected. Can someone please assist me? <?php session_start(); $id = $_SESSION['user_id']; $db = new PDO('mysql:host=lo ...

Utilize styling only when both classes are actively used within media queries or bootstrap

Despite specifying to apply the text-align to 'md-3' instead of 'sm', it still applies when the browser is resized to use the 'sm' style. Simply put, I only want the style to be applied if the media queries interpret my devic ...

Is Material UI equipped to handle CSS Container Queries?

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Container_Queries Recently introduced container queries for CSS are an exciting development. I'm curious if MUI 5.0 already supports them out of the box. It seems that the current SxProps do not in ...

Unable to use the select function in Android 2.2

When I click on the select button in my app, it displays multiple options. This functionality works smoothly on Android 4.1, but on Android 2.2, clicking on the select button does not have any effect and therefore the options cannot be selected. <sec ...

Using JavaScript to interact with elements inside an iframe on a webpage

In a simple scenario, I am experiencing incorrect results. Code snippet from MyFrame.HTML: <!DOCTYPE html> <html> <head> <title>My Frame</title> </head> <body> <a href="https://www.google.com& ...

Clicking the submit button on a PHP contact form will prompt the opening of

My project involves creating a custom contact form using html5 and php. I have successfully designed the form as per my requirements and now I am working on validating the entered values in the fields. To check if everything is functioning correctly, I am ...

Achieving the perfect alignment for expandable divs next to a consistently centered element using CSS

On my page, I want to ensure that the logo is always perfectly centered both horizontally and vertically when the page loads without any interactions. To achieve this, I used simple margin properties: margin: auto; position: absolute; top: 0; bottom: 0; ...

Incorporate a glyphicon into a webpage design that wasn't originally built with bootstrap while maintaining the original layout

Looking to jazz up my vintage website with some cool glyphicons on a specific page. Don't want to mess up the entire look of my site by installing bootstrap. Is there a way to add just the "glyphicons functionality" without changing everything else? ...

What methods can I use to visually distinguish external links from those on my website?

Can CSS be used to show users which links are external? ...

Is there a way to access a large HTML file?

Currently, I have been utilizing a Python script to compare data and the resulting differences are saved in an HTML file. However, the size of the file has ballooned to 158 MB due to significant differences, making it impossible for me to open in any brows ...