Keep last word and image on same line - html

With CSS, is there a way to break both the last word and the image to a new line at the narrower width?
http://jsfiddle.net/2koc2deo/3/
.text-two {
width: 125px;
}
.text {
font-size: 16px;
}
<div class="text text-one">
<p>Customer Service <img src="http://lorempixel.com/15/15/" alt="">
</div>
<div class="text text-two">
<p>Customer Service <img src="http://lorempixel.com/15/15/" alt="">
</div>

I added a negative right margin to the <a> elements, to match the image width.
This prevents the images from triggering a line wrap.
The line will only wrap if the text itself doesn't fit.
This works best in contexts where the right overflow of the container will still be visible.
.text {
font-size: 16px;
}
.text-two {
width: 118px;
}
.text a {
margin-right: -15px;
}
<div class="text text-one">
<p>Customer Service <img src="https://fakeimg.pl/15x15/" alt=""></p>
</div>
<div class="text text-two">
<p>Customer Service <img src="https://fakeimg.pl/15x15/" alt=""></p>
</div>
This solution is inspired by Beauceron's answer to "Attach font icon to last word in text string and prevent from wrapping"

I couldn't get the above to work. I fixed mine by putting a span around the last word and the image and use white-space: nowrap;.
Here's a fiddle:
.text-two {
width: 125px;
}
.text {
font-size: 16px;
}
.no-wrap {
white-space: nowrap;
}
<div class="text text-two">
<p>Customer <span class="no-wrap">Service <img src="http://lorempixel.com/15/15/" alt=""></span></p>
</div>

Related

How to put image right beside div element on webpage?

I'm having a hard time figuring out how to put my image right beside my paragraph. The goal is to set an image beside my text, not super close but on the same line. How can I do this in css? Below is my About.js and About.css. My paragraph is wrapped in a div called about-page. I tried taking my img tag outside that div but it causes an error. It's suppose to look like this sketch below.
import React from 'react'
import '../CSS/About.css'
import clock from '../clock.jpg'
const About = () => {
return (
<div className="about-page">
<h6 className="about-this-app-header">About This Application</h6>
<p className="description">
Do You Have A Habit of Forgetting Stuff? Don't Worry!!!
<br />
Here's <b>Reminder Me</b> to The Rescue.
</p>
<p className="description">
<b>Reminder Me</b> is a reminder management application
<br />
that lets you send reminders through text message.
<br />
It lets you set a time/duration and a message for your
<br />
reminder. And makes sure that it's sending out right on
<br />
time, not letting you or your friends miss out on the
<br />
important things in life.
</p>
<h6 className="tech-stack-header">Tech Stack</h6>
<h6 className="tech-stack">Front-End: <p className="tech">React, Materialize UI, Font Awesome</p></h6>
<h6 className="tech-stack">
Back-End: <p className="tech">Express.js, Firebase, Node.js, Twilio API</p>
</h6>
<h6 className="link-to">
Link to:
<a
href="https://github.com/jspades93?tab=repositories"
target="_blank"
rel="noopener noreferrer"
className="waves-effect waves-light btn-flat"
>
<i className="fa fa-github" style={{ fontSize: "36px" }}></i>
</a>
</h6>
<div className="clock-image"><img src={clock} alt="clock" /></div>
</div>
);
};
export default About
h6 {
font-weight: bold;
color: #056674;
}
.btn-flat {
color: black;
}
.about-this-app-header {
margin-top: 100px;
margin-left: 170px;
}
.description {
margin-top: 20px;
margin-left: 90px;
}
.tech-stack-header{
margin-top: 25px;
margin-left: 210px;
}
.tech-stack {
margin-left: 90px;
}
.link-to {
margin-left: 35px;
margin-top: 20px;
}
img {
width: 390px;
height: 250px;
float: right;
}
.tech {
display: inline;
vertical-align: top;
color: black;
font-weight: normal;
}
I wouldn't advice using CSS floats, you can use flex box but this way of work adds more divs to the DOM only to set the locations as needed.
I'd recommend using CSS grid, less DOM nodes required, less messy CSS. You can read all about it here. You have a more details expiation here or here.
Basically you would want to set a grid for the container and set the header, the left and right parts.
Something like so
.about-page {
display: grid;
grid-template-areas:
'header header'
'description image';
grid-gap: 10px;
}
.clock-image { grid-area: image; }
.header { grid-area: header; }
.about { grid-area: description; }
You should use floats like this example:
use float: left; for your text and use float: right; for your image.
.left {
float:left;
}
.right {
float:right;
}
figure img {
margin:10px;
width: 50%;
}
figcaption {
font-family:sans-serif;
font-weight:bold;
font-size:16px;
padding:10px;
}
.clear {
clear:both;
}
.centered {
margin: auto; /* margin:auto centers the figure element */
width: 50%;
background:#ccc;
text-align:center; /* text-align centers the text and image only - you need margin:auto to center the entire figure */
}
<figure> <img src="https://c1.staticflickr.com/9/8597/29295139165_94394aba37_z.jpg" width="640" height="427" alt="A_Dog Day 2016 at Andy Memorial Skate Park in Burlington, VT" class="left">
<figcaption> This is the caption for a left-floated photo. This element's width is has not been set and it will run the full width of the page. This is the caption for a left-floated photo. This element's width is has not been set and it will run the full width
of the page. </figcaption>
</figure>
<div class="clear"></div>
<figure> <img src="https://c1.staticflickr.com/9/8597/29295139165_94394aba37_z.jpg" width="640" height="427" alt="A_Dog Day 2016 at Andy Memorial Skate Park in Burlington, VT" class="right">
<figcaption> This is the caption for a right-floated photo. This div in between figures with the clear:both property clears the float from the div above it, so the next div can have a different or no float. </figcaption>
</figure>
<div class="clear"></div>
<figure class="centered"><img src="https://c1.staticflickr.com/9/8597/29295139165_94394aba37_z.jpg" width="640" height="427" alt="A_Dog Day 2016 at Andy Memorial Skate Park in Burlington, VT">
<figcaption class="centered-text"> Tip: margin:auto ONLY works if the width property is set, and not set to 100%, so the margins can push the less-than-full-width element into the middle. </figcaption>
</figure>
</div>

