CSS overflow to float left - css

I have a div that contains two lines. Top line is title and bottom line is name.
when the title is short enough, that the name is on the bottom line (left image)
However, when the title is long, I want the overflow to go to the second line and have the name next to it like in right image.
Could someone help me with this?
Here is the html structure:
<div class="container">
<div class="content_top">
<span class="content">Something</span>
</div>
<div class="name_top">
<span class="name">Steve</span>
</div>
</div>

CSS trick. Place the name on the next line or on the same one
I've used the :first-line pseudo-element and the word-spacing property.
If the first line is short, then the name is placed on the next line. If the first line is long, then the name is placed on the same line.
Please check the result: codepen, jsfiddle.
/* heart of the matter */
.container:first-line {
word-spacing: 240px; /* the width of the container, or more */
}
.content {
word-spacing: normal;
}
.insert {
margin-right: -11px; /* defined by the normal inter-word space */
}
/* nice look */
.container {
border: 5px solid black;
float: left;
font-family: Helvetica;
font-size: 20px;
font-weight: bold;
margin: 0 15px 30px 0;
min-height: 60px;
padding: 8px 10px;
width: 240px;
}
.name {
color: red;
}
/* little explanation */
.colored-background .content { background-color: lightblue; }
.colored-background .insert { background-color: orange; }
<div class="container">
<span class="content">Something</span>
<span class="insert"> </span>
<span class="name">Steve</span>
</div>
<div class="container">
<span class="content">Something Something Else</span>
<span class="insert"> </span>
<span class="name">Steve</span>
</div>
<div style="clear: left;"></div>
<div class="container colored-background">
<span class="content">Something</span>
<span class="insert"> </span>
<span class="name">Steve</span>
</div>
<div class="container colored-background">
<span class="content">Something Something Else</span>
<span class="insert"> </span>
<span class="name">Steve</span>
</div>

If the title is an <h1> tag for example, use CSS style:
h1 {
text-align: left;
}

Related

Fixing the Alignment of text and Article Images Using CSS

I would like the text to line up nicely with the article-images to the right.
I expect that the .watch-listen-link will have to be altered in order to change align it the right way.
I have tried adding a top-margin property to the class with no results. What should I try next?
.article-side-image{
float: left;
width: 140px;
margin-left: 8px;
margin-right:4px;
margin-top: 8px;
}
.watch-listen-link {
text-decoration: none;
color: black;
font-weight: bold;
font-size: 18px;
}
.watch-listen-link:hover{
color: #1167a8;
}
.side-article {
float: right;
width: 250px;
position: relative;
top: -13px;
}
.no-border{
border-left: none;
padding: 0;
}
.border-right{
border-right: 1px solid #CCCCCC;
}
</style>
body section:
img class="article-side-image" src="images/article3.png">
<div class= "side-article">
<p><a class= "watch-listen-link" href=""> SpaceX rocket explodes during landing </a></p>
<p> <img class="clock" src="images/Clock-image.png"> <span class= "date border-right"> 19 January 2016 </span> <br> <a class="topic-link no-border" href=""> Science & Environment </a> </p>
</div>
What you're trying to build looks a lot like a media object. This pattern is used all over the web.
You probably don't want to use float for this. More recent additions such as CSS grid or Flexbox make creating media objects way, way easier.
I adapted the recipe from the article on media objects I mentioned earlier:
.media {
display: grid;
grid-template-columns: fit-content(200px) 1fr;
grid-template-rows:1fr auto;
grid-template-areas:
"image content"
"image footer";
grid-gap: 20px;
margin-bottom: 4em;
}
.img {
grid-area: image;
}
.content {
grid-area: content;
}
<div class="media">
<div class="img">
<img src="https://mdn.github.io/css-examples/css-cookbook/balloon-sq2.jpg" alt="Balloons">
</div>
<div class="content">
<p>
<a>SpaceX rocket explodes during landing</a>
</p>
<p>
<img class="clock" src="images/Clock-image.png">
<span class= "date border-right"> 19 January 2016 </span> <br>
<a class="topic-link no-border" href=""> Science & Environment </a>
</p>
</div>

