I want to center an floated image and a container (paragraph + heading):
.row {
display: block;
/* width: 100%; */
}
.left {
float: left;
}
.right {
float: right;
}
<div class="row">
<div class="container">
<img class="right" src="" width="300" height="300" />
<h1>Heading</h1>
<p>Lorem ipsum dolor sit amet</p>
<div style="clear: both"></div>
</div>
</div>
<div class="row">
<div class="container">
<img class="left" src="" width="300" height="156" />
<h1>Heading</h1>
<p>Lorem ipsum dolor sit amet</p>
<div style="clear: both"></div>
</div>
</div>
Here is also a live version of the problem. I have cleared the floats but now I can't center the .img and .container element. What would you slove the problem?
You have no luck with floated elements since they don't obey vertical-align. You can instead use display: table-cell along with vertical-align: middle and that would work perfectly. Albeit you will need to modify your HTML structure a little bit to place the content first before the image and vice versa depending on the way you want the content and images to appear on the front-end.
.container {
display: table;
}
.content {
width: 50%;
display: table-cell;
vertical-align: middle;
}
.image {
width: 50%;
display: table-cell;
vertical-align: middle;
}
.image img {
width: 100%;
}
<div class="row">
<div class="container">
<div class="content">
<h1>Heading</h1>
<p>Lorem ipsum dolor sit amet</p>
</div>
<div class="image">
<img src="http://dev.dashbox.si/media/wysiwyg/vsebina-dashboxa.jpg" />
</div>
</div>
</div>
<div class="row">
<div class="container">
<div class="image">
<img src="http://dev.dashbox.si/media/wysiwyg/vsebina-dashboxa.jpg" />
</div>
<div class="content">
<h1>Heading</h1>
<p>Lorem ipsum dolor sit amet</p>
</div>
</div>
</div>
Basically what you can do to vertically align text is change the display of the container to table then the paragraph to table-cell and then set the vertical-align to middle looking like this:
.row {
display: table;
overflow: hidden;
}
.left, .right {
display: table-cell;
vertical-align: middle;
}
You might also need to set overflow to hidden on the container so that the height is maintained because of the floats.
Related
I've been working on an image gallery using flexbox with a flex-basis transition to grow and shrink the elements on hover. I am having a problem adapting the gallery to use flex-wrap to break to the next row after the 5th element.
The way I'm hoping for it to function is that each row has 5 elements that grow and shrink on the same row. After the a sixth element is placed in the container I want to break to the next row and have the elements continue functioning as expected (transitions included).
Elements stretch to fill available space (width of 20% for each of the 5 elements)
-> On hover, the other elements shrink 5% for a total of 20%
Hovered element grows to 40%
Sixth element that moves to next row inherits the size of the available space
Seventh element takes up 50% of row, eighth 33.3%...ect.
This is what I currently have: https://codepen.io/TommyBoyLab/pen/YdzGjB
(adapted from: https://codepen.io/joliveras/pen/GpLVKv)
HTML of element:
<div class="container">
<div class="item" style="background:url() center/cover">
CSS:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
display: flex;
flex-flow: row wrap;
width: 100%;
}
.container .item {
display: grid;
position: relative;
flex: 1;
transition: 500ms;
min-width: 15%;
max-width: 20%;
height: 50vh;
}
.container .item:hover {
transition: 500ms;
max-width: 40%;
flex-grow: 1;
}
.container .content {
margin: auto;
font-size: 1.5em;
}
It seems you only need to increase the value of flex-grow on hover:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
display: flex;
flex-flow: row wrap;
width: 100%;
}
.container .item {
display: grid;
position: relative;
flex: 1;
transition: 500ms;
min-width: 15%;
max-width: 20%;
height: 50vh;
}
.container .item:hover {
transition: 500ms;
max-width: 40%;
flex-grow: 5;
}
.container .content {
margin: auto;
font-size: 1.5em;
}
<div class="container">
<div class="item" style="background:url(https://images.unsplash.com/photo-1544227966-c89fe5bc0904?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1644&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
<div class="item" style="background:url(https://images.unsplash.com/photo-1544090372-c1fe7f8dee5a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
<div class="item" style="background:url(https://images.unsplash.com/photo-1544200502-6652e105f865?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
<div class="item" style="background:url(https://images.unsplash.com/photo-1544227966-c89fe5bc0904?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1644&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
<div class="item" style="background:url(https://images.unsplash.com/photo-1544090372-c1fe7f8dee5a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
<div class="item" style="background:url(https://images.unsplash.com/photo-1544200502-6652e105f865?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
<div class="item" style="background:url(https://images.unsplash.com/photo-1544200502-6652e105f865?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
<div class="item" style="background:url(https://images.unsplash.com/photo-1544227966-c89fe5bc0904?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1644&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
<div class="item" style="background:url(https://images.unsplash.com/photo-1544090372-c1fe7f8dee5a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1050&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
<div class="item" style="background:url(https://images.unsplash.com/photo-1544200502-6652e105f865?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80) center/cover">
<div class="content">
<h3>Title</h3>
<p>Lorem ipsum amet</p>
</div>
</div>
</div>
Addition to CSS:
.item:first-child:nth-last-child(n + 5),
.item:first-child:nth-last-child(n + 5) ~ *{
min-width: 17%;
}
The bb class background colour would be red if I move the code before the intro-block class css codes , however if I put it after the intro-block class css codes the colour is not changing and nothing happens!
Could anyone tell me why this happens?!
.left-column {
width: 35%;
margin-left: 20px;
margin-right: 50px;
float: left;
overflow: hidden;
}
.right-column {
overflow: hidden;
}
.intro-block {
background-color: #22AAA1;
margin: 0 auto;
/*max-width: 950px;*/}
}
.bb {
background-color: red;
}
<body>
<header>
<section class="intro-block">
<div class="left-column">
<img class="profile-pic right-column" src="img/11.jpg">
</div>
<div class="right-column">
<h1>lorem ipsum</h1>
<p>
<h4>lorem ipsum</h4>
</p>
</div>
</section>
</header>
<main>
<section class="bb">
<h3>lorem ipsum</h3>
<div class="left-column">
<div>
<p>lorem ipsum</p>
</div>
</div>
<div class="right-column">
<h5>lorem ipsum</h5>
</div>
You have an extra } just before .bb {}
Note: don't wrap headings (h1 to h6) in p
You should indent/tidy your code for better reading and with that you will find this mistakes easily
.left-column {
width: 35%;
margin-left: 20px;
margin-right: 50px;
float: left;
overflow: hidden;
}
.right-column {
overflow: hidden;
}
.intro-block {
background-color: #22AAA1;
margin: 0 auto;
/*max-width: 950px;*/
}
.bb {
background-color: red;
}
<body>
<header>
<section class="intro-block">
<div class="left-column">
<img class="profile-pic right-column" src="img/11.jpg">
</div>
<div class="right-column">
<h1>lorem ipsum</h1>
<h4>lorem ipsum</h4>
</div>
</section>
</header>
<main>
<section class="bb">
<h3>lorem ipsum</h3>
<div class="left-column">
<div>
<p>lorem ipsum</p>
</div>
</div>
<div class="right-column">
<h5>lorem ipsum</h5>
</div>
</section>
</main>
/max-width: 950px;/}
you have an extra } after the quoted line that messes up with the following rules ...
As simple as it normally is, for some reason I can't seem to get the same column heights for three divs in a row without using min-height or jQuery.
I am trying to use display:table on the main div and display:table-cell on the child's. I have no clue what is going wrong. I also tried other solutions, e.g. display:inline-block; and :before { content:"" }; as suggested in several other posts.
<div class="row">
<!-- 3 times in a row, other two blocks cut here for overview on SO -->
<div class="columns small-12 medium-4 large-4">
<div class="highlighted-contentblock">
<a href="#" target="_blank">
<div class="contentheader">
<div class="sequence">
1
</div>
<h2>
Lorem ipsum
</h2>
</div>
Lorem
</a>
</div>
</div>
</div>
Fiddle
.large-4 {
width: 33.33333%;
display:table; }
.highlighted-contentblock
{
background:gray;
display:table-cell;
}
Fiddle
The Problem
It looks like the original issue was trying to apply table-cell to the class highlighted-contentblock instead of columns. The columns class is the first child of your row class and table-cell should be applied there.
Columns
You had display table on your columns (applied on the highlighted-contentblock class). I replaced that with table-cell applied to the columns class.
Spacing
Float removes margin so I removed that and used border spacing on your table instead.
Support
This works but not in IE 7. CSS tricks has more info.
Click run code snippet below to see it in action.
/* Foundation replacement */
.row {
padding-bottom:10px;
max-width:980px;
margin:0 auto;
display:table;
border-spacing:0.9375rem;
}
.columns {
box-sizing:border-box;
display:table-cell;
background:gray;
}
.large-4 {
width: 33.33333%;
}
<div class="row">
<div class="columns small-12 medium-4 large-4">
<div class="highlighted-contentblock">
<a href="#" target="_blank">
<div class="contentheader">
<div class="sequence">
1
</div>
<h2>
Lorem ipsum
</h2>
</div>
Lorem
</a>
</div>
</div>
<div class="columns small-12 medium-4 large-4" data-equalizer-watch="">
<div class="highlighted-contentblock">
<a target="_blank" href=#>
<div class="contentheader">
<div class="sequence">
X
</div>
<h2>
Lorem ipsum <br/> Lorem ipsum
</h2>
</div>
Lorem ipsum</a>
</div>
</div>
<div class="columns small-12 medium-4 large-4" data-equalizer-watch="">
<div class="highlighted-contentblock">
<a target="_blank" href=#>
<div class="contentheader">
<div class="sequence">
3
</div>
<h2>
Lorem ipsum
</h2>
</div>
Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum Lorem ipsum</a>
</div>
</div>
</div>
Your html is a bit messy to be honest. You want to be following this simple structure:
<div class="row">
<div class="col">Content</div>
<div class="col">Content</div>
<div class="col">Content</div>
</div>
And then set a few simple CSS rules
.row {
display: table;
}
.col {
display:table-cell;
width:33.33%;
}
The crucial thing here is setting the width of your table cells.
I have updated your fiddle to show this in action
http://jsfiddle.net/eaqr1b17/5/
For your markup you have do to it like this:
http://jsfiddle.net/rvos3hx3/
/* Foundation replacement */
.row {
padding-bottom:10px;
max-width:980px;
margin:0 auto;
display:table;
}
.columns {
position: relative;
padding-left: 0.9375rem;
padding-right: 0.9375rem;
box-sizing:border-box;
background:gray;
display:table-cell;
}
.large-4 {
width: 33.33333%;
}
I think this is what you need:
HTML:
<div class="wrap">
<div class="row">
<div class="cell">
Lorem
</div>
<div class="cell">
Lorem impsum more text
</div>
<div class="cell">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</div>
And for the css:
.wrap{
overflow:hidden;
width:250px;
display: table;
border-collapse: collapse;
}
.row {
display: table-row;
}
.cell{
width: 33.33%;
display: table-cell;
background-color: #0f0;
}
if you want to see it running click here
Fiddle
Here is a jsfiddle : http://jsfiddle.net/nrnLbv14/
HTML :
<div class="container">
<div class="row">
<div class="column">content 1</div>
<div class="column">content 2</div>
<div class="column">content 3</div>
</div>
</div>
CSS :
.container {
display:table;
/* just styling to show it works */
border-collapse: separate;
border-spacing: 10px;
background-color: gray;
}
.row {
display:table-row;
}
.row .column {
display:table-cell;
background-color: #fff;
width: 33.33%; // If you want all 3 columns equal width
}
With floated element, the table/table-cell rendering is a bit difficult to render correctly...
If you can, remove the float element, and display them side by side with the table-cell.
The second problem of your code is that you have a background on the child element of the one that is rendered with a table-cell. So the height of all .column elements will be the same, but you won't see it with the background-color, because that property is setted to the child element that has its own height...
Here is an update of your fiddle without the float and with a border to show you that the height is correctly setted, but you don't see it because your bg-color is not applied to the right element :
http://jsfiddle.net/eaqr1b17/6/
/* Foundation replacement */
.row
{
padding-bottom:10px;
display:table;
border: 1px solid red;
}
.columns {
padding-left: 0.9375rem;
padding-right: 0.9375rem;
box-sizing: border-box;
display: table-cell;
border: 1px solid green;
}
.large-4 {
width: 33.33333%;
}
.highlighted-contentblock
{
background:lightgrey;
}
I have the following problem:
There's an image floating left and a margin to the right.
Next to it, there is a div containing a headline and a text (I don't know any height-values of both, because it will be inserted dynamically).
The headline must align to the top and the text to the bottom. I thought it's enough to position the text absolute, but if the height of the image is smaller than the height of the headline + the text, the text flows into the headline....
I haven't found any solution to position the text at the bottom but letting it stay in document flow.
I am not allowed to use any table-elements (on the otherhand, display: table and so on is allowed, but I haven't figured out any solution with that as well)
<<<HTML
<div>
<h4>A headline, that isn't involved</h4>
<div class="clearfix">
<div> <!-- float left, margin-right -->
<img>
</div>
<div> <!-- float left -->
<h5>The headline aligning to the top</h5>
<p>
Some text aligning to the bottom
</p>
</div>
</div>
</div>
HTML
Please help me, I just can't figure out any solution!
/EDIT:
Both the imnages' and text-/headline-containers' height may vary, so no fixed height.
What i got so far is (by assuming, the text wont have more than 4 lines (but thats not the best way). The next Problem is: Firefox adds the margin-bottom of .box to the bottom: 0; of the text (like: bottom: -35px; But shown as bottom: 0; ... Chrome does interpret that the right way):
<style>
.box {
width: 488px;
float: left;
margin-bottom: 33px;
margin-right: 22px;
padding-top: 5px;
}
.table {
display: table;
}
.box.wide .box-content {
display: table-row;
}
.box.wide .box-content > div {
display: table-cell;
position: relative;
width: 233px;
}
.box.wide .box-content > div:first-child {
margin-right: 22px;
}
.box.wide .box-content div h5 {
padding-bottom: 88px;
}
.box.wide .box-content div p {
position: absolute;
bottom: 0px;
}
</style>
<div class="box wide width-488">
<div>
<h4>Überschrift</h4>
<div class="table">
<div class="box-content">
<div>
<img alt="" height="401" width="233" src="res/dummy/233-130.jpg">
</div>
<div>
<h5>Überschrift Lorem ipsum dolor sit amet, con sectetuer adipiscing eliÜberschrift Lorem ipsum dolor sit amet, con sectetuer adipiscing elit</h5>
<p>
Lorem ipsum dolor sit amet, con sectetuer adipiscing elit. Aenean commodo ligula eget dolor. Lor em ipsum dolor amet.
mehr
</p>
</div>
</div>
</div>
</div>
</div>
try using display: inline-block; on both floating elements and the text element that you want aligned to the bottom.
Then put the property vertical-align: bottom; on the text element you want aligned to the bottom.
I assumed you can make the right column a fix height, since the left column & right are the same in your image example.
I made a jsfiddle for your convenience: http://jsfiddle.net/hLPXM/
Alternately, here is what I did, based on your original code:
<h4>A headline, that isn't involved</h4>
<div class="clearfix">
<div class="left"> <!-- float left, margin-right -->
<img src="http://placehold.it/150x350" alt="placeholder" />
</div>
<div class="right"> <!-- float left -->
<h5>The headline aligning to the top</h5>
<div class="bottom-text">
<p>
Some text aligning to the bottom
</p>
</div><!-- .bottom-text -->
</div>
</div>
Note I added a .bottom-text class around the <p> that you want to align bottom.
Here is the CSS for the divs to float properly, etc. Note the position:relative; :
.left {float:left; margin-right:20px;}
.right {float:left; background:#eeddff; /*background to see div*/}
.left, .right {height:350px; position:relative;}
And more importantly the text starting from the baseline:
.bottom-text {
position:absolute;
bottom:0;
}
Here is a solution for you, using display: table, relative and absolute positioning:
<div>
<h4>A headline, that isn't involved</h4>
<div style="display:table;">
<div style="display:table-row;">
<div style="display:table-cell;padding-right: 20px;">
<img style="display:block" src="http://baconmockup.com/300/200">
</div>
<div style="display:table-cell;background:green;position:relative;vertical-align:top;">
<p style="">Some text aligning to the top</p>
<p style="position:absolute;bottom:0;">Some text aligning to the bottom</p>
</div>
</div>
</div>
</div>
It does not rely on any fixed heights, and adopts to the height of the image automatically.
jsfiddle.net/EUvXh/
I don't know if you have already fixed this but the easiest way is to add margin-top: auto; to your p tag.
I have written the following HTML trying to center two div's next to each other.
<div id='wrapper' style='text-align:center;'>
<div style='float:left;'>
Lorem ipsum<br />
dolor sit amet
</div>
<div style='float:left;'>
Lorem ipsum<br />
dolor sit amet
</div>
</div>
However, the code I've written results in the two div's floating all the way to the left. What this does do correctly is float the two div's side by side.
What do I need to change to center the two div's side by side?
Instead of using float: left, use display: inline-block:
<div id='wrapper' style='text-align: center;'>
<div style='display: inline-block; vertical-align: top;'>
Lorem ipsum<br />
dolor sit amet,<br />
consectetur adipisicing elit
</div>
<div style='display: inline-block; vertical-align: top;'>
Lorem ipsum<br />
dolor sit amet
</div>
</div>
The top of each inner div is kept aligned by using vertical-align: top.
Example: http://jsfiddle.net/hCV8f/1/
You will have to automatically set the margin and probably a specific width to your wrapper div
<div id="wrapper"></div>
In your CSS:
#wrapper {
width: 70%;
margin: 0 auto;
}
Try this:
<div id='wrapper' style='text-align:center;'>
<div style='float:left;background-color:red;width:50%'>
Lorem ipsum<br />dolor sit amet
</div>
<div style='float:right;background-color:blue;width:50%'>
Lorem ipsum<br />dolor sit amet
</div>
</div>
http://jsfiddle.net/JDAyt/
Do you know the width of both divs in advance? If so, you can just do something like
<div class="wrapper" style="margin: 0 auto; width: 200px">
<div class="inner1" style="width: 100px; float:left;"></div>
<div class="inner2" style="width: 100px; margin-left: 100px"></div>
</div>
Below worked for me
<div id='wrapper'>
<div class='inner'>
content 1
</div>
<div class='inner'>
content 2
</div>
</div>
CSS:
#wrapper
{
text-align: center;width:auto;margin:0 auto
}
.inner
{
display:inline-block;
}
For the left div, set the left margin. For the right div, set the right. Like this:
#leftDiv {
margin-left: auto;
}
#rightDiv {
margin-right: auto;
}
This puts them back to back in the center of the screen.
Below code works fine with Zebra GC420d Printer:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<style type="text/css">
html, body { padding: 0px; margin: 0px; width: 100%; height: 100%; }
#div { min-height: 100%; }
</style>
</head>
<body>
<div style="border: 0px solid red;">
<table border="0" width="100%" align="center">
<tr>
<td style="text-align: center;">
<?php
echo $name;
?>
</td>
</tr>
<tr><td></td></tr>
<tr>
<td style="text-align: center;">
<?php
echo 'https://goo.gl/2QvRXf';
?>
</td>
</tr>
</table>
</div>
</body>
</html>
Hope it helps !