How to treat to attribute selector:hover - css

Im new with css, Im one of our homework in class is to make a box with 4 imgs, that while im mouse hover image it will be bigger in the center of the box.
each image must be with 2 files, small one will start with th and the big one start with big. need to practice the attribute selector.
so I make something, but I just cant fiqure why its not working.
body>div {
width: 100%;
height: 800px;
border: 2px solid black;
}
div {
position: relative;
}
div>div {
position: absolute;
top: 200px;
left: 400px;
}
img {
display: block;
margin: 20px 10px;
}
img[src*="th"] {
width: 250px;
border: 3px solid black;
box-shadow: 10px 10px 10px #666;
}
img[src*="/big"] {
display:none;
}
img[src*="th"]:hover {
box-shadow: inset 10px 10px 10px #666;
}
img[src*="th"]:hover+img[src*="/big"] {
display:block;
}
</style>
<div>
<img src="/HTML/HW/HW27_5/thCar.jpg" alt="">
<img src="/HTML/HW/HW27_5/thHome.jpg" alt="">
<img src="/HTML/HW/HW27_5/thTree.jpg" alt="">
<img src="/HTML/HW/HW27_5/thPlane.jpg" alt="">
<div>
<img src="/HTML/HW/HW27_5/bigCar.jpg" alt="">
</div>
<div>
<img src="/HTML/HW/HW27_5/bigHome.jpg" alt="">
</div>
<div>
<img src="/HTML/HW/HW27_5/bigTree.jpg" alt="">
</div>
<div>
<img src="/HTML/HW/HW27_5/bigPlane.jpg" alt="">
</div>
</div>

