As this fiddle shows, I have an outer DIV with an inner DIV on the left and two inner SPANS. I want the two SPANS to sit next to the DIV but if I separate them with a BR the outer DIV only resizes based on the width of the first SPAN. If the second SPAN is narrower than the first it sits in the correct position. If it is wider than the first it drops below the inner DIV.
CSS and HTML:
#wrapper {
border: 2px solid blue;
display: inline-block;
}
#imageContainer {
border: 1px solid red;
background-color: yellow;
display: inline-block;
height: 60px;
width: 60px;
}
.slab {
display: inline-block;
}
<div id="wrapper">
<div id="imageContainer">
Image
</div>
<span id="line1" class="slab">Sample Text</span>
<br>
<span id="line2" class="slab">Sample Text 2</span>
</div>
Your code is behaving exactly as it should have been. You have mentioned inline-block for imageContainer and other spans. That means if there is enough space within current line - that inner div imageContainer and other span will try to fit and if not enough space - span will fall to next line subsequently the last and then the first span.
Recommended approach is to structure your code properly in following manner:
<div class="outer">
<div class="img"> <!-- you can use inline-block or float (must have width) -->
Put image here
</div>
<div class="content"> <!-- you can use inline-block or float (must have width) -->
Put your spans here.
</div>
</div>
Related
I am trying to create very flexible grid, my code is approximately as
<div class="grid">
<div class="grid-row">
<td class="grid-cell">TEST</td>
<td class="grid-cell">TEST</td>
<td class="grid-cell">TEST</td>
</div>
</div>
But I have problems:
If I am using td elements inside row, then CSS class styles are not applied sometimes. Styles are applied in React environment, but are not applied in JSFiddle pure Bootstrap environment.
If I am suing div elements inside row, then those div elements are not positioned in one line but each div is in separate line inside the greater div (row) element.
So - is it possible (advisable) to use td elements inside div and without table elements? And if not, then how can I organize div elements (belonging to one row) in one row/line?
This question is related to my other question (there is the links to JSFiddle code as well):
Container div that contains scrollable table and that fills all the client area and that removes page scrolls
Use a list element and set the child elements to inline..
Set list-style-type to none to remove the bulletpoints markers.
ul{
list-style-type: none;
}
ul li{
display: inline;
}
<ul>
<li>
TEST
</li>
<li>
TEST
</li>
</ul>
Also, your div issue.. that's divs desired behaviour since a div is a block element compared to span which is an inline-block element. You should either use spans or set div to inline-block.
If you want a table layout then use display:table
.grid {
display: table;
border: 1px solid;
}
.grid-row {
display: table-row;
border: 1px solid red;
}
.grid-cell {
display: table-cell;
border: 1px solid green;
}
<div class="grid">
<div class="grid-row">
<div class="grid-cell">TEST</div>
<div class="grid-cell">TEST</div>
<div class="grid-cell">TEST</div>
</div>
<div class="grid-row">
<div class="grid-cell">TEST</div>
<div class="grid-cell">TEST Test</div>
<div class="grid-cell">TEST</div>
</div>
</div>
I have two or more divs which need to overlap each other (due some transition effects, i.e. switching tabs and slow disappear).
Below these two divs there is a footer element.
If I use position: absolute the parent element collapses and divs overlaps the footer element.
I can calculate tallest height in scripts. But maybe there is CSS way to keep parent element's height together with tallest one, but let children visually overlap?
EDIT: width of the children is unknown as well, as design is responsive.
.parent{
border: 1px solid green;
position: relative;
}
.one{
background-color: red;
position: absolute;
width: 300px;
}
.two{
background-color: blue;
position: absolute;
width: 400px;
}
footer{
border: 1px solid magenta;
text-align: right;
}
<div class="parent">
<div class="one">
Div one to overlap. Height unknown, width unknown<br>
Div one to overlap. Height unknown, width unknown<br>
Div one to overlap. Height unknown, width unknown<br>
</div>
<div class="two">
Div two to overlap. Height unknown, width unknown<br>
Div two to overlap. Height unknown, width unknown<br>
</div>
<!-- ... --->
<footer>
Footer must be below all divs
</footer>
</div>
No, this is not possible with CSS only on elements with absolute position. Have a look at the answers of this post for a detailed explanation.
if you want to achieve your goal, you can't use absolute position. Because absolutely positioned elements are taken away from the normal layout. Parent element doesn't know about its height.
you can do in the following way:
live Jsfiddle
CSS:
parent{
border: 1px solid green;
}
.one{
background-color: red;
float: left;
width: 300px;
}
.two{
margin-left:-300px;
float: left;
width: 300px;
background-color: blue;
}
footer{
clear: both;
border: 1px solid magenta;
}
HTML:
<div class="parent">
<div class="one">
Div one to overlap. Random unknown<br>
Div one to overlap. Random unknown<br>
Div one to overlap. Random unknown<br>
</div>
<div class="two">
Div two to overlap. Random unknown<br>
Div two to overlap. Random unknown<br>
</div>
<!-- ... --->
<footer>
Footer must be below all divs
</footer>
</div>
Limitations:
you have to know the widths of the divs
you have to use same width either or use left margin value as the negative value of the larger width of the previous divs.
Explanation:
Floated elements sit side by side. If you use the negative value of margin-left, it will overlay on the previous div. Then you set clear property to both for footer which makes the footer stay at the bottom all the divs.
I have 3 divs like so:
<div class="div1"></div>
<div class="div2"></div>
<div class="div3"></div>
with the following CSS:
div {
border: 1px solid black;
display: inline-block;
height: 100px;
width: 100px;
}
When the divs are empty, this code works fine. All divs align along the same horizontal plane. But! When I put any content in 1 or 2 divs, the divs with the content move down about 90% of the height:
<div class="div1">X</div>
<div class="div2">Y</div>
<div class="div3"></div>
Divs 1 and 2 are now spaced down in comparison to the normally aligned div 3. The plot really thickens when I add content to the final div:
<div class="div1">X</div>
<div class="div2">Y</div>
<div class="div3">Z</div>
Now all three divs are properly aligned at page top again. Not sure what's happening here or the proper work around?
This is happening because the default vertical-align for a inline block element is baseline*.
This image from CSS Tricks helps to demonstrate the baseline of text:
As you can see, the baseline isn't how far down the text goes, it is the line that the text is aligned on. With vertical-align:baseline, the div with no content aligns with the baseline created by the <div>'s with content.
This image may help you visualize what's happening(or, you can play with the jsfiddle):
To make all your <div>'s align, no matter the content, set vertical-align:top;:
div {
border: 1px solid black;
display: inline-block;
height: 100px;
width: 100px;
vertical-align:top;
}
This article also helps explain vertical-align some more
* W3 Specs
I am looking to take three divs and stack them horizontally (right next to each other) in a containing div. This containing div has a fluid width so that when the browser is scaled in (this is for a responsive design) the three child divs scale proportionally. In addition the three child divs have to be positioned individually so that the left-most div is on the left, the middle div is centered, and the right-most div is all the way to the right side of containing div.
I've tried to accomplish this by setting the container div to display: table and the three child divs to display: table-cell -- this works great except for I can't get the three child divs to be positioned in the way in which I described above. I tried border-spacing on the parent div; however, this doesn't work well with my goal.
Here's my Fiddle with code: http://jsfiddle.net/mkerny45/97mt7/7/
Screenshot of desired result: http://d.pr/i/KUfd
(Here you will see three child divs: left, middle, right in a containing div. The left and right divs are all the way to the left/right side respectively and center div is in center of containing div. The margin is is also depicted in the photo. I would like for the entire containing div and child divs to scale down proportionally and have the child divs always stay positioned in their appropriate location.)
Code
<div class="articles">
<article>
<img src="http://placehold.it/380x214/000000&text=Left" />
</article>
<article>
<img src="http://placehold.it/380x214/3D6AA2&text=Middle" />
</article>
<article>
<img src="http://placehold.it/380x214/98BD56&text=Right" />
</article>
</div>
You can achieve this using text-align:justify to space the div's evenly so they are flush to the edges. Then you add a span class with a width of 100% to force the width of the wrapper.
DEMO -> http://jsfiddle.net/spUKQ/2/
HTML
<div id="container">
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
<span class="stretch"></span>
</div>
CSS
#container {
border: 2px dashed #444;
height: 125px;
text-align: justify;
-ms-text-justify: distribute-all-lines;
text-justify: distribute-all-lines;
/* just for demo */
min-width: 612px;
}
.box1, .box2, .box3 {
width: 150px;
height: 125px;
vertical-align: top;
display: inline-block;
*display: inline;
zoom: 1
}
.stretch {
width: 100%;
display: inline-block;
font-size: 0;
line-height: 0
}
/* just for demo */
.box1, .box3 {
background: #ccc
}
.box2 {
background: #0ff
}
I can float an image on the left and have the text wrap around it no problem just by specifying float: left on the image. Like this:
<div id='foo'>
<img src='bar' alt='baz' style='float: left;' />
Lorem ipsum...
</div>
However if the image is wrapped in a div like the following i cannot achieve the same effect without declaring a fixed width on both the div#image_container and the div#text_container
<div id='image_container'>
<img src='blah' alt='blah' />
</div>
<div id='text_container'>
Lorem ipsum... long text
</div>
Is there a way to keep the flexibility of the first solution and avoid declaring a width and have the div#image_container float next to the div#text_container?
Try setting overflow: hidden on the wrapper div, that should automatically set the div to the width of the image.
OK maybe I misunderstood your question. Do you just want the text to flow around the image? If so, all you should need is this CSS:
#text_container { display: inline; }
#text_container,
#image_container {
display: inline;
}
should do it. Try this:
<html>
<head>
<style>
#top {
float: left;
display: inline;
border: 1px solid green;
}
#bottom {
float: right;
display: inline;
border: 1px solid red;
}
</style>
</head>
<body>
<div id="top">
I'm the top div
</div>
<div id="bottom">
I'm the bottom div
</div>
</html>
But if the content of your div's is bigger than the width you've left for them (which it probably is) then you will struggle. You should really give it a width but the above might work for you depending on how you want to use it.
Instead of the text container use a paragraph tag (<p></p>). It will wrap around the content plus it is more accessible and semantic.