Centre a background:url image with :after pseudo element in CSS - html

In CSS, how do I,
1) use an :after pseudo element to successfully insert,
2) a background:url image after some text,
3) that is always centred whenever viewed on a wide large screen or tall small screen?
(Example images below.)
I’ve tried different things with the code below, but it always aligns the image with the left side of the text, instead of remaining centred as the screen size changes.
Appreciate some help please.
Code
div:after {
content: '';
background:url("https://ippcdn-ippawards.netdna-ssl.com/wp-content/uploads/2018/07/34-1st-PANORAMA-Mateusz-Piesiak-1.jpg");
width: 1200px;
height: 474px;
display: block;
}
Images
Not correct.
This is the current result with above code.
Correct.
Expected result with centre aligned image on wide large screen.
Correct.
Expected result with centre aligned image on tall small screen.

I was able to achieve your desired result by putting the text within a p > span tag, and applying the :after element to the outer p. Then, I gave the span element a max-width and centered it, so that the image appears to overflow the span container. See my solution:
p:first-of-type:after {
content: '';
background:url("https://ippcdn-ippawards.netdna-ssl.com/wp-content/uploads/2018/07/34-1st-PANORAMA-Mateusz-Piesiak-1.jpg");
width: 1200px;
height: 474px;
display: block;
max-width: 100%;
margin: 10px 0;
}
span {
display: block;
max-width: 70%;
margin: 0 auto;
}
<p><span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris eu ipsum eget elit egestas ultrices. Sed dui mauris, efficitur sit amet malesuada at, dictum quis elit. Pellentesque ac placerat ante. Nulla augue arcu, blandit quis nulla vitae, posuere gravida neque. Aenean leo erat, porttitor maximus nunc non, mollis ultrices dui. Maecenas consectetur eleifend ligula, quis rutrum leo suscipit id. Morbi pulvinar et est sit amet lacinia.</span></p>
<p><span>Nulla vitae magna at mi tempus cursus. Vestibulum purus purus, facilisis a lectus ac, gravida porta tellus. Integer auctor justo at tempus ultricies. Mauris ut eleifend nibh. Nullam fermentum dui in sem congue semper. Quisque eget pharetra enim. Aliquam erat volutpat. Donec nec fringilla augue, sed blandit neque. Aliquam sollicitudin, ante id accumsan fermentum, urna turpis lacinia metus, a ullamcorper enim velit vitae diam.</span></p>

Related

100% width pseudo elements don't match the width of parent when in a justified text line

As you can see in the example, when setting the width of an absolute positioned pseudo element works find normally, but if it's within an element with text-align: justify set, the pseudo element seems to be the width that the parent element would be if justify were not set.
This is evident by it not happening on links outside the paragraph, or on the last line where the text is not justified.
Anyone know how to avoid this?
It appears to only happen in Firefox, not Chrome. Any ideas for a workaround?
I found this bug report on Bugzilla.
.test {
position: relative;
background: #ccc
}
.test::before {
content: '';
position: absolute;
background: red;
height: 2px;
width: 100%;
}
p {
width: 300px; margin: auto;
text-align: justify;
}
how it should look
<p>
Lorem ipsum dolor sit amet, i am text consectetur adipiscing elit. Maecenas tristique tristique sem at molestie. Maecenas eget lacus est. i am text Aenean leo eros, eleifend id blandit sit amet, feugiat sit amet velit. Quisque i am text mattis at diam et vulputate. Pellentesque vitae nisi lacinia, tempus lorem in, dapibus sem. Phasellus auctor urna risus, eu tempus enim scelerisque at. Proin at nunc quis tellus i am text ultricies faucibus quis eu risus. Donec hendrerit venenatis orci vitae aliquet. Praesent pretium sodales nisl, id commodo nisl accumsan ac. i am text </p>

Position a fixed div below another fixed div with variable height without using jquery

