Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Assuming I wanted to have a in a div picture on the left side of the screen and descriptions or any other group of text on in another div the right side.
What is the benefit of wrapping both in a third container div, rather than just using the inline-block display to put them side by side?
I'll try to answer this with a simple demo. The issue with using inline-block is that each element with inline-block is treated like a character of text. This means space will be provided to the sides of and below the element. This does not work well when you need to set container elements with a width. This is a common issue with horizontal navigation when using inline-block instead of float or flexbox.
Inline
nav a {
display: inline-block;
width: 25%;
text-align: center;
background-color: rgba(255, 192, 203, 0.5);
}
<nav>
Link 1
Link 2
Link 3
Link 4
</nav>
Notice the gaps between the anchor elements. This is due to them being "inlined."
Float
nav a {
float: left;
display: inline-block;
width: 25%;
text-align: center;
background-color: rgba(255, 192, 203, 0.5);
}
<nav>
Link 1
Link 2
Link 3
Link 4
</nav>
Now the space is gone and each element is 25% the width of the parent element without the extra space being added between the elements.
This demo is another common question and it has to do with and element being inline.
.img-border {
display: inline-block;
border: 2px solid #333;
}
<span class="img-border">
<img src="http://placehold.it/100x100">
</span>
Notice the space below the image? That again is due to the element being "inlined" and treated like a text character. The space is left for what is known as a descender. Lowercase letters like g, j, y drop below the baseline of the text and the portion that does is the descender.
Not only does the extra space become a headache to deal with, it's often easier to control content in general let alone the layout of related content when it's "encapsulated" in it's own container element.
I don't know if this will help you at all but here's a try....
Inline-block to an element generates an inline-level block container. Think of the text inside a tag. They are all ‘inline’ with one another while the tag itself is a block-level container. By understanding this behavior we can use the display property to inline our content next to each other. Since all of our elements remain in the normal flow, we have no issues with a collapsed parent element. In my opinion this is a much cleaner solution which still achieves the desired result. I hope this helps! If not....my apologies.
What is the benefit of wrapping both in a third container div, rather
than just using the inline-block display to put them side by side?
None, the former is actually worse, as it will give you more markup than needed and also make a responsive layout more difficult.
There is many ways to do this and each have its pros and cons, so may I suggest to use the most modern approach, flexbox, which will give you a whole new way to make it responsive, fully dynamic, where the image can have one size and the text will take the remaining.
.parent {
display: flex
}
.parent div {
flex: 1;
margin-left: 10px;
}
<div class="parent">
<img src="http://placehold.it/150" alt="image">
<div>
Some text that might or might not match the image. Some text that might or might not match the image. Some text that might or might not match the image. Some text that might or might not match the image. Some text that might or might not match the image. Some text that might or might not match the image.
</div>
</div>
Related
Is it possible to make element absolutely positioned to bottom in its relative parent, and retain the flow of its text content from top to bottom?
This is roughly the layout I am talking about ---
<div class="relative" style="padding-bottom: 2em;">
<div style="position: absolute; bottom: 0;">Bottom! Can I have normal text flow, plz?</div>
<div style="height: 10em;"></div>
</div>
--- and here is the Fiddle: https://jsfiddle.net/x507Ljyh/
Also I am opened to any creative solutions, such as pseudoelements etc. Just no changes to orders of elements in markup, please.
Thank you!
Edit
I know, that elements with absolute position "do not take up space" and that's ok - the gray article element has generous padding-bottom because of that.
I just need the h1 element text to flow from top to bottom, as would normal element do. So the scary long title "The Idler Wheel Is Wiser Than the Driver of the Screw and Whipping Cords Will Serve You More Than Ropes Will Ever Do" would end up at the bottom end of gray article element (and, sometimes, overflow it to bottom).
This would be similar question for floats: HTML float right element order
Does that make sense?
Edit 2
Flexbox solves my issue, thanks #RoToRa! However I am still interested if flow of text content of absolute elements can be reversed to normal somehow, so I am leaving the question opened.
If you are fine only supporting newest browsers you could use flexbox layout, and change the order of the elements:
article {
display: flex;
flex-direction: column;
}
article > h1 {
order: 2;
}
article > blockquote {
order: 1;
}
https://jsfiddle.net/8pqs49qf/
Once you make an element "position: absolute;" its space is no longer taken into consideration when the page is generated. That is why your paragraph text and header text overlap. The best way to avoid this is to add padding to the bottom of your blockquote element to make the article large enough to accommodate the space the header requires.
blockquote {
padding-bottom: 100px;
}
The issue with this solution is it isn't very dynamic if you have multiple headers of different sizes.
This question already has answers here:
Make container shrink-to-fit child elements as they wrap
(4 answers)
Closed 5 months ago.
I'm playing with flexbox system in CSS and I can't figure out how to solve my problem.
If I have box with long text, which breaks to two lines, box grows to full width available and I don't want that. If text is on many lines then I want box to grow to width of the longest line. Well, my english is not so good that's why I have images to better show what I want to achieve.
This is what I have now:
And this is what I want to have:
I looked for ready solution in google (and stackoverflow) but without luck.
I prepared also JSFiddle for that: http://jsfiddle.net/f98VN/4/
Is it possible to do what I want and if yes then how can I achieve that? If not, what are your suggestions?
Code:
HTML
<div class="flex-parent">
<div class="item1"></div>
<div class="item2">text is here</div>
<div class="item1"></div>
</div>
CSS
.flex-parent {
display: flex;
justify-content: center;
width: 550px; /* it has width of the whole page, in fiddle I changed it to fixed */
align-items: center;
}
.item1 {
background: red;
height: 100px; /* it has constant width */
width: 100px;
}
.item2 { /* it's width is fluid, depends on text inside which is modified by JS */
background: pink;
font-size: 100px;
}
You may shrink middle container to the longest word using :display:table;width:1%; DEMO or use inline-block and inline-table, dropping the flexmodel : DEMO (this is it)
To keep words together you may use a non breaking character in between words you want to keep aside on same line : DEMO
Edit:
Above demos works in some browsers. In Chrome and Opera content moves to the side of the page.
Fixed version: http://codepen.io/gc-nomade/pen/izKFG
Well, the extra space appears because the whitespace break is a fake effect applied by the browser. What in fact happens, and it's hidden by the browser, is that the word is breaking between its letters, then the extra-space is actually the space that the letters really take.
You can see it by setting the text div to word-break: break-all; http://jsfiddle.net/f98VN/10/
It's not the effect you asked, but at least it doesn't leave extra space.
I got it to appear the way you want using percentages not fixed pixel width,
changed width: 550px; to width:70%;
check out this Updated fiddle
This is a common problem but I can't figure out why it happens.
I have a parent div and inside that div I have 3 divs with width set to 33% (exactly, not 33.3%!) and display: inline-block.
In Chrome it works well, but in Mozilla and Opera it does not (I didn't test it in IE yet). I thought the problem might be in the algorithm browsers use to calculate pixel sizing from percentages. But when I checked the DOM metrics, I found that the parent's width is 864px and the child's is 285px (that's correct: 864 * .33 = 285.12). But why doesn't it fit in the parent? 285 * 3 = 855, that's 9px less than parent's width!
Oh, yes, padding, margin and border for all divs set to 0 and DOM metrics confirm that.
Whitespace in the HTML source code
In the HTML source code, When you have lines of text or images, or elements that are inline-block, if there is any whitespace between them (blank spaces, tabs, or new lines), a single blank space character will be added between them when the page is rendered. For example, in the following HTML, a blank space will appear between each of the four pieces of content:
one
two
<img src="three.png"/>
<span style="display: inline-block;">four<span>
This is very helpful for lines of text, and for small images or HTML elements that appear inside a line of text. But it becomes a problem when inline-block is used for layout purposes, rather than as way to add content inside a paragraph of text.
Removing the extra space
The safest, cross-browser way to avoid the extra 4px or so of space that's added between inline-block elements, is to remove any whitespace in the HTML source code between the HTML tags.
For instance, if you have a ul with 3 floated li tags:
<-- No space, tabs, or line breaks between </li> and <li> -->
<ul>
<li>...</li><li>...</li><li>...</li>
</ul>
Unfortunately, this hurts the maintainability of the website. Besides making the code unreadable, it severely compromises the separation of data and formatting.
If another programmer comes along later and decides to put each li tag on a separate line in the source code (unaware of why the tags were on the same line, or possibly running it through HTML Tidy and not even having a chance to notice any related HTML comments), suddenly the website has a formatting bug that may be difficult to identify.
Consider floating elements instead
The whitespace behavior strongly suggests that it may be inappropriate to use inline-block for general-layout purposes, to use it for anything other than adding content inside the flow of a paragraph of text.
Plus, in some cases inline-block content is very difficult to fully style and align, especially on older browsers.
Quick summary of other solutions
Put the close tag on the same line as the next open tag, with no white space between them.
Use HTML comments to fill all of the whitespace between the close tag and the next open tag (as #Arbel suggested).
Add a negative left margin to each element (usually -3px or -4px, based on the font-size). I don't recommend this particular approach.
Set the font-size for the container element to 0 or 0.01em. This doesn't work in Safari 5 (not sure about later versions), and it may interfere with Responsive Design websites, or any website that uses a font-size unit other than px.
Remove whitespace-only text nodes from the container using JavaScript or jQuery. This doesn't work in IE8 and earlier, as text nodes are not created in those browsers when there's only whitespace between elements, though space is still added between the elements.
Set letter-spacing and word-spacing for the container (as #PhillipWills suggested). Further info. This requires standardizing em sizes on the website, which may not be a reasonable option for all websites.
Add text-space-collapse: discard; to the container (previously called white-space-collapse). Unfortunately, this CSS3 style is not yet supported by any browsers, and the standard hasn't been fully defined.
If you don't want to mess up the HTML formatting e.g. having all the elements with inline-block written in one line for future readability and also get rid of the extra white space that is added between them, you can "comment" the white space.
For example in your code this will solve the problem, it will even work with 33.3% instead of 33%:
.parent {
width: 100%;
}
.child{
display: inline-block;
width: 33.3%;
}
/\
<div class="parent">
<div class="child">bla-bla1</div><!--
--><div class="child">bla-bla2</div><!--
--><div class="child">bla-bla3</div>
</div>
A space is added between the inner divs. There is some CSS voodoo to correct this problem:
div {
letter-spacing: -.31em;
word-spacing: -.43em;
}
div div {
letter-spacing: normal;
word-spacing: normal;
}
Of course, you'll probably prefer to use classes or something to differentiate between parent and children.
Add float:left;
.parent{
width: 100%
}
.child{
float:left;
display: inline-block;
width: 33%
}
http://jsfiddle.net/H6Whc/1/
Has anyone tried display: table? If that's not a good idea, why not? This works in all modern browsers and I tested it down to IE9.
.parent{
display: table;
width: 100%;
}
.containers {
box-sizing: border-box;
border: 1px solid #000;
height: 50px;
width: 33.3%;
display: table-cell;
}
This is a mentioned by a number of comments and by #Avin, but removing display: inline-block and replacing it with float: left works.
.parent{
width: 100%
}
.child{
float:left;
width: 33%
}
This is a common problem, but it can be sorted out very easily by assigning the display: table CSS property to the parent div.
http://www.fccorp.us/index.php
The vertical column to the left is my site menu system. The column is a div with a height:100%, and the different details are div's laid over it.
The buttons are DIV's with blank buttons as backgrounds, with links on them. I have two different size buttons, the big one 60px tall and the small one 30px. Using CSS can I get the links to be centered vertically regardless of the height of the button's DIV?
I've looked here and used a few CSS sites & Android Apps. The site here suggests I can't, but I can't understand why the CSS group would not create a vertically centering function since it seems so needed.
Am I just missing something or am I really trying to get something that isn't available with CSS?
Based off your site, you can use line-height to adjust the vertical positioning of the text.
Try applying this to your 30px tall links:
line-height: 30px;
And this for the 60px tall:
line-height: 60px;
Additionally, you should not be nesting <div> tags within <a> tags.
Use this:
.menubuttonthick{line-height:60px;}
.menubuttonthin{line-height:30px;}
That will center all of your links.
On another note, currently you have the following structure:
<a href="#">
<div>text</div>
</a>
That is invalid HTML. I'm not a "HTML must be valid at all times" type of guy, but when you can fix it that easily, I think it wouldn't hurt making it valid. You should use the following:
<div>
text
</div>
Add this to your CSS. It will work regardless of the height of your buttons:
.menubar a div {
display: table-cell;
vertical-align: middle;
}
I am trying to create a liquid 3 column area within a list item. To achieve this, I use the standard method of having 3 elements, of which the first two are the sides, floated left and right, and the 3rd is the content, which should sit between the two. The following HTML works fine in Firefox, but doesn't render correctly in IE7 - the content is rendered below the two floated elements. Any ideas what the problem is?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
*
{
margin: 0;
padding: 0;
border: 0;
}
span
{
display:block;
}
.corner
{
width: 12px;
height: 12px;
}
.tl
{
float: left;
}
.tr
{
float: right;
}
.fill
{
margin: 0px 12px;
}
</style>
</head>
<body>
<ul>
<li>
<span class="tl corner">a</span>
<span class="tr corner">b</span>
<span class="fill">text text text</span>
</li>
</ul>
</body>
</html>
Note that replacing the spans with divs results in the same effect. I've also tried various DocTypes with no luck. And it works fine outside of the list, hence the problem appears to be intrinsic to using the list.
The way IE sees it, you're trying to jam in the lines one after the other, but the first two have already taken up their space, FLOATing away, which leaves the last SPAN in the LI to fight for the next level, which in IE looks like the next level below.
Since the last SPAN is not floated, that's also why it gets knocked to the next level.
Also, SPANs are inline styles, not block level elements. You should replace SPAN with DIV is you still want to try and style this in a LI element.
You should also use a full DOCTYPE so the browsers will know how to render the page. Otherwise they will default to quirks mode, ugly and all over the place.
But the better tactic is to create this float of 3 columns outside of the LI and in a DIV setting.
Have a read of CSSplay or Max Design on creating 3 column layouts.
It's a bit difficult to say anything without seeing the markup, but why don't you just put the two elements which should float inside the content element? You would have to adjust with some padding on the content element, but that should do the job. Then again, not quite sure what you mean. If you supply us with a bit more markup, it would be easier to tell.
The center block should have margins to either side allowing room for the floated blocks.
The answer is to wrap the spans in a block level element (say, a div), with overflow set to hidden. Example came from a more in depth look at the 2nd CSSplay example.