Line height is set to 0, why is there space between lines?

Short form question: Why is there vertical space between the bottom lines, and not the upper ones? I don't want the vertical space.
TL;DR
Same behavior in Firefox and Chrome, so not likely a bug, but clearly something I don't understand.
While I understand the font-size for .wrap3 is larger than the previous ones, the line-height is zero, and calculated that way according to the debugger. Also notice that although the text is 16pt, and its line-height calculates to 19.2pt, and that is very close to the font-size of wrap1. Further notice that .wrap2 uses a 40pt font... over twice as big, yet has no effect on the line spacing. But bigger than 40pt, and space appears. .wrap3 uses 60pt, which produces an obviously visible vertical space... but far less than 60pt.
I've read about struts here, another similar issue is here. And there is loads of info and several techniques for eliminating the space between inline and inline-block elements, and I've successfully used them in circumstances where that was appropriate.
Now, however, I'm facing the other case... where the space between inline elements is desired, to work in a fluid layout to provide space between elements. Although I have a <br> in the snippet, imagine far more than 4 items in the list, and varying sizes of browser windows or device screens.
What I'm trying to do, is to vary the space between inline elements, which is a quarter of the .wrapN font size, to my advantage. Spacing these items apart with padding or margin would not allow both the last item on one line to reach the very right edge of the window and also have the first item on the next line reach the very left edge of the window, although a solution using padding or margin or something else would be OK, as long as both sides of the window can be reached under appropriate circumstances.
body { margin: 0; padding: 0; }
.wrap1 { font-size: 20pt; line-height: 0; }
.wrap2 { font-size: 40pt; line-height: 0; }
.wrap3 { font-size: 60pt; line-height: 0; }
.foo2 { display: inline-block; line-height: 0; font-size: 0; }
.foo2 p {
}
.foo2 span {
font-size: 16pt;
line-height: 1.2;
display: block;
background: #ccc;
}
<div class="wrap1">
<div class="foo2"><p><span>text1</span></p></div>
<div class="foo2"><p><span>2text</span></p></div><br>
<div class="foo2"><p><span>3biggertext</span></p></div>
<div class="foo2"><p><span>4text</span></p></div>
</div>
<div class="wrap2">
<div class="foo2"><p><span>text1</span></p></div>
<div class="foo2"><p><span>2text</span></p></div><br>
<div class="foo2"><p><span>3biggertext</span></p></div>
<div class="foo2"><p><span>4text</span></p></div>
</div>
<div class="wrap3">
<div class="foo2"><p><span>text1</span></p></div>
<div class="foo2"><p><span>2text</span></p></div><br>
<div class="foo2"><p><span>3biggertext</span></p></div>
<div class="foo2"><p><span>4text</span></p></div>
</div>
If you want to remove the vertical space, change the vertical-align property to something other than the default value which is baseline (vertical-align: baseline).
.foo2 {
vertical-align: top;
}
When the value is set to baseline this vertical space is reserved for letters such as j, y, p, q.
body {
margin: 0;
padding: 0;
}
.wrap1 {
font-size: 20pt;
line-height: 0;
}
.wrap2 {
font-size: 40pt;
line-height: 0;
}
.wrap3 {
font-size: 60pt;
line-height: 0;
}
.foo2 {
display: inline-block;
line-height: 0;
font-size: 0;
vertical-align: top;
}
.foo2 p {} .foo2 span {
font-size: 16pt;
line-height: 1.2;
display: block;
background: #ccc;
}
<div class="wrap1">
<div class="foo2">
<p><span>text1</span>
</p>
</div>
<div class="foo2">
<p><span>2text</span>
</p>
</div>
<br>
<div class="foo2">
<p><span>3biggertext</span>
</p>
</div>
<div class="foo2">
<p><span>4text</span>
</p>
</div>
</div>
<div class="wrap2">
<div class="foo2">
<p><span>text1</span>
</p>
</div>
<div class="foo2">
<p><span>2text</span>
</p>
</div>
<br>
<div class="foo2">
<p><span>3biggertext</span>
</p>
</div>
<div class="foo2">
<p><span>4text</span>
</p>
</div>
</div>
<div class="wrap3">
<div class="foo2">
<p><span>text1</span>
</p>
</div>
<div class="foo2">
<p><span>2text</span>
</p>
</div>
<br>
<div class="foo2">
<p><span>3biggertext</span>
</p>
</div>
<div class="foo2">
<p><span>4text</span>
</p>
</div>
</div>
I dont know if this is quite the solution you are looking for but you could make each line a seperate wrap and then do something with padding-bottom like this
body { margin: 0; padding: 0; }
.wrap1 { font-size: 20pt; line-height: 0; padding-bottom: 10;}

