Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make div or span elements really nest

Tags:

html

css

I have repeatedly run into this problem and did not find a solution yet.

Example 1: http://jsfiddle.net/0f5u0w6e/1/

What it Looks like:

enter image description here

What it should look like:

enter image description here

In this example the divs should behave like lines, but it is more than that: I want the divs or spans to be bounding boxes for all their Contents. (Then it is also impossible that part of their contents pushes parts of another divs content.)

Example 2: http://jsfiddle.net/gd7Losru/1/

I want the inner (red) span to make the outer (blue) div have a height such that it contains it. (again: bounding box).

copies of the code if jsFiddle goes offline: Example 1:

<div style="width: 200px;">
    <div>
        Title Title Title 
        <span class="button" 
            style="float: right; padding: 10px;">
            button</span>
    </div>
    <div>
        Config: 
        <input type="text" />
    </div>
</div>

Example 2:

<div style="background-color: blue;">
    <span style="padding: 10px; background-color:red; "></span>
</div>
like image 297
ben Avatar asked Jul 07 '15 09:07

ben


People also ask

Is it possible to nest divs within divs?

So, you can always nest divs within divs, and attach unique classes or id attributes to them so you don't get confused. The span tag is an inline element that you use to make a smaller part of content stand out with CSS or JavaScript.

When is it appropriate to use div and span elements?

It is suitable to make use of these elements whenever style considerations or other functionality requires the use of a container of some kind, but no other HTML element is appropriate to represent the meaning of the content contained. Div and span elements are simply containers that apply no meaning whatsoever to the content.

What is nesting in HTML and how to do it?

This tutorial will teach you how to nest HTML elements in order to apply multiple HTML tags to a single piece of content. HTML elements can be nested, meaning that one element can be placed inside another element. Nesting allows you to apply multiple HTML tags to a single piece of content.

What is a span element in HTML?

Using the <span> Element The span element is an inline element by default. This sets it apart from the div and section elements. The span element is often used to wrap a specific piece of content, normally text, to give it an additional "hook" that can be styled later.


1 Answers

Just clear the float on the second inner div:

<div style="width: 200px;">
    <div>
        Title Title Title 
        <span class="button" 
            style="float: right; padding: 10px;">
            button</span>
    </div>
    <div style="clear: both;">
        Config: 
        <input type="text" />
    </div>
</div>

JSFiddle

like image 169
KittMedia Avatar answered Oct 19 '22 18:10

KittMedia