CSS vertical align based on height of other element - html

I'm trying to create a block with an image that contains content by your side, where I intend to align the image vertically based on the content's height. I've tried alignments with table/table-cell technique but I can't get what I want..
click here to see what I want:

You can solve this with flex-box, here is one way to do it:
body
{
padding: 20px 0;
}
.gallery-item
{
background-color: gray;
display: flex;
margin-bottom: 60px;
}
.gallery-item
{
flex: 1 0 auto;
}
.gallery-item .image-container
{
position: relative;
flex: 0 0 auto;
align-self: center;
}
.gallery-item .image-container img
{
margin: -20px auto;
}
.gallery-item .text-container
{
padding: 10px;
}
<div class="gallery-item">
<div class="image-container">
<img src="http://www.placehold.it/200x200">
</div>
<div class="text-container">
<p>Text</p>
</div>
</div>
<div class="gallery-item">
<div class="image-container">
<img src="http://www.placehold.it/200x200">
</div>
<div class="text-container">
<p>Text</p><p>Text</p><p>Text</p><p>Text</p><p>Text</p><p>Text</p><p>Text</p><p>Text</p>
</div>
</div>
And here is a codePen: http://codepen.io/anon/pen/xGdGPV
A lot of the code is not mandatory, like the margins for .gallery-item and the padding for the body, but you get the idea.

This solve my question without using flex-box, I need it works in old browsers
.container {
position: relative;
background:#eee;
padding: 20px 20px 20px 450px;
}
img {
position:absolute;
left:0;
top:50%;
margin-top:-100px;
}

the first thing you have to do is put display: table to de container and display:table-cell; vertical-align:middle to lements inside the container.

Related

3 Horizontal Box - CSS

What i would like to achieve is that i want to change the way my boxes appear only on the mobile version of the website. I experimented with display: and flex: rules but had no luck. I want them stick to each other but Couldn't find the right CSS rule. Help.
Thanks.
Example picture of how i want them:
The way they appear on desktop version of the website:
.m-image {
border: 5px dashed black;
width: 100%;
height: 70px;
margin: 10px 0;
position: relative;
display: inline-block;
vertical-align: top;
overflow: hidden;
}
img {
width: 100%;
height: 300px;
}
<div class="m-image">
<img srcset="https://images.pexels.com/photos/6395415/pexels-photo-6395415.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1"><img/></div>
<div class="m-image">
<img src="https://images.pexels.com/photos/6044656/pexels-photo-6044656.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1"><img/></div>
<div class="m-image">
<img alt="" loading="lazy" src="https://images.pexels.com/photos/6045291/pexels-photo-6045291.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" /><img /></div>
.m-image {
border: 5px dashed black;
width: 100%;
height: 70px;
margin: 10px 0;
position: relative;
display: inline-block;
vertical-align: top;
overflow: hidden;
}
img {
width: 100%;
height: 300px;
}
#media only screen and (max-width: 1000px) {
.main {
display: flex;
gap: 10px;
}
.m-image {
border: solid black 3px;
}
}
<div class = "main">
<div class="m-image">
<img srcset="https://images.pexels.com/photos/6395415/pexels-photo-6395415.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1">
</div>
<div class="m-image">
<img src="https://images.pexels.com/photos/6044656/pexels-photo-6044656.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1">
</div>
<div class="m-image">
<img alt="" loading="lazy" src="https://images.pexels.com/photos/6045291/pexels-photo-6045291.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" />
</div>
</div>
Couple of things to think about when it comes to mobile view. When wanting certain items to display a certain way on mobile view you want to use #media only screen and (max-width: /* width of the device */. Place all the CSS rules inside of here. These rules will change the rules set above or run new rules that you have define below.
Also, when it comes to display: flex; you want to make sure you wrap it into another div. This "wrapper" or "container" will provide the structure to the way you want the images to display.
add a main container to compress the class m-image then add display flex
Ex:
<div id="main-container">
<div class="image"></div>
<div class="image"></div>
<div class="image"></div>
</div>
.main-container {
display: flex;
}
then add padding left and right to your m-image class
what you should do in this case is put your images in a single container and apply flex property in the css.
basically manipulate your html and css like this.
HTML:
<div class="image-container">
<div class="m-image">
<img srcset="https://images.pexels.com/photos/6395415/pexels-photo-6395415.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1"><img/>
</div>
<div class="m-image">
<img src="https://images.pexels.com/photos/6044656/pexels-photo-6044656.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1"><img/>
</div>
<div class="m-image">
<img alt="" loading="lazy" src="https://images.pexels.com/photos/6045291/pexels-photo-6045291.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=1" /><img />
</div>
</div>
CSS:
.image-container{
display:flex;
gap: 8px;
flex-wrap: nowrap;
}
.m-image {
border: 5px dashed black;
width: 100%;
height: 70px;
margin: 10px 0;
position: relative;
display: inline-block;
vertical-align: top;
overflow: hidden;
}
img {
width: 100%;
height: 300px;
}
This should give you your desired result for the desktop and for the mobile version you will have to apply media query into your CSS code.
hope this answers your question!
I couldn't really understand your code, So I wrote one for you suiting your needs
I hope it will fix your problem!
#content{
display: flex;
justify-content: space-around;
width: 100%;
}
.imagecontainer{
overflow: hidden;
width: 30%;
}
img{
width: 100%;
}
<div id="content">
<div class="imagecontainer"><img src="https://images.pexels.com/photos/6395415/pexels-photo-6395415.jpeg"></div>
<div class="imagecontainer"><img src="https://images.pexels.com/photos/6044656/pexels-photo-6044656.jpeg"></div>
<div class="imagecontainer"><img src="https://images.pexels.com/photos/6045291/pexels-photo-6045291.jpeg"></div>
</div>

