Creating responsive div on text content with no Bootstrap - css

I am trying to create responsive div with image, text and spacing between divs.
I had divs with spacing and text but they wouldn't resize on more text.
I am trying to achive something like this.
This one is with more text in some of the divs and every other div resizes as well.
Here is what i got:
.img{
width:300px;
height:100%;
}
.image {
position: relative;
width: 100%; /* for IE 6 */
}
h3{
background-color: rgba(0, 0, 0, 0.5);
position: absolute;
top: 105px;
left: 0;
width: 300px;
color:white;
}
<div class="image">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h3>A Movie in the Park:<br />Kung Fu Panda</h2>
</div>
<div class="image">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h3>A Movie in the Park:<br />Kung Fu Panda</h2>
</div>
<div class="image">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h3>sadasdsadsadsadsadsadasdasdsadsadsadsadas</h2>
</div>
i need div to resize to amount of text and covers bottom part of it.
Currently i am breaking text with < br > which is not how i am aiming to do it.

Try word-wrap: break-word; property on h3
h3{
word-wrap: break-word;
}
Hope this Helps..

In case you'd like to have hyphenation, you could use the css "hyphens" property, which lets the browser decide where to break. See an example (with fallback, as the browser support is not so good, especially for some languages)
h3 {
word-break: break-word; /* non standard for webkit */
-webkit-hyphens: auto;
-moz-hyphens: auto;
hyphens: auto;
}
.img{
width:300px;
height:100%;
}
.image {
position: relative;
width: 100%; /* for IE 6 */
}
h3{
background-color: rgba(0, 0, 0, 0.5);
position: absolute;
top: 105px;
left: 0;
width: 100px;
color:white;
}
<div class="image">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h3 lang="en">A Movie in the Park:<br />Kung Fu Panda</h2>
</div>
<div class="image">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h3 lang="en">A Movie in the Park: Kung Fu Panda</h2>
</div>
<div class="image">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h3 lang="en">Incomprehensibilities </h2>
</div>

sadasdsadsadsadsadsadasdasdsadsadsadsadas this is garbage value, this is not word you can use like this
.img{
width:300px;
height:100%;
}
.image {
position: relative;
width: 100%; /* for IE 6 */
}
h3{
background-color: rgba(0, 0, 0, 0.5);
position: absolute;
top: 105px;
left: 0;
width: 300px;
color:white;
}
<div class="image">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h3>A Movie in the Park:<br />Kung Fu Panda</h2>
</div>
<div class="image">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h3>A Movie in the Park:<br />Kung Fu Panda</h2>
</div>
<div class="image">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h3>sadasdsa dsadsadsadsa dasdasdsads adsadsadas</h2>
</div>

you can use word-break: break-all; to h3
.img{
width:300px;
height:100%;
}
.image {
position: relative;
width: 100%; /* for IE 6 */
}
h3{
background-color: rgba(0, 0, 0, 0.5);
word-break: break-all;
position: absolute;
top: 105px;
left: 0;
width: 300px;
color:white;
}
<div class="image">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h3>A Movie in the Park:<br />Kung Fu Panda</h2>
</div>
<div class="image">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h3>A Movie in the Park:<br />Kung Fu Panda</h2>
</div>
<div class="image">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h3>sadasdsadsadsadsadsadasdasdsadsadsadsadas</h2>
</div>

Related

Flex div won't align to the left but to the right