CSS: Two texts side by side, one fits content, the other shrinks and overflows until min-width then wraps

I have a title and a url one after the other and I want them to be side by side until they fit on the container width. When they can't fit side by side I want the url to shrink and overflow, until it reaches its min-width, then it can wrap.
Within the same container there are other elements that, if possible, I'd like to keep untouched, behaving like inline or flex items.
I tried to achieve this with flexbox, which had to have flex-wrap: wrap in order to make the subsequent elements flow, but the url would either shrink and make the date element come up on the first row (flex-basis: 0) or expand too much until it wrapped to the following line (flex-basis: some%).
I tried to use float and clear, but couldn't get it to work.
Here's what I'm trying to achieve:
desired result
Here's what I'm getting with flexbox:
url not expanding
Here's what I want when the available space is less than the url min-width:
url wrapping to next line
Here's the relevant html code:
<div class="section" data-section="header">
<h5 class="section-title" style="display: none;">Section title</h5>
<div class="field" data-field="title">
<span class="field-label" style="display: none;">Field label</span>
<span class="field-value">
Some variable length, possibly quite long text here
</span>
</div>
<div class="field" data-field="link">
<span class="field-label" style="display: none;">Link</span>
<span class="field-value">
<a href="https://www.example.com/some-path/a-variable-length-and-possibly-quite-long-url-here.html">
https://www.example.com/some-path/a-variable-length-and-possibly-quite-long-url-here.html
</a>
</span>
</div>
<div class="field" data-field="date">
<span class="field-label">Date</span>
<span class="field-value">2018-05-03 00:00:00</span>
</div>
[...]
</div>
And the current CSS:
.section[data-section=header] .field {
display: inline;
}
.section[data-section=header] .field[data-field=title] {
font-weight: 700;
font-size: 1.2em;
line-height: 0.8;
}
.section[data-section=header] .field[data-field=link] {
font-weight: 600;
font-size: 0.7em;
white-space: nowrap;
max-width: 100%;
position: relative;
}
.section[data-section=header] .field[data-field=link] .field-value {
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
max-width: calc(100% - .8em);
display: inline-block;
vertical-align: -.1em;
}
.section[data-section=header] .field[data-field=link]::before {
content: "[";
font-size: 1.2em;
}
.section[data-section=header] .field[data-field=link]::after {
content: "]\000A";
white-space: pre;
font-size: 1.2em;
}
.section[data-section=header] .field[data-field=status]::after {
content: "\000A";
white-space: pre;
}
.section[data-section=header] .field[data-field=title]::after {
content: " ";
line-height: 0px;
font-size: 2.8em;
}
Is there any way I can achieve this without resorting to edit the html layout or isolating the title and url into their own container?

Right scrollbar causes text to shrink when it is shown