Overlapped, multiple images in a responsive container

I have a responsive container and one image within it. It works good. The container and the image are resizing when the window-size changes. But, I need not only one, but more images which exactly overlap each other (all of them have the same size). How can I achieve this with HTML and CSS?
.pageCenter {
display: block;
max-width: 980px;
margin: 0 auto;
width: auto;
float: none;
border: 5px solid red;
}
.imageContainer img {
display: block;
margin: 0 auto;
width: auto;
}
img {
position: relative;
}
<div class="pageCenter">
<div class="imageContainer">
<img src="https://placeimg.com/400/200/nature" style="width:100%;" />
</div>
</div>
You could use grid in order to specify what row and column you want all images to sit in. They will then overlap and adjust to your container (do note the images I am using are all the same picture, but if you inspect, the 3 images are on-top of eachother:
.pageCenter {
display: block;
max-width: 980px;
margin: 0 auto;
width: auto;
float: none;
border: 5px solid red;
}
.imageContainer{
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr;
margin: 0 auto;
width: auto;
}
img {
position: relative;
grid-row: 1;
grid-column: 1;
}
<div class="pageCenter">
<div class="imageContainer">
<img src="http://www.fillmurray.com/800/400" style="width:100%;" />
<img src="http://www.fillmurray.com/800/400" style="width:100%;" />
<img src="http://www.fillmurray.com/800/400" style="width:100%;" />
</div>
</div>
Also note that grid isn't supported by all old browser
.imageContainer {
position:relative;
}
.imageContainer .img-1 {
position:absolute;
top:0;
left:0;
}
.imageContainer .img-2 {
float:left;
}
.clearfix {
clear:both;
}
<div class="pageCenter">
<div class="imageContainer">
<img src="https://wow.olympus.eu/webfile/img/1632/x=1024/oly_testwow_stage.jpg" class="img-1" />
<img src="https://wow.olympus.eu/webfile/img/1632/x=1024/oly_testwow_stage.jpg" class="img-1" />
<img src="https://wow.olympus.eu/webfile/img/1632/x=1024/oly_testwow_stage.jpg" class="img-1" />
<img src="https://wow.olympus.eu/webfile/img/1632/x=1024/oly_testwow_stage.jpg" class="img-2" />
<div class="clearfix"></div>
</div>
</div>
You need to set the parent to position:relative; and the images (.child) to position:absolute;
Updated with images, they overlap.
Updated again with your code.
Update #3: You need to set one image to position:absolute and one to float and add a clearfix element.

Horizontally align floated texts to both sides of centered img

I'm relatively new to some concepts in HTML and CSS and this one I can't seem to wrap my head around. I'm trying to align two floating <p> elements to both sides of a centered <img>. Here's what I'm basically trying to achieve.
I want the two texts to stick to the sides of the centered image even when the window is at full width. I have it working only when I resize the window to the point where they're squished to the sides of the image.
.header {
margin-top: 85px;
text-align: center;
}
<div class="header">
<p style="float: left; margin-top: 115px;">listen</p>
<img id="main_cover" src="img/into-me.png" width="250" height="250" draggable="false">
<p style="float: right; margin-top: 115px;">download</p>
</div>
Any help is appreciated, especially since this may be a dumb question. Thanks in advance.
You can use Flexbox or CSS tables Fiddle
.content {
display: flex;
align-items: center;
justify-content: center;
}
<div class="content">
Listen
<img src="http://placehold.it/250x250">
Download
</div>
Do this...
<div class="header">
<p>listen</p>
<img id="main_cover" src="img/into-me.png" width="250" height="250" draggable="false">
<p>download</p>
</div>
And your CSS...
.header { width: 400px; margin: 0 auto}
.header img { margin: 0 20px; float: left; }
.header p { margin: 115px 0 0; float: left;}
You could have just given the container a defined width so the floated elements wouldn't float further away as the window size changes:
.header {
margin-top: 85px;
text-align: center;
width: 370px;
margin: 0 auto;
}
https://jsfiddle.net/8sfvgvgr/
How about flexboxes to obtain this?
header{
display: flex;
justify-content: space-around;
align-items:center;
}
span{
text-align:center
}
img{
margin-left: 10px;
margin-right: 10px;
width: 250px;
}
<header>
<span>Text</span>
<img src="https://pixabay.com/static/uploads/photo/2014/07/27/20/29/landscape-403165_960_720.jpg" alt="Landascape">
<span>Text</span>
</header>
You can do this with Flex.
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: -webkit-flex;
display: flex;
width: 400px;
height: 250px;
}
.flex-item {
width: 100px;
height: 100px;
margin: 10px;
}
</style>
</head>
<body>
<div class="flex-container">
<div class="flex-item">listen</div>
<div class="flex-item">Image </div>
<div class="flex-item">Download</div>
</div>
</body>
</html>