I have a bootstrap 4 header and I'm using flex in it for some custom layout.
Here is the html:
<nav class="navbar">
<div class="container-fluid">
<a class="navbar-brand" href="#">
<img src="logo.png" alt="">
</a>
<div class="box">
<div class="title">TITLE HERE</div>
<div class="flex-inline-container">
<div class="icon"><img src="icon1.png" alt="" /></div>
<div class="icon"><img src="icon2.png" alt="" /></div>
<div class="icon"><img src="icon3.png" alt="" /></div>
<div class="icon"><img src="icon4.png" alt="" /></div>
<div class="icon"><img src="icon5.png" alt="" /></div>
</div>
</div>
</div>
</nav>
And the CSS:
.box {
display: flex;
border-right: 2px solid #bfbfbf;
width: min-content;
flex-direction: column;
}
.box .flex-inline-container {
display: inline-flex;
}
.box .icon {
margin-right: 20px;
cursor: pointer;
}
.box .title {
position: relative;
left: 7px;
font-size: 1.40rem;
font-weight:300;
color: #727272 !important;
}
My issue is that the "flex-inline-container" div is going all the way to the right and I need it to be on the left.
How can I make it go to the left?
The .container-fluid class of bootstrap has a Justify-content:space-between property. Which distributes items evenly (The first item is flush with the start,
the last is flush with the end).
In your case the <a class="navbar-brand"><a> will be moved to first and elements inside <div class="box"></div> moved to last.
If you want to override Justify-content property of .Container-fluid and move to left, Then add a .justify-content-start along with .Container-fluid.
.box {
display: flex;
border-right: 2px solid #bfbfbf;
width: min-content;
flex-direction: column;
}
.box .flex-inline-container {
display: inline-flex;
}
.box .icon {
margin-right: 20px;
cursor: pointer;
}
.box .title {
position: relative;
left: 7px;
font-size: 1.40rem;
font-weight:300;
color: #727272 !important;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<nav class="navbar">
<div class="container-fluid flex-row-reverse">
<a class="navbar-brand" href="#">
<img src="logo.png" alt="">
</a>
<div class="box">
<div class="title">TITLE HERE</div>
<div class="flex-inline-container">
<div class="icon"><img src="icon1.png" alt="" /></div>
<div class="icon"><img src="icon2.png" alt="" /></div>
<div class="icon"><img src="icon3.png" alt="" /></div>
<div class="icon"><img src="icon4.png" alt="" /></div>
<div class="icon"><img src="icon5.png" alt="" /></div>
</div>
</div>
</div>
</nav>

How to center a floated image gallery

I would like the content of the .container to be centered.
I am using the float:left property to create the gallery. It is also part of the code to resize and crop the thumbnails.
Any way to do this at all?
CSS:
.grid-item {
float: left;
width: 175px;
height: 120px;
overflow: hidden;
}
.grid-item img {
display: block;
max-width: 100%;
border: 2px solid #fff;
border-radius: 5px;
margin: 0 auto;
}
HTML:
<div class="container text-center">
<div id="links">
<a href="img.jpg" title="" class="grid-item" data-gallery>
<img src="img.jpg" alt="" class="img-rounded">
</a>
<a href="img.jpg" title="" class="grid-item" data-gallery>
<img src="img.jpg" alt="" class="img-rounded">
</a>
<a href="img.jpg" title="" class="grid-item" data-gallery>
<img src="img.jpg" alt="" class="img-rounded">
</a>
</div>
</div>
You should use flexbox to align your contents and you can read the comments in the CSS as to why which property is meant for. Hope, it helps.
#links{
display: flex; /*Generates a flexbox layout with default flex direction as row */
width: 100%; /* Not really required */
align-items: center; /*Aligns contents vertically */
justify-content: space-around; /*Aligns contents horizontally */
text-align: center; /*Aligns further text in the center */
flex-direction:row; /*By default its row, you can change to column for vertical alignment */
flex-flow:row wrap; /*To wrap items in other row when no more can be fit in the same row*/
}
.grid-item {
/* flex:1; If you need them to grow or shrink flexibly */
width: 175px;
height: 120px;
overflow: hidden;
}
.grid-item img {
display: block;
max-width: 100%;
border: 2px solid #fff;
border-radius: 5px;
margin: 0 auto;
}
<div class="container text-center">
<div id="links">
<a href="http://via.placeholder.com/400x400" title="" class="grid-item" data-gallery>
<img src="http://via.placeholder.com/400x400" alt="" class="img-rounded">
</a>
<a href="http://via.placeholder.com/400x400" title="" class="grid-item" data-gallery>
<img src="http://via.placeholder.com/400x400" alt="" class="img-rounded">
</a>
<a href="http://via.placeholder.com/400x400" title="" class="grid-item" data-gallery>
<img src="http://via.placeholder.com/400x400" alt="" class="img-rounded">
</a>
</div>
</div>
Usually when I run into this situation I will nest everything in the container in a sub-container and tweak that to my liking:
.container .center {
/*
This is just a way of centering the element,
you can use whatever technique.
*/
position:absolute;
top:50%;
left:50%;
transform:translateX(-50%) translateY(-50%);
}
<div class="container text-center">
<div class="center">
<div id="links">
<a href="img.jpg" title="" class="grid-item" data-gallery>
<img src="img.jpg" alt="" class="img-rounded">
</a>
<a href="img.jpg" title="" class="grid-item" data-gallery>
<img src="img.jpg" alt="" class="img-rounded">
</a>
<a href="img.jpg" title="" class="grid-item" data-gallery>
<img src="img.jpg" alt="" class="img-rounded">
</a>
</div>
</div>
</div>