How to glue :after to last word

I has this code
.cont {
width: 150px;
overflow: hidden;
resize: both;
border: solid;
}
.wrap:after {
content: 'A';
background: #ccc;
display: inline;
}
<div class="cont">
<span class="wrap">
<span class="inner">
Hello, my name is Mao
</span>
<span class="emptyornot">
</span>
</span>
</div>
http://jsfiddle.net/rcsd7L74/
I need that :after always stay with last word in .wrap.
And if container too small - break line before last word.
The CSS you have will do this perfectly well; the problem you're having is that new-lines, in HTML, collapse to a single white-space character; remove those and it works (leading to this, admittedly ugly, HTML):
<div class="cont">
<span class="wrap">
<span class="inner">
Hello, my name is Mao</span><span class="emptyornot"></span></span>
</div>
To allow for slightly prettier HTML (though, in fairness, HTML should be minimsed when sent to the client anyway), such as:
<div class="cont">
<span class="wrap">
<span class="inner">
Hello, my name is Mao</span>
<span class="emptyornot"></span>
</span>
</div>
JS Fiddle demo.
The following CSS can be used:
.wrap {
/* sets the wrapping element's font-size to 0, to hide the collapsed white-spaces: */
font-size: 0;
}
.inner {
/* overrides the parent's font-size:
font-size: 16px;
}
.wrap:after {
/* as above, to make the text of the pseudo element visible */
/* no other changes */
font-size: 16px;
}
JS Fiddle demo.
Change width:
.cont { width: 160px }

float:right property on <p> which is inside a <div> is not working

I want to right align the number which is inside the tag which is again inside the tag.
<div class="first">
<a class="background-type1" >
<p>Some text</p>
<p class="pull-right">{{=val1}}</p>
</a><a class="background-type2">
<p>SOme other text</p>
<p class="pull-right">{{=val2}}</p>
</a>
</div>
css:
.pull-right {
float: right;
}
Need Help as what should I add to css so that so that val1and val 2 get right align to right of
Thanks in advance!
You could put the values inside a <span class="pull-right"> tag, like:
HTML:
<div class="first">
<a class="background-type1">
<p>Some text
<span class="pull-right">1</span>
</p>
</a>
<a class="background-type2">
<p>Some other text
<span class="pull-right">2</span>
</p>
</a>
</div>
CSS:
.pull-right {
float: right;
}
DEMO: http://jsfiddle.net/sJQu4/1/
The floated element needs to precede or be a child of the element you want it floated next to. You would be better off doing a float:left on the first paragraph given the current source order, or just moving the <p class="pull-right"> before the other <p> in the HTML.
You can do it like this: http://jsfiddle.net/62dDV/
.pull-right { float: right; }
.first{ border: 1px solid red; }
p{ border: 1px solid silver; display: inline-block; }
a{ border: 1px solid green; display: block; }

CSS - How to line up buttons?

