Hover property not working for div from different containers - html

I have two different boxes when want to be able to change the properties of one box as I hover somewhere else.
I figured that if I have both boxes in the same container it works as you can see on my snippet here.
Here my html and css:
.first {
color: blue;
background: black;
width: 299px;
}
.first:hover + .second {
background: red;
color: white;
}
.second {
color: red;
background: black;
width: 300px;
margin-left: 10px;
}
.container {
display: flex;
}
<div class="container">
<div class="first">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nam delectus quo corporis, n
</div>
<div class="second">
Lorem ipsum dolor sit amet, consectetur adipisicing e et magnam rem, doloribus libero quas numquam esse in culpa!
</div>
</div>
But how can I do the same thing if the boxes are not in the same container?
Thanks in advance.

If you use display: inline-block you can remove the use of the container but because you are using display: flex on the container class you won't have the advantage of the child items (the first and second divs) stretching to fill the height of the container. You could use a fixed height to keep them at an equal height.
.first{
color: blue;
background: black;
width: 299px;
display: inline-block;
}
.first:hover + .second {
background: red;
color: white;
}
.second{
color: red;
display: inline-block;
background: black;
width: 300px;
margin-left: 10px;
}
<div class="first">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nam delectus quo corporis, n
</div>
<div class="second">
Lorem ipsum dolor sit amet, consectetur adipisicing e et magnam rem, doloribus libero quas numquam esse in culpa!
</div>