I have a fixed top div with variable height. All I need is to push the bottom contents below the fixed div to re-position itself as the height of the fixed div changes in various pages.
P.S. I'm currently doing it with jquery but it takes some rendering time and shows broken contents until the page loads completely as it is added at the end of body tag. I want to load jquery and other scripts at the very end so trying to find a way to do this completely with CSS if possible for getting rid of those rendering effects.
Following is a demo code which needs to work with CSS only -
.container {
width: 100%;
height: auto;
}
.top {
position: fixed;
height: auto;
width: 100%;
background-color: black;
color: white;
top: 0;
}
.bottom {
height: auto;
width: 100%;
}
<div class="container">
<div class="top">
This is a fixed div with variable height and the bottom content are supposed to pushed and stayed below as the height increases.
</div>
<div class="bottom">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum consectetur porttitor neque at vestibulum. Nulla facilisi. Nullam tempus ligula sapien, dictum scelerisque libero tristique et. Ut sit amet magna eros. Suspendisse potenti. Donec vitae sodales nunc. Nunc eget condimentum urna. Nulla sit amet lectus ac nunc mattis porttitor eget quis purus. Ut rhoncus nulla eget velit tincidunt luctus. Donec in justo tempus, porttitor magna nec, semper eros. In bibendum magna eget lectus viverra ultricies. Integer pharetra augue lorem, eu tempus nulla volutpat dignissim.
Morbi vulputate arcu sit amet lectus porttitor hendrerit. Donec id pharetra urna, sit amet tincidunt nulla. Nam semper felis vitae odio elementum posuere. Vivamus blandit accumsan sapien, vitae blandit est lacinia et. Nam sit amet diam massa. Quisque et erat et orci dignissim congue. Maecenas pellentesque pretium sodales. Donec pellentesque rhoncus tortor et hendrerit. Phasellus nec dictum mi. Interdum et malesuada fames ac ante ipsum primis in faucibus. Fusce nec ligula mollis, iaculis est a, lobortis est. Phasellus faucibus varius arcu, eget volutpat quam venenatis vel. Sed felis nulla, pulvinar ut metus ac, luctus finibus tortor. Aliquam vulputate, nulla quis accumsan pretium, lacus elit sollicitudin ipsum, non faucibus erat mauris a felis.
</div>
</div>
try this
.bottom {
height: auto;
width: 100%;
margin-top: 1cm;
}
I have updated the fiddle and its working:https://jsfiddle.net/m0615z32/1/
Below is a pure javascript code that will work for you. Please check
What i have done is set the padding-top of below container to be equal to height of top container without using jquery.
document.getElementById("bottom-div").style.paddingTop = document.getElementById("top-div").clientHeight+"px";
OR
If You can change your top container to be relative than fixed, then also this works but is not keeping the div fixed on top
.top {
position: relative;
height: auto;
width: 100%;
background-color: black;
color: white;
top: 0;
}
Now the top content will always be on top. It will adjust according to content and below container will start after top ends.

How do you align an image with the top of some text with line height > 1 and allow the text to wrap around it?

Given the following markup and css:
<p>
<img src="" width="80" height="200"/>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras rhoncus
ante in vehicula mollis. Praesent auctor pellentesque erat, et sagittis
lorem lobortis sed. Nunc mollis pharetra massa, in dictum augue
fermentum eget. Nulla facilisi. Phasellus aliquet velit in purus
venenatis, eget tristique lacus eleifend. Proin eget magna mollis,
venenatis eros eu, semper ante. Suspendisse potenti. Duis vel facilisis
velit. Donec quis lacus commodo, consectetur tortor vel, venenatis
nisl. Vestibulum ac nisl non purus consectetur sodales.
</p>
p {
line-height: 4;
}
img {
vertical-align: text-top;
}
The img correctly aligns to the top of the text, regardless of line-height, but the text does not wrap around the img in the natural way.
If I change the css to this:
p {
line-height: 4;
}
img {
float: left;
vertical-align: text-top;
}
the text wraps, but the img no longer aligns to the top of the text.
Is there any way of doing this that will allow the img to stay aligned with the top of the text as I change line-height without needing me to change some other property at the same time (such as a negative margin or other such evils)?
Here's a jsfiddle: https://jsfiddle.net/5vk555nm/
It's not ideal, because it introduces a semantically superfluous extra parent element, but I have a solution. vertical-align: text-top aligns an image to the top of the font of the parent element. By floating the img element itself that context is lost; however, if the img itself is not floated but is inside an element that is both floated and has the same line-height, it aligns to the top of the (theoretical) font in that element and so you get the desired result:
<style>
p {
line-height: 4;
}
span {
float: left;
}
img {
vertical-align: text-top;
}
</style>
<p>
<span><img src="" width="80" height="200"/></span>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras rhoncus
ante in vehicula mollis. Praesent auctor pellentesque erat, et sagittis
lorem lobortis sed. Nunc mollis pharetra massa, in dictum augue
fermentum eget. Nulla facilisi. Phasellus aliquet velit in purus
venenatis, eget tristique lacus eleifend. Proin eget magna mollis,
venenatis eros eu, semper ante. Suspendisse potenti. Duis vel facilisis
velit. Donec quis lacus commodo, consectetur tortor vel, venenatis
nisl. Vestibulum ac nisl non purus consectetur sodales.
</p>
JSFiddle here:
https://jsfiddle.net/8urdcou0/

how can I center the image in my div? (so far the left border of my img is centered)

