I have a div with a title, a description and a button. I want to show the title but hide the description and button until the user hovers over the div.
The problem I am having is that the description is dynamic and the number of lines can be different so you can see some of the content under the title.
.short-description-block {
position: absolute;
z-index: 1;
margin-bottom: 0;
background-color: rgba(93,179,193, .7);
align-items: center;
display: flex;
width: 100%;
transform: translateY(70%); /* push the content down but show the title */
transition: all .5s ease;
bottom: 0;
}
.block-content:hover .short-description-block {
transform: translateY(0%);
}
jsfiddle
What you could do is use the calc() css function to calculate the value in the initial translateY.
What you want is first know the height of the title. The title is an h5 html element that have this styles that impact his height (just use the inspector to get them):
h5 {
font-size: 0.83em;
margin-block-start: 1.67em;
margin-block-end: 1.67em;
}
So the height of the title will be approximatively .8em + (2 * 1.6em).
So now you want to translate .short-description-block so that just the title is visible. That can be done using:
translateY(calc(100% - (.8em + (2 * 1.6em))));
Here is the example:
.main-feature-block {
margin-bottom: 12px;
overflow: hidden;
width: 49.5%;
}
.block-content {
height: 100%;
position: relative;
}
.feature-image {
background-position: center !important;
background-size: cover !important;
padding: 52% 0;
height: 100%;
}
.short-description-block {
position: absolute;
z-index: 1;
margin-bottom: 0;
background-color: rgba(93, 179, 193, .7);
align-items: center;
display: flex;
width: 100%;
transform: translateY(calc(100% - (.8em + (2 * 1.6em))));
transition: all .5s ease;
bottom: 0;
}
.content {
padding: 0px 35px 10px;
}
.main-feature-block .block-content:hover .short-description-block {
transform: translateY(0%);
}
<div class="col-sm-6 main-feature-block">
<div class="block-content">
<div class="feature-image" style="background: url('https://via.placeholder.com/150')"></div>
<div class="short-description-block">
<div class="content">
<h5>Lorem Ipsum</h5>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus eu lacus euismod, finibus orci ut, interdum velit. Donec sit amet lorem et velit auctor commodo at non purus.</p>
<div class="btn-content">
<a class="c-btn" href="#" target="_self">Learn More</a>
</div>
</div>
</div>
</div>
</div>
<div class="col-sm-6 main-feature-block">
<div class="block-content">
<div class="feature-image" style="background: url('https://via.placeholder.com/150')"></div>
<div class="short-description-block">
<div class="content">
<h5>Lorem Ipsum</h5>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus eu lacus euismod, finibus orci ut, interdum velit. Donec sit amet lorem et velit auctor commodo at non purus.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus eu lacus euismod, finibus orci ut, interdum velit. Donec sit amet lorem et velit auctor commodo at non purus.</p>
<div class="btn-content">
<a class="c-btn" href="#" target="_self">Learn More</a>
</div>
</div>
</div>
</div>
</div>
Related
I am struggling with the setting three responsive columns in a row using Bootstrap 4.
.buttons {
border: 1px solid #e86225;
color: #e86225 !important;
padding: 10px 20px;
font-size: 14px;
}
.buttons:hover {
border: 1px solid #ffffff;
background-color: #e86225;
color: #ffffff !important;
transition: background-color 1s, border 1s, color 1s;
}
.container-custom {
padding-top: 80px;
padding-bottom: 80px;
}
.custom-link {
padding: 15px 0;
}
.container p {
margin: 25px 0;
max-width: 400px;
}
#media(max-width: 767px){
.container-custom {
padding-top: 50px;
padding-bottom: 50px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<div class="container">
<div class="row container-custom justify-content-center">
<h2>Blog</h2>
<div class='d-flex flex-wrap pt-5'>
<div class="col-sm-12 col-md-4">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSHBMzMRL0g3ELio_DdCRvdajdF812AQPP2AbmM_jYr_66CnwgS" alt="">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nibh sem, ultricies sit amet tellus ut, iaculis interdum ante. Quisque at nibh ac diam faucibus congue.
</p>
<div class="custom-link">
Read more >
</div>
</div>
<div class="col-sm-12 col-md-4">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQh3Afn3A1FKb24tpO6eAdqr8hCdnafjqwCNqgjdSAWK1igoWfk" alt="">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nibh sem, ultricies sit amet tellus ut, iaculis interdum ante. Quisque at nibh ac diam faucibus congue.
</p>
<div class="custom-link">
Read more >
</div>
</div>
<div class="col-sm-12 col-md-4">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQFsb6TxovKdTNx-8gdo1R01nZXxRRauett2KQ_ci76VgjK2hR7" alt="">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nibh sem, ultricies sit amet tellus ut, iaculis interdum ante. Quisque at nibh ac diam faucibus congue.
</p>
<div class="custom-link">
Read more >
</div>
</div>
</div>
</div>
The problem is when media screen width is smaller than 767px, they are going to the left. I want to make image in the middle of column, the beginning of the text should be at the beginning of the imaeg and the end of text at the end of image, button should be at the beginning of the image too like here:
enter image description here
Just add width:100% to your images so they take all the space on the column
.buttons {
border: 1px solid #e86225;
color: #e86225 !important;
padding: 10px 20px;
font-size: 14px;
}
.buttons:hover {
border: 1px solid #ffffff;
background-color: #e86225;
color: #ffffff !important;
transition: background-color 1s, border 1s, color 1s;
}
.container-custom {
padding-top: 80px;
padding-bottom: 80px;
}
.custom-link {
padding: 15px 0;
}
.container p {
margin: 25px 0;
max-width: 400px;
}
img{
width:100%;
}
#media(max-width: 767px){
.container-custom {
padding-top: 50px;
padding-bottom: 50px;
}
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<div class="container">
<div class="row container-custom justify-content-center">
<h2>Blog</h2>
<div class='d-flex flex-wrap pt-5'>
<div class="col-sm-12 col-md-4">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSHBMzMRL0g3ELio_DdCRvdajdF812AQPP2AbmM_jYr_66CnwgS" alt="">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nibh sem, ultricies sit amet tellus ut, iaculis interdum ante. Quisque at nibh ac diam faucibus congue.
</p>
<div class="custom-link">
Read more >
</div>
</div>
<div class="col-sm-12 col-md-4">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQh3Afn3A1FKb24tpO6eAdqr8hCdnafjqwCNqgjdSAWK1igoWfk" alt="">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nibh sem, ultricies sit amet tellus ut, iaculis interdum ante. Quisque at nibh ac diam faucibus congue.
</p>
<div class="custom-link">
Read more >
</div>
</div>
<div class="col-sm-12 col-md-4">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQFsb6TxovKdTNx-8gdo1R01nZXxRRauett2KQ_ci76VgjK2hR7" alt="">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque nibh sem, ultricies sit amet tellus ut, iaculis interdum ante. Quisque at nibh ac diam faucibus congue.
</p>
<div class="custom-link">
Read more >
</div>
</div>
</div>
</div>
The image width could be the issue. Add img { width: 100%; } and class="img-fluid" to your img tags.
Add to css:
img { width: 100%; }
HTML:
<img class="img-fluid" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQh3Afn3A1FKb24tpO6eAdqr8hCdnafjqwCNqgjdSAWK1igoWfk" alt="">
or something similar so the images look like the layout in your attached image.
Example codepen using your code: https://codepen.io/brooksrelyt/pen/MZdLYj
What is the best or right way to vertically align small absolutely positioned ::before content so that its baseline lines up with the parent’s text?
In this snippet, I want the “XL” to have the same baseline as the “Lorem ipsum”. Just tweaking top: would be too fragile.
body {margin: 0 30px; position: relative}
p {background-color: lightblue}
p::before {content: "XL"; font-size: 75%; position: absolute; right: 100%; background-color: lightgray}
<body>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in dictum purus. Etiam accumsan quam et turpis elementum, in tempor. </p>
</body>
Correct answer: use line-height and use em.
You've come across a classic example when you want to use the text baseline as transform-origin of your element. I recommend this reading to understand limitations.
Also, in your current solution you're relying on the width of the closest relatively positioned ancestor to move the :before. You don't really need that. You can safely leave it at it's current position (top left corner of parent) and simply move it 100% of its own width towards the left:
document.querySelector('input[type="range"]').addEventListener('input', function(){
document.body.style.fontSize = this.value + 'px'
})
body {
padding: 24px 0 0 1em;
font-size: 18px;
}
p {
margin-top: 0;
background-color: lightblue;
}
p::before {
content: "XL";
position: absolute;
transform: translateX(-100%);
background-color: lightgray;
font-size: 75%;
line-height: 1.75em;
}
input[type=range] {
width: 80vw;
left: 10vw;
top: 0;
position: absolute;
}
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in dictum purus. Etiam accumsan quam et turpis elementum, in tempor. </p>
<input type="range" value="18" step=".01" min="10" max="84">
Initial answer (when I believed you simply want to align to bottom):
You need to give the parent position:relative; and child: bottom: 0:
body {padding: 0 30px; }
p {
background-color: lightblue;
position: relative
}
p::before {
content: "XL";
font-size: 75%;
position: absolute;
right: 100%;
background-color: lightgray;
bottom: 0;
}
<body>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in dictum purus. Etiam accumsan quam et turpis elementum, in tempor. </p>
</body>
Out of scope: don't use margin on <body>. Use padding instead.
I would adjust the line-height
div {
margin: 0 30px;
position: relative
}
p {
background-color: lightblue
}
p::before {
content: "XL";
font-size: 75%;
position: absolute;
right: 100%;
background-color: lightgray;
line-height:1.75;
}
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in dictum purus. Etiam accumsan quam et turpis elementum, in tempor. </p>
</div>
<div style="font-size:20px;">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in dictum purus. Etiam accumsan quam et turpis elementum, in tempor. </p>
</div>
<div style="font-size:35px;">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in dictum purus. Etiam accumsan quam et turpis elementum, in tempor. </p>
</div>
Aloha - I have been recreating this tutorial - [CSS Positioning][1] -
[1]: http://www.barelyfitz.com/screencast/html-training/css/positioning/ to really understand the tutorial. It's at step 7 in the tutorial that I have bumped into something that I can't figure out. In step 7 of the tutorial you will see that the text "id = div-1" sits above the "div-1a" which is floated left.
If you take a look at my code pen - http://codepen.io/DarrenHaynes/pen/gLoYpp/ - You will see that the text "id = div-1" is aligning to the right of the "div-1a". I am not expecting this since "div-1" is the parent of "div-1a". Thus I can't figure out how to get my codepen to replicate the tutorial.
My code on codepen:
HTML:
<div id="div-before">
id = div-before
</div>
<div id="div-1">
id = div-1
<div id="div-1a">
id = div-1a
<br>
<br> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer pretium dui sit amet felis. Integer sit amet diam. Phasellus ultrices viverra velit.
</div>
<div id="div-1b">
id = div-1b<br><br> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer pretium dui sit amet felis. Integer sit amet diam. Phasellus ultrices viverra velit. Nam mattis, arcu ut bibendum commodo, magna nisi tincidunt tortor, quis accumsan
augue ipsum id lorem.
</div>
<div id=div-1c>
id = div-1c
</div>
</div>
<div id="div-after">
id = div-after
</div>
CSS:
#div-before,
#div-after {
margin: 0 auto;
width: 400px;
font-size: 20px;
background-color: #8888DD;
padding: 2px 5px;
}
#div-1 {
position: relative;
margin: 0 auto;
width: 400px;
color: white;
padding: 20px 10px 10px 10px;
background-color: black;
}
#div-1a {
float: left;
width: 200px;
padding: 3px;
background-color: red;
}
#div-1b {
padding: 3px;
background-color: green;
}
#div-1c {
padding: 3px;
background-color: #33D;
}
You can wrap your heading for "div-1" around a 'p' tag:
<div id="div-before">
id = div-before
</div>
<div id="div-1">
<p>id = div-1</p>
<div id="div-1a">
id = div-1a
<br>
<br> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer pretium dui sit amet felis. Integer sit amet diam. Phasellus ultrices viverra velit.
</div>
<div id="div-1b">
id = div-1b<br><br> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer pretium dui sit amet felis. Integer sit amet diam. Phasellus ultrices viverra velit. Nam mattis, arcu ut bibendum commodo, magna nisi tincidunt tortor, quis accumsan
augue ipsum id lorem.
</div>
<div id=div-1c>
id = div-1c
</div>
</div>
<div id="div-after">
id = div-after
</div>
CSS:
#div-before,
#div-after {
margin: 0 auto;
width: 400px;
font-size: 20px;
background-color: #8888DD;
padding: 2px 5px;
}
#div-1 {
position: relative;
margin: 0 auto;
width: 400px;
color: white;
padding: 20px 10px 10px 10px;
background-color: black;
}
#div-1a {
float: left;
width: 200px;
padding: 3px;
background-color: red;
}
#div-1b {
padding: 3px;
background-color: green;
}
#div-1c {
padding: 3px;
background-color: #33D;
}
Here's the Codepen: http://codepen.io/anon/pen/xRJNdZ
I've been working on a design that has been giving me some trouble. This is how it should look
My code is very similar to this design but I'm not able to get it to fit properly.
I currently have it set up with a media query to scale the images down and stack under the section of text as the screen size gets smaller; however, while the window size is in the process of getting smaller, it drops the images under the text and leaves a large white space.
Additionally, at full resolution, there is a gap between the images and the text but I can't seem to get them to sit beside each other. Is there something I could be doing that will make this design easier to achieve?
.redText{
background-color: #f0f0f0;
max-width: 400px;
display: block;
float: left;
padding-left: 10%;
}
.redText h1{
font-size: 40px;
color: #424242;
padding: 45px 20px 40px 20px;
}
.redText h2{
color: #d0112b;
padding: 0px 20px 40px 20px;
margin-top: -66px;
border-bottom: 10px solid #d0112b;
}
.redText p{
font-size: 18px;
line-height: 24px;
color: #424242;
padding: 0px 20px 110px 20px;
}
.redMore a{
text-decoration: none;
}
.blueText{
background-color: #f0f0f0;
max-width: 400px;
display: block;
float: right;
padding-right: 10%
}
.blueText h1{
font-size: 40px;
color: #424242;
padding: 45px 20px 40px 20px;
}
.blueText h2{
color: #0a5587;
padding: 0px 20px 40px 20px;
margin-top: -66px;
border-bottom: 10px solid #0a5587;
}
.blueText p{
font-size: 18px;
line-height: 24px;
color: #424242;
padding: 0px 20px 110px 20px;
}
.blueMore a{
text-decoration: none;
}
.space{
float: right;
}
.wrapper{
overflow: hidden;
}
.redImg img{
float: right;
max-width: 100%;
}
.blueImg img{
float: left;
max-width: 100%;
}
#media screen and (max-width: 480px) {
.redImg img{
float: none;
margin-left: 0;
width: auto;
}
.blueImg img{
float: none;
margin-right: 0;
width: auto;
}
}
<div class="wrapper">
<div class="redText">
<h1>RETAIL</h1><br>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed laoreet porta libero, nec efficitur neque scelerisque at. Maecenas a ligula nec ante tincidunt pellentesque in quis libero. Nulla lorem ante, pulvinar at ultricies ut, tempor vel mi.</p><br>
<div style="clear: both"></div>
<div class="redMore">
<h2>LEARN MORE<span class="space">></span></h2>
</div>
</div>
<div class="redImg"><img src="images/work/retail.jpg"></div>
</div>
<div class="wrapper">
<div class="blueText">
<h1>INDUSTRIAL</h1><br>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed laoreet porta libero, nec efficitur neque scelerisque at. Maecenas a ligula nec ante tincidunt pellentesque in quis libero. Nulla lorem ante, pulvinar at ultricies ut, tempor vel mi.</p><br>
<div style="clear: both"></div>
<div class="blueMore">
<h2>LEARN MORE<span class="space">></span></h2>
</div>
</div>
<div class="blueImg"><img src="images/work/industrial.jpg"></div>
</div>
<div class="wrapper">
<div class="redText">
<h1>COMMERCIAL</h1><br>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed laoreet porta libero, nec efficitur neque scelerisque at. Maecenas a ligula nec ante tincidunt pellentesque in quis libero. Nulla lorem ante, pulvinar at ultricies ut, tempor vel mi.</p><br>
<div style="clear: both"></div>
<div class="redMore">
<h2>LEARN MORE<span class="space">></span></h2>
</div>
</div>
<div class="redImg"><img src="images/work/commercial.jpg"></div>
</div>
<div class="wrapper">
<div class="blueText">
<h1>HOTELS</h1><br>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed laoreet porta libero, nec efficitur neque scelerisque at. Maecenas a ligula nec ante tincidunt pellentesque in quis libero. Nulla lorem ante, pulvinar at ultricies ut, tempor vel mi.</p><br>
<div style="clear: both"></div>
<div class="blueMore">
<h2>LEARN MORE<span class="space">></span></h2>
</div>
</div>
<div class="blueImg"><img src="images/work/hotels.jpg"></div>
</div>
There is several way to approach your problem:
Use % in width of your element instead of number px: Go with 50%-50% for the block of image and the block of text, use display:inline-block to put them next to each other
Use a CSS Framework like Bootstrap or Foudation, a best example to use in your case is media
EDIT1
Update with codepen: http://codepen.io/thovo/pen/EKwYpq
Please see this Fiddle...
I have two columns. Left column holds paragraph titles. Right column holds the actual paragraphs. I'm trying to align the paragraph title, with the top line of each paragraph and I'm having the hardest time doing it without using unnecessary and sloppy line breaks.
.col1 {float: left; width: 300px; border: 1px solid black; padding: 10px;}
.col2 {float: left; width: 300px; border: 1px solid black; padding: 10px;}
.line1 { height: auto; margin: 10px 0 20px 0; }
.line2 { height: auto; margin: 10px 0 20px 0; }
.line3 { height: auto; margin: 10px 0 20px 0; }
Thoughts?
This is a perfect example of when good ol' <table> elements are still useful! Also, notice that if the overall width is not big enough, your paragraphs wrap under all the titles completely.
The proper way to do this is to use a definition list
See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/dl
<dl>
<dt>Firefox</dt>
<dt>Mozilla Firefox</dt>
<dt>Fx</dt>
<dd>A free, open source, cross-platform, graphical web browser
developed by the Mozilla Corporation and hundreds of volunteers.</dd>
<!-- other terms and definitions -->
</dl>
If you did want to do this in div elements and not dt, have a look at this fiddle
HTML
<div class='wrapper'>
<div class="col1">
Paragraph 1
</div>
<div class="col2">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eros est, pellentesque in leo at, molestie mattis sem. Phasellus at est in ligula malesuada ullamcorper nec et massa.
</div>
<div style='clear:both;'></div>
<div class="col1">
Paragraph 2
</div>
<div class="col2">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eros est, pellentesque in leo at, molestie mattis sem. Phasellus at est in ligula malesuada ullamcorper nec et massa.
</div>
<div style='clear:both;'></div>
<div class="col1">
Paragraph 3
</div>
<div class="col2">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eros est, pellentesque in leo at, molestie mattis sem. Phasellus at est in ligula malesuada ullamcorper nec et massa.
</div>
<div style='clear:both;'></div>
</div>
CSS
body{
width:100%;
}
.wrapper{
border: 1px solid black;
}
.col1, .col2 {
float: left; width: 300px; padding: 10px;
}
.col2{
border-left:1px solid black;
}