How can we target the right most Child of Parent - html

I have a parent container 'Streams' and it's Childs 'course' i what to set the margin-right: 10px to every child except the child appear at the rightmost of a parent
if I use '.course:nth-child(3n)' it works fine until the browser window is full when we zoom the page or window size change it didn't work properly.
.Streams {
width: 100%;
height: auto;
padding: 40px 60px 40px 60px;
display: flex;
flex-wrap: wrap;
}
.course {
width: 300px;
border: 1px solid black;
margin-right: 10px;
}
<div class="Streams">
<div class="course">
<img src="#" alt="BCA" />
<p>
div 1
</p>
</div>
<div class="course">
<img src="#" alt="BCA" />
<p>
div 2
</p>
</div>
<div class="course">
<img src="#" alt="BCA" />
<p>
div 3
</p>
</div>
<div class="course">
<img src="#" alt="BCA" />
<p>
div 4
</p>
</div>
<div class="course">
<img src="#" alt="BCA" />
<p>
div 4
</p>
</div>
</div>
to Fix this i use but this only effect the very last child.
.course:last-of-type{
margin-right:0px;
}

What you need to do is take a similar approach that Bootstrap does for its column implementations:
.Streams {
padding: 60px -5px;
...
}
.course {
...
margin: 0 5px;
}
If the distance between components you want is Xpx then
The formula is horizontal margin (x/2)px on the children, and horizontal padding -(x/2)px on parent.
.Streams {
width: 100%;
height: auto;
padding: 40px -5px;
display: flex;
flex-wrap: wrap;
}
.course {
width: 300px;
border: 1px solid black;
margin: 0 5px;
}
<div class="Streams">
<div class="course">
<img src="#" alt="BCA" />
<p>
div 1
</p>
</div>
<div class="course">
<img src="#" alt="BCA" />
<p>
div 2
</p>
</div>
<div class="course">
<img src="#" alt="BCA" />
<p>
div 3
</p>
</div>
<div class="course">
<img src="#" alt="BCA" />
<p>
div 4
</p>
</div>
<div class="course">
<img src="#" alt="BCA" />
<p>
div 4
</p>
</div>
</div>

You can use this code in (.course:last-child)
body {
margin: 0;
}
.Streams {
width: 100%;
height: auto;
padding: 40px 60px 40px 60px;
display: flex;
flex-wrap: wrap;
}
.Streams .course {
width: 300px;
border: 1px solid black;
margin-right: 10px;
margin-bottom: 5px;
}
.Streams .course:last-child {
margin-right: 0px;
}
<div class="Streams">
<div class="course">
<img src="#" alt="BCA" />
<p>
div 1
</p>
</div>
<div class="course">
<img src="#" alt="BCA" />
<p>
div 2
</p>
</div>
<div class="course">
<img src="#" alt="BCA" />
<p>
div 3
</p>
</div>
<div class="course">
<img src="#" alt="BCA" />
<p>
div 4
</p>
</div>
<div class="course">
<img src="#" alt="BCA" />
<p>
div 4
</p>
</div>
</div>

Related

How do you center an image float? Also its text at the bottom

