I am currently making a carousel of images. (Carousel is based on a library, not in the scope of this question as i also cannot get it to work outside the carousel).
What i am trying to do is the following: I have a div and within that div i have an image and a caption (just a line of text or 2). I want the image above the caption and as large as possible (taking up all the space the caption does not take up). But the image keeps exceeding the parent div's height. It is never contained in the parent div. And if it is, it loses its ratio (keeps the same width but height is contained which gives a weird result obviously).
I tried a lot with flexbox like this:
<div class="container"> // <---- this wrapper need a specific height depending on screen size.
<img src="/images/thing.png" class="img" alt="pict">
<div class="caption">
<span>Text</span>
</div>
</div>
and the CSS:
.container {
height: 800px;
}
.img {
width: auto;
height: 100%; // this fills the container with the image but i wont see the caption anymore.
// also tried giving this one flex (NOT in combination with above. Just showing all i tried)
flex: 1 1 0%;
}
.caption {
flex: 1 1 0%; // Or 1 1 auto; no difference
}
How do i go about this? Maybe it is just me struggling with flexbox. Maybe i am approaching this wrongly. Any help/tips is appreciated!
/* Horizontal flex: rows
=====================
*/
.row {
display: flex;
}
.item {
flex: 1 1 30%;
padding-left: 1.5rem;
}
.item:first-child {
padding-left: 0;
}
/* Vertical flex: items
===================
*/
.item {
display: flex;
flex-direction: column;
}
.item-heading {
flex: 1;
}
/* Third flex: item heading
========================
*/
.item-heading {
display: flex;
justify-content: center;
flex-direction: column;
}
/* Not so important CSS - base styles:
===================================
*/
body {
margin: 2rem;
}
img {
max-width: 100%;
height: auto;
}
h2,
p {
margin: 0;
padding: 0;
}
.row {
margin-bottom: 1.5rem;
}
.item-heading {
padding: 1rem;
font-size: 1rem;
background: yellow;
text-align: center;
}
.meta {
color: #666;
margin-bottom: 3rem;
}
.meta p {
margin-bottom: .5rem;
}
<div class="meta">
<h1>
Image gallery with a vertically centered captions
</h1>
<p>
Yellow headings we want both vertical and horizontal centered.
</p>
<p>
There are three nested flexbox containers – row, item and item heading.
</p>
</div>
<div class="row">
<div class="item">
<p class="item-image">
<img src="https://parosrentcar.com/wp-content/uploads/2018/05/kawasaki-250-600-200.jpg" alt="Image">
</p>
<h2 class="item-heading">
Lorem ipsum dolor sit amet
</h2>
</div>
<div class="item">
<p class="item-image">
<img src="https://parosrentcar.com/wp-content/uploads/2018/05/kawasaki-250-600-200.jpg" alt="Image">
</p>
<h2 class="item-heading">
Proin pede metus, vulputate nec, fermentum fringilla, vehicula vitae, justo
</h2>
</div>
<div class="item">
<p class="item-image">
<img src="https://parosrentcar.com/wp-content/uploads/2018/05/kawasaki-250-600-200.jpg" alt="Image">
</p>
<h2 class="item-heading">
Consectetuer adipiscing elit
</h2>
</div>
</div>
<div class="row">
<div class="item">
<p class="item-image">
<img src="https://parosrentcar.com/wp-content/uploads/2018/05/kawasaki-250-600-200.jpg" alt="Image">
</p>
<h2 class="item-heading">
Nunc dapibus tortor vel mi dapibus sollicitudin
</h2>
</div>
<div class="item">
<p class="item-image">
<img src="https://parosrentcar.com/wp-content/uploads/2018/05/kawasaki-250-600-200.jpg" alt="Image">
</p>
<h2 class="item-heading">
Mauris elementum mauris vitae tortor
</h2>
</div>
<div class="item">
<p class="item-image">
<img src="https://parosrentcar.com/wp-content/uploads/2018/05/kawasaki-250-600-200.jpg" alt="Image">
</p>
<h2 class="item-heading">
Aliquam ornare wisi eu metus
</h2>
</div>
</div>
codepen : Link
i hope this help you
Related
[how do I make something like this, I need it for a school project :)(https://i.stack.imgur.com/n86zT.png)
<div class = "content">
<p class = "title">Intro to HTML</p>
<p class = "text">Sample Subtitle</p>
<div>
<img class = "thumbnail" src="personalp.webp">
</div>
</div>
this is what i got
Try this:
.content {
background: #f0f0f0;
width: 100%;
display: grid;
grid-template-columns: repeat(2, 1fr);
align-items: center;
justify-items: center;
padding: 1.5rem;
}
.text {
text-align: center;
}
<div class="content">
<div class="text">
<h1 class="title">Intro to HTML</h1>
<p class="text">Sample Subtitle</p>
</div>
<div>
<img class="thumbnail" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS4wGRN9XoPDaJKv2hBEKC0RDsWAoNSrYMlTw&usqp=CAU">
</div>
</div>
It's really difficult to give you an answer since we don't know what you will add to this basic template.
I agree with the grid solution and flex may help you to.
Please add more details about your whole project.
You don't tell anything about the manner of you want to align the elements to the top, if there will be additional content below this layout...
html body{
font-family: Gotham, "Helvetica Neue", Helvetica, Arial, "sans-serif";
box-sizing: border-box;
}
.content{
display:flex;
padding:20px;
}
.leftColumn{
background-color:antiquewhite;
padding-left: 20px;
padding-right: 20px;
}
.title{
text-align: center;
font-size: 1.8em;
margin:0px;
}
.text{
text-align: center;
}
.lipsum{
font-style: italic;
}
.image{
border:1px solid #000000;
width:200px;
height:200px;
}
<div class = "content">
<div class="leftColumn">
<p class = "title">Intro to HTML</p>
<p class = "text">Sample Subtitle</p>
<p class = "lipsum">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur id volutpat felis, ut maximus tellus. Quisque convallis accumsan volutpat. Etiam a eros quis tellus scelerisque elementum sed at nibh.
</p>
</div>
<div class="image">
<img class = "thumbnail" src="https://dummyimage.com/200x200/999999/fff.png" alt="image">
</div>
</div>
Have a nice day.
you can use inline CSS in your parent div like this:
<div style="width:800px;margin:0 auto;display: flex;flex-direction:row;align-items:center;justify-content: space-around">
<div>
<h1>title here</h1>
<p>Stuff inside paragraph</p>
</div>
<div>
Image here
</div>
</div>
This is your code :
<div class = "content">
<p class = "title">Intro to HTML</p>
<p class = "text">Sample Subtitle</p>
<div>
<img class = "thumbnail" src="personalp.webp">
</div>
</div>
You can add the title and subtitle inside a div like this :
<div class = "content">
<div class ="text-info>
<p class = "title">Intro to HTML</p>
<p class = "text">Sample Subtitle</p>
</div>
<div>
<img class = "thumbnail" src="personalp.webp">
</div>
</div>
Then you can use css to style the content as you want like this :
.content{
display:flex;
justify-content: center;
align-items : center;
}
It will move the titles on the left and image on the right side as you asked for.
If you need more help ask in the comments I will answer
I'm a rookie "programing" and asking for help here in stackoverflow.
I'm having a little problem trying to fit an image inside my grid.
For example, when I have an image with a specific size it works but when I have an image a bit larger or in a different format (16:9 instead of 9:16) it show the image not in the full size but not cut it to fit the grid .
What I Want
What I Get
I use the same HTML code and CSS for both grids the only thing that changes it's the image. I'm using Bootstrap with some CSS.
The CSS
.grid {
position: relative;
clear: both;
margin: 0 auto;
max-width: 1000px;
list-style: none;
text-align: center;
}
.grid figure {
position: relative;
overflow: hidden;
margin: 10px 0;
height: auto;
text-align: center;
cursor: pointer;
}
.grid figure img {
position: relative;
display: block;
min-height: 100%;
width: 100%;
opacity: 0.8;
}
<div class="gallery">
<div class="container">
<div class="gallery-grids">
<div class="col-md-4 gallery-grid">
<div class="grid">
<figure class="effect-roxy">
<a class="example-image-link" href="/images/fotos/gal1.jpg" data-lightbox="example-set" data-title="In lacinia pharetra ipsum vel dapibus. Ut vitae tristique nisi, mattis pellentesque elit. Proin mollis sed nisi ac sodales.">
<img src="/images/fotos/gal1.jpg" alt="" />
<figcaption>
<h3>Maecenas <span>lacus</span></h3>
<p> Aenean fermentum nisl ac imperdiet commodo</p>
</figcaption>
</a>
</figure>
</div>
</div>
<div class="clearfix"> </div>
</div>
</div>
</div>
I believe you have 2 options, one is to add images as backround and use cover, or if images comes from img tag then use
img {
object-fit: cover;
object-position: center;
}
Note, this does not work in old ie version
You can try to create a trim class in CSS like this
div.trim {
max-height:292.2px;
max-width: 350px;
overflow: hidden;
}
then add a new out of the image div to lock it
<div class="gallery">
<div class="container">
<div class="gallery-grids">
<div class="col-md-4 gallery-grid">
<div class="grid">
<figure class="effect-roxy">
<div class="trim" >
<a class="example-image-link" href="{$product.image}" data-lightbox="example-set" data-title="">
<img src="{$product.image}" alt="" />
<!--<figcaption>
<h3>Maecenas <span>lacus</span></h3>
<p> Aenean fermentum nisl ac imperdiet commodo</p>-->
</figcaption>
</a></div>
</figure>
</div>
</div>
<div class="clearfix"> </div>
</div>
</div>
</div>
I`m using flexbox and trying to make 6 things like this:
So far this is my code:
<section class="focus-item">
<div class="item focus-item-1">
<h4>Brand </h4>
<p>Identity
aliquam ipsum ante morbi sed ipsum mollis.
Sollicitudin viverra, vel varius eget sit mollis.
</p>
<img src="D:/htmlCss/landing/assets/img/focus-item-1.png" alt="" />
</div>
</section>
CSS
.focus-item {
display: flex;
flex-wrap: wrap;
}
.focus-item .item {
display: flex;
width: 50%;
text-align: left;
}
.focus-item .item p {
display: flex;
flex: 1;
align-items: flex-end;
}
.focus-item .item h4 {
display: flex;
align-items: flex-start;
}
I have problem with placing h4 on top of p. I would be great if someone can point me in the right direction.
As I already posted it in my first comment, you can do it by making the first flex item a flex box as well:
<div class="container">
<div class="item-1">
<h4>Header</h4>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.</p>
</div>
<img src="https://cdn.tutsplus.com/net/uploads/legacy/958_placeholders/placehold.gif" />
</div>
.container {
display: flex;
align-items: flex-end;
}
.item-1 {
display: flex;
flex-direction: column;
align-items: flex-end;
}
See here for the working example: http://jsfiddle.net/0j7abre9/1/
The key to this layout is to create one flex container that holds two flex items – the first holding the <h4> and <p>, and the other holding the <img> – and align them in a row. Then convert the first flex item to a flex container as well, and align its two items in a column.
HTML
<section class="focus-item">
<div class="item focus-item-1"><!-- nested flex container 1 -->
<h4>Brand</h4>
<p>Identity aliquam ipsum ante morbi sed ipsum mollis.
Sollicitudin viverra, vel varius eget sit mollis.</p>
</div>
<div class="item focus-item-2"><!-- nested flex container 2 -->
<img src="http://i.imgur.com/60PVLis.png" height="200" width="200" alt="">
</div>
</section>
CSS
.focus-item {
display: flex;
}
.item {
display: flex;
flex: 1 1 50%;
}
.focus-item-1 {
flex-direction: column;
align-items: flex-end;
}
DEMO
I want all inner divs containing text and with background color to be of the same height as the outer div (col).
Is there a way to accomplish that without jQuery?
.row {
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
flex-direction: row;
}
.col {
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
padding: 1em;
border: solid;
justify-content:center;
align-items:center;
overflow:hidden;
}
.col img{
width:100%;
height:auto;
}
<div class="row">
<div class="col">
<div><img src="" alt="" /></div>
<div style="background-color:#ff0000;">
This is a short text.
</div>
</div>
<div class="col">
<div><img src="" alt="" /></div>
<div style="background-color:#ff3654;" >
This is a text about how much I hate fucking around ith CSS when I'm not a pro. I wasted so much time on this issue I don't even mind writing this text. Sure you could use lorem ipsum but why bother, time you enjoy wasting is not wasted.
</div>
</div>
<div class="col">
<div><img src="" alt="" /></div>
<div style="background-color:#ff0000;">
This is CSS, just wanted to let you know that you suck.
</div>
</div>
</div>
https://jsfiddle.net/hcmnztof/1/
You can use flexible boxes again for .col:
.col {
display: flex;
flex-direction: column;
align-items: stretch; /* default value */
}
.col > :last-child {
flex-grow: 1;
}
.row {
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
flex-direction: row;
}
.col {
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
padding: 1em;
border: solid;
justify-content: center;
overflow: hidden;
display: flex;
flex-direction: column;
}
.col > :last-child {
flex-grow: 1;
}
.col img {
width: 100%;
height: auto;
}
<div class="row">
<div class="col">
<div>
Image1
</div>
<div style="background-color:#ff0000;">
Lorem ipsum.
</div>
</div>
<div class="col">
<div>
Image2 <br/> Image2
</div>
<div style="background-color:#ff3654;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue.
</div>
</div>
<div class="col">
<div>
Image3 <br/> Image3 <br/> Image3
</div>
<div style="background-color:#ff0000;">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris.
</div>
</div>
</div>
thanks for your input. I used this page to gain some basic knowledge about flexbox. CSS-Tricks A guide to flexbox
Then I started from scratch and came up with what I wanted: jsfiddle
.row {
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
flex-direction:row;
padding: 10% 0 10% 0;
-webkit-flex-wrap: wrap;
-ms-flex-wrap: wrap;
flex-wrap: wrap;
}
.row img{
width:100%;
height:auto;
}
.col {
-webkit-box-flex:1;
-moz-box-flex:1;
flex:1;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
flex-direction:column;
align-items:stretch;
margin-right:3%;
}
.subcol{
-webkit-box-flex:1;
-moz-box-flex:1;
flex:1 auto;
margin-top:-1%;
-webkit-border-bottom-right-radius: 15px;
-webkit-border-bottom-left-radius: 15px;
-moz-border-radius-bottomright: 15px;
-moz-border-radius-bottomleft: 15px;
border-bottom-right-radius: 15px;
border-bottom-left-radius: 15px;
}
.subcol h2{
text-align:center;
color:#ffffff;
padding: 10% 10% 0 10%;
font-weight:200;
font-size:2.2em;
line-height:1;
text-transform:uppercase;
margin-bottom:13%;
}
.subcol p{
text-align:center;
padding: 0 10% 10% 10%;
margin-top:-1.2%;
color:#ffffff;
font-size:0.9em;
}
<div class="row">
<div class="col">
<img src="http://lorempixel.com/50/50" alt="" />
<div class="subcol" style="background-color:#ff0000;">
<h2>Test headline</h2>
<p>This is a short text.</p>
</div>
</div>
<div class="col">
<img src="http://lorempixel.com/120/150" alt="" />
<div class="subcol" style="background-color:#ff3654;" >
<h2>Test headline</h2>
<p>This is a text about how much I hate fucking around ith CSS when I'm not a pro. I wasted so much time on this issue I don't even mind writing this text. Sure you could use lorem ipsum but why bother, time you enjoy wasting is not wasted.</p>
</div>
</div>
<div class="col">
<img src="http://lorempixel.com/100/150" alt="" />
<div class="subcol" style="background-color:#ff0000;">
<h2>Test headline</h2>
<p>This is CSS, just wanted to let you know that you suck.</p>
</div>
</div>
</div>
I have two p tags
<p style="margin: 0; display: inline;">content1</p>
<p style="margin: 0; display: inline;" align="right">content2</p>
The Output is content1content2. My expectation is like this:
content1 content2
Can anyone help. I want one "content1" in the left p and "content2" in the right 'p'.
You can use CSS flexbox for this. Below is the minimal CSS for the requested layout:
<div style="display: flex; justify-content: space-between;">
<p style="background-color: papayawhip;">Lorem ipsum dolor sit amet.</p>
<p style="background-color: palegoldenrod;">Donec eget luctus lacus.</p>
</div>
For longer content, you can use fixed-width columns:
<div style="display: flex; justify-content: space-between;">
<p style="flex-basis: 49.5%; background-color: papayawhip;">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec eget luctus lacus. Cras consectetur elementum mi sed consequat.</p>
<p style="flex-basis: 49.5%; background-color: palegoldenrod;">Pellentesque aliquet condimentum augue in mattis. Praesent sagittis nisl magna, a volutpat arcu imperdiet vel. Quisque et orci sed ligula cursus luctus.</p>
<!-- 49.5% + 49.5% = 99%, remaining 1% is distributed according to justify-content -->
</div>
You could do it with floats:
<p style="margin:0;display:inline;float:left">content1</p>
<p style="margin:0;display:inline:float:right" >content2</p>
The idea of the tag <p></p> is to display a paragraph. So HTML offers you the <div></div> which is a container conecpt. So you should use Salman A's Solution, because there aren't just different tags in html for no reason. Actually you can style a paragraph with css so it is getting displayed the same as a div container, but it is not meant to be like that.
I don't want to say to you, what you have to do. I just wanna help you using the "correct" tags for the things they were made for.
What you really want is something that doesn't assume sufficent width to fit both paragraphs into one line:
* { box-sizing: border-box; }
.two { width: 30em; max-width: 100%; }
.two p { display: inline-block; max-width: 50%; }
.two p:nth-child(1) { float:left; }
.two p:nth-child(2) { float:right; }
<div class="two">
<p>This is the first paragraph of two.</p>
<p>This is the second paragraph of two.</p>
</div>
Here's another quick turnaround to achieve this:
p{
text-align: center;
}
.item p{
display: inline-block;
}
.leftContent{
text-align: left;
width: 50%;
}
.rightContent{
text-align: right;
width: 50%
}
<br>
<!--Use both P tags in the same line without space -->
<article class="item">
<p class="leftContent">Content1</p><p class="rightContent">Content2</p>
</article>
float:left, float:right.... or
width:49.9%;
display:inline;
text-align:left;
text-align:right;