HTML:
<div id="parent">
<div class="child1">
<img src="img/top.png"/>
</div>
<div class="child2">
<img src="img/middle.png"/>
</div>
<div class="child3">
<img src="img/bottom.png"/>
</div>
</div>
CSS:
.child1{
display: inline-block;
}
.child2{
display: inline-block;
}
.child3{
display: inline-block;
}
I have the display set to inline-block because I want the divs to fit the size of the image that they contain (different size for each). However, when I have it set up this way, they appear one after another from left to right. I would like them to be stacked, where child2 is directly below child1 and child3 is directly below child2.
Thanks
Solve using flexbox
#parent {
display: flex;
flex-direction: column;
align-items: center;
}
<div id="parent">
<div class="child1">
<img src="http://lorempixel.com/400/200" alt="random">
</div>
<div class="child2">
<img src="http://lorempixel.com/600/200" alt="random">
</div>
<div class="child3">
<img src="http://lorempixel.com/400/200" alt="random">
</div>
</div>
If you really want to make divs elements to fit exactly the size of its contained image, then you must remove empty spaces or set font-size: 0 on parent's container (or child divs)
Check this pen
Give each child div width:100% and give the #parent div text-align:center
#parent {
text-align: center;
}
#parent>div {
width: 100%;
display: inline-block;
}
<div id="parent">
<div class="child1">
<img src="http://dingo.care2.com/pictures/greenliving/uploads/2013/10/cat-with-stunning-green-eyes.jpg" />
</div>
<div class="child2">
<img src="http://www.top13.net/wp-content/uploads/2015/02/cat-photography-zoran-milutinovic-Fluffy-The-King.jpg" />
</div>
<div class="child3">
<img src="https://i.imgur.com/D2cqjmO.jpg" />
</div>
</div>
Update your css like this
.child1{
float:left;
clear:both;
}
.child2{
float:left;
clear:both;
}
.child3{
float:left;
clear:both;
}
Related
Below are the div elements I am trying to place it side by side. Is it possible?
<div style="padding-left:10px">
<h4 class="line">{sev_pas}</h4>
</div>
<div style="padding-left:10px">
<div>sdfds</div>
Of course you can by using display: inline-block property. Just make sure what you are doing
div {
display: inline-block;
}
<div style="padding-left:10px">
<h4 class="line">{sev_pas}</h4>
</div>
<div style="padding-left:10px">
<div>sdfds</div>
SIMPLE CSS CAN HELP :
div{
width : 150px;
height: 150px;
display:inline-block;
}
<div style='background: red'></div>
<div style='background: blue'></div>
<div style='background: orange'></div>
<div style='background: green'></div>
Using text in div element it's not best practice! it's better to put your text in p or span tag.
also you can use "display:flex" for div element so it's better to write something like below code:
.container{
display: flex;
flex-direction:row;
gap: 10px;
align-item:center;
}
.line{
margin: 0
}
<div class="container">
<h4 class ="line" >{sev_pas}</h4>
<span>sdfds</span>
</div>
You can use display: inline-block for set div or any other elements.
div{
display:inline-block;
}
h2{
background:orange;
color:#fff;
}
<div style="padding-left:10px">
<h2>DIV 2</h2>
</div>
<div style="padding-left:10px">
<h2>DIV 1</h2>
</div>
Else there are also other methods like using display: flex or display: grid etc.
Just use display: inline-block;
.first-div{
display: inline-block;
}
.second-div{
display:inline-block;
}
<div class="first-div" style = "padding-left:10px">
<h4 class ="line" >ok</h4>
</div>
<div class="second-div" style= "padding-left:10px"> sdfds</div>
.container {
display:inline-block;
position:relative;
box-shadow: 0 0 20px 2px yellow;
}
img {
vertical-align: bottom;
}
.overlay {
background-color: skyblue;
width:;
float:left;
}
<div class="container">
<img src="https://athome.reachtheworld.org/wp-content/uploads/2020/09/NYC.jpg">
<div class="overlay">
Text
</div>
</div>
In this example, the width of the float is the size of its content, the text, and the container has expanded just enough to contain both siblings.
Now, if i increase the width of the float in pixels, the float will start pushing the sibling, which will expand the container.
.container {
display:inline-block;
position:relative;
box-shadow: 0 0 20px 2px yellow;
}
img {
vertical-align: bottom;
}
.overlay {
background-color: skyblue;
width:100px;
float:left;
}
<div class="container">
<img src="https://athome.reachtheworld.org/wp-content/uploads/2020/09/NYC.jpg">
<div class="overlay">
Text
</div>
</div>
You might have to use the full screen on the snippet to see it.
Now, if i use percentage instead of pixels on the width of the float, the container only expands as much as to contain the text of the float, while the extra width does not push the sibling. Instead the float is pushed down on a new line, and the extra width on the container remains to be seen on the right side. Why is this happening?
Or to be more specific, the width of the float does push the sibling but only until there's no more space, then the float is pushed down, and it keeps expanding on a new line, while the container maintains the width space of the original float size.
.container {
display:inline-block;
position:relative;
box-shadow: 0 0 20px 2px yellow;
}
img {
vertical-align: bottom;
}
.overlay {
background-color: skyblue;
width:10%;
float:left;
}
<div class="container">
<img src="https://athome.reachtheworld.org/wp-content/uploads/2020/09/NYC.jpg">
<div class="overlay">
Text
</div>
</div>
Here's a screenshot to illustrate my question.
Screenshot
I sort of understand that the float is based on percentage, so the container has to maintain that ratio, but even if both siblings are based on percentage, and then you increase the percentage of one of them, the container still maintains the width of when both siblings had perfect 100% width.
Here's what i mean:
.container {
display:inline-block;
position:relative;
box-shadow: 0 0 20px 2px yellow;
}
img {
vertical-align: bottom;
width:90%;
}
.overlay {
background-color: skyblue;
width:10%;
float:left;
}
<div class="container">
<img src="https://athome.reachtheworld.org/wp-content/uploads/2020/09/NYC.jpg">
<div class="overlay">
Text
</div>
</div>
Now, you see how both sibling take exactly 100%, but now if you make the width of the float 20%, it will be pushed down, as there is no more space, but the container will remain the same width as it was before based on the float being 10% only. This is what i mean. Why does it maintain that width?
Percentage need a reference so in order to find the percentage width of the childs we first need to know the one of the parent element which is also based on its content. We have a loop.
In such case, we first consider the width of the childs to be auto in order to identify the width of the parent then we use that width to calculate the one of the child elements and the size of the parent element will no more change (otherwise we will have an infinite loop).
Here is an illustration to better understand
.container {
display: inline-block;
position: relative;
border: 10px solid yellow;
margin:5px;
}
img {
vertical-align: bottom;
}
.overlay {
background-color: skyblue;
float: left;
}
<p>Initial state</p>
<div class="container">
<img src="https://picsum.photos/id/17/200/200">
<div class="overlay">
Text
</div>
</div>
<p>let use some percentage</p>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:20%">
<div class="overlay" style="width:80%">
Text
</div>
</div>
<br>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:80%">
<div class="overlay" style="width:80%">
Text
</div>
</div>
<br>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:40%">
<div class="overlay">
Text
</div>
</div>
<br>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:100%">
<div class="overlay" style="width:100%">
Text
</div>
</div>
<br>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:200%">
<div class="overlay" style="width:150%">
Text
</div>
</div>
You can clearly notice that in all the cases, the width of the parent remain unchanged and the child will use that width as a reference for their calculation. That width was initially calculated based on the default width of the text and the image.
Using pixel value is a different story as we don't have any complex calcuation and the parent will adjust its width based on the childs:
.container {
display: inline-block;
position: relative;
border: 10px solid yellow;
margin:5px;
}
img {
vertical-align: bottom;
}
.overlay {
background-color: skyblue;
float: left;
}
<p>Initial state</p>
<div class="container">
<img src="https://picsum.photos/id/17/200/200">
<div class="overlay">
Text
</div>
</div>
<p>let use some percentage</p>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:20px">
<div class="overlay" style="width:80px">
Text
</div>
</div>
<br>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:80px">
<div class="overlay" style="width:80px">
Text
</div>
</div>
<br>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:40px">
<div class="overlay">
Text
</div>
</div>
<br>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:100px">
<div class="overlay" style="width:100px">
Text
</div>
</div>
<br>
<div class="container">
<img src="https://picsum.photos/id/17/200/200" style="width:200px">
<div class="overlay" style="width:150px">
Text
</div>
</div>
For more accurate details you can refer to the specification: https://www.w3.org/TR/css-sizing-3/#intrinsic-contribution
I am really struggling with getting a text in the middle of every picture.
My gallery has 50 pictures and each of them has a different size. This is my code but it doesn't work :/ could anyone help, please? All "sample" text should be in the middle of the.
html:
<div class="row">
<div class="column">
<img src="/artbook/1.jpg">
<p> sample </p>
<img src="/artbook/2.jpg">
<p> sample</p>
<img src="/artbook/3.jpg">
<p> sample</p>
<img src="/artbook/4.jpg">
<P> sample </p>
<img src="/artbook/5.jpg">
<P> sample </p>
</div>
</div>
css:
.column img {
margin-top: 10px;
vertical-align: middle;
display: block;
align-content: center;
max-width: 250px;
.column p{
position: absolute;
text-align: center;
z-index: 1;
Something like this should work using an outer div container. It also uses the translate function to properly center the text both vertically and horizontally according to the parent container. One thing to keep in mind is that when you want to position something according to the parent container then the parent container must also be relative or absolute.
Also, by default, a paragraph element has extra padding around it. You may prefer to use a div instead.
.column div.outer {
position: relative;
display: inline-block;
}
.column img {
margin-top: 10px;
vertical-align: middle;
align-content: center;
max-width: 250px;
}
.outer div{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
<div class="row">
<div class="column">
<div class="outer">
<img src="1.jpg">
<div> sample1 </div>
</div>
<div class="outer">
<img src="2.jpg">
<div> sample2</div>
</div>
<div class="outer">
<img src="3.jpg">
<div> sample3</div>
</div>
<div class="outer">
<img src="4.jpg">
<div> sample4 </div>
</div>
<div class="outer">
<img src="5.jpg">
<div> sample5 </div>
</div>
</div>
</div>
Check it out.
First create a holder for your image and text. Give it position:relative.
Now, absolutely position your p element containing text relative to its holder. But extend your p to all sides with top:0; bottom:0; left:0; right:0;. Now p is occupying the whole holder. Just apply display:flex; to it and center the content with justify-content:center; align-items:center.
.holder{
position:relative;
display:inline-block;
}
.holder:hover img{
filter: brightness(100%);
}
.column img {
filter: brightness(45%);
margin-top: 10px;
vertical-align: middle;
display: block;
align-content: center;
max-width: 250px;
}
.column p{
position: absolute;
display:flex;
justify-content:center;
align-items:center;
z-index: 1;
top:0;
bottom:0;
left:0;
right:0;
}
<div class="row">
<div class="column">
<div class='holder'>
<img src="http://via.placeholder.com/350x100">
<p> sample </p>
</div>
<div class='holder'>
<img src="http://via.placeholder.com/350x150">
<p> sample </p>
</div>
</div>
</div>
I try to center pictures in the container. I've set left and right margin to 0 and still something is not working right.
#navbut {
width: 100%;
background: red;
margin: -7px 0 0 0;
color: white;
}
.container .box {
display: inline-block;
width: 10%;
margin-left: auto;
margin-right: auto;
}
.box img.Newspaper_pic {
width: 50%;
margin-left: auto;
margin-right: auto;
}
<section id="navbut">
<div class="container">
<div class="box">
<img src="http://placehold.it/100x100" alt="News-pic" class="Newspaper_pic">
</div>
<div class="box">
<img src="http://placehold.it/100x100" alt="News-pic" class="Newspaper_pic">
</div>
<div class="box">
<img src="http://placehold.it/100x100" alt="News-pic" class="Newspaper_pic">
</div>
</div>
</section>
What I am doing wrong that I cannot center pictures in one line?
If your images are set to inline-block, you have to use
text-align:center;
. If your images are set to block,
margin: 0 auto;
will work.
Looks like your pictures are centered, but only within the .box divs, you have to center those .box divs in the .container aswell. The .container also needs to be width 100% so it spans over the whole #navbut.
Try to use flex. flex is very easy to make for these kind of layouts because of these alignment properties.
Reference Link flex
Stack Snippet
body {
margin: 0;
}
.container, .box {
display: flex;
justify-content: center;
}
.Newspaper_pic {
display: block;
max-width: 100%;
}
<section id="navbut">
<div class="container">
<div class="box">
<img src="http://lorempixel.com/400/200/sports" alt="News-pic" class="Newspaper_pic">
</div>
<div class="box">
<img src="http://lorempixel.com/400/200/sports" alt="News-pic" class="Newspaper_pic">
</div>
<div class="box">
<img src="http://lorempixel.com/400/200/sports" alt="News-pic" class="Newspaper_pic">
</div>
</div>
</section>
Put whatever you want inside the center tags, e.g:
<center>
<img src="" alt="">
</center>
Description of Problem:
I'm attempting to arrange the kittens in a star-like pattern with 3 DIV "rows." I would like for the first top row's kitten to be centered on the page (easy enough); the second (or '#middle') row to have their cats left-aligned and right-aligned, respectively; and the third ('#bottom') row to have its cats aligned similar to the second row, but slightly indented on both sides. Again, like a star.
I know the float property essentially makes the element(s) absolutely positioned, which collapses the bottom two rows' height, so that's probably not the right answer. But I've also tried text-align and futzing with margins. My brain is fried. What am I doing wrong?
Fiddle:
http://jsfiddle.net/k97CG/1/
HTML Structure:
<div id="top">
<div id="container1" class="containers">
<div id="cat1">
<img src="http://placekitten.com/g/125/125" />
</div>
</div>
</div>
<div id="middle">
<div id="container2" class="containers">
<div id="cat2">
<img src="http://placekitten.com/g/125/125" />
</div>
</div>
<div id="container3" class="containers">
<div id="cat3">
<img src="http://placekitten.com/g/125/125" />
</div>
</div>
</div>
<div id="bottom">
<div id="container4" class="containers">
<div id="cat4">
<img src="http://placekitten.com/g/125/125" />
</div>
</div>
<div id="container5" class="containers">
<div id="cat5">
<img src="http://placekitten.com/g/125/125" />
</div>
</div>
</div>
CSS Structure:
.containers {
position: relative;
width: 125px;
height: 125px;
}
#top, #middle, #bottom {
position: relative;
width: 100%;
border: 1px solid red;
}
#container1 {
margin: 0 auto;
}
#container2 {
float: left;
}
#container3 {
float: right;
}
#container4 {
float: left;
}
#container5 {
float: right;
}
Is there a reason you can't just place them all in one div, then position them with CSS?
<div>
<img id="img01" src="img1">
<img id="img02" src="img1">
<img id="img03" src="img1">
<img id="img04" src="img1">
<img id="img05" src="img1">
</div>
then
div {
position:relative;
width:100%;
height:100%;
}
div img {
position:absolute;
}
#img01 {
top:x;
left:y;
} etc
As a rule, you shouldn't rely on HTML for visually styling content unless you have no other option. That's what CSS is for.
Is this the one you are looking for:
#top, #middle, #bottom {
position: relative;
width: 100%;
border: 1px solid red;
clear:both;
}
DEMO