I have headings that have display:inline-block set, but one of them is really long and takes two lines. The issue is that when it breaks to the second line, it automatically makes it take the whole width of the parent container (even though it has plenty of space around it, and it is set to inline-block).
Anyone knows how to prevent this? Also, I can't really use a solution that only applies to this one if it will break other headings, because the same code is generating other 9 non-line-breaking headings using the same structure.
Thank you for your time.
CodePen: https://codepen.io/anon/pen/gGdYmB#anon-signup
<div id="parent1" class="parent">
<h2>SHORT HEADING</h2>
<h2>THE REALLY LONG HEADING THAT
<span>BREAKS TO A SECOND LINE</span></h2>
</div>
<style>
.parent {
width:50vw;
background-color:#046;
height:20vw;
text-align:center;
}
.parent h2 {
display:inline-block;
color:#fff;
font-family:sans-serif;
font-weight:900;
font-size:2vw;
background-color:#28a;
padding:10px 0;
}
.parent h2 span {
display:inline-block
}
</style>
By default, an inline-block will get pushed to the second line unless the entire element can fit on the first line. The whole group of words are a single element and are trying to be inserted into your header directly to the right of the word "That". Because the words as a group are bigger than what can fit on the first line, it puts them all on the next line, but only after expanding the parent (your h2) trying to accommodate it.
If you're ok with the text in span breaking to a new line:
Have your span display as block instead of inline-block, or switch to div instead of span. This will ensure that the text goes to a new line without first trying to expand your parent horizontally. Since an inline-block in this situation is probably going to have a line break anyways, why not do it this way?
If you want to make sure that the span doesn't break:
Then you can't style it as Inline-block or block. Inline-block won't break only as long as it's width is small enough. Don't set the display (in span) at all and your header will wrap the text and take up the entire width available. Then add a small left/right margin to your header, as the most wasted space you'll have is the width of your longest word being pushed to the next line.
You could set max-width: 80%; on your .parent h2 css (line 7) to get the desired result
You can set a max-width. See example below:
.parent {
width:50vw;
background-color:#046;
height:20vw;
text-align:center;
}
.parent h2 {
display:inline-block;
color:#fff;
font-family:sans-serif;
font-weight:900;
font-size:2vw;
background-color:#28a;
padding:10px 0;
max-width: 45vw; /* 5vw smaller then your parent width, which is 50vw */
}
.parent h2 span {display:inline-block}
<div id="parent1" class="parent">
<h2>SHORT HEADING</h2>
<h2>THE REALLY LONG HEADING THAT <span>BREAKS TO A SECOND LINE</span></h2>
</div>
Related
Heys guys. I've made a simple sample of a problem that has had me stumped for a long time - the code below has no purpose at all, it just shows the problem in a more legible way.
HTML:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="teste.css">
</head>
<body>
<div id="bar"></div>
<span>
Palavra
</span>
</body>
</html>
CSS:
#bar {
position: relative;
width: 100%;
height: 7%;
background-color: #5959AB;
color: white;
font-family: "Arial";
font-size: 150%;
font-weight: bold;
}
html, body {
height: 100%;
}
The result is:
So, I tried to make "Palavra" go up by adding a padding-bottom to it:
span {
padding-bottom: 2000px;
}
The result is:
"Palavra" just stays at the same precise position but a vertical scrolling bar appeared. It seems that "Palavra" is pushing down it's bottom part because it just can't go up from where it is.
This problem is appearing for me in so many ways that my mind is already blowing - can someone please help?
You need to give your span display: block
Then use a negative margin-top value
Example
There are 2 good ways to position the span.
You can make it display:block, and use a negative top margin as Lloyd Banks describes. The span needs to change from the default inline element to a block element because the top margin does not work with inline elements, but it does work with block elements.
From W3C "Margin properties specify the width of the margin area of a box. ... These properties apply to all elements, but vertical margins will not have any effect on non-replaced inline elements."
With this technique, you'll need a z-index on the div and span, so the span will be on top of the div, and not slide underneath it. MDN z-index article
JSFiddle Example
#bar {
position:relative;
width:100%;
height:25px;
background-color:#5959AB;
color:white;
font-family:"Arial";
font-size:150%;
font-weight:bold;
z-index:1;
}
span{
position:relative;
display:block;
margin-top:-25px;
z-index:2;
}
The second way is to absolutely position the span so it will be pulled out of the document flow, and placed at the top of its container.
JSFiddle Example
span{
position:absolute;
top:0px;
}
Adding padding to the bottom of the span will increase the size/length of the span by adding space to the bottom only. It will not push the span up from its original location, but it will push elements below it further down the page (because the span is now larger).
When you added 2000px bottom padding to the span, it was over 2000px tall, and was taller than your browser window, thus causing the scrollbar. Adding a background color to your element is a good way to see how padding and sizing work.
Here's a good detailed article from W3C on the box model including margins and padding http://www.w3.org/TR/CSS21/box.html#box-margin-area
And here is an easy article with a "Try it yourself" example: http://www.w3schools.com/css/css_boxmodel.asp
You should rather write 'Palavra' in the div itself.
<div id="bar">
<span>
Palavra
</span>
</div>
Once you close the tag the will start from the very next line.
Moreover, If you want to take the content upward you have to work with the padding-top not the bottom one ! But, this will not help you taking your content into the as starts after the .
By adding padding-bottom you are increasing the size of the CSS box as per the Box model which is worthless here!
I have a layout, where I have to make a vertical centeret, with a rotated text inside.
See fiddle: http://jsfiddle.net/C7mCc/3/
I use display:table; and display:table-cell; to make the vertical centering, which is working good.
The HTML
<div id="top-banner-advertising-text" class="v-center">
<p class="v-c-content">Annonce</p>
</div>
and the CSS
.v-center {
display: table;
}
.v-center .v-c-content {
display: table-cell;
vertical-align: middle;
}
But I only want the #top-banner-advertising-text to have a width of 15px. Like in this fiddle: http://jsfiddle.net/C7mCc/4/ where I have removed the .v-center and .v-c-content and therefore do not have the text vertical centered.
How can I control the width of the div?
This ended up being a lot more complicated than I expected. To control the width you must take into consideration your parent divs. There is an excellent explanation of this here:
100% height block with vertical text
Although this in order to help you out I went ahead and figured out how to switch this code up to swap the text to the other side of the img for you.
my jsfiddle:
http://jsfiddle.net/C7mCc/6/
To answer your questions, "How do I control the width".
This is done by taking the following lines in the css and making sure they match,
padding-left:2em; /* line-height of .wrapper div:first-child span */
width:2em; /* line-height of .wrapper div:first-child span */
height:2em; /* line-height of .wrapper div:first-child span */
line-height:2em; /* Copy to other locations */
Remember since your vertical now you must think about the padding left.
basically your line height padding left width and height come in to play.
We control them with em in order to make sure they are sized correctly.
Let me know if you need anymore help.
Sounds like you're looking for the not selector:
/* if it must not have the vertical centering class */
#top-banner-advertising-text:not(.v-center) {
width: 15px;
}
http://jsfiddle.net/C7mCc/5/
Alternately, you could leave the width as you have it in your 2nd example and add this:
#top-banner-advertising-text.v-center {
width: auto;
}
to set a width for an element displayed as table, you use : table-layout:fixed;
But you do not say that you want as well to rotate a text.
To rotate that text, you will need white-space:nowrap if more than one word (15px is really small).
to replace that text in middle, you will need translate(); and set that text in a container displayed as a table, so it expands over 15px and makes translate usable.
here an example with 2 version rotated 90 and -90 degres : http://codepen.io/gc-nomade/pen/JuAio.
For older IE, search for the old writing-mode http://msdn.microsoft.com/en-us/library/ie/ms531187%28v=vs.85%29.aspx ;) .
So I have a horizontal bar on my page and I want to put several <p> tags on that bar so that when I, later down the road, want to start to make a responsive layout, can have them removed when reaching a specific screen size. But what I want to know is how can I put these p sections on the same line without pushing one down underneath the containing section? I've used display:inline and float: left but I dont like that because you can't format the text to be text-align: right or have it be a specific distance from the left side of the screen. For example, if you put margin-left: 6% it wont be 6% away from the left side of the screen, it will be 6% away from the element before it and I don't want that. I'm assuming the overall outcome will be absolute positioning but I want to see if there were any other options first.
give display:inline-block; to your p class and a proper width and they will come in one line, having display:inline remove the option of giving dimension to the element!
do it this way :
demo here
p {
height:100%;
width:50%;
display:inline-block;
border:1px solid red;
}
display:inline-block is the key
the main difference in between inline and inline-block is that, inline-block allows you to specify the dimensions, paddings, and margins of the element whereas inline block simply wraps the element.
Try display:inline and give paddings as you like just as like this:
p{
display:inline;
padding:5px;
}
here is a demo: http://jsfiddle.net/gyHXx/
Is it possible to evenly space many elements in a div with changeable width.
Here's not working example. If we use text-align:center; elements will be centered, but margin:0 auto; is not working. I want to accomplish something like justify+center:
|..<elem>..<elem>..<elem>..<elem>..| // for one container width
|..<elem>..<elem>..<elem>..| // for smaller container width
|....<elem>....<elem>....| // even smaller container
Container will be user resizable.
One picture is worth a 1000 words:
Container(red box) width:100%; So user can resize it (browser window, js, whatever).
<--> represent even spaces.
In second row <--> are bigger because there is more room. I was able to fake it with:
text-align:center;
word-spacing:3em; // but any fixed value looses proportion
I recently read about a very clever technique to do exactly what you're asking.
In short, you just need to use text-align:justify; on the container element to achieve this, in conjunction with an extra invisible block at the end.
This works because inline-block elements are seen as being part of the text content, each being effectively a single word.
Using justify will spread out the words in your text so that they fill the entire width of the element with extra space between the words. For inline-block elements, this means that they are spaced out with even spaces between them.
I mentioned an extra invisible block at the end. This is required because normal text-align:justify won't justify the last line of text. For normal text, that's exactly what you want, but for aligning inline-block boxes, you want them all to be aligned.
The solution is to add an extra invisible but 100% width element to the end of your list of inline-block elements. This will become effectively the last line of text, and thus the justify technique will work for the rest of your blocks.
You can use the :after pseudo-selector to create the invisible element without needing to modify your markup.
Here's an updated version of your jsFiddle to demonstrate: http://jsfiddle.net/ULQwf/298/
And here's the original article that explains it in more detail: http://www.barrelny.com/blog/text-align-justify-and-rwd/
[EDIT]
One final update after seeing the image you've added to the question. (I don't have a better answer, but some additional thoughts that might be useful).
Ideally what you need here is a :last-line selector. Then you could text-align:justify the main text and text-align:center the last line. That would do what you want.
Sadly, :last-line isn't a valid selector (:first-line is, but not :last-line), so that's the end of that idea.
A slightly more hopeful thought is text-align-last, which does exist as a feature. This could do exactly what you want:
text-align:justify;
text-align-last:center;
Perfect.
Except that it's non-standard and has very limited browser support.
You can read about here on MDN.
I guess as a last resort it might be an option for you, if you can live with only partial browser support. It would at least get what you want for some of your users. But that's not really a sensible way to go.
My gut feeling though is that this as as close as you're going to get. Tantalisingly close to what you want, but just not quite there. I hope I'm proved wrong, but I'll be surprised. Too bad though, because I it would seem like a perfectly logical thing to want to do.
I worked on your example, you have to make a combination of block / inline style since the justify alone just work for inline (text).
div{
width:530px; /* I changed the div size, because you a have fixed width otherwise you should use scrolling */
border:1px red solid;
text-align:justify; /* You will justify to 100$ of containing div, if you want to "center" just add another div with % size and centered */
}
div span{ /* I worked with your example you may use a class */
width:60px;
border:1px yellow solid;
display: inline-block; /* Inline-block */
position: relative; /* relative to container div*/
}
div:before{
content: ''; /* position for block element*/
display: block; /* the block part for the last item*/
width: 100%;
}
div:after {
content: '';
display: inline-block; /* inline-block for the first (and middle elements) */
width: 100%;
}
If tried a different approach, in the fiddle looks pretty similiar to the picture but the space is fixed in both lines but the elements are intercalated.
div{
width:250px; /* I changed the div size, because you a have fixed width otherwise you should use scrolling */
border:1px red solid;
text-align:center; /* You will justify to 100$ of containing div, if you want to "center" just add another div with % size and centered */
}
div span{ /* I worked with your example you may use a class */
width:60px;
float:justify;
border:1px yellow solid;
display: inline-block; /* Inline-block */
margin-left:2%;
margin-right:2%;
}
I'm writing a style sheet, and I want to display three elements horizontally (width=33%) within a container, with a relative layout.
Here's the code:
#container
{
margin:auto;
width:85%;
padding:0;
}
#element
{
display:inline-block;
width:33.3%;
margin-left:0;
margin-right:0;
border:0px;
text-align:center;
}
I don't understand why with three elements:
<div id="container">
<div id="element">hi</div>
<div id="element">every</div>
<div id="element">one</div>
</div>
The last element is shown below the first two, while I believed that they would be drawn on the same line. There are no margins,padding or borders.
If width is set to 32%, in a Full browser window, they are on the same line (it works), but when I reduce the browser-window width, the last element falls on a new line.
Does anybody know why this happens?
These are inline blocks, so they get laid out just like characters would (think of them as really big characters, basically). And in your case you have whitespace between them, so that whitespace becomes a single space on the line between the inline-blocks in the rendering; you can see this if you put borders on them. So the space taken up by all three together ends up being "99.9% of container width plus width of two spaces in the container's font". When you reduce to 32%, then you get line-breaking once two spaces add up to more than 4% of the container width (which is why it only happens at smaller container widths).
If you really want your inline-blocks flush up against each other, take out the spaces between them.
Make you element a class (thanx Jarrett) and add float:left style to that class.
.element
{
display:inline-block;
width:33.3%;
margin-left:0;
margin-right:0;
float:left;
border:0px;
text-align:center;
}
<div id="container">
<div class="element">hi</div>
<div class="element">every</div>
<div class="element">one</div>
</div>
I recommend to play arround with the containers width.
A tip that works for me is giving them a line. Below is my contribution:
http://jsfiddle.net/8dWhF/