How to center group of divs inside div?

I am a bit newbie with CSS and i am pretty obfuscated trying to center a group of divs inside a div. What i want:
divs 2,3 and 4 should be centered inside div1.
My approach:
.div1 {
display: inline-block;
margin: 0 auto;
text-align: center;
}
.restofdivs {
width: 470px;
margin: 20px;
min-height: 1px;
float:center
}
the result is: the 3 divs (2,3 and 4) one on top of another...
Regards,
This can easily be done with table display:
.table-display {
display: table;
width: 100%;
}
.cell-display {
display: table-cell;
}
.div1, .div2, .div3, .div4 {
padding: 40px;
}
.div1 {
background: #ABC;
}
.div2 {
background: #DEF;
}
.div3 {
background: #CAD;
}
.div4 {
background: #FAD;
}
<div class="div1">
<div class="table-display">
<div class="cell-display div2"></div>
<div class="cell-display">
<div class="div3"></div>
<div class="div4"></div>
</div>
</div>
</div>
Maybe set a width on .div1 and remove inline-block from .div1
.div1 {
width: 960px;
margin: 0 auto;
text-align: center;
}
.restofdivs {
width: 470px;
margin: 20px;
min-height: 1px;
}
The most common way to center a block element if you know it's width is to define the width and use "margin: 0 auto". This tells the browser to give a top and bottom margin of 0, and to automatically determine equal margins on the left and right.
Using floats, you can create the layout you described as follows:
http://jsfiddle.net/ynt4suee/
Markup:
<div>
<div id="one" class="border clearfix">one
<div id="wrapper">
<div id="two" class="border">two</div>
<div class="subcontainer">
<div id="three" class="border">three</div>
<div id="four" class="border">four</div>
</div>
</div>
</div>
CSS:
div.border{
border: 1px solid red;
}
div#wrapper{
width: 400px;
margin: 0 auto;
}
div#two{
width: 250px;
float: left;
}
div.subcontainer{
float: right;
width: 130px;
}
.clearfix:after {
content: " "; /* Older browser do not support empty content */
visibility: hidden;
display: block;
height: 0;
clear: both;
}
Here's another approach, using inline-block elements for the inner divs instead:
http://jsfiddle.net/xojqq4v5/
Markup:
<div id="one" class="border">
div 1
<div id="wrapper">
<div id="two" class="border">div 2</div>
<div id="subcontainer">
<div id="three" class="border">div 3</div>
<div id="four" class="border">div 4</div>
</div>
</div>
</div>
CSS:
div.border{
border: 1px solid red;
margin-bottom: 5px;
}
div#wrapper{
width: 450px;
margin: 0 auto;
}
div#two, div#subcontainer{
display: inline-block;
vertical-align: top;
}
div#two{
width: 300px;
}
div#three, div#four{
width: 140px;
}
Still, so long as you know the total width of the inner divs, you can center the wrapper using "margin: 0 auto", which has the advantage of not centering text on all child elements unless otherwise specified.
The difference here is that to lay out the inner divs in columns, div 2 and the container div containing divs 3 and 4 are defined as inline-block elements.