I already centered the images inside the "main-content" class, but I don't know if I do it right. And the second thing that I want to do is to center also the text, but if I use margin it also affects the images. Sorry for my confusing code, I'm just only a beginner.
Here is the HTML5 markup:
<main class="main-content">
<div class="item">
<a href="Products/groceries-pets/Fruits-Package.html">
<img
src="./Images/Products/groceries-pets/fruits-package.jpg"
class="img-content"
/>
</a>
<br />
<div>
<p href="Products/groceries-pets/Fruits-Package.html" class="desc">
<strong>Fruit Package</strong> <em> P1200 </em>
</p>
</div>
</div>
<div class="item">
<a href="products/home-living/Indoor-Plant.html">
<img
src="./Images/products/home-living/indoor-plant.jpg"
class="img-content"
/>
</a>
<br />
<div>
<p href="products/home-living/Indoor-Plant.html" class="desc">
<strong> Indoor Plant </strong> <em> P300 </em>
</p>
</div>
</div>
<div class="item">
<a href="Products/babies-toys/Baby-Cars.html">
<img
src="./Images/Products/babies-toys/baby-cars.jpg"
class="img-content"
/>
</a>
<br />
<div>
<p href="Products/babies-toys/Baby-Cars.html" class="desc">
<strong> Baby Cars</strong> <em> P150 </em>
</p>
</div>
</div>
<div class="item">
<a href="Products/fashion/Black-Shirt.html">
<img
src="./Images/Products/fashion/black-shirt.jpg"
class="img-content"
/>
</a>
<br />
<div>
<p href="Products/fashion/Black-Shirt.html" class="desc">
<strong> Black Shirt</strong> <em> P400 </em>
</p>
</div>
</div>
</main>
Here is my CSS:
.main-content {
background-color: rgb(219, 95, 95);
height: 340px;
width: 95%;
margin: 140px auto 0;
text-align: center;
}
.item {
float: left;
}
.img-content {
width: 200px;
height: 200px;
padding: 10px;
margin: 30px -200px 0px 200px;
object-fit: cover;
}
.desc {
text-align: center;
}
Try removing the margin from .img-content so that it aligns to the text, then add a margin to .item instead:
.main-content {
background-color: rgb(219, 95, 95);
height: 340px;
width: 95%;
margin: 140px auto 0;
text-align: center;
}
.item {
float: left;
margin: 20px;
}
.img-content {
width: 200px;
height: 200px;
padding: 10px;
object-fit: cover;
background-color: green;
}
.desc {
text-align: center;
}
<main class="main-content">
<div class="item">
<a href="Products/groceries-pets/Fruits-Package.html">
<img src="./Images/Products/groceries-pets/fruits-package.jpg" class="img-content" />
</a>
<br />
<div>
<p href="Products/groceries-pets/Fruits-Package.html" class="desc">
<strong>Fruit Package</strong> <em> P1200 </em>
</p>
</div>
</div>
<div class="item">
<a href="products/home-living/Indoor-Plant.html">
<img src="./Images/products/home-living/indoor-plant.jpg" class="img-content" />
</a>
<br />
<div>
<p href="products/home-living/Indoor-Plant.html" class="desc">
<strong> Indoor Plant </strong> <em> P300 </em>
</p>
</div>
</div>
<div class="item">
<a href="Products/babies-toys/Baby-Cars.html">
<img src="./Images/Products/babies-toys/baby-cars.jpg" class="img-content" />
</a>
<br />
<div>
<p href="Products/babies-toys/Baby-Cars.html" class="desc">
<strong> Baby Cars</strong> <em> P150 </em>
</p>
</div>
</div>
<div class="item">
<a href="Products/fashion/Black-Shirt.html">
<img src="./Images/Products/fashion/black-shirt.jpg" class="img-content" />
</a>
<br />
<div>
<p href="Products/fashion/Black-Shirt.html" class="desc">
<strong> Black Shirt</strong> <em> P400 </em>
</p>
</div>
</div>
</main>
One of the Css properties is the display property.
This property will put the items you have horizontally.
If you want to place it vertically,
Add this property after display: flex; and flex-direction: column;.
Then use the properties justify-content: center; and align-items: center;
This property will center the item.

Scaling down images with different widths in smaller parent div

