I created a small website using two flex containers and the result is good. Is it possible to achieve the same result by using only one flexbox container on the container class?
I have been trying to do it but it does not work and I don't want to have two flexbox containers at the same time.
/* Framework.css */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
ul {
padding: 0;
margin: 0;
list-style: none;
}
body {
font-family: 'Electrolize', sans-serif;
}
.container {
max-width: 940px;
margin: 0 auto;
padding: 0 5%;
}
.gallery {
display: flex;
}
.gallery li {
width: 200px;
background-color: #1c1c1c;
color: #bdc3c7;
margin: 0% 0.5% 0% 0.5%;
}
.gallery img {
width: 100%;
height: auto;
}
.gallery p {
margin: 0;
padding: 6%;
font-size: 1.25em;
background-color: #483636;
color: #bdc3c7;
text-align: center;
}
.galleryproducts {
display: flex;
}
.galleryproducts li {
width: 200px;
margin: 2%;
}
.galleryproducts img {
width: 100%;
height: auto;
border: 3px solid white;
}
.latest {
margin-top: 1%;
background-color: #1c1c1c;
}
.latest h1 {
color: white;
font-size: 1.5em;
font-weight: 300;
border-bottom: 3px solid white;
margin-bottom: 5%;
padding: 2%;
}
a {
text-decoration: none;
}
<!-- index.html -->
<div class="container">
<section class="boxes">
<ul class="gallery">
<li>
<a href="img/electrical.png">
<img src="img/electrical.png" alt="">
<p>Electrical Installations</p>
</a>
</li>
<li>
<a href="img/lighting.png">
<img src="img/lighting.png" alt="">
<p>Lighting Decorations</p>
</a>
</li>
<li>
<a href="img/homeappliances1.png">
<img src="img/homeappliances1.png" alt="">
<p>Electrical Appliances</p>
</a>
</li>
<li>
<a href="img/homeappliances2.png">
<img src="img/homeappliances2.png" alt="">
<p>Kitchen Appliances</p>
</a>
</li>
</ul>
</section>
<section class="latest">
<h1>Our latest products</h1>
<ul class="galleryproducts">
<li>
<a href="img/1.jpg">
<img src="img/1.jpg" alt="">
</a>
</li>
<li>
<a href="img/2.jpg">
<img src="img/2.jpg" alt="">
</a>
</li>
<li>
<a href="img/3.jpg">
<img src="img/3.jpg" alt="">
</a>
</li>
<li>
<a href="img/4.jpg">
<img src="img/4.jpg" alt="">
</a>
</li>
</ul>
</section>
</div>
No, you can't set display: flex on the containter an have the gallery's/galleryproducts's children behave as they do now, as they aren't direct children of the container.
If you would change it like that, the boxes/latest will become flex children and the gallery's/galleryproducts's children will just become normal li items, stacked on top of each other, not side by side.
So what you have is what you need, to get the result you say is good (if to assume you want to use flex of course).
Related
I want to make a simple gallery.
I set up the width and height of li and the img size, but it doesn't fit.
I want to make unified image sizes.
What's wrong with my code?
body
{
margin: 0;
padding: 0;
background-color: #fff;
color: black;
font-family: sans-serif, Arial;
font-size: 15px;
line-height: 1.5;
}
.container
{
max-width: 1200px;
margin: auto;
}
.container a
{
background: #ccc;
color: #666;
text-decoration: none;
padding: 10px;
border-radius: 20px;
padding-left: 20px;
padding-right: 20px;
display: inline-block;
transition: background 0.5s, color 0,5s;
}
.container a:hover
{
background: #aaa;
color: #fff;
}
main
{
background: #111;
padding: 25px;
width: 100%;
float: left;
box-sizing: border-box;
}
/*Gallery*/
.gallery
{
background: #999;
margin: 0;
padding: 0;
list-style: none;
margin-bottom: 25px;
display: block;
width: 100%;
}
.gallery li
{
background: red;
width: 31%;
float: left;
margin-left: 1%;
margin-right: 1%;
height: 175px;
}
.gallery img
{
background: blue;
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
}
.container .gallery a
{
background: transparent;
padding: 0;
display: block;
width: 100%;
}
<!DOCTYPE html>
<html>
<head>
<meta lang="hu">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="res/teszt.css">
<title>test</title>
</head>
<body>
<!-- FEJLÉC -->
<div class="container">
<main>
<ul class="gallery">
<li>
<a href="https://cdn.wallpapersafari.com/29/92/9VWQiO.jpg" target="_blank">
<img src="https://cdn.wallpapersafari.com/29/92/9VWQiO.jpg" alt="">
<span>lorem ipsim</span>
</a>
</li>
<li>
<a href="https://upload.wikimedia.org/wikipedia/commons/a/a2/1121098-pink-nature-wallpaper-1920x1080-lockscreen.jpg" target="_blank">
<img src="https://upload.wikimedia.org/wikipedia/commons/a/a2/1121098-pink-nature-wallpaper-1920x1080-lockscreen.jpg" alt="">
<span>lorem ipsim</span>
</a>
</li>
<li>
<a href="https://images.template.net/wp-content/uploads/2016/04/27043339/Nature-Wallpaper1.jpg" target="_blank">
<img src="https://images.template.net/wp-content/uploads/2016/04/27043339/Nature-Wallpaper1.jpg" alt="">
<span>lorem ipsim</span>
</a>
</li>
<li>
<a href="https://wallpaperaccess.com/full/255959.jpg" target="_blank">
<img src="https://wallpaperaccess.com/full/255959.jpg" alt="">
<span>lorem ipsim</span>
</a>
</li>
<li>
<a href="https://wallpapercave.com/wp/wp4013939.jpg" target="_blank">
<img src="https://wallpapercave.com/wp/wp4013939.jpg" alt="">
<span>lorem ipsim</span>
</a>
</li>
<li>
<a href="https://images.hdqwalls.com/wallpapers/best-nature-image.jpg" target="_blank">
<img src="https://images.hdqwalls.com/wallpapers/best-nature-image.jpg" alt="">
<span>lorem ipsim</span>
</a>
</li>
</ul>
</main>
</div>
</body>
</html>
Just define some height and width to image it will start working.
body
{
margin: 0;
padding: 0;
background-color: #fff;
color: black;
font-family: sans-serif, Arial;
font-size: 15px;
line-height: 1.5;
}
.container
{
max-width: 1200px;
margin: auto;
}
.container a
{
background: #ccc;
color: #666;
text-decoration: none;
padding: 10px;
border-radius: 20px;
padding-left: 20px;
padding-right: 20px;
display: inline-block;
transition: background 0.5s, color 0,5s;
}
.container a:hover
{
background: #aaa;
color: #fff;
}
main
{
background: #111;
padding: 25px;
width: 100%;
float: left;
box-sizing: border-box;
}
/*Gallery*/
.gallery
{
background: #999;
margin: 0;
padding: 0;
list-style: none;
margin-bottom: 25px;
display: block;
width: 100%;
}
.gallery li
{
background: red;
width: 31%;
float: left;
margin-left: 1%;
margin-right: 1%;
height: 175px;
}
.gallery img
{
background: blue;
width: 100%;
height: 100%;
object-fit: cover;
object-position: center;
}
.container .gallery a
{
background: transparent;
padding: 0;
display: block;
height: 200px;
width: 100%;
background-size: cover;
}
<!DOCTYPE html>
<html>
<head>
<meta lang="hu">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="res/teszt.css">
<title>test</title>
</head>
<body>
<!-- FEJLÉC -->
<div class="container">
<main>
<ul class="gallery">
<li>
<a href="https://cdn.wallpapersafari.com/29/92/9VWQiO.jpg" target="_blank">
<img src="https://cdn.wallpapersafari.com/29/92/9VWQiO.jpg" alt="">
<span>lorem ipsim</span>
</a>
</li>
<li>
<a href="https://upload.wikimedia.org/wikipedia/commons/a/a2/1121098-pink-nature-wallpaper-1920x1080-lockscreen.jpg" target="_blank">
<img src="https://upload.wikimedia.org/wikipedia/commons/a/a2/1121098-pink-nature-wallpaper-1920x1080-lockscreen.jpg" alt="">
<span>lorem ipsim</span>
</a>
</li>
<li>
<a href="https://images.template.net/wp-content/uploads/2016/04/27043339/Nature-Wallpaper1.jpg" target="_blank">
<img src="https://images.template.net/wp-content/uploads/2016/04/27043339/Nature-Wallpaper1.jpg" alt="">
<span>lorem ipsim</span>
</a>
</li>
<li>
<a href="https://wallpaperaccess.com/full/255959.jpg" target="_blank">
<img src="https://wallpaperaccess.com/full/255959.jpg" alt="">
<span>lorem ipsim</span>
</a>
</li>
<li>
<a href="https://wallpapercave.com/wp/wp4013939.jpg" target="_blank">
<img src="https://wallpapercave.com/wp/wp4013939.jpg" alt="">
<span>lorem ipsim</span>
</a>
</li>
<li>
<a href="https://images.hdqwalls.com/wallpapers/best-nature-image.jpg" target="_blank">
<img src="https://images.hdqwalls.com/wallpapers/best-nature-image.jpg" alt="">
<span>lorem ipsim</span>
</a>
</li>
</ul>
</main>
</div>
</body>
</html>
I am currently doing the Freecodecamp course, but I got a little stuck on one of the projects. This is the current state of my project: https://codepen.io/otapadar/full/JjRaoex (I am trying to copy this website: https://codepen.io/freeCodeCamp/full/zNBOYG). The second section (My Projects) has images within a grid. I am trying to resize the images, such that each image covers about 80-90% of the parent element, just like the site I am trying to copy.
When you look at my website, you see that the images are not resizing. I have tried fixing this by using the following property on my images: object-fit: cover. This doesn't fix it, sadly.
Any help on fixing this would be greatly appreciated. Thanks in advance!
HTML:
<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
<nav id="navbar">
<ul>
<li><a class="nav-link" href="#welcome-section">About</a></li>
<li><a class="nav-link" href="#projects">Work</a></li>
<li><a class="nav-link" href="#contact">Contact</a></li>
</ul>
</nav>
<main>
<section id="welcome-section">
<h1>Hey, I am Brad</h1>
<p>Web Developer</p>
</section>
<section id="projects">
<h2 class="projects-header">My Projects</h2>
<container class="project-grid">
<a class="project-tile" target="_blank">
<img class="project-image" src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/images/tribute.jpg">
<p class="project-title">Tribute Page</p>
</a>
<a class="project-tile" target="_blank">
<img class="project-image" src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/images/random-quote-machine.png">
<p class="project-title">Random Quote Machine</p>
</a>
<a class="project-tile" target="_blank">
<img class="project-image" src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/images/calc.png">
<p class="project-title">JavaScript Calculator</p>
</a>
<a class="project-tile" target="_blank">
<img class="project-image" src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/images/map.jpg">
<p class="project-title">Map Data</p>
</a>
<a class="project-tile" target="_blank">
<img class="project-image" src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/images/wiki.png">
<p class="project-title">Wikipedia Viewer</p>
</a>
<a class="project-tile" target="_blank">
<img class="project-image" src="https://raw.githubusercontent.com/freeCodeCamp/cdn/master/build/testable-projects-fcc/images/tic-tac-toe.png">
<p class="project-title">Tic Tac Toe</p>
</a>
</container>
<a class="button" href="https://codepen.io/otapadar" target="_blank">Show All</a>
</section>
<section id="contact">
<h1>Let's Work Together</h1>
<p>How do you take your coffee?</p>
<container class="social-icons">
<a class="profile-link" target="_blank"></a>
<a class="profile-link" target="_blank"></a>
<a class="profile-link" target="_blank"></a>
<a class="profile-link" target="_blank"></a>
<a class="profile-link" target="_blank"></a>
</container>
</section>
</main>
CSS
#import url('https://fonts.googleapis.com/css2?family=Raleway&display=swap');
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html, body {
font-family: 'Raleway';
scroll-behavior: smooth;
}
#navbar {
position: fixed;
display: flex;
justify-content: flex-end;
top: 0;
left: 0;
right: 0;
width: 100%;
background-color: #be3144;
}
#navbar ul {
display: flex;
list-style: none;
margin-right: 2rem;
}
a {
text-decoration: none;
color: #f0f0f0;
}
.nav-link {
display: block;
padding: 1.5rem;
font-size: 1.3rem;
font-weight: bold;
}
#welcome-section {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #000;
background-image: linear-gradient(62deg, #3a3d40 0%, #181719 100%)
}
#welcome-section > h1 {
font-size: 4rem;
color: #f0f0f0;
font-weight: 700;
margin-bottom: 1.5rem;
}
#welcome-section > p {
color: #be3144;
font-size: 2rem;
}
#projects {
background-color: #45567d;
padding: 6rem 2rem;
text-align: center;
}
.projects-header {
font-size: 3rem;
color: #f0f0f0;
margin: 0 auto 2rem auto;
}
.project-grid {
display: grid;
width: 100%;
grid-gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
margin-bottom: 4rem;
}
.project-tile {
background-color: #303841;
}
.project-image {
height: calc(100%-6rem);
width: 100%;
object-fit: cover;
}
.project-title {
font-size: 1.5rem;
padding: 1.5rem 0.5rem;
font-weight: bold;
}
.button {
padding: 0.5rem 1rem;
font-size: 1.5rem;
background-color: #303841;
}
The problem in your css is this line height: calc(100%-6rem); just add spaces around the operator to be height: calc(100% - 6rem);
you should take white space after and befor minis like that
height:calc(100% - 6rem)
add max-width:100%; to project-image class in css and also you can set a custom width or height in img element
project-image{
height: 300px;
width: 600px;
max-width:100%;
object-fit: cover;
}
I know the question of overlapping has been answered multiple times, however, I'm trying to place the text on top of the image centred once hovered.
I tried z-index, I tried relative and absolute, decided I'd ask for help.
.flex-container {
display: flex;
flex-direction: row;
align-items: center;
flex-wrap: wrap;
margin: 0;
padding-left: 7%;
padding-right: 7%;
padding-top: 25px;
padding-bottom: 50px;
justify-content: center;
}
.flex-container div {
margin: 0;
position: relative;
margin-bottom: -5px;
font-size: medium;
text-align: center;
z-index: 0;
}
.flex-container div:hover {
filter: brightness(50%);
}
.flex-container div p {
display: none;
}
.flex-container div:hover p {
position: absolute;
color: black;
z-index: 1;
text-align: center;
}
<div>
<a href="Printing/menus.html">
<img src="../Images/menus-01.png" alt="Printing" style="width:100%; height:auto;"/>
</a>
<p>Menus</p>
</div>
You should have set the text CSS first and just then get the hover to show the styles, also, you forgot to set the container styles on your code.
See if that's what you want :
HTML :
<div class="container">
<img src="../Images/menus-01.png" alt="Image" style="width:100%;">
<div class="topTxt">Text on Center/div>
</div>
CSS :
.container {
position: relative;
text-align: center;
}
.topTxt {
position: absolute;
display: none;
top: 0;
left: 50%;
transform: translate(-50%, 0%);
}
.container:hover .topTxt {
display: block;
}
OR use JavaScript :
document.getElementsByClass("container").addEventListener("mouseover", function() {
document.getElementsByClass("topTxt").style.display = "block";
});
Get the following snippet to better view of what you requested :
#container {
max-width: 900px;
margin: 0 auto;
padding: 5px;
font-size: 0;
list-style: none;
background-color: #444;
}
#container li {
display: inline-block;
width: 25%;
vertical-align: middle;
box-sizing: border-box;
margin: 0;
padding: 0;
}
.container-cells {
margin: 5px;
box-shadow: 0 0 6px rgba(0, 0, 0, 0.3);
display: block;
position: relative;
overflow: hidden;
}
.imgs {
display: block;
width: 100%;
height: auto;
border: none;
filter: brightness(100%);
transition: all 0.3s;
}
#container li:hover .imgs {
filter: brightness(50%);
}
.overlay {
position: absolute;
display: block;
top: 0;
left: 0;
width: 100%;
height: 0px;
margin: auto;
background: #000;
background-size: 50px 50px;
transition: all 0.3s;
}
#container li:hover .overlay {
height: 30px;
}
.title {
display: block;
padding: 5px 30px;
box-sizing: border-box;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 40px;
text-align: center;
font-size: 18px;
color: white;
opacity: 0;
transform: translateY(-20px);
transition: all .3s;
}
#container li:hover .title {
transform: translateY(0px);
opacity: 0.9;
}
#media (max-width: 9000px) {
#container li {
width: 25%;
}
}
#media (max-width: 700px) {
#container li {
width: 33.33%;
}
}
#media (max-width: 550px) {
#container li {
width: 50%;
}
}
<ul id="container">
<li>
<a class="container-cells" href="#">
<img class="imgs" src="https://www.madpaws.com.au/wp-content/uploads/2019/02/Raise-Your-Kitten-to-Use-the-Litter-Box-300x200.jpg">
<span class="overlay"></span>
<span class="title">Hello World !</span>
</a>
</li>
<li>
<a class="container-cells" href="#">
<img class="imgs" src="https://www.madpaws.com.au/wp-content/uploads/2019/02/Raise-Your-Kitten-to-Use-the-Litter-Box-300x200.jpg">
<span class="overlay"></span>
<span class="title">Hello World !</span>
</a>
</li>
<li>
<a class="container-cells" href="#">
<img class="imgs" src="https://www.madpaws.com.au/wp-content/uploads/2019/02/Raise-Your-Kitten-to-Use-the-Litter-Box-300x200.jpg">
<span class="overlay"></span>
<span class="title">Hello World !</span>
</a>
</li>
<li>
<a class="container-cells" href="#">
<img class="imgs" src="https://www.madpaws.com.au/wp-content/uploads/2019/02/Raise-Your-Kitten-to-Use-the-Litter-Box-300x200.jpg">
<span class="overlay"></span>
<span class="title">Hello World !</span>
</a>
</li>
<li>
<a class="container-cells" href="#">
<img class="imgs" src="https://www.madpaws.com.au/wp-content/uploads/2019/02/Raise-Your-Kitten-to-Use-the-Litter-Box-300x200.jpg">
<span class="overlay"></span>
<span class="title">Hello World !</span>
</a>
</li>
<li>
<a class="container-cells" href="#">
<img class="imgs" src="https://www.madpaws.com.au/wp-content/uploads/2019/02/Raise-Your-Kitten-to-Use-the-Litter-Box-300x200.jpg">
<span class="overlay"></span>
<span class="title">Hello World !</span>
</a>
</li>
<li>
<a class="container-cells" href="#">
<img class="imgs" src="https://www.madpaws.com.au/wp-content/uploads/2019/02/Raise-Your-Kitten-to-Use-the-Litter-Box-300x200.jpg">
<span class="overlay"></span>
<span class="title">Hello World !</span>
</a>
</li>
<li>
<a class="container-cells" href="#">
<img class="imgs" src="https://www.madpaws.com.au/wp-content/uploads/2019/02/Raise-Your-Kitten-to-Use-the-Litter-Box-300x200.jpg">
<span class="overlay"></span>
<span class="title">Hello World !</span>
</a>
</li>
<li>
<a class="container-cells" href="#">
<img class="imgs" src="https://www.madpaws.com.au/wp-content/uploads/2019/02/Raise-Your-Kitten-to-Use-the-Litter-Box-300x200.jpg">
<span class="overlay"></span>
<span class="title">Hello World !</span>
</a>
</li>
</ul>
You just need to put the text before the image, as the image will be the background either way, just add the the text before, then the image and the text will be siblings, not parent and child, so the effect of hover won't affect the child, therefore, the text color will remain without having the filter applied to it.
Maybe the below is what you're looking for?
How to put text over images in html?
The answer suggests using a "div" tag rather than an "img" tag. Here is an example:
.image {
width:400px;
height:400px;
background-image: url(http://lorempixel.com/output/cats-q-c-640-480-4.jpg);
background-size:cover;
}
<div class="image">Text on top of image</div>
I have a horizontal timeline made with list (li) elements that has display:flex; at it's parent attribute. That make the list horizontal. But I want the texts to be maximum two rows in the boxes.
So for example, I want this:
The first
asphalt is
delivered.
To become this:
The first asphalt
is delivered.
I want that to be a rule and the width can become as wide as it need to be.
And I have seen a lot of examples where the solution is to work with overflow:hidden. But that's not what I looking for. I wanna know if there is a way to get a dynamic width so all the text is visible.
Edit:
Here is the snippet:
.left-arrow,.right-arrow {
float:left;
width: 0;
height: 0;
border-top: 20px solid transparent;
border-bottom: 20px solid transparent;
margin-top:20px;
cursor:pointer;
}
.left-arrow {
border-right:20px solid black;
padding-left: 5px;
}
.right-arrow {
border-left:20px solid black;
padding-right: 5px;
}
.timeline {
list-style-type: none;
display: flex;
justify-content: left;
margin: 0 5px;
padding: 0;
float: left;
width: calc(100% - 60px);
overflow-x: scroll;
vertical-align: top;
}
.timeline li {
width: 300px;
vertical-align: top;
}
.timeline li a {
color: #000;
}
.timestamp {
margin-bottom: 10px;
padding: 10px 10px 0;
display: flex;
flex-direction: column;
align-items: center;
white-space: nowrap;
font-weight: 600;
}
.status {
padding: 10px 5px 10px;
display: flex;
justify-content: center;
border-top: 3px solid #000;
position: relative;
font-size: 1.1rem;
}
.status:before {
content: "";
width: 4px;
height: 8px;
background-color: black;
position: absolute;
top: -10px;
left: calc(50% - (4px / 2));
}
<ul class="timeline" id="timeline">
<li>
<a href="#pop-1910-20">
<div class="timestamp">1910-20</div>
<div class="status">The founder buys land.</div>
</a>
</li>
<li>
<a href="#pop-1928">
<div class="timestamp">1928</div>
<div class="status">The refinery goes into operation.</div>
</a>
</li>
<li>
<a href="#pop-1929">
<div class="timestamp">1929</div>
<div class="status">The first asphalt is delivered.</div>
</a>
</li>
<li>
<a href="#pop-1931">
<div class="timestamp">1931</div>
<div class="status">New cracker.</div>
</a>
</li>
<li>
<a href="#pop-1939-45">
<div class="timestamp">1939-45</div>
<div class="status">Central role for the national sales</div>
</a>
</li>
<li>
<a href="#pop-1950s">
<div class="timestamp">1950-talet</div>
<div class="status">The network of petrol stations is being expanded</div>
</a>
</li>
<li>
<a href="#pop-1956">
<div class="timestamp">1956</div>
<div class="status">The second refinery is finished</div>
</a>
</li>
<li>
<a href="#pop-1958">
<div class="timestamp">1958</div>
<div class="status">The founder dies.</div>
</a>
</li>
<li>
<a href="#pop-1960">
<div class="timestamp">1960</div>
<div class="status">Risky sales strategy</div>
</a>
</li>
<li>
<a href="#pop-1967-69">
<div class="timestamp">1967-69</div>
<div class="status">Increased capacity of bitumen</div>
</a>
</li>
<li>
<a href="#pop-1974">
<div class="timestamp">1974</div>
<div class="status">The energy crisis leads to financial crisis</div>
</a>
</li>
</ul>
We can also fix the two lines for li elements by fixing the height and and line-height by halve of it. So two lines only can come but width can be maximum. So that there will be two lines but it would expand on x side. It will be something like this link below.
.twolines{
height: 50px;
line-height: 25px;
width: 100%;
overflow-y: hidden;
}
<ul>
<li class="twolines">Hello World. If it has two lines but have max width it can to be produced so that it will not expanded. Some extra text will be truncated when even two lines exceeded</li>
<li class="twolines">Hello World. If it has two lines but have max width it can to be produced so that it will not expanded.</li>
</ul>
Taken your code snippet and used the same logic of height and line-height. Along with that used box-sizing border box as there was some alignment (like padding and boader) so the width will be controlled including those also.Now only two lines and taken width accrodingly.
.left-arrow,.right-arrow {
float:left;
width: 0;
height: 0;
border-top: 20px solid transparent;
border-bottom: 20px solid transparent;
margin-top:20px;
cursor:pointer;
}
.left-arrow {
border-right:20px solid black;
padding-left: 5px;
}
.right-arrow {
border-left:20px solid black;
padding-right: 5px;
}
.timeline {
list-style-type: none;
display: flex;
justify-content: left;
margin: 0 5px;
padding: 0;
float: left;
width: calc(100% - 60px);
overflow-x: scroll;
vertical-align: top;
}
.timeline li {
width: 300px;
vertical-align: top;
}
.timeline li a {
color: #000;
}
.timestamp {
margin-bottom: 10px;
padding: 10px 10px 0;
display: flex;
flex-direction: column;
align-items: center;
white-space: nowrap;
font-weight: 600;
}
.status {
padding: 10px 5px 10px;
display: flex;
justify-content: center;
border-top: 3px solid #000;
position: relative;
font-size: 1.1rem;
box-sizing: border-box;
height: 43px;
line-height: 15px;
width: 213px;
}
.status:before {
content: "";
width: 4px;
height: 8px;
background-color: black;
position: absolute;
top: -10px;
left: calc(50% - (4px / 2));
}
<ul class="timeline" id="timeline">
<li>
<a href="#pop-1910-20">
<div class="timestamp">1910-20</div>
<div class="status">The founder buys land.</div>
</a>
</li>
<li>
<a href="#pop-1928">
<div class="timestamp">1928</div>
<div class="status">The refinery goes into operation.</div>
</a>
</li>
<li>
<a href="#pop-1929">
<div class="timestamp">1929</div>
<div class="status">The first asphalt is delivered.</div>
</a>
</li>
<li>
<a href="#pop-1931">
<div class="timestamp">1931</div>
<div class="status">New cracker.</div>
</a>
</li>
<li>
<a href="#pop-1939-45">
<div class="timestamp">1939-45</div>
<div class="status">Central role for the national sales</div>
</a>
</li>
<li>
<a href="#pop-1950s">
<div class="timestamp">1950-talet</div>
<div class="status">The network of petrol stations is being expanded</div>
</a>
</li>
<li>
<a href="#pop-1956">
<div class="timestamp">1956</div>
<div class="status">The second refinery is finished</div>
</a>
</li>
<li>
<a href="#pop-1958">
<div class="timestamp">1958</div>
<div class="status">The founder dies.</div>
</a>
</li>
<li>
<a href="#pop-1960">
<div class="timestamp">1960</div>
<div class="status">Risky sales strategy</div>
</a>
</li>
<li>
<a href="#pop-1967-69">
<div class="timestamp">1967-69</div>
<div class="status">Increased capacity of bitumen</div>
</a>
</li>
<li>
<a href="#pop-1974">
<div class="timestamp">1974</div>
<div class="status">The energy crisis leads to financial crisis</div>
</a>
</li>
</ul>
I've searched for this problem, but can't find an answer.
I have a file where I have 1 column on "mobile" and 2 columns on "tablet" and I'd like 3 columns on "desktop", or anything larger than 1100px.
I've tried a bunch of different nth-child values. It works from 1 to 2, but not 2 to 3. I can't figure it out. Thanks!
/*Grid ------------------*/
/* Max out, center */
.image-grid {
max-width: 1600px;
margin: 0 auto;
padding: 0;
list-style: none;
}
.image-grid li {
float: left;
width: 47%;
margin: 1.5%;
text-align: center;
}
.image-grid p {
margin: 0;
padding: 2% 0 5% 0;
color: black;
font-size: 1.15rem;
font-weight: 400;
}
.image-grid:after {
content: "";
display: table;
clear: both;
}
.image-grid li:nth-child(odd) {
clear: both;
}
img {
max-width: 100%;
height: auto;
}
/*Breaks ------------------*/
#media (max-width: 640px) {
* {
margin: 0;
padding: 0;
}
.image-grid ul {
margin: 0;
padding: 0;
}
.image-grid li {
float: none;
width: auto;
padding: 0 0 4%;
}
.image-grid li a p {
font-size: 1.65rem;
}
}
#media (min-width: 1100px) {
body {
background: green;
}
.image-grid p {
font-size: 1.3rem;
}
.image-grid li {
float: left;
width: 31.3333%;
margin: 1.5%;
border: 1px solid white;
}
.image-grid li:nth-child(4) {
clear: right;
background: aqua;
}
.image-grid {
border: 1px solid yellow;
}
}
<div class="wrapper">
<div>
<ul class="image-grid">
<li>
<a href="http://jonschafer.com/portfolio/logos.html">
<img src="http://jonschafer.com/gfx/-main/gfx-main_0007_WHT_web_logos-06.jpg" alt="" />
<p>Logos
</p>
</a>
</li>
<li>
<a href="http://jonschafer.com/portfolio/akqa.html">
<img src="http://jonschafer.com/gfx/-main/gfx-main_0011_WHT_web_AKQA-MAIN.jpg" alt="" />
<p>AKQA
</p>
</a>
</li>
<li>
<a href="http://jonschafer.com/portfolio/nike2021.html">
<img src="http://jonschafer.com/gfx/-main/gfx-main_0004_WHT_web_Nike2021_8.jpg" alt="" />
<p>Nike 2021
</p>
</a>
</li>
<li>
<a href="http://jonschafer.com/portfolio/gimme5.html">
<img src="http://jonschafer.com/gfx/-main/gfx-main_0009_WHT_web_Gimme5-02.jpg" alt="" />
<p>Walnut's Gimme 5
</p>
</a>
</li>
<li>
<a href="http://jonschafer.com/portfolio/xbox.html">
<img src="http://jonschafer.com/gfx/-main/gfx-main_0000_WHT_web_Halo-03.jpg" alt="" />
<p>Xbox Master Chief Collection
</p>
</a>
</li>
<li>
<a href="http://jonschafer.com/portfolio/opolis.html">
<img src="http://jonschafer.com/gfx/-main/gfx-main_0003_WHT_web_Opolis-13.jpg" alt="" />
<p>Opolis Design
</p>
</a>
</li>
<li>
<a href="http://jonschafer.com/portfolio/votesolar.html">
<img src="http://jonschafer.com/gfx/-main/gfx-main_0002_WHT_web_VoteSolar-05.jpg" alt="" />
<p>Vote Solar
</p>
</a>
</li>
<li>
<a href="http://jonschafer.com/portfolio/nikeevents.html">
<img src="http://jonschafer.com/gfx/-main/gfx-main_0005_WHT_web_Nike-Sales-Rec_1_0.jpg" alt="" />
<p>Nike Sales Events
</p>
</a>
</li>
<li>
<a href="http://jonschafer.com/portfolio/classicben.html">
<img src="http://jonschafer.com/gfx/-main/gfx-main_0010_WHT_web_Classic-Ben-05.jpg" alt="" />
<p>Classic Ben
</p>
</a>
</li>
<li>
<a href="http://jonschafer.com/portfolio/medigap.html">
<img src="http://jonschafer.com/gfx/-main/gfx-main_0006_Medigap-N1_Who-is-AC.jpg" alt="" />
<p>AllCare Medigap
</p>
</a>
</li>
<li>
<a href="http://jonschafer.com/portfolio/levi.html">
<img src="http://jonschafer.com/gfx/-main/gfx-main_0008_WHT_web_Levi-06.jpg" alt="" />
<p>Levi
</p>
</a>
</li>
</ul>
<!-- End group -->
</div>
</div>
<!-- End wrapper -->
its because you never overwrote this rule:
.image-grid li:nth-child(odd) {
clear: both;
}
So the third item is clearing both sides.
Add/change the following properties within the 1100px media query:
#media (min-width: 1100px) {
.image-grid li {
margin: 1%;
box-sizing: border-box;
}
.image-grid li:nth-child(odd) {
clear: none;
}
}
The issues with your code are:
You have odd columns clearing, which isn't needed with a three column layout
Your margin is set to 1.5%. So the combined width of each li is 34.3333%, too wide to fit three on one row.
Each li has a 1px border, so even with 1% margins for 33.3333% total width, the 1px border will push the third item to the next line. box-sizing: border-box includes the border in the li's width, so the element is truly 33.3333% width.
This has worked for me :
#media (min-width: 1100px) {
/*keep all others*/
/**remove this one
.image-grid li:nth-child(2n+1) {
clear: both;
}
*/
/* modify this one*/
.image-grid li {
float: left;
width: 31.3333%;
margin: 1%; /* Change from 1.5% */
border: 1px solid white;
box-sizing: border-box; /*Added this line*/
}
Hope it helps T04435.