How to glue :after to last word - css

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 }

Related

css not being applied to a nested class

I have this structure...
<body>
<div class="page-wrapper">
<div class="page-content">
<div class="login-welcome">
<h3 class="welcome">
<form class="form-login">
I'm able to apply css to login-welcome and welcome but not to form-login.
In Chrome Debugger, I don't see the styles I've set. These are the relevant styles...
.login-welcome {
position: absolute;
top: 15%;
left: 40%;
display: block;
}
.welcome {
font-weight: 600px;
font-size: 30px;
color: #653487;
}​
.form-login {
padding-top: 500px;
}
In debugger, I can adjust the same padding setting by adjusting element.style so I figured using form .form-login or .form-login would work but the classes I've tried has not applied any formatting to the class. Any reason why that would be the case?
When I copy your css to chrome, there is some weird character right after the closing } of .welcome
​
it seems like it's stopping chrome from interpreting the next css lines
When you remove this character the following css selectors (e.g. .form-login {) are evaluated again and will be applied to your form element - everything should work then
You can use the following:
div.login-welcome .welcome{
color:red;
}
<div class="page-wrapper">
<div class="page-content">
<div class="login-welcome">
Im not affected
<h3 class="welcome">Some heading text</h3>
<form class="form-login">
some form text
</form>
</div>
</div>
</div>
It works as follows: div.login-welcome .welcome is a CSS selector which applies styles for element which have a welcome class and a <div> parent elements which have the class login-welcome.
Hopefully this was helpful.
Your code works:
.login-welcome {
position: absolute;
top: 15%;
left: 40%;
display: block;
}
.welcome {
font-weight: 600px;
font-size: 30px;
color: #653487;
}
.form-login {
padding-top: 500px;
}
<div class="page-wrapper">
<div class="page-content">
<div class="login-welcome">
<h3 class="welcome">Welcome header</h3>
<form class="form-login">
<input type="text">
</form>
</div>
</div>
</div>
look at this fiddle: https://jsbin.com/segiqoyoci/edit?html,css,output

HTML/CSS Lining up divs

This is pretty basic CSS question. I have this as my result:
I want the name and date to be on a single line next to the menu icon
HTML:
<div class="topnav">
<span style="font-size:30px;cursor:pointer;" onclick="openNav()">☰</span>
<div class="topline">
<div id="name">John Doe</div>
<div id="date">04/27/2018</div></div>
</div>
CSS:
.topnav{
background-color: #3071a9;
color: #ffffff;
}
.topline{
padding-left: 20px;
}
#name {
float:left;
}
#date {
float:left;
}
add to your CSS :
.topline{
display: inline-block;
}
<span style="font-size:30px;cursor:pointer;" onclick="openNav()">☰</span>
This needs to be made into a div and floated left like your name and date are. You also need topnav to be of the right width (whether it's fixed or not) for everything to fit inside, otherwise it'll be pushed down.
<div style="font-size:30px;cursor:pointer;float:left;" onclick="openNav()">☰</div>
You could keep it a span by using display:inline or inline-block, but since you're floating the other divs, might as well keep it consistent. Display in CSS
Stop all the floating! Use flexbox instead:
.topnav {
display: flex;
align-items: center;
background-color: #3071a9;
color: #ffffff;
}
#name,
#date {
margin-left: 20px;
}
<div class="topnav">
<span style="font-size:30px;cursor:pointer;" onclick="openNav()">☰</span>
<div id="name">John Doe</div>
<div id="date">04/27/2018</div>
</div>
</div>

CSS overflow to float left

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;
}

Using CSS, how to add a pseudo element before every odd child element that is "outside" of that child element?