I'm trying to recreate a chat using images as the messages. The images are of different widths and are in a parent div with a fixed width of 500px. The images are however bigger than 500px, which means that if I scale them down with "max-width: 80%", they do scale down but all to the same width. How can I keep the different widths while scaling them down? Can I achieve that with flexbox? Or with table?
Edit: This is roughly what it should look like:
Here's the snippet of the situation:
.wrapper {
margin: 0 auto;
margin-top: 20px;
width: 500px;
}
.chat {
border: 2px solid #b7b7b7;
}
.chat .chat-header {
width: 496px;
margin-bottom: -2.5px;
position: relative;
}
.chat .chat-history {
padding: 2%;
overflow-y: scroll;
height: 700px;
overflow-x: hidden;
}
.message {
max-width: 80%;
height: auto;
padding: 2px;
}
.float-right {
float: right;
}
<div class="wrapper">
<div class="chat">
<div class="chat-header">
<img class="chat-header" src="https://via.placeholder.com/1280x212"/>
</div>
<div class="chat-history">
<div class="test">
<img class="message float-right" src="https://via.placeholder.com/736x143" width="736" height="143" />
</div>
<div class="test">
<img class="message" src="https://via.placeholder.com/530x384" width="530" height="384"/>
</div>
<div class="test">
<img class="message float-right" src="https://via.placeholder.com/591x140" width="591" height="140" />
</div>
<div class="test">
<img class="message" src="https://via.placeholder.com/546x152" width="546" height="152" />
</div>
<div class="test">
<img class="message float-right" src="https://via.placeholder.com/561x101" width="561" height="101" />
</div>
<div>
<img class="message float-right" src="https://via.placeholder.com/698x124" width="698" height="124" />
</div>
<div>
<img class="message" src="https://via.placeholder.com/840x203" width="840" height="203" />
</div>
<div>
<img class="message" src="https://via.placeholder.com/824x141" width="824" height="141" />
</div>
<div>
<img class="message" src="https://via.placeholder.com/770x141" width="770" height="141" />
</div>
<div>
<img class="message float-right" src="https://via.placeholder.com/748x139" width="748" height="139" />
</div>
<div>
<img class="message float-right" src="https://via.placeholder.com/725x85" width="725" height="85" />
</div>
<div>
<img class="message" src="https://via.placeholder.com/812x197" width="812" height="197" />
</div>
<div>
<img class="message" src="https://via.placeholder.com/859x189" width="859" height="189" />
</div>
<div>
<img class="message float-right" src="https://via.placeholder.com/740x140" width="740" height="140" />
</div>
<div>
<img class="message float-right" src="https://via.placeholder.com/596x125" width="596" height="125" />
</div>
<div>
<img class="message" src="https://via.placeholder.com/857x109" width="857" height="109" />
</div>
</div>
</div>
</div>
Use several :nth-child(expr) presets with different max-width. expr should be some formula with n so you get repeated patterns of image scaling or some patterns that look random.
Example:
.message-container:nth-child(2n) .message {
max-width: 60%;
}
.message-container:nth-child(2n+1) .message {
max-width: 50%;
}
.message-container:nth-child(3n+2) .message {
max-width: 40%;
}
Set message-container class to divs inside chat-history or use .chat-history > div instead.
https://developer.mozilla.org/ru/docs/Web/CSS/:nth-child

How to merge borders of multiple img and fulfill imgs completely to div

I have two questions:
How can I merge the borders of multiple images? I have 4 images in a row and there's space between each so the borders are separated.
I want each of the four images of each div to fully fill the div to both right and left.
img {
width: 20%;
border: 1px solid black;
}
.works {
text-align: center;
padding: 10px;
}
<div class="works" id="works">
<div class="img">
<img src="http://up.pestools.ir/151810863547341_works1.png" />
<img src="http://up.pestools.ir/151810863547341_works1.png" />
<img src="http://up.pestools.ir/151810863547341_works1.png" />
<img src="http://up.pestools.ir/151810863547341_works1.png" />
</div>
<div class="img">
<img src="http://up.pestools.ir/151810863547341_works1.png" />
<img src="http://up.pestools.ir/151810863547341_works1.png" />
<img src="http://up.pestools.ir/151810863547341_works1.png" />
<img src="http://up.pestools.ir/151810863547341_works1.png" />
</div>
</div>
The space is coming because <img> are inline-block elements by default and inline-block elements take space to align itself.
You can use Flexbox to remove the spaces...
...and to merge the borders you can use margin negative values.
Stack Snippet
.img {
display: flex;
align-items: baseline;
justify-content: center;
}
img {
width: 20%;
border: 1px solid red;
margin-left: -1px;
margin-top: -1px;
}
.works {
padding: 10px;
}
<div class="works" id="works">
<div class="img">
<img src="http://up.pestools.ir/151810863547341_works1.png" />
<img src="http://up.pestools.ir/151810863547341_works1.png" />
<img src="http://up.pestools.ir/151810863547341_works1.png" />
<img src="http://up.pestools.ir/151810863547341_works1.png" />
</div>
<div class="img">
<img src="http://up.pestools.ir/151810863547341_works1.png" />
<img src="http://up.pestools.ir/151810863547341_works1.png" />
<img src="http://up.pestools.ir/151810863547341_works1.png" />
<img src="http://up.pestools.ir/151810863547341_works1.png" />
</div>
</div>
By adding float:left; to your images, you can remove the gap.
Then, by only adding a left border to the first image per row, and only adding a top border to the first row of images, you can make the borders more even.
img {
width: 20%;
border: 1px solid black;
border-left: none;
border-top: none;
float: left;
}
#works .img:first-child img {
border-top: 1px solid black;
}
.img img:first-child {
border-left: 1px solid black;
}
<div class="works" id="works">
<div class="img">
<img src="http://up.pestools.ir/151810863547341_works1.png" />
<img src="http://up.pestools.ir/151810863547341_works1.png" />
<img src="http://up.pestools.ir/151810863547341_works1.png" />
<img src="http://up.pestools.ir/151810863547341_works1.png" />
</div>
<div class="img">
<img src="http://up.pestools.ir/151810863547341_works1.png" />
<img src="http://up.pestools.ir/151810863547341_works1.png" />
<img src="http://up.pestools.ir/151810863547341_works1.png" />
<img src="http://up.pestools.ir/151810863547341_works1.png" />
</div>
</div>

