Can the parent element containing this floated div also have text wrapping around it?

Hello there, I've been following along with this tutorial on CSS Positioning to enhance my understanding: . Everything was going smoothly until I reached step 7 and ran into a roadblock. In that step, the text "id = div-1" is supposed to be positioned above "div-1a" which is floated left.

Upon checking out my CodePen version here: http://codepen.io/DarrenHaynes/pen/gLoYpp/, you'll notice that the text "id = div-1" aligns to the right of "div-1a". This is not the desired outcome since "div-1" is the parent of "div-1a". So I'm stuck trying to replicate the tutorial successfully in my CodePen.

Here's the code snippet from my CodePen:

HTML:

<div id="div-before">
  id = div-before
</div>
<div id="div-1">
  id = div-1
  <div id="div-1a">
    id = div-1a
    <br>
    <br> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer pretium dui sit amet felis. Integer sit amet diam. Phasellus ultrices viverra velit.
  </div>
  <div id="div-1b">
    id = div-1b<br><br> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer pretium dui sit amet felis. Integer sit amet diam. Phasellus ultrices viverra velit. Nam mattis, arcu ut bibendum commodo, magna nisi tincidunt tortor, quis accumsan
    augue ipsum id lorem.
  </div>
  <div id=div-1c>
    id = div-1c
  </div>
</div>
<div id="div-after">
    id = div-after
</div>

CSS:

#div-before,
#div-after {
  margin: 0 auto;
  width: 400px;
  font-size: 20px;
  background-color: #8888DD;
  padding: 2px 5px;
}

#div-1 {
  position: relative;
  margin: 0 auto;
  width: 400px;
  color: white;
  padding: 20px 10px 10px 10px;
  background-color: black;
}

#div-1a {
  float: left;
  width: 200px;
  padding: 3px;
  background-color: red;
}

#div-1b {
  padding: 3px;
  background-color: green;
}

#div-1c {
  padding: 3px;
  background-color: #33D;
}

Answer №1

To enclose your title for "div-1", use a 'p' tag:

<div id="div-before">
  id = div-before
</div>
<div id="div-1">
  <p>id = div-1</p>
  <div id="div-1a">
    id = div-1a
    <br>
    <br> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer pretium dui sit amet felis. Integer sit amet diam. Phasellus ultrices viverra velit.
  </div>
  <div id="div-1b">
    id = div-1b<br><br> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer pretium dui sit amet felis. Integer sit amet diam. Phasellus ultrices viverra velit. Nam mattis, arcu ut bibendum commodo, magna nisi tincidunt tortor, quis accumsan
    augue ipsum id lorem.
  </div>
  <div id=div-1c>
    id = div-1c
  </div>
</div>
<div id="div-after">
    id = div-after
</div>

CSS:

#div-before,
#div-after {
  margin: 0 auto;
  width: 400px;
  font-size: 20px;
  background-color: #8888DD;
  padding: 2px 5px;
}

#div-1 {
  position: relative;
  margin: 0 auto;
  width: 400px;
  color: white;
  padding: 20px 10px 10px 10px;
  background-color: black;
}

#div-1a {
  float: left;
  width: 200px;
  padding: 3px;
  background-color: red;
}

#div-1b {
  padding: 3px;
  background-color: green;
}

#div-1c {
  padding: 3px;
  background-color: #33D;
}

Check out the Codepen example: http://codepen.io/anon/pen/xRJNdZ

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

How can I modify the color of JavaFX text using CSS?

Looking to jazz up my JavaFX application with some custom CSS styling. Managed to link a stylesheet to my app using the following code: //Java code FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("/path/to/fxml")); Scene sce ...

What is the best way to align the 5th and 6th list items to the left using CSS?

I am experimenting with Bootstrap to create a customized navbar. I am encountering difficulties in aligning the login and logout buttons to the right. Is there a way to achieve this? I have included my HTML and CSS code below: HTML code: <body> &l ...

CSS or jQuery: Which is Better for Hiding/Showing a Div Within Another Div?

Show only class-A at the top of the page while hiding all other classes (x,x,c). Hide only class-A while showing all other classes (x,x,c). Is it possible to achieve this? <div class="x"> <div class="y"> <div class="z"&g ...