Center responsive stack of divs CSS

I am trying to center stack of divs 3 per row if browser window is biger than 1024 and if it is less than 1024 to show 2 divs in row but keep showing all 6 of them.
Currently i am having a problem to migrate what i have already found out as it doesn't really fit to the div.
Here is my previous example of how it was supposed to look but i did not tried to pack it into divs.
h3 {
word-break: break-word; /* non standard for webkit */
-webkit-hyphens: auto;
-moz-hyphens: auto;
hyphens: auto;
}
.img{
width:640px;
height:100%;
}
.image {
position: relative;
width: auto; /* for IE 6 */
max-height: 640px;
}
h2{
background-color: rgba(0, 0, 0, 0.5);
padding-left: 20px;
padding-right: 20px;
padding-bottom: 10px;
padding-top: 10px;
position: absolute;
bottom:-15px;
left: 0;
width: 600px;
color:white;
}
<div class="image">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h2 lang="en">A Movie in the Park: Kung Fu Panda</h2>
</div>
<div class="image">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h2 lang="en">A Movie in the Park: Kung Fu Panda saasffdafadfadfda</h2>
</div>
<div class="image">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h2 lang="en">Incomprehensibilities ffafefeafea fefeefes gregrgregregerge</h2>
</div>
Here is what i tried to do with divs but it doesn't work the same way so i couldn't implement those properties.
body{
background-color:darkgrey;
}
.row{
left: 50%;
}
#media (min-width: 1024px) { .col-3::after{width:37.5%} }
.col-3 {width: 25%; float: left; border: 1px solid white;
margin-right: 20px;height:auto; margin-bottom: 20px;
}
img {
max-width: 100%;
max-height: 100%;
}
h2{
background-color: rgba(0, 0, 0, 0.5);
word-break: break-word; /* non standard for webkit */
font-size: 2vw;
max-width: 100%;
max-height: 100%;
color:white;
}
<body>
<div class="row">
<div class="col-3">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h2 lang="en">Incompres ffafefeafea fefeefes gregrgreg regerge</h2>
</div>
<div class="col-3">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h2 lang="en">Incompres ffafefeafea fefeefes gregrgreg regerge</h2>
</div>
<div class="col-3">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h2 lang="en">Incompres ffafefeafea fefeefes gregrgreg regerge</h2>
</div>
<div class="col-3">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h2 lang="en">Incompres ffafefeafea fefeefes gregrgreg regerge</h2>
</div>
<div class="col-3">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h2 lang="en">Incompres ffafefeafea fefeefes gregrgreg regerge</h2>
</div>
<div class="col-3">
<img src="http://i.imgur.com/VCsr2MH.png" alt="" class="img" />
<h2 lang="en">Incompres ffafefeafea fefeefes gregrgreg regerge</h2>
</div>
</div>
</body>
Using floats and media queries you can achieve this result. I used flexbox to center a wrapper that contains all 6 divs so everything remains centered despite div width or viewport dimensions. Next you can float all inner divs left and use nth-child to target only the ones you want to clear which will determine your column number. Use a media-query to target a viewport wider than 1025pxand simply change your nth-child pseudoclass accordingly.
* {
box-sizing: border-box;
}
html,
body {
height: 100%;
margin: 0;
}
html {
font-family: sans-serif;
}
body {
display: flex;
align-items: center;
}
.wrapper {
overflow: hidden;
margin: auto;
padding: 0.75rem;
background-color: #eee;
}
div > div {
padding: 0.75rem 1.5rem;
margin: 0.5rem;
background-color: #ccc;
float:left;
}
div:nth-child( 4n ){
clear: left;
}
.note {
width: 100%;
padding: 1rem;
background-color: #000;
color: #eee;
text-align: center;
position: absolute;
top: 0;
left: 0;
}
#media( max-width: 1025px ){
div:nth-child( 2n + 3 ){
clear: left;
}
div:nth-child( 4n ){
clear: none;
}
.note {
background-color: rgba( 0, 0, 0, 0.6 );
}
}
<div class="note">Resize viewport to change layout</div>
<div class="wrapper">
<div>div1</div> <div>div2</div> <div>div3</div>
<div>div4</div> <div>div5</div> <div>div6</div>
</div>

