Slide a floating container downward by one line

I am in search of a way to create a unique text effect resembling a "sidebox" within my writing. The goal is to have a note or heading positioned at the start of a paragraph, floating either to the left or right with some negative space surrounding it and slightly below the top similar to the layout seen in this example:

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

Simply applying a float property does not allow me to shift the note below the initial line, resulting in an incomplete look like so:

.note {
  float: right;
  margin: 0.5em 0 0.5em 0.5em;
}
<p>
<span class="note">29 July, 1814.</span>
Exhausted with sickness and fatigue, I walked over the sands with my companions to the hotel. I heard for the first time the confused buzz of voices speaking a different language from that to which I had been accustomed;</p>

Adjusting padding or margins only enlarges the box size without achieving the desired downward movement. As for absolute positioning, it eliminates reserved space and leads to overlapping elements.

Is there a method to accomplish this effect while ensuring the note precedes the paragraph?

Answer №1

shape-outside offers a solution:

.note {
  float: right;
  margin: 0 0.5em;
  padding-top:1.1em; /* set padding to match one line */
  shape-outside:inset(1.2em 0 0 0); /* creates the desired effect */
}

p {
  line-height:1.2em; /* defines line height */
}
<p>
<span class="note">29 July, 1814.</span>
Exhausted with sickness and fatigue, I walked over the sands with my companions to the hotel. I heard for the first time the confused buzz of voices speaking a different language from what I was used to;</p>

including the margin:

.note {
  float: right;
  margin: 0.5em 0 0.5em 0.5em;
  padding-top:1.2em; /* adjust padding */
  shape-outside:inset(1.2em 0 0 0); 
}

p {
  line-height:1.2em; 
}
<p>
<span class="note">29 July, 1814.</span>
Exhausted with sickness and fatigue, I walked over the sands with my companions to the hotel. I heard for the first time the confused buzz of voices speaking a different language from what I was used to;</p>

Answer №2

After exploring various options, I have found that the Shape-outside technique, as explained by Temani Afif, offers a clean and flexible solution. Despite being initially deemed impossible, there is an alternative method that can be used. By introducing a floated span preceded by a zero-width float with a specified height using a ::before pseudo-element, you can achieve the desired positioning effect. In this scenario, we utilize the height of one text line for the constructed float. Subsequently, the floated span should be configured to clear this constructed float.

.note {
  float: right;
  margin: 0.5em 0 0.5em 0.5em;
  clear: right;
}

p::before {
  float: right;
  content: '\200C';
}

body {
  width:375px;
}
<p>
<span class="note">29 July, 1814.</span>
Exhausted with sickness and fatigue, I walked over the sands with my companions to the hotel. I heard for the first time the confused buzz of voices speaking a different language from that to which I had been accustomed; and saw a costume very unlike</p>

Answer №3

Unfortunately, it may not be possible to consistently move the content down by one line or a fixed amount of pixels in all cases.

One workaround is to reposition the span element within your HTML structure, placing it later inside the p tag:

.note {
  float: right;
  margin: 0.5em 0 0.5em 0.5em;
}
<p>
Feeling exhausted and sick, I trudged across the sandy terrain with my companions towards the hotel. <span class="note">July 29, 1814.</span> For the first time, I heard the cacophony of voices speaking in an unfamiliar language;</p>

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

Is Django_compressor concealing the CSS code?

I have implemented django_compressor in my project. This is how I set it up in my template : {% load compress %} {% compress css %} <link rel="stylesheet" href="/media/css/master.css" type="text/css" charset="utf-8"> {% endcompress %} In my setti ...

The text area within the input group is misaligned with the input group button

Here is an example of how I'm using the input group feature with Bootstrap: <div class="input-group" style="padding-left: 20px;"> <input type="text" name="s" class="form-control input-sm"> <span class="input-group-btn"> ...

Changing the position of the icon in the bootstrap validator

I'm currently attempting to validate form fields in a web project. The goal is to achieve a specific result, similar to the image below: https://i.stack.imgur.com/EVeJf.png While I have made progress with a simple solution that almost meets the requi ...

When clicked, the DIV expands to fit the size of the window

Is there a way to use jQuery to expand a div to full screen when clicked, and change the content or layout of the div? I have a small div on my webpage with text or images inside, and I want it to enlarge when clicked so it fills the entire window with m ...