I have the following css code:
.tos{
width: 500px;
margin: 0 auto;
}
.tos-logo{
width: 10px;
margin: 0 auto;
}
and I want to put my logo (inside tos-logo div) in the center (horizontally). So far the left border is centered, how can I change it so the center of my image is directly in the middle?
here is my fiddle:
http://jsfiddle.net/pxzr679o/
Setting .tos-logos width makes the div 10px wide, but the <img> inside it still has its original dimensions - hence, it appears that the image is not centered. One way to fix that would be to let the .tos-logo have its original width that spans the whole screen (<div>s are block elements by default), and simply center align its text, like the following:
.tos-logo {
text-align: center;
margin: 0 auto;
}
.tos {
margin: 0 auto;
width: 500px;
}
.tos-logo {
text-align: center;
margin: 0 auto;
}
<div class="tos">
<div class="tos-logo">
<img src="http://placehold.it/100x120" alt="logologologo">
</div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam laoreet commodo magna sed blandit. Curabitur vehicula libero mi, ac molestie eros congue ut. Aliquam erat volutpat. Nam pharetra felis sapien, at porta nunc hendrerit a. Integer augue ligula,
pretium accumsan molestie nec, ornare at metus. Nunc in justo dignissim libero fringilla scelerisque ac eget tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi vitae diam eros. Vestibulum tempus elit in lorem scelerisque rhoncus.
Etiam eu erat arcu. Vivamus at turpis dui. Nulla erat est, iaculis ac leo quis, luctus auctor ex. Nulla sapien ex, egestas quis risus ac, consequat congue odio. Integer tincidunt non metus quis dapibus. Vestibulum at mauris ante. Integer vestibulum
in lectus in accumsan. Ut ligula velit, gravida in lacus in, commodo dignissim metus. Etiam luctus, orci dictum bibendum iaculis, augue dui ultricies risus, vel volutpat nisl ligula id massa. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Duis eleifend est tristique turpis lacinia, sit amet gravida neque efficitur. Duis sapien sem, faucibus vitae scelerisque sed, commodo sed lectus.</p>
</div>
</div>
.tos-logo is a block element, which means that naturally it will take the entire width of its parent. If you don't set a fixed with and don't set the margins, you can simply use text-align:center to center any inline or inline-block elements contained within the block element.
Demo
.tos{
width: 500px;
margin: 0 auto;
}
.tos-logo {
text-align: center;
}

Positioning small image on left of text?

If i do:
<img src="" style="float:left"> SOME TEXT BLA BLA BLA BLA
It doesn't work because text goes down to the image when the image height stops. I mean:
it would do this:
http://img8.imageshack.us/img8/9379/senzatitolo1yt.jpg
While what i want to get is:
http://img28.imageshack.us/img28/606/senzatitolo2rd.jpg
I could use old good table (<td>img</td><td>text</td>) but in 2011 that doesnt' seem the way to go :)
Any easy cross-browser trick to do that?
Edit: I can't know the image-width
Thanks!
Use two div tags, float them both to left. Give a width of 30% to one of them and 70% to the other. Put the image in the first one, text in the second one.
Given the simplistic html:
<img src="path/to/img.png" />
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec dui odio, luctus ut viverra vitae, dignissim a mauris. Vestibulum vel massa at sapien tincidunt venenatis id sed purus. Ut quam libero, mollis a ullamcorper sed, gravida id ligula. Sed nec augue enim. Phasellus accumsan aliquet erat interdum ullamcorper. Cras tellus libero, tincidunt non placerat interdum, venenatis id arcu. Nulla facilisi. Maecenas malesuada vestibulum venenatis. Nam vel tellus arcu. Sed non dui urna. Proin fermentum aliquet lectus non fermentum. Donec aliquet purus et tortor lobortis gravida. Duis vehicula ligula nec enim consequat ut tempor diam molestie. Aenean egestas eros sem. Phasellus ullamcorper pretium nunc molestie luctus. Mauris semper ultricies nulla, at tempus purus eleifend vel. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Maecenas ac est nunc.</p>
The following CSS works:
p {
margin-left: 100px; /* width of image plus some padding for white-space */
}
img {
float: left;
width: 90px;
height: 90px;
}
JS Fiddle demo.
Surprisingly enough the following works, albeit only tested on Chromium 8/Ubuntu 10.10:
img {
width: 100px;
height: 100px;
background-color: #f90;
float: left;
margin: 0 10px 100% 0;
}
JS Fiddle demo (ignore the colours, they were just so's I could see where things were sitting).
Second (post-edit) JS Fiddle demo, featuring an img with non-specified dimensions.
There are multiple ways to realize that.
1) two divs. assign to both a width. float the image-div to the left, the text-div to the right.
2) use margins!
give it a shot and give me feedback.