Column not reaching its desired postion

Having an issue where i have two columns a left and right that will not go the 100% to the bottom where it should meet with the about dealer section. I have a left column and a center column and a right column. The center is the one that is filled with content and the right and left column should flow along with it without any content in it.
I have researched this for the last two day and have tried a ton of different things found on stack overflow and other sites, the big difference was setting the html, body{ height: 100%} to 100% along with using the vh on the columns. this works somewhat and am looking for a little help on what I'm doing wrong
I can't figure out why it only goes down 740px and stops.
I can post all the code but there is a lot of it so for now ill just give the parts I'm talking about.
If there is more information/code needed please let me know so i can present it.
Here is the site hosted for viewing Site Here
<!DOCTYPE html>
<html>
<head>
<title>Home Page</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="frontDoor.css"/>
</head>
<body>
<div class="wrapper">
<div class="header">
<div class="leftColumn"></div>
<div class="centerBox">
<div class="slideShow">
Slide Show
</div>
<div class="autoSearch">
auto Search
</div>
<div class="recList">
<span><b>Recent</b></span>Listings
</div>
<div class="recImg">
<img src="images/honda.png" alt="car" width="200" />
<img src="images/honda.png" alt="car" width="200"/>
<img src="images/honda.png" alt="car" width="200"/>
<img src="images/honda.png" alt="car" width="200"/>
</div>
<div class="srchSell">
<div class="srchBut">
<button class="button but1">SEARCH</button>
</div>
<div class="sellBut">
<button class="button but2">SELL</button>
</div>
</div>
<div class="recBlog">
<div class="blogImg">
<img src="images/honda.png" alt="car" width="200"/>
</div>
<div class="blogImg">
<img src="images/honda.png" alt="car" width="200"/>
</div>
<div class="blogImg">
<img src="images/honda.png" alt="car" width="200"/>
</div>
</div>
<div class="autoNews">
Auto news
<hr class="style-six">
<div class="news">
<img src="images/honda.png" alt="car" width="150" />
</div>
<div class="news">
<img src="images/honda.png" alt="car" width="150" />
</div>
</div>
</div>
<div class="rightColumn"></div>
</div>
</body>
</html>
Here is the CSS
html {height: 100%;}
body{
background-color: #222222;
font-family: 'PT Sans Narrow', sans-serif;
}
.wrapper {
margin: 0 auto;
width: 960px;/*1688px*/
height: 100%;
background-color:#b3ffb3;
}
.rightColumn{
float: left;
width: 70px;
height: 100%;
border: 1px sold gray;
background-color:#fff;
}
/* This is the center column */
.centerBox{
float:left;
width: 820px;
height: 100%;
border: 1px sold gray;
background-color:#fff;
overflow: hidden;
}
The simplest way is to wrap the three columns in a container DIV (.wrap_3 in my snippet below) and apply display: flex and height: 100% to that container. Also html and body need height: 100% for that height setting to work.
html,
body {
height: 100%;
margin: 0;
}
html {
height: 100%;
}
body {
background-color: #222222;
font-family: 'PT Sans Narrow', sans-serif;
}
.wrapper {
margin: 0 auto;
width: 960px;
/*1688px*/
height: 100%;
background-color: #b3ffb3;
}
.wrap_3 {
display: flex;
height: 100%;
}
.leftColumn {
width: 70px;
border: 1px sold gray;
background-color: #fb7;
}
.rightColumn {
width: 70px;
border: 1px sold gray;
background-color: #b8f;
}
.centerBox {
width: 820px;
border: 1px sold gray;
background-color: #fff;
overflow: hidden;
}
<div class="wrap_3">
<div class="leftColumn"></div>
<div class="centerBox">
<div class="slideShow">
Slide Show
</div>
<div class="autoSearch">
auto Search
</div>
<div class="recList">
<span><b>Recent</b></span>Listings
</div>
<div class="recImg">
<img src="images/honda.png" alt="car" width="200" />
<img src="images/honda.png" alt="car" width="200" />
<img src="images/honda.png" alt="car" width="200" />
<img src="images/honda.png" alt="car" width="200" />
</div>
<div class="srchSell">
<div class="srchBut">
<button class="button but1">SEARCH</button>
</div>
<div class="sellBut">
<button class="button but2">SELL</button>
</div>
</div>
<div class="recBlog">
<div class="blogImg">
<img src="images/honda.png" alt="car" width="200" />
</div>
<div class="blogImg">
<img src="images/honda.png" alt="car" width="200" />
</div>
<div class="blogImg">
<img src="images/honda.png" alt="car" width="200" />
</div>
</div>
<div class="autoNews">
Auto news
<hr class="style-six">
<div class="news">
<img src="images/honda.png" alt="car" width="150" />
</div>
<div class="news">
<img src="images/honda.png" alt="car" width="150" />
</div>
</div>
</div>
<div class="rightColumn"></div>
</div>