CSS masonry problems

I've recently discovered a neat way to do a masonry layout using columns, as found here: http://w3bits.com/css-masonry/. I've expanded on this layout by adding an overlay with text to each item upon hover. This works pretty well except for 2 problems:
The overlay is slightly larger than the image at the bottom. This persists even when different images are used.
The transitions do not work; the hover effect displays and hides abruptly.
Some explanations regarding my code:
I've added margin: 0; border:0; padding:0 to the entire document in an attempt to prevent problems just like this one, to no avail. .item has margin: 0 0 20px to provide vertical spacing between each item.
Because .item-overlay requires position:absolute to work, and position:absolute requires the parent element to be positioned, I've added position:relative to .item. This doesn't affect anything beyond allowing the overlay to show up correctly, as far as I can tell.
.item-overlay uses display:flex to vertically and horizontally centralise its content, but this shouldn't affect the layout.
Could someone help me figure out what's wrong with the code? JSFiddle: https://jsfiddle.net/nattanyz/sfn47me9/1/
Use vertical-align: top on the img and transition opacity instead of display
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
box-sizing: inherit;
}
body {
background-color: honeydew;
font-size: 16px;
line-height: 120%;
color: #333;
margin: 0;
padding: 0;
}
.wrapper {
min-height: 100vh;
width: 80vw;
max-width: 1200px;
margin: auto;
border: 0;
padding: 0;
column-count: 3;
column-gap: 20px;
}
.item {
display: block;
margin: 0 0 20px;
border: 0;
width: 100%;
position: relative;
}
.item-overlay {
position: absolute;
top: 0;
left: 0;
bottom: 0;
width: 100%;
background-color: rgb(51, 51, 51, 0.6);
color: white;
opacity: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
background-color: rgba(51, 51, 51, 0.6);
transition: opacity 300ms ease;
}
.item:hover .item-overlay {
opacity: 1;
}
.project-img {
width: 100%;
margin: 0;
border: 0;
padding: 0;
vertical-align: top;
}
.project-name {
font-weight: bold;
}
.project-category {
font-size: 0.75em;
text-transform: uppercase;
<body>
<div class="wrapper">
<div class="item">
<img class="project-img" src="https://images.pexels.com/photos/297755/pexels-photo-297755.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb" />
<div class="item-overlay">
<p class="project-name">Project Name</p>
<p class="project-category">Commercial</p>
</div>
</div>
<div class="item">
<img class="project-img" src="https://images.pexels.com/photos/297755/pexels-photo-297755.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb" />
<div class="item-overlay">
<p class="project-name">Project Name</p>
<p class="project-category">Commercial</p>
</div>
</div>
<div class="item">
<img class="project-img" src="https://images.pexels.com/photos/297755/pexels-photo-297755.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb" />
<div class="item-overlay">
<p class="project-name">Project Name</p>
<p class="project-category">Commercial</p>
</div>
</div>
<div class="item">
<img class="project-img" src="https://images.pexels.com/photos/297755/pexels-photo-297755.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb" />
<div class="item-overlay">
<p class="project-name">Project Name</p>
<p class="project-category">Commercial</p>
</div>
</div>
<div class="item">
<img class="project-img" src="https://images.pexels.com/photos/297755/pexels-photo-297755.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb" />
<div class="item-overlay">
<p class="project-name">Project Name</p>
<p class="project-category">Commercial</p>
</div>
</div>
<div class="item">
<img class="project-img" src="https://images.pexels.com/photos/297755/pexels-photo-297755.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb" />
<div class="item-overlay">
<p class="project-name">Project Name</p>
<p class="project-category">Commercial</p>
</div>
</div>
<div class="item">
<img class="project-img" src="https://images.pexels.com/photos/297755/pexels-photo-297755.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb" />
<div class="item-overlay">
<p class="project-name">Project Name</p>
<p class="project-category">Commercial</p>
</div>
</div>
</div>
</body>
The overlay is slightly larger than the image at the bottom. This persists even when different images are used.
The problem is on the image itself, it has height smaller than the container. Try to put some background-color on the container, you will see that the overlay height is equal with it's parent.
The transitions do not work; the hover effect displays and hides abruptly.
You cannot implements transition on the css display, other alternative would be using opacity. I modified your code, please try.
html {
box-sizing:border-box;
}
*, *:before, *:after {
box-sizing:inherit;
}
body {
background-color:honeydew;
font-size: 16px;
line-height: 120%;
color: #333;
margin: 0;
padding: 0;
}
.wrapper {
min-height: 100vh;
width: 80vw;
max-width:1200px;
margin: auto;
border: 0;
padding: 0;
column-count: 3;
column-gap: 20px;
}
.item {
display:block;
margin: 0 0 20px;
border:0;
width:100%;
position:relative;
background-color: black;
}
.item-overlay {
position:absolute;
top:0;
left:0;
bottom:0;
width:100%;
background-color: rgba(51,51,51,0.6);
color:white;
opacity:0;
transition:300ms ease opacity;
display: flex;
flex-direction:column;
justify-content:center;
align-items:center;
}
.item:hover .item-overlay {
opacity: 1;
background-color: rgba(51,51,51,0.6);
}
.project-img {
width:100%;
margin:0;
border:0;
padding:0;
}
.project-name {
font-weight:bold;
}
.project-category {
font-size:0.75em;
text-transform:uppercase;
<body>
<div class="wrapper">
<div class="item">
<img class="project-img" src="https://images.pexels.com/photos/297755/pexels-photo-297755.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb"/>
<div class="item-overlay">
<p class="project-name">Project Name</p>
<p class="project-category">Commercial</p>
</div>
</div>
<div class="item">
<img class="project-img" src="https://images.pexels.com/photos/297755/pexels-photo-297755.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb"/>
<div class="item-overlay">
<p class="project-name">Project Name</p>
<p class="project-category">Commercial</p>
</div>
</div>
<div class="item">
<img class="project-img" src="https://images.pexels.com/photos/297755/pexels-photo-297755.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb"/>
<div class="item-overlay">
<p class="project-name">Project Name</p>
<p class="project-category">Commercial</p>
</div>
</div>
<div class="item">
<img class="project-img" src="https://images.pexels.com/photos/297755/pexels-photo-297755.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb"/>
<div class="item-overlay">
<p class="project-name">Project Name</p>
<p class="project-category">Commercial</p>
</div>
</div>
<div class="item">
<img class="project-img" src="https://images.pexels.com/photos/297755/pexels-photo-297755.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb"/>
<div class="item-overlay">
<p class="project-name">Project Name</p>
<p class="project-category">Commercial</p>
</div>
</div>
<div class="item">
<img class="project-img" src="https://images.pexels.com/photos/297755/pexels-photo-297755.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb"/>
<div class="item-overlay">
<p class="project-name">Project Name</p>
<p class="project-category">Commercial</p>
</div>
</div>
<div class="item">
<img class="project-img" src="https://images.pexels.com/photos/297755/pexels-photo-297755.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb"/>
<div class="item-overlay">
<p class="project-name">Project Name</p>
<p class="project-category">Commercial</p>
</div>
</div>
</div>
</body>

Float left image with no space below

How can I float an image to the left with no space below?
I have used float: left.
HTML
* {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: 'Raleway', sans-serif;
}
#container {
width: 100vw;
overflow: hidden;
}
#container img {
min-width: 33.33333333333%;
max-width: 33.33333333333%;
float: left;
}
<div id="container">
<img src="http://lorempixel.com/100/100/">
<img src="http://lorempixel.com/150/150/">
<img src="http://lorempixel.com/120/120/">
<img src="http://lorempixel.com/100/100/">
<img src="http://lorempixel.com/110/110/">
<img src="http://lorempixel.com/100/100/">
<img src="http://lorempixel.com/100/100/">
<img src="http://lorempixel.com/100/100/">
<img src="http://lorempixel.com/100/100/">
<img src="http://lorempixel.com/100/100/">
</div>
You can wrap images in columns, like this fiddle
<div class = "container">
<div class="column">
<img src = "http://placehold.it/200x300">
<img src = "http://placehold.it/200x200">
</div>
<div class="column">
<img src = "http://placehold.it/200x400">
<img src = "http://placehold.it/200x200">
</div>
<div class="column">
<img src = "http://placehold.it/200x500">
<img src = "http://placehold.it/200x200">
</div>
</div>
css
.container{
width: 100vw;
position: relative;
}
.column {
float:left;
width:33.333333%;
}
.column img{
width:100%;
border: 1px solid;
}
if you need to apply with no divs inside the container, I would recommend Roy's answer

Resources