I want to create a grid with two columns whose width will be equal. My base HTML code looks like this:
<div class="linkgrid">
<div class="gridentry">
Loooooooooooooong
</div>
<div class="gridentry">
Short
</div>
<div class="gridentry">
Meeeedium
</div>
</div>
In this example, the first and the second gridentry should lie in the the first row. The thrid gridentry should lie in the second row. All gridentrys should have the same width.
~~~
I came up with a solution that uses a CSS table. However, to make sure the row "breaks" after every second cell, it currently requires non-semantic elements to force these "row breaks":
.linkgrid {
display: table;
border-spacing: 2px;
table-layout: fixed;
width: 50%;
}
.gridentry {
display: table-cell;
background-color: red;
padding: 5px;
text-align: center;
}
.gridentry a {
color: white;
}
.THIS-SHOULD-BE-A-PSEUDO-ELEMENT-BEFORE-EVERY-ODD-CHILD {
/* I imagine a selector that looks somewhat like this:
.linkgrid .gridentry:nth-child(odd):outsidebefore {
*/
display: table-row;
}
<div class="linkgrid">
<span class="THIS-SHOULD-BE-A-PSEUDO-ELEMENT-BEFORE-EVERY-ODD-CHILD"></span>
<div class="gridentry">
Loooooooooooooong
</div>
<div class="gridentry">
Short
</div>
<span class="THIS-SHOULD-BE-A-PSEUDO-ELEMENT-BEFORE-EVERY-ODD-CHILD"></span>
<div class="gridentry">
Meeeedium
</div>
</div>
Is there a way to remove my <span>s from my HTML (because they do not have any semantics) and use a clever CSS selector that adds them as pseudo elements at the right positions instead?
I do know that :before will "create" a pseudo-element within the selected element. Is there a non-JavaScript, CSS-only way to add a pseudo-element outside of the selected element like required in this example?
Another edit: For all those familiar with the Chrome developer tools, I want my result to look somewhat like this in the DOM tree:
<div class="linkgrid">
::outsidebefore
<div class="gridentry">
Loooooooooooooong
</div>
<div class="gridentry">
Short
</div>
::outsidebefore
<div class="gridentry">
Meeeedium
</div>
</div>
...where the ::outsidebefore pseudo-elements should have the CSS property display: table-row;.
Update 2016-01-04: While this specific question remains unanswered, my original problem was solved another way: https://stackoverflow.com/a/34588007/1560865
So please only post replies to this question that answer precisely the given question.
Display Level 3 introduces display: contents:
The element itself does not generate any boxes, but its children and
pseudo-elements still generate boxes as normal. For the purposes of
box generation and layout, the element must be treated as if it had
been replaced with its children and pseudo-elements in the document
tree.
Then, you can:
Wrap each cell in a container element
Set display: contents to those containers
Add ::before or ::after pseudo-elements to those containers
The result will look like as if the pseudo-elements were added to the cell, but outside it.
.wrapper {
display: contents;
}
.wrapper:nth-child(odd)::before {
content: '';
display: table-row;
}
.linkgrid {
display: table;
border-spacing: 2px;
table-layout: fixed;
width: 50%;
}
.wrapper {
display: contents;
}
.wrapper:nth-child(odd)::before {
content: '';
display: table-row;
}
.gridentry {
display: table-cell;
background-color: red;
padding: 5px;
text-align: center;
}
.gridentry a {
color: white;
}
<div class="linkgrid">
<div class="wrapper">
<div class="gridentry">
Loooooooooooooong
</div>
</div>
<div class="wrapper">
<div class="gridentry">
Short
</div>
</div>
<div class="wrapper">
<div class="gridentry">
Meeeedium
</div>
</div>
</div>
Note display: contents is not widely supported yet, but works on Firefox.
The most straightforward way is using an actual table structure. That is, one table divided into rows, in which the entries sit.
Also, you had width:50% on the table, but I believe from the question text that you meant every table cell to be 50% wide, rather than the table taking up 50% of the window width; so I corrected that.
.linkgrid {
display: table;
border-spacing: 2px;
}
.gridrow { /* new */
display: table-row;
}
.gridentry {
display: table-cell;
background-color: red;
padding: 5px;
text-align: center;
width: 50%; /* moved */
}
.gridentry a {
color: white;
}
<div class="linkgrid">
<div class="gridrow">
<div class="gridentry">
Loooooooooooooong
</div>
<div class="gridentry">
Short
</div>
</div>
<div class="gridrow">
<div class="gridentry">
Meeeedium
</div>
</div>
</div>

How can I truncate the text to better fit available space

here is some sample code http://jsfiddle.net/XpLSZ/2/
<div class="main">
<div class="item ">
<img class ="icon" src="https://www.google.com/s2/favicons?domain=http://planet.clojure.in"/>
<span class="title" >This is a test Long title</span>
<span class="count" >(22)</span>
</div>
</div>
.main{
border-style: solid;
border-width: medium;
width:200px;
}
.icon{
width:12px;
height:12px;
}
.truncate {
width: 100%;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
What I want is to have a row with icon.title and count and have the title truncate instead of wrapping, is this possible with css ? (I do not want count to be truncated)
Now I do some truncation in JS but the problem is that layout is different in some browsers and also it brakes on some zoom levels.
1) Update your markup so that count element is before the title.
2) float the count element right
FIDDLE
<div class="main">
<div class="item truncate">
<span class="count">(22)</span>
<img class="icon" src="https://www.google.com/s2/favicons?domain=http://planet.clojure.in" />
<span class="title ">This is a test Long title </span>
</div>
</div>
CSS
.count {
float:right;
}
UPDATE:
Well it seems that FF doesn't like floating the count element, instead we can absolutely position the count element to the right, and leave space for it in the parent.
Like so:
FIDDLE
.item {
padding-right:25px;
-moz-box-sizing: border-box;
box-sizing: border-box;
position:relative;
}
.count {
position:absolute;
right:0;
}

Resources