I am trying to create a form with multiple rows. Each row has an optional input field followed by a mandatory button. The buttons should line up vertically - something like this:
_____________ _______________
| input 1 | | button 1 |
|___________| |_____________|
_______________
| button 2 |
|_____________|
I tried to float the button left with a fixed left margin, but doing so moves the input field to the right of the button - even though the input field appears first in the markup:
<div>
<input type="text">
<button>Action 1</button>
</div>
Please see my jsfiddle here. Why is this happening and what's the correct solution?
You need thee div container to do this as shown in this jsFiddle.
HTML Code
<div class="container">
<div class="left">
<button>
</div>
<div class="right">
<button>
</div>
</div>
CSS Code
.container {
width: 190px;
height: 22px;
margin: 0;
}
.left {
float: left;
width: 95px;
height: 22px;
}
.right {
float: right;
width: 95px;
height: 22px;
}
​
Use rows.
<div class="row-rap">
<div class="right">
<input type="text">
</div>
<div class="left">
<input type="button" value="Action 1">
</div>
</div>
<div class="row-rap">
<div class="right">
<input type="text">
</div>
<div class="left">
<input type="button" value="Action 2">
</div>
</div>​
With the following styling.
div.row-rap {
width: 100%;
}
div.row-rap .right, div.row-rap .left {
width: 50%;
float: left;
}
Here's an alternative, the margins and colors may need modification. See jsfiddle link for sample result.
It has a left-aligned label and right-aligned input (button style) in a div, for each line. The non-breaking space is needed as a placeholder in the span element that represents an "empty label".
http://jsfiddle.net/qallar/kfgCb/5/
The html is:
<div class='line'>
<span class='formlabel'>label 1</span>
<input class='formbutton' type='button' value='button 1 text ' />
</div>
<div class='line'>
<span class='formlabel'> </span>
<input class='formbutton' type='button' value='button 2 text' />
</div>
and the css:
.line
{
display: block;
background-color: #ddd; /* also try #fff */
margin: 0px;
padding: 2px;
height: 30px;
width: 200px;
}
.formlabel
{
float: left;
background-color: #eee; /* also try #fff */
margin: 0px;
padding: 2px;
width: 75px;
height: 100%;
clear: both;
}
.formbutton
{
float: right;
background-color: #0f0;
margin: 0px;
padding: 0px;
width: 100px;
height: 100%;
}
The input field is flying to the right of the button because it is an inline element. Float works on block elements only, inline elements will always flow around the floated elements. This explains the behavior in the original jsFiddle.
Having said that, even if I put display:block on the input element it still behaves like inline. I was able to make the basic concept work for a div though, which is a true block element. See the jsFiddle here.
<div class="row">
<button>Action 1</button>
<div class="in"></div>
</div>
.row {
clear: both;
}
.in {
background-color: green;
height: 24px;
width: 100px;
}
button {
float: left;
margin-left: 110px;
width: 150px;
}
The only workaround seems to be the one offered by Musa (see this jsFiddle) where he aligns the buttons to the right using text-align and limiting the width of the div.
I am not a CSS expert and usually this task works for me using table
<table>
<tr><td>Optional Input</td><td>Button</td></tr>
<tr><td>Optional Input</td><td>Button</td></tr>
</table>
if table by some reason is not an option you can use div/span
<div style="display: table-row">
<span style="display: table-cell">Optional Input</span>
<span style="display: table-cell">Button</span>
</div>
It will about like this
using Block formatting context https://developer.mozilla.org/en-US/docs/CSS/Block_formatting_context
jsfiddle code: http://jsfiddle.net/EeNFH/9/
the html code:
<div class="inp">
<input type="text">
</div>
<div class="btns">
<p><button>Action 1</button></p>
<p><button>Action 2</button></p>
</div>
and the styles:
input {
width: 100px;
}
button {
width: 150px;
}
.inp{
float:left;
}
.btns{
overflow:hidden;
}
​

Resources