When the fullscreen modal is opened, the initial image displayed is not the same as the one that was clicked on

Seeking assistance: I've been struggling with a problem for quite some time now and could really use some help from you guys. My issue involves an 'AngularJS' webapp that retrieves posts from an 'FB page' via 'OpenGraph' ...

How to link a CSS file from a JavaScript file

I have a form for users with fields for first name, last name, password, and confirm password. I've implemented validation to check if the password and confirm password fields match using JavaScript: $(document).ready(function() { $("#addUser").cl ...

Tips for positioning two divs side by side in block formation

I'm attempting to display two distinct div elements as blocks, one for the name and the other for the names. Here is the Fiddle The names block should be displayed as a separate block next to the name div, regardless of screen responsiveness. How ca ...

Implementing the CSS link tag in the header of a NextJS 13 application for enhanced styling

In my Next 13 application, I'm looking to link directly to a basic CSS file. Unfortunately, adding it to the Head component hasn't yielded any results for me. I've also attempted to create a custom _document within both the app and pages, bu ...

Tips for aligning Picture boxes using CSS

As a newbie, I recently received training in HTML, CSS, and a bit of JavaScript. Currently, I am involved in a project where I display the latest news from an API. The technical aspects of fetching data from the API using JavaScript are handled by a senior ...

Different div elements are clashing with each other when it comes to hiding and

Having added multiple div elements with different IDs, I am facing an issue where clicking one div displays it while hiding the others. I am looking for a solution to prevent conflicts between the divs. Any suggestions on what might be causing this problem ...

unwelcome tab spacing in CSS

I am facing an issue with unwanted spacing in my lists. I have a code that generates three-column lists, each containing about eight rows. However, the first list item of the last row is causing an unwanted space. It seems to shift entirely to the next col ...

Align the last line of flex cells to the left side

I have created a flex structure that resembles a table and adjusts the number of cells based on the content. However, when the last row is not full, the remaining cells stretch to fill the empty space, which affects the overall appearance of the structure. ...

Incorporating SCSS directly in React component along with material-ui styling

I'm having trouble incorporating styles into my React component. I'd prefer to use SCSS instead of either using the withStyle function or importing a plain CSS file into a JS file. For instance, I would like to import my SCSS file into ButtonCom ...

Unused DIV elements in jQueryUI are identified using XSLT

Just a heads-up, I'm new to xslt so please bear with me if this seems like a silly question. In my xsl:template I have created a div element like this: <div id="objectLocked"> This object is locked by another user. Do you want to remove the lo ...

Expanding SVG Button Overlay in ChakraUI

I am trying to design a uniquely shaped button that sits on top of an image, but I am encountering some challenges with the scaling of the button mask. Both the image and masks were created at the same base dimensions for easy alignment at 0,0. Here is a ...

Is there a way to display the form values on the table once it has been submitted?

I'm struggling with my form input not showing up under the table headings after submission. I've reviewed the code multiple times, but can't figure out what's causing the issue. If you have any suggestions on how to write the code more ...

Trying to align a Material UI button to the right within a grid item

I'm attempting to right float the button below using material-ui, but I'm unsure of how to achieve this: <Grid item xs={2}> <Button variant="contained" color="secondary&quo ...

Is it achievable to modify the appearance of the "Saved Password" style on Firefox?

After creating a login page that initially had a certain aesthetic, I encountered an issue when saving login data in Firefox. The saved login page took on a yellow-ish tint that was not present in my original design: https://i.sstatic.net/LURG3.png I am ...

Refresh the page and witness the magical transformation as the div's background-image stylish

After browsing the internet, I came across a JavaScript script that claims to change the background-image of a div every time the page refreshes. Surprisingly, it's not functioning as expected. If anyone can provide assistance, I would greatly appreci ...

Ways to stop a background image from repeating?

<div style="background-image: url('https://i.ibb.co/v1B855w/bg15.png'); color: #000000; background-color: #000000; background-attachment: fixed; text-align: center;"><img src="https://i.ibb.co/L6zQY0S/name6.png" /> ...

Assignment of Microsoft Avatar Colors

What method does Microsoft utilize to assign colors to contacts in their applications? I've searched online with no luck in finding an answer. In programs like Outlook or Android, when a new contact is added without an image for the avatar, Microsof ...