I would like to create a div with text Some meaningful text and a heart icon behind word meaningful exactly.
I am using font awesome icon, so the icon is <i class="fas fa-heart"></i> with width.
How can I can achieve with existing DOM?
<div id="text-over-icon">
<span>Some</span>
<span>meaningful</span>
<span>text</span>
</div>
Hopefully, it can be done without float or position: absolute.
Hopefully, it can be done without float or position: absolute.
You can make use of CSS grid, and position both items on the same column/row.
The only drawback is you'll have to edit the html and nest another span to wrap the text.
[grid] {
display: inline-grid;
}
[grid]>i {
grid-row: 1/1;
grid-column: 1/1;
text-align: center;
color: red;
}
[grid]>span {
grid-row: 1/1;
grid-column: 1/1
}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.13/css/all.css">
<div id="text-over-icon">
<span>Some</span>
<span grid>
<i class="fas fa-heart"></i>
<span>meaningful</span>
</span>
<span>text</span>
</div>
Using position:absolute you can achieve this
.parent {
position: relative;
}
.fa.fa-heart {
color: #ff0;
}
.fa.fa-heart:before {
font-size: 50px;
}
#text-over-icon {
position: absolute;
top: 0;
left: 0;
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="parent">
<div class="fa fa-heart"></div>
<div id="text-over-icon">
<span>Some</span>
<span>meaningful</span>
<span>text</span>
</div>
</div>
Related
I'm trying to build a CSS selector that specifically distinguishes between elements that have a background image given as data URI:
<div style="background-image: url("data:image/svg+xml;base64,PD94b…9zdmc+");">
And elements that have a background image provided as internet URL:
<div style="background-image: url("https://my.example.com.png");">
To select data URL images only, I built this selector (escaped special characters):
.card-img[style*='background-image\: url\(\"\;data\:image'] {
display: none;
}
But unfortunately, it doesn't work:escaping the first open bracket worked, but I got stuck by escaping the ampersand.
EDIT: Since the HTML is rendered by a CMS and isn't alterable, a correct answer has to deal with the CSS!
span {
background: yellow
}
.card-img {
height: 120px;
width: calc(100% - 4rem);
display: block;
margin: 2rem;
}
.card-img[style*='background-image\: url\(\"\;data\:image'] {
display: none;
}
<div class="card-img" style="background-image: url("https://upload.wikimedia.org/wikipedia/commons/3/3b/Pyramide_von_Athribis.jpg");">
<span>Image with Internet URL</span>
</div>
<div class="card-img" style="background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAMAAADXqc3KAAAB+FBMVEUAAAA/mUPidDHiLi5Cn0XkNTPmeUrkdUg/m0Q0pEfcpSbwaVdKskg+lUP4zA/iLi3msSHkOjVAmETdJSjtYFE/lkPnRj3sWUs8kkLeqCVIq0fxvhXqUkbVmSjwa1n1yBLepyX1xxP0xRXqUkboST9KukpHpUbuvRrzrhF/ljbwaljuZFM4jELaoSdLtElJrUj1xxP6zwzfqSU4i0HYnydMtUlIqUfywxb60AxZqEXaoifgMCXptR9MtklHpEY2iUHWnSjvvRr70QujkC+pUC/90glMuEnlOjVMt0j70QriLS1LtEnnRj3qUUXfIidOjsxAhcZFo0bjNDH0xxNLr0dIrUdmntVTkMoyfL8jcLBRuErhJyrgKyb4zA/5zg3tYFBBmUTmQTnhMinruBzvvhnxwxZ/st+Ktt5zp9hqota2vtK6y9FemNBblc9HiMiTtMbFtsM6gcPV2r6dwroseLrMrbQrdLGdyKoobKbo3Zh+ynrgVllZulTsXE3rV0pIqUf42UVUo0JyjEHoS0HmsiHRGR/lmRz/1hjqnxjvpRWfwtOhusaz0LRGf7FEfbDVmqHXlJeW0pbXq5bec3fX0nTnzmuJuWvhoFFhm0FtrziBsjaAaDCYWC+uSi6jQS3FsSfLJiTirCOkuCG1KiG+wSC+GBvgyhTszQ64Z77KAAAARXRSTlMAIQRDLyUgCwsE6ebm5ubg2dLR0byXl4FDQzU1NDEuLSUgC+vr6urq6ubb29vb2tra2tG8vLu7u7uXl5eXgYGBgYGBLiUALabIAAABsElEQVQoz12S9VPjQBxHt8VaOA6HE+AOzv1wd7pJk5I2adpCC7RUcHd3d3fXf5PvLkxheD++z+yb7GSRlwD/+Hj/APQCZWxM5M+goF+RMbHK594v+tPoiN1uHxkt+xzt9+R9wnRTZZQpXQ0T5uP1IQxToyOAZiQu5HEpjeA4SWIoksRxNiGC1tRZJ4LNxgHgnU5nJZBDvuDdl8lzQRBsQ+s9PZt7s7Pz8wsL39/DkIfZ4xlB2Gqsq62ta9oxVlVrNZpihFRpGO9fzQw1ms0NDWZz07iGkJmIFH8xxkc3a/WWlubmFkv9AB2SEpDvKxbjidN2faseaNV3zoHXvv7wMODJdkOHAegweAfFPx4G67KluxzottCU9n8CUqXzcIQdXOytAHqXxomvykhEKN9EFutG22p//0rbNvHVxiJywa8yS2KDfV1dfbu31H8jF1RHiTKtWYeHxUvq3bn0pyjCRaiRU6aDO+gb3aEfEeVNsDgm8zzLy9egPa7Qt8TSJdwhjplk06HH43ZNJ3s91KKCHQ5x4sw1fRGYDZ0n1L4FKb9/BP5JLYxToheoFCVxz57PPS8UhhEpLBVeAAAAAElFTkSuQmCC");">
<span>Image with Data URL</span>
</div>
If you can change the HTML, replace the " with actual quotes, as you can use double combined with single for this sort of stuff:
span {
background-color: yellow
}
.card-img {
height: 120px;
width: calc(100% - 4rem);
display: block;
margin: 2rem;
}
.card-img[style*='background-image\: url\(\'data\:image'] span {
background-color: cyan;
}
<div class="card-img" style="background-image: url('https://upload.wikimedia.org/wikipedia/commons/3/3b/Pyramide_von_Athribis.jpg');">
<span>Image with Internet URL</span>
</div>
<div class="card-img" style="background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAMAAADXqc3KAAAB+FBMVEUAAAA/mUPidDHiLi5Cn0XkNTPmeUrkdUg/m0Q0pEfcpSbwaVdKskg+lUP4zA/iLi3msSHkOjVAmETdJSjtYFE/lkPnRj3sWUs8kkLeqCVIq0fxvhXqUkbVmSjwa1n1yBLepyX1xxP0xRXqUkboST9KukpHpUbuvRrzrhF/ljbwaljuZFM4jELaoSdLtElJrUj1xxP6zwzfqSU4i0HYnydMtUlIqUfywxb60AxZqEXaoifgMCXptR9MtklHpEY2iUHWnSjvvRr70QujkC+pUC/90glMuEnlOjVMt0j70QriLS1LtEnnRj3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QmCC');">
<span>Image with Data URL</span>
</div>
If HTML cannot be changed, one workaround could be to split your attribute selector into two:
span {
background-color: yellow
}
.card-img {
height: 120px;
width: calc(100% - 4rem);
display: block;
margin: 2rem;
}
.card-img[style*="background-image"][style*="data\:image"] span {
background-color: cyan;
}
<div class="card-img" style="background-image: url("https://upload.wikimedia.org/wikipedia/commons/3/3b/Pyramide_von_Athribis.jpg");">
<span>Image with Internet URL</span>
</div>
<div class="card-img" style="background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAMAAADXqc3KAAAB+FBMVEUAAAA/mUPidDHiLi5Cn0XkNTPmeUrkdUg/m0Q0pEfcpSbwaVdKskg+lUP4zA/iLi3msSHkOjVAmETdJSjtYFE/lkPnRj3sWUs8kkLeqCVIq0fxvhXqUkbVmSjwa1n1yBLepyX1xxP0xRXqUkboST9KukpHpUbuvRrzrhF/ljbwaljuZFM4jELaoSdLtElJrUj1xxP6zwzfqSU4i0HYnydMtUlIqUfywxb60AxZqEXaoifgMCXptR9MtklHpEY2iUHWnSjvvRr70QujkC+pUC/90glMuEnlOjVMt0j70QriLS1LtEnnRj3qUUXfIidOjsxAhcZFo0bjNDH0xxNLr0dIrUdmntVTkMoyfL8jcLBRuErhJyrgKyb4zA/5zg3tYFBBmUTmQTnhMinruBzvvhnxwxZ/st+Ktt5zp9hqota2vtK6y9FemNBblc9HiMiTtMbFtsM6gcPV2r6dwroseLrMrbQrdLGdyKoobKbo3Zh+ynrgVllZulTsXE3rV0pIqUf42UVUo0JyjEHoS0HmsiHRGR/lmRz/1hjqnxjvpRWfwtOhusaz0LRGf7FEfbDVmqHXlJeW0pbXq5bec3fX0nTnzmuJuWvhoFFhm0FtrziBsjaAaDCYWC+uSi6jQS3FsSfLJiTirCOkuCG1KiG+wSC+GBvgyhTszQ64Z77KAAAARXRSTlMAIQRDLyUgCwsE6ebm5ubg2dLR0byXl4FDQzU1NDEuLSUgC+vr6urq6ubb29vb2tra2tG8vLu7u7uXl5eXgYGBgYGBLiUALabIAAABsElEQVQoz12S9VPjQBxHt8VaOA6HE+AOzv1wd7pJk5I2adpCC7RUcHd3d3fXf5PvLkxheD++z+yb7GSRlwD/+Hj/APQCZWxM5M+goF+RMbHK594v+tPoiN1uHxkt+xzt9+R9wnRTZZQpXQ0T5uP1IQxToyOAZiQu5HEpjeA4SWIoksRxNiGC1tRZJ4LNxgHgnU5nJZBDvuDdl8lzQRBsQ+s9PZt7s7Pz8wsL39/DkIfZ4xlB2Gqsq62ta9oxVlVrNZpihFRpGO9fzQw1ms0NDWZz07iGkJmIFH8xxkc3a/WWlubmFkv9AB2SEpDvKxbjidN2faseaNV3zoHXvv7wMODJdkOHAegweAfFPx4G67KluxzottCU9n8CUqXzcIQdXOytAHqXxomvykhEKN9EFutG22p//0rbNvHVxiJywa8yS2KDfV1dfbu31H8jF1RHiTKtWYeHxUvq3bn0pyjCRaiRU6aDO+gb3aEfEeVNsDgm8zzLy9egPa7Qt8TSJdwhjplk06HH43ZNJ3s91KKCHQ5x4sw1fRGYDZ0n1L4FKb9/BP5JLYxToheoFCVxz57PPS8UhhEpLBVeAAAAAElFTkSuQmCC");">
<span>Image with Data URL</span>
</div>
So i have a HTML list where each List item has an image with a header title above it and a box behind the image that transforms on hover. I'm close to getting it to appear the way I'd like, but the list items don't seem to co-operate the way they should. I'm trying to get the box to be directly behind the image and the text to be centered above the box, but currently they are slightly misaligned. Here is a JSFiddle JSFiddle showcasing the List.
an example of one of the list items looks like this:
<li>
<a href = "" class="linkChange">
<h6 class ="headings">Appointment App</h6>
<img class="imgs" id="imgs" src = "https://img.icons8.com/clouds/2x/calendar.png"/>
<span class="box rotate" id = "box"></span>
</a>
</li>
The css is detailed in the JSFiddle. I'm slightly new to Web Dev so forgive me if some of the CSS is redundant.
Instead of using a <span> after your <img> tag, instead put your image in a <div> and apply the background to that div. Also remove the position: absolute; that you have on .imgs and .box.
<div class="box rotate" id = "box">
<img class="imgs" id="imgs" src = "https://img.icons8.com/clouds/2x/calendar.png"/>
</div>
I think you have far too much CSS going on, and many of the styles are redundant, or being overwritten by other styles. But I don't want to focus too much on that, since this isn't a code review, but rather, a solution. I did trim away styles that were unnecessary.
Here is what I changed:
Moved the h6 outside of the link, since users are likely going to click on the large target area of the image, rather than the heading text.
Added display: flex to your anchor and justified its contents to the center.
I set white-space: nowrap on the h6 so that it looks cleaner above the boxes. Here's the result.
.appsList {
list-style: none;
display: flex;
margin: 0 auto;
}
.appsList li {
position: relative;
padding: 100px;
}
.appsList .headings {
color: purple;
white-space: nowrap;
margin: 0 0 .75rem;
font-size: 1.2rem;
}
.linkChange {
display: flex;
justify-content: center;
}
.imgs {
position: absolute;
z-index: 1;
display: inline-block;
}
.box {
position: absolute;
background: #5FCF80;
width: 200px;
height: 200px;
z-index: -1;
}
/* Box comes immediately after imgs, so it can be selected to transform on hover with imgs */
#imgs:hover+#box {
transform: rotate(360deg);
background: #9351A6;
border-radius: 50%;
transition: all 1s ease-in-out;
z-index: -1;
}
.box:hover {
transform: rotate(360deg);
background: #9351A6;
border-radius: 50%;
z-index: -1;
}
.rotate {
transition: all 0.5s ease-in-out;
}
.rotate:hover {
transition: all 1s ease-in-out;
}
<ul class="appsList">
<li>
<h6 class="headings">Appointment App</h6>
<a href="" class="linkChange">
<img class="imgs" id="imgs" src="https://img.icons8.com/clouds/2x/calendar.png" />
<span class="box rotate" id="box"></span>
</a>
</li>
<li>
<h6 class="headings">Second App</h6>
<a href="" class="linkChange">
<img class="imgs" id="imgs" src="https://img.icons8.com/clouds/2x/brain.png" />
<span class="box rotate" id="box"></span>
</a>
</li>
<li>
<h6 class="headings">Banking App</h6>
<a href="" class="linkChange">
<img class="imgs" id="imgs" src="https://img.icons8.com/clouds/2x/bank-building.png" />
<span class="box rotate" id="box"></span>
</a>
</li>
<li>
<h6 class="headings">Reimburement App</h6>
<a href="" class="linkChange">
<img class="imgs" id="imgs" src="https://img.icons8.com/clouds/2x/cash-in-hand.png" />
<span class="box rotate" id="box"></span>
</a>
</li>
</ul>
jsFiddle
div class container is used to wrap all other items of the web page, I want to align this container centrally but can't seem to figure out why it isn't working.
edit: I would like the container itself center on the viewpoint and then I want to individually align the items within their own containers.
edit 2: I have figured it out, thanks for everyone's responses. I changed the HTML selector to the body, then changed the display to flex and finally justify-self to align-items.
* {
margin: 0;
padding: 0;
}
html {
display: block;
flex-direction: column;
width: 100%;
justify-self: center;
}
h1 {
text-align: center;
}
.sub {
position: absolute;
}
.container {
width: 50%;
border: 2px black solid;
box-sizing: border-box;
display: inline-flex;
position: relative;
}
<!DOCTYPE html>
<html>
<head>
<link href='style.css' rel='stylesheet'>
<link href="https://fonts.googleapis.com/css?family=Notable|Work+Sans&display=swap" rel="stylesheet">
</head>
<h1 id='main_title'>Website Style Specification - 1</h1>
<body>
<div class='container' id='color_container'>
<h2 class='sub'>Colours</h2>
<div class='color_box' id='color_one'>
<p>Coal</p>
<p>#252525</p>
<p>Background Colour</p>
</div>
<div class='color_box' id='color_two'>
<p>Blood</p>
<p>#ff0000</p>
<p>Text colour</p>
</div>
<div class='color_box' id='color_three'>
<p>Crimson</p>
<p>#af0404</p>
<p>Borders ad foreground colouring</p>
</div>
<div class='color_box' id='color_four'>
<p>Slate</p>
<p>#414141</p>
<p>Element background</p>
</div>
</div>
<div class='container' id='font_container'>
<h2 class='sub'>Fonts</h2>
<div class='font_box' id='font_1'>
<h3>Notable</h3>
<p class='normal'>This is how the text will display on the website.</p>
<p class='italic'>This is how the text will display on the website.</p>
<p class='bold'>This is how the text will display on the website.</p>
</div>
<div class='font_box' id='font_2'>
<h3>WorkSans-Regular</h3>
<p class='normal'>This is how the text will display on the website.</p>
<p class='italic'>This is how the text will display on the website.</p>
<p class='bold'>This is how the text will display on the website.</p>
</div>
</div>
<div class='container' id='text_styles'>
<h2 class='sub'>Text styles</h2>
<div class='text_box' id='text_1'>
<h3 id='sub_headings'>This is a sub heading</h3>
<ul>
<li>HTML: h2</li>
<li>font family: Notable</li>
<li>Font weight: 28px</li>
<li>Text decoration: underline</li>
</ul>
</div>
<div class='text_box' id='text_2'>
<p id='text'>
<ul>
<li>HTML: p</li>
<li>font family: Work sans</li>
<li>font weight: 18px</li>
</ul>
</p>
</div>
</div>
</body>
<footer></footer>
</html>
Are you looking for something like this?
Give the parent element of container display: flex and justify-content: center; and let flexbox do its magic. This will center the child elements on the X-Axis
* {
margin: 0;
padding: 0;
}
html {
display: block;
flex-direction: column;
width: 100%;
}
body{
width: 100%;
display: flex;
justify-content: center;
color: black;
}
h1 {
text-align: center;
}
.sub {
position: absolute;
}
.container {
width: 50%;
border: 2px black solid;
box-sizing: border-box;
text-align: center;
}
<html>
<body>
<div class="container">
HELLO IM THE CONTAINER
</div>
</body>
</html>
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;
}
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 }