Content goes out of div

Really can't figure out what's wrong with it, but all the content I add into div, goes out of it, just like it's not in it.
Check it here: JSFiddle!
HTML___
<div id="wrapper">
<div id="container">
<div id="header">
<div id="logo">
TEXT GOES OUTSIDE OF DIV :'((
</div>
</div>
</div>
</div>
CSS___
#container {
width: 960px;
margin: 20px auto 0 auto;
background: yellow;
}
#header {
position: relative;
width: 100%;
background: yellow;
border: 1px solid black;
padding: 2px; /*just to see the div*/
}
#logo {
float: left;
}
You need to clear your floats:
<div id="wrapper">
<div id="container">
<div id="header">
<div id="logo">
TEXT NOW APPEARS INSIDE DIV :)
</div>
<div style="clear: both;"></div>
</div>
</div>
</div>
Because you've floated your logo, any content following it will wrap around it. Which is what is causing the effect you're seeing.
Add overflow:auto to your #header div to restore the expected behavior:
#header {
position: relative;
width: 100%;
background: yellow;
border: 1px solid black;
overflow:auto;
}
jsFiddle example
Floating the child essentially removes it from the flow and the parent collapses. Adding the overflow rule gives you the behavior you expected.
I'd urge you to use flex. It's quite robust and lets you create any kind of layout you want without any issues really. I've added a menu to the right hand side just to illustrate your logo in actual context.
<!-- HTML -->
<div id="wrapper">
<div id="container">
<div id="header">
<div id="logo">
TEXT GOES OUTSIDE OF DIV :'((
</div>
<div id="content-menu">
<div id="menu">
Home
Contact
About
About
</div>
</div>
</div>
</div>
</div>
Corresponding CSS:
/* CSS */
#container {
width: 960px;
margin: 20px auto 0 auto;
background: yellow;
}
#header {
position: relative;
width: 100%;
margin: 1.2em auto;
background: yellow;
border: 1px solid black;
padding: 2px; /*just to see the div*/
display: flex;
}
#logo { flex: 1; }
#content-menu { flex: 4;}
#menu { display: flex; }
#menu > a {
display: inline-block;
text-align: center;
line-height: 32px;
text-decoration: none;
color: #000;
flex: 1;
}