CSS floating images in center

So I have that html code with images+title:
<div class="container">
<div class="box"><img src="image1.jpg" class="thumb"><p>Title 1</p></div>
<div class="box"><img src="image2.jpg" class="thumb"><p>Title 2</p></div>
<div class="box"><img src="image3.jpg" class="thumb"><p>Title 3</p></div>
<div class="box"><img src="image4.jpg" class="thumb"><p>Title 4</p></div>
...
<div class="box"><img src="image49.jpg" class="thumb"><p>Title</p></div>
<div class="box"><img src="image50.jpg" class="thumb"><p>Title</p></div>
</div>
And css:
.container {
width:80%;
margin: 0 auto;
}
.box {
display:inline-block;
width:150px;
margin-right:5px;
float:left;
}
With that code I have more "white" space on right, I want to have these pictures in the center for different browser size without setting up width for container.
Is it possible with css?
add to your container class text-align: center; and remove float:left; from box class.
That's what you call a centered, widthless float.
#outer {
/* This is just so you can see that they're centered */
width: 400px;
border: 1px solid black;
overflow: hidden;
}
.centered {
position: relative;
float: left;
left: 50%;
/* This and below is just because I have them stacked */
height: 100px;
padding-bottom: 10px;
clear: left;
}
.centered div {
position: relative;
float: left;
left: -50%;
}
<div id="outer">
<div class="centered">
<div>
<img src="http://placehold.it/200x100" alt="" />
</div>
</div>
<div class="centered">
<div>
<img src="http://placehold.it/300x100" alt="" />
</div>
</div>
<div class="centered">
<div>
<img src="http://placehold.it/50x100" alt="" />
<img src="http://placehold.it/50x100" alt="" />
<img src="http://placehold.it/50x100" alt="" />
<img src="http://placehold.it/50x100" alt="" />
<img src="http://placehold.it/50x100" alt="" />
</div>
</div>
</div>