I have a problem with the y-scrollbar.
My problem is that when i hover the DIV, a y-scrollbar is shown afterward, which this will makes everything in that DIV shrink a bit. How can i avoid this?
html
<div class="ht ov-h darker-bg p-r-3">
<h1 class="widget-title p-y-2 p-x-1"> Top Artists </h1>
<ul class="list-artists p-l-0">
{{#each artists as |artist|}}
<li>
<a href="#" class="link-nostyle">
<div class="row p-y-1">
<div class="col-xs-4">
<img src="{{artist.image}}" class="img-fluid img-circle">
</div>
<div class="col-off-xs-1 col-xs-7 vertical-text">
<span> {{artist.name}} </span>
</div>
</div>
</a>
</li>
{{/each}}
</ul>
</div>
css
.widget-title {
font-size: 1rem;
font-weight: bold;
text-transform: uppercase;
margin-bottom: $spacer-y;
}
.widget {
background: #E0EAEC;
}
.ov {
overflow: auto;
}
.ov-h {
overflow: hidden;
&:hover, &:focus {
overflow: auto;
}
}
.ht {
height: 100%;
}
You have a bit of options here.
1- You can set the scrollbar to be always visible, using overflow-y:scroll
2- You can use jQuery scrollbars, like 'Perfect scrollbar'
3- You can always hide the overflow while hovering, using overflow-y:hidden in your &:hover, &:focus.

CSS alignment issue with image

Take a look at this image:
http://i.gyazo.com/a5bf5097e6783d4879f12fdba0b2bbec.png
I want to get the "Test" below Profile, a BR html tag produces the above result and I don't want that.
Heres my code for this:
http://pastebin.com/raw.php?i=hW3jDrLu
<i class="fa fa-user fa-3x" style="vertical-align:middle;"></i>
<span style="font-size: 12pt;">Profile <span style="color: #979797;font-size:8pt;">Test</span></span>
I would like the "Test" to be right below the Profile header
It's tough to say without seeing more of your code, but I imagine your best bet is putting the img in one div and the text in another. Then you can have a line break in the text within its div and still have the div to the right of the img.
Here is a mock up of how it should work.
Apparently I have to add the fiddle code here as well:
HTML:
<div class="imgC"><img src="http://www.w3schools.com/tags/smiley.gif" alt="Smiley face" height="42" width="42"> </img></div>
<div class="textC"><span style="font-size: 12pt;">Profile <br></span><span style="color: #979797;font-size:8pt;">Test</span></div>
CSS:
.imgC{
display:inline-block;
float:left;
}
.textC{
display:inline-block;
float:left;
}
You may also want to preserve their block property by floating them and using the clear attribute (fiddle here):
HTML:
<div id="Container">
<img id="Icon" src="http://goo.gl/OJhO2s"/>
<div id="InfoContainer">
<span class="profile">Profile</span>
<span class="test">Test</span>
</div>
</div>
CSS:
#Icon {
width: 64px;
height: 64px;
float: left;
}
#InfoContainer {
float: left;
}
#InfoContainer span {
display: block;
clear: both;
}
.profile {
font-size: medium;
}
.test {
font-size: small;
color: #444;
}

Cannot get image to follow text in same div

I'm trying to create a line of text followed by an image on the same line of a grid with 12 columns.
For some reason image 2 is displaying in line with image 1 text and then image 2 is showing with image 1 text.
It looks like the text and image elements within the div are above/below each other. What I want is them to be side by side.
How do I resolve this? I've posed the code here
http://jsfiddle.net/Dano007/P7nzy/embedded/result/
http://jsfiddle.net/Dano007/P7nzy/
HTML
<div class="grid_6">
<p class="text">"The best selection of cheese I've ever seen! Cannot wait for our next order!"</p>
<p class="image omega"><img src="img/cheese1.jpg" alt="Contact us"></p>
</div>
<div class="grid_5">
<p class="image"><img src="img/cheese2.jpg" alt="Contact us"></p>
<p class="text omega" id="text_left">"Wow, amazing cheese selection and very fast delivery!"</p>
</div>
CSS
.text {
display:inline-block;
font-size: 3.0em;
text-align: right;
}
#text_left {text-align: left; }
.image {
display:inline-block;
text-align: center;
border:solid 4px #6b5221;
}
You can try adding a width to your text content like shown below.
.text {
display:inline-block;
font-size: 3em;
text-align: right;
width: 80%; /* added this */
}
Demo Fiddle
You can use it the way Ollie has mentioned in his answer also. It depends on how you want the appearance to be.
Like this fiddle: http://jsfiddle.net/Hive7/P7nzy/2/
What you needed to do was set the display of the text elements to inline like this:
display: inline;
Also add:
white-space: nowrap;
How about:
CSS:
div.newSection {
overflow: hidden;
}
div.floatLeft {
float: left;
margin-right: 10px;
}
img { height: 50px; }
HTML:
<body>
<div class="newSection">
<div class="floatLeft">
<img src="img/cheese1.jpg" alt="Contact us" />
</div>
<div class="floatLeft">"The best selection of cheese I've ever seen! Cannot wait for our next order!"</div>
</div>
<div class="newSection">
<div class="floatLeft">
<img src="img/cheese2.jpg" alt="Contact us" />
</div>
<div class="floatLeft">"Wow, amazing cheese selection and very fast delivery!"</div>
</div>
</body>
JSFiddle: http://jsfiddle.net/markwylde/P7nzy/6/