"Enhancing the Spacing of Elements Using Margin and Padding in a Floating

I'm facing an issue with adding margins and padding to my paragraph tag. This problem seems to be related to floating the img element to the left. I am trying to position the paragraph on the right side of the image while adding both left and right padding to the p tag so it doesn't overlap with the wrapper or the image.

<body>
<div class="wrapper">
    <div class="section">
        <img src="about.jpg">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tempus et neque non volutpat. Aliquam ut pharetra ante. Proin cursus aliquam posuere. Quisque vitae ligula a lectus feugiat elementum. Pellentesque sodales arcu sed tortor dapibus, ut ultricies nibh ultricies. Vivamus accumsan gravida hendrerit. Vivamus nec ornare velit, quis volutpat velit. Fusce consectetur sagittis metus, et molestie mauris consequat at. Cras fermentum, ligula at vestibulum eleifend, leo turpis cursus libero, non porttitor diam tortor eu turpis. Nulla sodales velit metus, sit amet consectetur leo dapibus vitae. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    </div>
</div>

*{
margin: 0;
padding: 0;
}

.wrapper {
    width: 80%;
    margin: 0 auto;
    background-color: blue;
}

.section img {
    float: left;
}

.section p {
    padding: 0 40px;
    display: inline;
    text-align: justify;
}

.section:after {
    content: "";
    display: block;
    clear: both;    
}

EDITED

The paragraph block appears to start at the wrapper instead of next to the image Inspect in Developer Tool

Answer №1

The issue you are encountering stems from the fact that you have applied the display: inline property to the .section p selector. You can resolve this by either removing it entirely or opting for display: inline-block instead. Implementing this change should address your problem. Give this code a try.

.section p {
    padding: 0 40px;
    margin-left: 100px;
    text-align: justify;
}

Answer №2

UPDATE 2019

Enhanced element positioning with FlexBox

Following a similar design as shown above, utilizing flexbox can simplify the process:

.parentFlex {
  display: flex;
}

.childflex {
  width: 30vw;
}

.secondChildflex {
  width: 70vw;
  padding: 1rem;
}
<div class="parentFlex">
  <div class="childflex"><img src="image" alt=""></div>
  <div class="secondChildflex">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Exercitationem vero iure deleniti enim ratione iste, illum facilis non magni nostrum, repellendus error placeat blanditiis, veritatis quidem consequuntur molestias deserunt necessitatibus!</div>
</div>

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 utilizing Blazor to create dynamic URLs within a loop, incorrect references may be generated

While working on a Blazor web assembly application, I encountered an issue when trying to loop over content to generate URLs for MP3 files. The media files are named "1.mp3", "2.mp3", "3.mp3", and so on. Imagine having a razor page with the following con ...

Is it possible for images to align vertically instead of horizontally in IE?

Encountering an issue with image alignment in Internet Explorer - they are aligning vertically instead of horizontally, while functioning correctly in all other browsers. Moreover, in IE8 and 7, the images are not being displayed at all. Is there a workaro ...

Steps to set a background image for an entire page in VueJS

I recently downloaded a Background Image to add to the Home Page of my VueJS application. Unfortunately, when the page loads, only a portion of the page is covered by the image. My goal is to have the entire page filled with the image, except for the Navba ...

Middle-Click JavaScript Action

On the website I'm currently working on, there is a button that uses a sprite sheet animation and therefore needs to be set as a background image. I want the button to have a slight delay when clicked, so the animation can play before redirecting to a ...

Ways to include text underneath icons using HTML and CSS

Seeking guidance on how to display text below icons on my website. Can someone please provide instructions? <div class="bottom-bar div-only-mobile" style="position: fixed; bottom: 0; width:100%"> <a href="Dashboard"> <ion-icon name= ...

Tips for creating a cursor follower that mirrors the position and size of another div when hovering over it

I am trying to create a cursor element that follows the mouse X and Y position. When this cursor hovers over a text element in the menu, I want it to change in size and position. The size of the cursor should match the width and height of the text being h ...

Launch Internet Explorer and input variable values (such as ScriptEngineMinorVersion)

I am looking to create a script that can open a file and inject values into it. Here is a basic example: Set WshShell = WScript.CreateObject("WScript.Shell") Return = WshShell.Run("iexplore.exe google.com", 1) However, I also need to modify some variab ...

What is preventing the necessary form from being submitted?

I'm creating a mandatory form for my web development class, but I'm encountering some issues. I've copied the exact code from the course materials, and I'm not sure if it's a browser problem. When I click on the register button, on ...

How can I address the problem of adjusting the title to match the size of the image in question?

I need help creating a design where an image and h2 title appear on top of the image... Below is a reference screenshot: I am looking to achieve something similar. CSS: h2 { position: relative; letter-spacing: 1px; display: inline-block; positi ...

Various forms of HTML Canvas shapes

I have been working on a HTML Canvas and after checking, I found that it has width and height attributes. However, I want my canvas to take the shape of something specific, like a Star or any other PNG image that can be converted into a canvas. I have expe ...

Using Ajax to implement a content slider with lightSlider

Seeking assistance in developing a dynamic content slider utilizing the LightSlider plugin. Currently, I have a script that fetches content from a database table (outputting JSON to HTML) and displays 6 div's of content per slide. However, I aim to di ...

Extracting the value of an HTML element from a string variable in AngularJS

I am facing an issue with my application where the content of an HTML element is received as a template from the server. I am attempting to assign this template, which is essentially a string, and have the variables within the template linked to the contro ...

Refreshed the page following a setAttribute function call in Javascript

I have successfully implemented a function to switch between light and dark mode on my webpage using VueJS. However, I am facing an issue with the code: <a @click="toggleTheme" class="switch-mode" href> <div class=&q ...

Assign a CSS class to a specific option within a SelectField in a WTForms form

Could someone explain the process of assigning a CSS class to the choices values? I am looking to customize the background of each choice with a small image. How can this be done using wtforms and CSS? class RegisterForm(Form): username = TextField( ...

Say goodbye to <!DOCTYPE html> when using htmlAgilityPack

I'm currently developing an app for WP 8.1, and one of the tasks I need to accomplish is parsing a webpage with the following structure: <!DOCTYPE html> <html> <body> <table cellspacing="0" cellpadding="0" border="0" style="b ...

The Clash of Backdrop Modals and Dropdown Menus

Looking for a solution to ensure that the datetimepicker stays in place when a user scrolls, even after opening the Modal backdrop with the "Open" button? The challenge lies in maintaining the position fixed for the modal while preventing the datetimepic ...

How can I prevent scrolling while a loader is displayed during page loading?

How can I disable scrollability on my personal website when the page is loading with the loader wrapper? I have tried using overflow: hidden; in the CSS but it didn't work. The loader is set to display for 2.5 seconds. Any suggestions on how to achiev ...

Common Error for Novice HTML/CSS Learners

I am currently enrolled in an introductory web design course on Treehouse, and I have encountered a problem with centering a class in HTML using CSS. Despite following the instructions closely, my code is not displaying correctly. Can someone please review ...

Exceeded maximum file size event in Dropzone

I am currently implementing dropzone in my form to allow users to upload images. In the event that a user selects a file that exceeds the configured limit, I want to display an alert message and remove the file. Below is my current configuration: Dropzone ...

Connecting Angular directive to a controller

While diving into some Angular JS tutorials, I decided to apply what I learned in the Ionic framework. Unfortunately, I hit a roadblock when attempting to create a reusable HTML control because the model isn't syncing with the view as expected. Here&a ...