(post edited)
Use Jquery : Create a new class with a background color, it will be added with Jquery and removed when your mouse left the div.
Code : (don't forget to add jquery)
$('div.one').hover(function(){
$('div.two').toggleClass('three');
});
Check this :
https://jsfiddle.net/wnszbhj3/1/

Related

How to add hover effects on different elements from the same div

Hey everyone this is my first time posting here.
My question is how can I add different hover effects on elements from a div.
I have a div that contains 3 elements: an image, a button, and a paragraph, I already have a scale effect on the div to change the size of the whole container but I would also want to change the color of the button when hovering anywhere on the div, not necessarily on the button. The same applies to the image.
The div:
<div class="float_tab">
<img src="./NY_2.jpeg" alt="imagine"/>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Laboriosam vel ipsum autem, sed aut sunt repellat laudantium aspernatur fugiat dolorem.</p>
<input class="btn" value="Enter" type="button">
</div>
The stylization
.float_tab:hover{
transform: scale(1.3);
}
.float_tab {
width: 210px;
height: 300px;
background-color: rgb(128, 177, 220, 0.7);
border-radius: 10px;
display: inline-block;
position: relative;
top:-1500px;
left: 570px;
margin-right: 100px;
transition: 1s;
}
img {
width: 210px;
height: 120px;
border-radius: 5px;
opacity: 0.8;
}
p {
text-align: center;
font-weight: bold;
font-size: 14px;
}
.btn {
height: 20px;
width: 90px;
margin-left: 60px;
margin-top: 10px;
background-color: #80b1dc;
align-items: center;
border-radius: 5px;
}
css will only apply effects to matching elements so:
<script>
.float_tab:hover img{
//css here
}
.float_tab:hover .btn{
//css here
}
</script>
<div class="float_tab">
<img src="./NY_2.jpeg" alt="imagine"/>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Laboriosam vel ipsum autem, sed aut sunt repellat laudantium aspernatur fugiat dolorem.</p>
<input class="btn" value="Enter" type="button">
</div>
will do.
It is possible to add a new style for child elements:
.float_tab:hover input, img {
background-color: lightgreen;
}
An example:
.float_tab:hover input, img{
background-color: lightgreen;
}
<div class="float_tab">
<img alt="foo image" alt="imagine" />
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Laboriosam vel ipsum autem, sed aut sunt repellat
laudantium aspernatur fugiat dolorem.</p>
<input class="btn" value="Enter" type="button">
</div>

Can css be used to collapse whitespace around inline elements with varying line heights?

Suppose one is designing a box to frame some content, and wants that box to always have consistent space between its borders and text inside of it, regardless of the text's line-height. Is there a solution aside from custom negative margin on each box?
In theory this should actually be the "responsibility" of the content (in this case, the text), assuming our box is some kind of component allowing transclusion (e.g. web component slots), so I'd be especially interested in any way to style an inline element so that its line-height-generated top and bottom spaces collapse, regardless of line-height value (intentionally not calling them margins to not confuse them with the margin css property).
Here as an runnable example of the issue - the space between the magenta border and the inner text varies due to line height, and if the magenta border wasn't there, it would appear that each box has different padding.
This has probably been answered, but unfortunately since the terms are so generic it's hard to research (though I did try).
.foo {
max-width: 200px;
border: 2px solid blue;
padding: 20px;
line-height: 2;
display: inline-block;
vertical-align: top;
}
.foo>* {
border: 1px solid magenta;
}
.baz {
line-height: 1;
}
.bar {
line-height: 3;
}
<div class="foo">
<div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci impedit porro fuga ab magnam.</div>
</div>
<div class="foo">
<div class="baz">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci impedit porro fuga ab magnam.</div>
</div>
<div class="foo">
<div class="bar">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci impedit porro fuga ab magnam.</div>
</div>
One idea that may solve half the issue is to change the line-height of the text to a smaller value than the container. Doing so, the height of the text will be smaller than the linebox and you can align it to the top. For this you need to consider an extra container.
.foo {
max-width: 200px;
border: 2px solid blue;
padding: 20px;
line-height: 2;
display: inline-block;
vertical-align: top;
}
.foo>* {
border: 1px solid magenta;
}
.baz {
line-height: 1;
}
.bar {
line-height: 3;
}
span {
line-height:1;
vertical-align:top;
}
<div class="foo">
<div><span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci impedit porro fuga ab magnam.</span></div>
</div>
<div class="foo">
<div class="baz"><span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci impedit porro fuga ab magnam.</span></div>
</div>
<div class="foo">
<div class="bar"><span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Adipisci impedit porro fuga ab magnam.</span></div>
</div>
As you can see the text will always be aligned on the top whataver the line-height but the issue remains on the bottom. It's like we moved the issue to only one side.
Here is a related answer to better understand the alignment trick : https://stackoverflow.com/a/54190413/8620333

CSS grid or flexbox for aligning image and text

I want to make sure the image and the text in the columns are aligned to the bottom of the image and the top of the text. Does anybody know how to achieve this without explicitly setting the height of .c-grid__content to 200px?
Right now I'm using flexbox. Maybe css-grid is the answer?
Please take a look at this codepen: https://codepen.io/simoncoudeville/pen/VMZmVa?editors=1100#0.
p {
margin-bottom: 10px;
font-family: sans-serif;
}
img {
max-width: 100%;
margin-bottom: 10px;
}
.c-grid {
padding: 40px 20px;
display: flex;
}
.c-grid__item {
flex-basis: 100%;
margin: 0 20px;
display: flex;
flex-wrap: wrap;
flex-direction: column;
justify-content: flex-end;
}
.c-grid__picture {
width: 100%;
}
.c-grid__content {
display: flex;
flex-direction: column;
}
<div class="c-grid">
<article class="c-grid__item">
<div class="c-grid__picture">
<img src="http://via.placeholder.com/400x200">
</div>
<div class="c-grid__content">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolore ratione commodi corrupti itaque sed architecto!</p>
</div>
</article>
<article class="c-grid__item">
<div class="c-grid__picture">
<img src="http://via.placeholder.com/400x600">
</div>
<div class="c-grid__content">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolore ratione commodi corrupti itaque sed architecto! Lorem ipsum dolor sit amet consectetur adipisicing elit.</p>
</div>
</article>
<article class="c-grid__item">
<div class="c-grid__picture">
<img src="http://via.placeholder.com/400x400">
</div>
<div class="c-grid__content">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Dolore ratione commodi corrupti itaque sed architecto! </p>
</div>
</article>
</div>
Align-items: baseline on the flexbox container seems to do the trick in this case. See the solution here: https://codepen.io/simoncoudeville/pen/yzOvmN?editors=1100
Looking at your codepen, this doesn't appear to require responsive design. If I am correct in that assumption, why not use a table that separates the images and text by rows, vertically aligning the top row at the bottom and the bottom row at the top?
It would limit the important CSS to just a few lines:
.c-grid__picture {
vertical-align: baseline;
}
.c-grid__content {
vertical-align: top;
}
Here is an example in JSFiddle
I was able to achieve this setting the flex-basis of the .c-grid__content to 0.
.c-grid__content {
...
flex-basis: 0;
}
https://codepen.io/antibland/pen/WZrXpw?editors=1100

Using CSS and HTML, change text-align if text doens't fit in a line

I have a list of items to show on screen. This is an example list with two items I would like to have:
My HTML is this:
<div class="row">
<div class="label">Site</div>
<div class="value">12 Oak Street, Vancouver</div>
</div>
<div class="row">
<div class="label">Note</div>
<div class="value">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.</div>
</div>
The tricky part which I don't know how to accomplish is aligning text of the values. If value can fit in the same row, it should be right-aligned. Otherwise, it should start in the next line and it should be left-aligned (like in the image above).
Can this be done with CSS? If needed, I can change the structure of HTML as well.
I'm afraid it's not doable using CSS only,
With JS and some calculations could help to change an element style when it "overlaps" another element.
The closest using plain CSS is this example which will only work when there's either really a small amount of text in .value or a really huge amount of text (that will make the float:right element actually span the full available width). Otherwise you'll rin into this issue: http://jsbin.com/vazaka/2/edit?html,css,output
(Don't use before reading the above)
html, body{height:100%; margin:0; font:16px/24px sans-serif;}
.row{
padding: 8px 16px;
border-bottom: 1px solid #ccc;
overflow:auto;
}
.row .label{
float:left; /* yaba */
color:#aaa;
}
.row .value{
float:right; /* doooo */
}
<div class="row">
<div class="label">Site</div>
<div class="value">12 Oak Street, Vancouver</div>
</div>
<div class="row">
<div class="label">Note</div>
<div class="value">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consequatur beatae magni consequuntur. Nostrum veritatis fugit quas rerum, dicta quod consequuntur, deleniti totam consectetur ex eligendi blanditiis quibusdam voluptatibus culpa et.</div>
</div>
I have found solution that works, but it is somehow ugly because it duplicates the text in HTML. But at least, it doesn't need JavaScript. If someone provided a better solution, I would gladly accept it as the right answer.
html, body{height:100%; margin:0; font:16px/20px sans-serif;}
.row{
padding: 3px 16px;
border-bottom: 1px solid #ccc;
overflow: hidden;
width: 420px;
position: relative;
}
.label {
display: inline-block;
}
.value-right{
color: #aaa;
display: inline-block;
float: right;
}
.value-left{
color: #aaa;
display: inline-block;
position: absolute;
top: 23px;
left: 16px; /* the same as padding in .row */
right: 16px; /* the same as padding in .row */
background: white;
}
<div class="row">
<div class="label">Site</div>
<div class="value-right">12 Oak Street, Vancouver</div>
<div class="value-left">12 Oak Street, Vancouver</div>
</div>
<div class="row">
<div class="label">Note</div>
<div class="value-right">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.</div>
<div class="value-left">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.</div>
</div>
<div class="row">
<div class="label">Something long enough that</div>
<div class="value-right">almost fits in one row, but it doesn't</div>
<div class="value-left">almost fits in one row, but it doesn't</div>
</div>

Make HTML table-row snap to height of largest image in table-cell

In this fiddle: http://jsfiddle.net/tvqdrwp9/3/
I want the images (which could be any size), to stretch the height of all the adjacent table-cells to match. There will only be 2 rows, with 2 cells each. One cell contains an image, the other text. I want the text cells to match the height of the adjacent image. I have overflow:hidden on the cells, so image overflowing horizontally is not an issue.
The text in boxes 2 and 3 should be vertically aligned in the middle, and the rows should be dictated by the heights of the images in boxes (cells) 1 and 4.
I can't understand why I am still getting a red line at the bottom of each image.
.about-boxes {
display: table;
max-width: 600px;
}
.about-box-row {
display: table-row;
height: 100%;
}
.about-box {
display: table-cell;
vertical-align: middle;
width: 50%;
overflow: hidden;
}
.about-box img {
width: auto;
height: 100%;
}
.about-box-1,
.about-box-4 {
background: red;
}
.about-box-2,
.about-box-3 {
background: #CCC;
color: #000;
}
<div class="about-boxes">
<div class="about-box-row">
<div class="about-box about-box-1">
<img src="http://dummyimage.com/300x180/000/fff">
</div>
<div class="about-box about-box-2">
<h2>Text</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perspiciatis velit repellat voluptate eum est re- iciendis eius recusandae molestiae iusto, dolor quis- quam voluptas.</p>
</div>
</div>
<div class="about-box-row">
<div class="about-box about-box-3">
<h2>Text</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisic- ing elit. Perspiciatis velit repellat voluptate eum est reiciendis eius recusandae molestiae iusto, dolor quisquam voluptas.</p>
</div>
<div class="about-box about-box-4">
<img src="http://dummyimage.com/320x360/000/fff">
</div>
</div>
</div>
You can either use display: block or vertical-align: middle on the image to correct the alignment of it.
.about-box img {
width: auto;
height: 100%;
display: block;
}
Or..
.about-box img {
width: auto;
height: 100%;
vertical-align: middle;
}
Both work.