Try it by keeping the imgs next to each other.
The "+" works for next sibling element only.
.outer-div {
width: 100%;
height: 500px;
border: 2px solid black;
}
.outer-div {
position: relative;
}
img {
display: block;
margin: 15px 10px;
}
img[src*="/100"] {
border: 3px solid black;
box-shadow: 10px 10px 10px #666;
}
img[src*="/200"] {
display: none;
}
img[src*="/100"]:hover {
box-shadow: inset 10px 10px 10px #666;
}
img[src*="/100"]:hover+img[src*="/200"] {
display: block;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
<div class="outer-div">
<div class="inner-div">
<img src="https://picsum.photos/100" alt="">
<img src="https://picsum.photos/200" alt="">
</div>
<div class="inner-div">
<img src="https://picsum.photos/100" alt="">
<img src="https://picsum.photos/200" alt="">
</div>
<div class="inner-div">
<img src="https://picsum.photos/100" alt="">
<img src="https://picsum.photos/200" alt="">
</div>
<div class="inner-div">
<img src="https://picsum.photos/100" alt="">
<img src="https://picsum.photos/200" alt="">
</div>
</div>

The +-selector select an element that is directly after another element.
Therefor your big image div must be directly next to the smaller image. And you have use this selector img[src*="th"]:hover+div img[src*="/big"] instead of img[src*="th"]:hover+img[src*="/big"] to address the nested image.
body>div {
width: 100%;
height: 800px;
border: 2px solid black;
}
div {
position: relative;
}
div>div {
position: absolute;
top: 200px;
left: 400px;
}
img {
display: block;
margin: 20px 10px;
}
img[src*="th"] {
width: 250px;
border: 3px solid black;
box-shadow: 10px 10px 10px #666;
}
img[src*="/big"] {
display: none;
}
img[src*="th"]:hover {
box-shadow: inset 10px 10px 10px #666;
}
img[src*="th"]:hover+div img[src*="/big"] {
display: block;
}
</style>
<div>
<img src="https://dummyimage.com/250x50/f00&text=/th" alt="">
<div>
<img src="https://dummyimage.com/500x200/f00&text=/big" alt="">
</div>
<img src="https://dummyimage.com/250x50&text=/th" alt="">
<div>
<img src="https://dummyimage.com/500x200&text=/big" alt="">
</div>
<img src="https://dummyimage.com/250x50/0f0&text=/th" alt="">
<div>
<img src="https://dummyimage.com/500x200/0f0&text=/big" alt="">
</div>
<img src="/HTML/HW/HW27_5/thPlane.jpg" alt="">
<div>
<img src="/HTML/HW/HW27_5/bigPlane.jpg" alt="">
</div>
</div>

Related

How to make div overflow instead of going on new line?

Want to create a slider containing 3 image containers and need all of them in the same row. All image containers are same size, but the size of slider is around 2.5 images, so the third one should overflow instead of going to new row. Example:
How it should look
What i have for now
html
<div class="image-slide-wrapper">
<div class="images-section">
<div class="image-slide">
<div class="slide-img">
</div>
</div>
<div class="image-slide">
<div class="slide-img">
</div>
</div>
<div class="image-slide">
<div class="slide-img">
</div>
</div>
</div>
</div>
scss
.image-slide-wrapper {
float: left;
height: 55%;
width: 100%;
.images-section {
float: left;
height: 85%;
width: 100%;
border: 1px solid grey;
border-radius: 8px;
overflow: hidden;
padding: 0.125rem;
}
.image-slide {
float: left;
height: 100%;
width: 100px;
margin-right: 0.25rem;
.slide-img {
float: left;
height: 100%;
width: 100%;
border-radius: 0.35rem;
background-color: #e9e9e9;
in the text slider is images-section class and image container is image-slide class. Forgot to mention that I have to go with float to make consistent with the rest of the code.
<div style="overflow: hidden; width: 250px;">
<div style="width: max-content">
<img style="width: 100px;height: 100px;border: 1px solid black;" src="...">
<img style="width: 100px;height: 100px;border: 1px solid black;" src="...">
<img style="width: 100px;height: 100px;border: 1px solid black;" src="...">
</div>
</div>
You could remove float from the thumb elements and use display: flex and flex-flow: row nowrap on the container
.container {
height: 100px;
width: 250px;
border: 1px solid grey;
border-radius: 8px;
overflow: hidden;
padding: 0.125rem;
display: flex;
flex-flow: row nowrap;
}
.image-slide {
height: calc(100% - .25rem);
flex: 1 0 100px;
margin-right: 0.25rem;
border: 1px #ccc solid;
}
<div class="container">
<div class="image-slide"></div>
<div class="image-slide"></div>
<div class="image-slide"></div>
<div class="image-slide"></div>
</div>
This works. You can add display:flex; and flex-wrap:nowrap; to .image-slide-wrapper class.
.main {
float: left;
height: 85%;
width: 100%;
border: 1px solid grey;
border-radius: 8px;
overflow: hidden;
padding: 0.125rem;
display:flex;
flex-wrap:nowrap;
}
.slider {
float: left;
height: 100%;
width: 300px;
margin-right: 0.25rem;
display: block;
}
<div class="main">
<div class="slider">
<img class="img" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTBYCs940TLL3tx_nyn7fVeidXWZyfOKV5QrQ&usqp=CAU">
</div>
<div class="slider">
<img class="img" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTBYCs940TLL3tx_nyn7fVeidXWZyfOKV5QrQ&usqp=CAU">
</div>
<div class="slider">
<img class="img" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTBYCs940TLL3tx_nyn7fVeidXWZyfOKV5QrQ&usqp=CAU">
</div>
</div>

Align vertical/horizontal image inside a div(which is inside a container) where the image sizes and ratio varies, and have pading

I want to align images vertical where the image ratio varies, the image can be bigger or not than the content and also have padding;
I tried solutions that I found here, but I didn't found something that covers everything; I need to work in IE9;10
.thumb {
border: 2px solid #4FA738;
border-radius: 0.1875rem;
cursor: pointer;
display: inline-block;
font: 0/0 a;
margin: 0 0.5rem 0 0;
transition: 0.2s;
text-align: center;
}
.thumb img {
max-height: 100%;
max-width: 100%;
width: auto;
height: auto;
display: block;
vertical-align:middle;
margin:auto;
padding: 2px;
}
<div class="thumbs">
<div class="thumb" style="width:70px;height:70px;">
<img src="http://via.placeholder.com/350x150">
</div>
<div class="thumb" style="width:70px;height:70px;">
<img src="http://via.placeholder.com/200x100">
</div>
<div class="thumb" style="width:70px;height:70px;">
<img src="http://via.placeholder.com/140x100">
</div>
<div class="thumb" style="width:70px;height:70px;">
<img src="http://via.placeholder.com/50x50">
</div>
<div class="thumb" style="width:70px;height:70px;">
<img src="http://via.placeholder.com/150x450">
</div>
</div>
The first pure CSS way to achieve the desired result is to do it with the background-image & co. properties:
.thumb {
border: 2px solid #4FA738;
box-shadow: inset 0 0 0 2px #fff; /* 2px margin/padding trick */
border-radius: 0.1875rem;
cursor: pointer;
display: inline-block;
font: 0/0 a;
margin: 0 0.5rem 0 0;
transition: 0.2s;
text-align: center;
width: 70px;
height: 70px;
}
.thumb {
background-repeat: no-repeat;
background-position: center;
background-size: contain; /* keeps / "contains" the ratio */
}
.thumb:first-child {background-image: url(http://via.placeholder.com/350x150)}
.thumb:nth-child(2) {background-image: url(http://via.placeholder.com/200x100)}
.thumb:nth-child(3) {background-image: url(http://via.placeholder.com/140x100)}
.thumb:nth-child(4) {background-image: url(http://via.placeholder.com/50x50)}
.thumb:last-child {background-image: url(http://via.placeholder.com/150x450)}
<div class="thumbs">
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
<div class="thumb"></div>
</div>
And the second one where you have to sacrifice the border-radius effect:
.thumb {
border: 2px solid #fff;
outline: 2px solid #4FA738;
border-radius: 0.1875rem;
cursor: pointer;
display: inline-block;
font: 0/0 a;
margin: 0 0.5rem 0 0;
transition: 0.2s;
text-align: center;
position: relative;
}
.thumb img {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
max-height: 100%;
max-width: 100%;
width: auto;
height: auto;
display: block;
}
<div class="thumbs">
<div class="thumb" style="width:70px;height:70px;">
<img src="http://via.placeholder.com/350x150">
</div>
<div class="thumb" style="width:70px;height:70px;">
<img src="http://via.placeholder.com/200x100">
</div>
<div class="thumb" style="width:70px;height:70px;">
<img src="http://via.placeholder.com/140x100">
</div>
<div class="thumb" style="width:70px;height:70px;">
<img src="http://via.placeholder.com/50x50">
</div>
<div class="thumb" style="width:70px;height:70px;">
<img src="http://via.placeholder.com/150x450">
</div>
</div>

Border With a Square

How do I add a square onto a border between a div and an image? The square will be position on the left or the right depending on the position of the text (if the text is aligned right, the square will be bottom left; if the text is aligned left, the square will be top right).
.item {
flex: 1;
background: #8d0700;
margin-left: 5px;
margin-right: 5px;
padding: 0px;
width: 250px;
border: 5px solid #000;
}
.image img {
width: auto;
height: 450px;
outline: 2px solid rgba(218, 236, 255, 0.6);
outline-offset: -6px;
}
.name {
height: 100px;
text-overflow: wrap;
background-color: #8d0700;
color: #fff;
}
.bottomborder {
border-bottom: 5px solid #000;
}
.topborder {
border-top: 5px solid #000;
}
.name .left {
padding-left: 10px;
}
.name .right {
float: right;
margin-right: 10px;
}
<link href="https://www.w3schools.com/lib/w3.css" rel="stylesheet" />
<div class="w3-container w3-row-padding indexcontainer">
<div class="items">
<div class="w3-col l3 item">
<div class="name bottomborder">
<h3 class="right">Die Casting and Machining</h3>
</div>
<div class="image">
<img src="http://via.placeholder.com/200x200">
</div>
</div>
<div class="w3-col l3 item">
<div class="image">
<img src="http://via.placeholder.com/200x200">
</div>
<div class="name topborder">
<h3 class="left">Plastic Injection Products</h3>
</div>
</div>
</div>
</div>
You can achieve this using pseudoelements.
You can adjust the position as you need. I've used calc() here to take into account the width of the border.
.item {
flex: 1;
background: #8d0700;
margin-left: 5px;
margin-right: 5px;
padding: 0px;
width: 250px;
border: 5px solid #000;
}
.image img {
width: auto;
height: 450px;
outline: 2px solid rgba(218, 236, 255, 0.6);
outline-offset: -6px;
}
.name {
height: 100px;
text-overflow: wrap;
background-color: #8d0700;
color: #fff;
}
.bottomborder:after,
.topborder:before {
content: '';
width: 3em;
height: 3em;
background: black;
transform: rotate(45deg);
position: absolute;
}
.bottomborder {
border-bottom: 5px solid #000;
position: relative;
}
.bottomborder:after {
left: 3em;
bottom: calc(-1.5em - 3px);
}
.topborder {
border-top: 5px solid #000;
position: relative;
}
.topborder:before {
right: 3em;
top: calc(-1.5em - 3px);
}
.name .left {
padding-left: 10px;
}
.name .right {
float: right;
margin-right: 10px;
}
<link href="https://www.w3schools.com/lib/w3.css" rel="stylesheet" />
<div class="w3-container w3-row-padding indexcontainer">
<div class="items">
<div class="w3-col l3 item">
<div class="name bottomborder">
<h3 class="right">Die Casting and Machining</h3>
</div>
<div class="image">
<img src="http://via.placeholder.com/200x200">
</div>
</div>
<div class="w3-col l3 item">
<div class="image">
<img src="http://via.placeholder.com/200x200">
</div>
<div class="name topborder">
<h3 class="left">Plastic Injection Products</h3>
</div>
</div>
</div>
</div>

box shadow not working

I'm trying to add a box shadow to #main-content-area on the top side and 50% to both the left and right sides (from top), but it is not working.
What is wrong with my code?
.wrapper {
position: relative;
}
#main-content-area {
position: relative;
background-color: white;
margin: -80px auto auto auto;
z-index: 4;
border: 1px solid red;
}
.halfshadow {
position: absolute;
z-index: -1;
top: 0;
height: 50%;
box-shadow: 1px 0px 10px rgba(0, 0, 0, 0.5);
}
<div class="wrapper">
<div class="line"></div>
<div class="row">
<div class="col-sm-2">
</div>
<div class="col-sm-8" id="main-content-area">
<div class="row">
<div class="col-sm-4">
<img src="me.jpg" class="img-responsive" />
</div>
<div class="col-sm-4">
<img src="me.jpg" class="img-responsive" />
</div>
<div class="col-sm-4">
<img src="me.jpg" class="img-responsive" />
</div>
</div>
</div>
<div class="halfshadow"></div>
<div class="col-sm-2">
</div>
</div>
</div>
Just for showing box-shadow I have removed the position: absolute; you can position your div as per your requirement.
.wrapper{
position:relative;
}
#main-content-area{
position:relative;
background-color:white;
margin: -80px auto auto auto;
z-index:4;
border:1px solid red;
}
.halfshadow{
z-index:-1;
height: 50px;
width: 100%;
box-shadow: 1px 0px 10px rgba(0, 0, 0, 0.5);
}
<div class="wrapper">
<div class="line"></div>
<div class="row" >
<div class="col-sm-2"></div>
<div class="col-sm-8" id="main-content-area">
<div class="row">
<div class="col-sm-4">
<img src="me.jpg" class="img-responsive"/>
</div>
<div class="col-sm-4">
<img src="me.jpg" class="img-responsive"/>
</div>
<div class="col-sm-4">
<img src="me.jpg" class="img-responsive"/>
</div>
</div>
</div>
<div class="halfshadow"></div>
<div class="col-sm-2" ></div>
</div>
</div>
use width "halfshadow" div or place content. Here is example with added "width"
.halfshadow {
position: absolute;
z-index: -1;
top: 0;
height: 50%;
width: 100%;
box-shadow: 1px 0px 10px rgba(0, 0, 0, 0.5);
}
Remove position: absolute; from .halfshadow class
you need to mention div tag
.div1
{
padding: 28px 10px;
/* width: 230px; */
width:200px;
/* margin-right: 31px;
margin-left: 105px;*/
margin-top:10px;
height: 226px;
box-shadow: 10px 10px 5px #888888;
margin-top: -20px;
color: blue;
text-align: center;
}

How can I center two images side-by-side with Bootstrap 3?

I can't seem to get the two images of class 'social' side-by-side, central to the column of class 'col-sm-4'. Instead they stack as the screen size decreases. Any ideas how I can do this responsively?
HTML
<div class="container-fluid">
<div class="row" style="border-top: 0">
<!-- Profile info -->
<div class="col-sm-4 col-sm-offset-4">
<img class="img-circle img-responsive" src="images/callum.jpg">
<h1 class="text-center" style="color: #3FA2B2">Callum Messiter</h1>
<p class="text-center">100-trillion cells and 100-billion neurons defying the 1 in 10<sup>2,685,000</sup> chance of me existing.</p>
<img class="social" src="images/twitter.png">
<img class="social" src="images/github.png">
</div>
</div>
</div>
CSS
.row{
border-top: 1px solid #808080;
padding-left: 10px;
padding-right: 10px;
padding-bottom: 10px;
}
.col-sm-4{
height: 450px;
}
.img-circle{
height: 200px;
width: 200px;
margin: 0 auto;
position: relative;
top: 40px;
border: 2px solid white;
}
.text-center{
font-family: helvetica;
color: white;
position: relative;
top: 50px;
}
span{
font-weight: bold;
background-color: #e6e6e6;
color: #3FA2B2;
padding: 3px 7px 3px 7px;
}
.social{
height: 50px;
width: 50px;
margin: 0 auto;
}
Add class text-center before img. like this
<div class="text-center">
<img class="social" src="https://files.allaboutbirds.net/wp-content/themes/html5blank-stable/images/blue-winged-warbler.jpg">
<img class="social" src="https://files.allaboutbirds.net/wp-content/themes/html5blank-stable/images/blue-winged-warbler.jpg">
</div>
DEMO HERE https://jsfiddle.net/ff9g43t8/
Try:
.social {
display: inline-block;
}

Resources