Steps for making a yii2 inline checkbox listWould you like to learn

I posted my issue on the yii2 forum, but unfortunately, I did not receive any help. It seems like no one knows the answer to my problem. Here is a brief overview: I want to achieve the following: <label class="checkbox-inline"><input type="chec ...

Obtain the href using a combination of xpath and id

I need to extract the href links for 9 search results from this specific website. The xpath and selectors for the first, second, and third items are as follows: '//*[@id="search-results"]/div[4]/div/ctl:cache/div[3]/div[1]/div/div[2]/div[2]/div[2]/p[ ...

Adjust the contents of a div to automatically fit within a hidden overflow div

Trying to fit the contents of a div (including an image, search bar, and three buttons stacked on top of each other) into another div with CSS styling that hides overflow has been a challenge. The CSS code for the div in question is: .jumbotron { overfl ...

Error: Attempting to assign a value to 'onclick' property of null object [Chrome Extension]

window.onload = function() { document.getElementById('item_save').onclick = function() { var item_name = document.getElementById('item_name').value; var item_size = document.getElementById('item_size').value; v ...

Retrieving PHP information in an ionic 3 user interface

We are experiencing a problem with displaying data in Ionic 3, as the data is being sourced from PHP REST. Here is my Angular code for retrieving the data: this.auth.displayinformation(this.customer_info.cid).subscribe(takecusinfo => { if(takecusi ...

Changing the content of a PHP div with an Ajax callback inside another Ajax callback?

In the index.php file, there is a script that triggers an ajax call when a header element is clicked. This call sends a $selection variable to ajax.php, which then replaces #div1 with the received HTML data. <script> $(document).ready(function(){ ...

The content within the iframe is not displayed

I've set up a dropdown menu with separate iframes for each option. Here's the code I used: $(function(){ $('#klanten-lijst').on('change',function(){ $('#klanten div').hide(); $('.klant-'+t ...

Inquiring about the best method to determine the intersection point between two lines in Draw i.o

Can someone please explain how to find the coordinate point where two lines intersect in draw i.o? I am looking for a Javascript function to solve this issue and get the coordinate of the point only. For example: ...

Set up a mouseover function for a <datalist> tag

Currently, I am delving into the world of javascript/jquery and I have set up an input field with a datalist. However, I am encountering a slight hurdle - I am unable to trigger an event when hovering over the datalist once it appears. Although I have man ...

Navigating through an array in jquery that has been populated by a php script

I am trying to access an array in jQuery that is returned by a PHP script through an Ajax call This is the Ajax call I am making to get the result from the PHP script: $.ajax({ url: "http://localhost/WCPM/index.php/demand/check_demand_ ...

How to set a default checked value in a custom DropDownList in AngularJS?

I have created a bespoke DropDownList in my HTML code. I'm looking for guidance on how to set one of the options as default within the following scenario: <div class="dropdownlistheader" ng-click="toggle('subdiv','item')"> ...

What methods does a webpage use to track views and determine when content has been seen?

I am interested in learning about the code and mechanism that detects when a specific section of a webpage has been viewed (purely for educational purposes). For instance, on websites like StackExchange it tracks when someone has thoroughly read the "abou ...

What are the steps for utilizing the skrollr library to manipulate text within a div without it being

I'm a beginner in html, css, and javascript and I'm trying to create my first one-page website using the skrollr library. However, I'm facing an issue where the content in my second div is not displaying properly and is hidden behind the fir ...

Utilizing iPad, Safari, and CSS to effortlessly fill a page with flexbox properties

Trying to create a flex layout that expands the main area to fill the entire height of the display, with a footer always at the bottom when the content is small. However, if the main content exceeds the display size, it should behave like a normal page and ...

The significance of tabindex in CSS

Is it possible to manipulate tabindex using CSS? If so, which browsers support this feature and on what types of elements? UPDATE I am interested in capturing keydown events on a div element. After researching keyboard events on http://www.quirksmode.org ...

Issue with jQuery DataTables column.width setting failing to meet expectations

Currently, I am utilizing datatables for my project. According to the datatables documentation found here, it suggests using the columns.width option to manage column width. However, when I implement columns.width and render the table, it seems to disreg ...