Showing: inline with float to the Left

I'm attempting to design a container element that will contain two types of content: items and text. The structure should follow these guidelines:

  • Background always styled with inline display (with line-breaks for background instead of blocks);

  • List items displayed separately.

Please take a look at my code example on JSFiddle: http://jsfiddle.net/BZw4A/

The hierarchy I used is as follows:

<div id="container">
    <div class="text bg">This is just a sample text to demonstrate the issue with inline text and background.</div>
    <div class="item bg">Item One</div>
    <div class="item bg">Item Two</div>
    <div class="item bg">Item Three</div>
    <div class="item bg">Item Four</div>
    <div class="item bg">Item Five</div>
</div>

As you can see in the fiddle, due to the width setting of the container, the background of the text element appears like a block rather than inline. The only solution I found was to remove these lines:

float: left;
clear: both;

When those lines are removed, the text background reverts to the desired style, but the items lose their alignment (due to the inline formatting) and appear on the same line like regular text.

I would appreciate any suggestions for solving this issue, which primarily revolves around typographic concerns.

Answer №1

To ensure that each item is displayed on its own line within the markup, you can enclose them in separate <div> tags and then style the content within with an inline element.

<div id="container">
    <div class="text"><span class="bg">This is just a demonstration text highlighting the issue with inline text and background.</span></div>
    <div class="item"><span class="bg">Item One</span></div>
    <div class="item"><span class="bg">Item Two</span></div>
    <div class="item"><span class="bg">Item Three</span></div>
    <div class="item"><span class="bg">Item Four</span></div>
    <div class="item"><span class="bg">Item Five</span></div>
</div>

In this way, float/clear CSS properties will be unnecessary.

For an illustration, visit: http://jsfiddle.net/BZw4A/3/

Answer №2

If you're looking for a creative solution, try using the :after pseudo-element to insert a line break:

.bg:after {
    content: '';
    display: block;
}

Check out this live demonstration: http://jsfiddle.net/BZw4A/4/

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

Adjusting (css styles like top, left, and width) for an external occurrence within fullcalendar

$scope.ctrlstartDragging = function(id) { var book = document.getElementById(id); var domRect = book.getBoundingClientRect(); book.style.position = 'fixed'; book.style.top = domRect.top + & ...

What is preventing the button inside my div element from responding?

I created a button inside of a moving div with a background image and added an animation to the div for movement. However, I encountered an issue where the button did not react as expected. To test this, I added another stationary button which reacted acco ...

When you hover over the image, the text will disappear

When I hover over the image of LTC, a thumbnail appears with text and a button. However, I do not want the text "LTC" to be shown when hovering over the image. <div class="col-md-4"> <div class="view view-eighth"> <div class=" ...

Highlighting table rows when hovering in IE with JQuery - compatibility across all versions

I have a table on my jsp page with multiple rows and columns. I want to ensure that visitors can easily follow along when they interact with the table - by highlighting the row or column they hover over with a different background color. Although the **hov ...

Unable to trigger click event

I apologize for asking the same question again, but I'm really struggling with this seemingly easy (or difficult?) code problem. Despite trying out Rory McCrossan's solution from here, it still doesn't seem to work for me. Can someone please ...

Issue with Bootstrap 5: Mobile Menu Failure to Expand

I’ve been struggling with this issue for days now. I’ve searched everywhere for a solution, but to no avail. That’s why I’m turning to the experts here for help :-) The problem I’m facing is that my mobile menu won’t open. Nothing happens when ...

Interactive horizontal web design with center alignment and dynamic scrolling feature

We have designed a unique web layout that is centered horizontally and includes an animated scrolling effect. There are 5 menu options that use simple anchor links to navigate between different pages. Our layout features static content with only the body s ...

Achieving striped tables in Bootstrap without the need to include the class "table table-striped"

If you're looking to add striped rows to tables using Bootstrap, check out this link. Simply adding a specific class to the HTML table element should do the trick: <table class="table table-striped"> ... </table> But what if y ...

Utilize ngClass for every individual section

I have completed the implementation of all UI components, which are visually appealing. https://i.sstatic.net/hxJQr.png Here is the data structure I am using: public filters = [ { tag: 'Year', label: 'ye ...

Retrieve the Query String Information from a Link and Generate a New Link in Another List

I am looking to extract information from a link in #list and then use that information to create a new link in #list3. The link I have is http://jsfiddle.net/4y5V6/24/. Is there a way to set it up so that when a link is clicked, it automatically gets added ...

Increased wait time during initial execution

Currently facing an issue with delaying the first run of a function. I've developed a basic slideshow that is causing problems due to this delay in the initial run. My goal is to have the first run wait for 10 seconds and then maintain a 4-second del ...

Effective Strategies for Preserving Form Input Values during Validation Failure in Spring MVC

I am currently working on validating user input, and I want the user's input fields to be retained in case of any validation errors. This is how I have set up my input fields: <form:input path="firstName" class="text short" id="firstName" value=" ...

Designing CSS for devices with specific aspect ratios below a defined threshold

My mind is malfunctioning. Is there a way to target devices with a device aspect ratio less than 16/9, meaning wider than 16/9? I can't seem to get this code working correctly. This is specifically for portrait mode within an iOS/Android cordova app ...

Can a custom structural directive be utilized under certain circumstances within Angular?

Presently, I am working with a unique custom structural directive that looks like this: <div *someDirective>. This specific directive displays a div only when certain conditions are met. However, I am faced with the challenge of implementing condit ...

Transferring Data from Python Script to Browser (with an xserver running on a Linux system)

Looking for suggestions on how to efficiently transfer data from a Python script to a web browser. The Python script, as well as the browser, are operating under an xServer environment in Linux (specifically Raspbian on Raspberry Pi). The script is respon ...

Retrieving the inner li element within the 'keyup' event listener

How can I target a specific nested li element that is being edited on 'keyup' event without using jQuery? I attempted to add an event listener for 'keyup' to all li elements, but the e.target is returning the outermost li (li#1) instea ...

What is the best way to choose an item from one div and place it into another?

I am attempting to select an anchor from one div to another using jQuery functions such as next(), siblings(), parents(), parent(), and more. Below is an example of the HTML structure: <div class="thumbs"> <div class="section"> <d ...

Automated resizing of product card images for a seamless browsing experience

I'm currently developing a CRUD application for an ecommerce platform. My main focus right now is on displaying product cards in a uniform and aligned manner, regardless of the uploaded images' original size. https://i.sstatic.net/YhsWx.png Her ...

Setting the vertical navbar height to match the footer in a HTML layout

When the screen size is larger than 768px, my layout looks like this: https://i.sstatic.net/nE4Qp.png However, I want the vertical navbar height to extend to the footer even when resizing the screen. This is the desired layout: https://i.sstatic.net/z98Uf ...

The controller and node js request associated are invisible to my HTML page

Here is my HTML code. I have just created a unique controller for a specific part of the code. <div class="mdl-grid" ng-controller="ValueController"> <div class="mdl-card mdl-shadow--4dp mdl-cell--12-col"> <div class ...