Center Div inside Div with width as auto - html

I have a Div1, and Div2 inside Div1.
Div2 has image and variable length text in it, I want to keep the width of Div2 as auto and center it in the Div1.
As described below:
+-------------------------------------------------------------------+
| Div1 |
| +-------+--------------------+ |
| | Image | Some Test Text | <- Div2: Width:auto; |
| +-------+--------------------+ |
| |
+-------------------------------------------------------------------+
I am trying to create it but facing issue to cover the background Div to text and image, actually I want to keep Div height and width as Auto but it is not working as expected:
http://jsfiddle.net/2vhr4nLz/

If I've understood your question correctly, you should use css3 flexbox for this.
.text-and-image-holder {
justify-content: center;
align-items: center;
display: flex;
}
Above css will create the following layout as you can see in working snippet that text and image will be vertically middle aligned with respect to each other:
+---------------------------------------+
| | Lorem ipsum dolor sit |
| | amet, lorem ipsum dolor |
| | sit amet, lorem ipsum |
| <img> | dolor sit amet, lorem |
| | ipsum dolor sit amet, |
| | lorem ipsum dolor sit |
| | amet, lorem ipsum |
+---------------------------------------+
.image-style {
margin-right: 20px;
}
.text-style {
font-size: 15px;
}
.text-and-image-holder {
justify-content: center;
align-items: center;
margin-bottom: 20px;
background: red;
padding: 20px;
display: flex;
}
<div class="text-and-image-holder">
<img src="http://placehold.it/30x30" class="image-style">
<div class="text-style">Vertically Center Text Here</div>
</div>
<div class="text-and-image-holder">
<img src="http://placehold.it/30x30" class="image-style">
<div class="text-style">Very long text Vertically Center Here and lorem ipsum dolor sit amet.</div>
</div>

If I have understood, you can wrap all your content into a and then add this to your css
.wrapper {width:100%; text-align: center }

For Auto width: You can use CSS3 Flexbox.
For fixed width:
I modified your code and following are the changes;
HTML:
<div class="div1">
<div class="div2">
<img src="#">
<span >Vertically Center Text Here</span>
</div>
</div>
CSS:
.div1 {
background: red;
padding: 20px;
width: 100%;
}
.div2 {
background: white;
margin: auto;
width: 350px;
}

Related

Text that is absolute positioned to the bottom of a div needs to grow/expand down instead of up

I have a three column layout that positions the Calls to action at the bottom using absolute positioning and a height of 100% on the container:
----------------------------- ----------------------------- -----------------------------
| Col 1 | | Col 2 | | Col 3 |
| | | | | |
| Copy | | Copy | | Copy |
| | | | | |
| | | | | Call to action 3 is |
| Call to action 1 | | Call to action 2 | | Longer text |
----------------------------- ----------------------------- -----------------------------
But what I want is for the first sentence in the paragraph to be even with the others, even if the rest overflows outside the container:
----------------------------- ----------------------------- -----------------------------
| Col 1 | | Col 2 | | Col 3 |
| | | | | |
| Copy | | Copy | | Copy |
| | | | | |
| | | | | |
| Call to action 1 | | Call to action 2 | | Call to action 3 is |
----------------------------- ----------------------------- -----------------------------
Longer text
I was able to accomplish this by using top:300px instead of bottom:10px on the CTAs, but this isn't ideal because you can't change the # of characters in the copy without adjusting the top:300px value. Any help is appreciated.
You can manage this by pushing the CTA completely out of the card and then using a transform value equal to your line-height to pull it back up.
.container {
display: flex;
}
.card {
flex: 1;
padding: 1em;
padding-bottom: 2.2em;
border: 1px solid green;
margin: 1em;
position: relative;
}
.cta {
position: absolute;
top: 100%;
border: 1px solid red;
padding: 0 1em;
left: 0;
right: 0;
transform: translateY(-1.2em);
/* line-height */
}
<div class="container">
<div class="card">
<h2>Heading 1</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Natus, voluptatem.</p>
<div class="cta">CTA</div>
</div>
<div class="card">
<h2>Heading 2</h2>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Voluptate repellendus in magni?</p>
<div class="cta">CTA Lorem ipsum dolor sit amet</div>
</div>
<div class="card">
<h2>Heading 3</h2>
<p>Lorem ipsum dolor sit amet.</p>
<div class="cta">CTA Lorem ipsum dolor sit amet, consectetur adipisicing elit. </div>
</div>
</div>
Paulie_D's idea may also work, but 04FS's solution to use top:calc(100% - 10px) instead of bottom worked like a charm! Thanks

How to custom align content in columns but keep text left aligned?

I'm trying to align text content in a column. I don't know how I can align the whole content to the center or to the right of the column and keep the text content left aligned.
I have added an example to jsfiddle: https://jsfiddle.net/k58vr4nq/
Currently the colums looks like this:
|-----------------------------------------------|
|Lorem ipsum |Lorem ipsum |Lorem ipsum |
|dolor sit |dolor sit |dolor sit |
|amet |amet |amet |
|consetetur |consetetur |consetetur |
|sadipscing |sadipscing |sadipscing |
|-----------------------------------------------|
I'm expecting something like this:
|-----------------------------------------------|
|Lorem ipsum | Lorem ipsum | Lorem ipsum|
|dolor sit | dolor sit | dolor sit |
|amet | amet | amet |
|consetetur | consetetur | consetetur |
|sadipscing | sadipscing | sadipscing |
|-----------------------------------------------|
First, you add your text for each column in div, so it becomes element with width of widest text line. Then, you use flexbox on parent (col element) and align the divs however you want.
HTML:
<div class="container">
<div class="row">
<div class="col text-container left">
<div>
Lorem ipsum<br/>
dolor sit<br/>
amet<br/>
consetetur<br/>
sadipscing<br/>
</div>
</div>
<div class="col text-container middle">
<div>
Lorem ipsum<br/>
dolor sit<br/>
amet<br/>
consetetur<br/>
sadipscing<br/>
</div>
</div>
<div class="col text-container right">
<div>
Lorem ipsum<br/>
dolor sit<br/>
amet<br/>
consetetur<br/>
sadipscing<br/>
</div>
</div>
</div>
</div>
and CSS:
.col {
border: solid 1px #6c757d;
}
.text-container {
display: flex;
}
.middle {
justify-content: center;
}
.right {
justify-content: flex-end;
}
Here's jsfiddle of complete solution: https://jsfiddle.net/7vetqkrz/
For that purpose, you would have to wrap your col content inside an element:
<div class="col">
<div class="content">
Lorem ipsum<br/>
dolor sit<br/>
amet<br/>
consetetur<br/>
sadipscing<br/>
</div>
</div>
Then you would have to style that content class with some margin:
.content {
margin-left: 1rem;
}
Fiddle: https://jsfiddle.net/2ukyqo7m/4/
Add your content into a block with display: inline-block so it fits to size, align within that block, then align the block.
See below (I wouldn't do it inline, it's there to show what's going on):
Here's your fiddle updated
<div class="container">
<div class="row">
<div class="col">
<div style="display: inline-block; text-align: left">
Lorem ipsum<br/> dolor sit<br/> amet
<br/> consetetur
<br/> sadipscing
<br/>
</div>
</div>
<div class="col text-center">
<div style="display: inline-block; text-align: left">
Lorem ipsum<br/> dolor sit<br/> amet
<br/> consetetur
<br/> sadipscing
<br/>
</div>
</div>
<div class="col text-right">
<div style="display: inline-block; text-align: left">
Lorem ipsum<br/> dolor sit<br/> amet
<br/> consetetur
<br/> sadipscing
<br/>
</div>
</div>
</div>
</div>
add to CSS
.text-center{
text-align: left;
padding-left: 1em;
}
.text-right{
}

Equal height table cells with a canvas inside: padding bug?

We have a fix set of CSS rules, but when we modify the HTML markup to include a canvas, a weird padding appears on the neighboring cell. Here is the CSS:
.wrap{
width:100%;
display: table;
}
.row {
display: table-row;
}
.left{
width: 100px;
display: table-cell;
background-color: #0f0;
}
.right{
background-color: #f00;
display: table-cell;
}
Normal case:
See the fiddle here. Note the position of the text in the red cell: top, aligned with the top of the cell.
<div class="wrap">
<div class="row">
<div class="left">
Lorem<br>Lorem<br>Lorem<br>Lorem<br>Lorem
</div>
<div class="right">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</div>
</div>
Canvas case:
See the fiddle here. We swapped the Lorem text in the left cell for a 90x90 canvas. See how the text on the red cell is now aligned with the bottom of the canvas and a padding is applied to the cell.
<div class="wrap">
<div class="row">
<div class="left">
<canvas width='90px' height='90x'></canvas>
</div>
<div class="right">
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</div>
</div>
</div>
Question: Could you explain why this padding appears on the left cell, and if this is an expected behavior? Also, could you propose a solution that gets rid of this 'bug'?

Wrap text which does not fit page instead of moving it below others

I am trying to place elements in my header. I would like to have 3 elements inline - button, image and simple text. The height of the header should be equal height of image. All elements should be centered vertically. Here's my HTML:
<div class="btn">
...
</div>
<img src="image.jpg">
<span style="float:none; display: inline-block; vertical-align: middle">lorem ipsum lorem ipsum</span>
On image "a" I present expected behavior. On image "b" and "c" my results were shown.
So, the expected result is to wrap text if it doesn't fit the page. But it still should be on the right side.
Legend:
red rectangle - button
orange rectangle - image
Does anyone know what styles I should use?
You should make them:
display: inline-block
Here is pretty simple demo:
HTML:
<div class="row">
<div class="btn">...</div>
<img src="http://www.ibm.com/developerworks/data/library/techarticle/dm-0504stolze/test_1.jpg" />
<span>lorem ipsum<br/> lorem ipsum</span>
</div>
CSS:
* {
margin: 0;
padding: 0;
}
.row > * {
display: inline-block;
vertical-align: middle;
}
.btn {
width: 30px;
height: 30px;
background-color: red;
}
Demo
Another way.
HTML:
<div class="row">
<div class="btn">...</div>
<img src="http://www.ibm.com/developerworks/data/library/techarticle/dm-0504stolze/test_1.jpg" /> <span>lorem ipsum<br/> lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsumlorem ipsum lorem ipsum</span>
</div>
CSS:
* {
margin: 0;
padding: 0;
}
.row > * {
display: table-cell;
vertical-align: middle;
}
.btn {
width: 30px;
height: 30p;
background-color: red;
}
.row { display: table-row; }
Demo
Try giving your span a width. That would force the line breaks in your "a" example.
HTML
<div class="header">
<div class="btn">BUTTON</div>
<img src="http://lorempixel.com/200/200" /><span>lorem ipsum lorem ipsum lorem ipsum lorem ipsum</span>
</div>
CSS
.header {
width: 500px;
background: dimgrey;
}
.btn, img, span {
display:inline-block;
vertical-align: middle;
}
span {
width:200px;
text-align: center;
}
http://jsfiddle.net/abN39/1

How to make text wrapable around following, floated element?

HTML:
<div class="heading">
<h2 class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</h2>
<a class="action" href="#">Print</a>
</div>
Desired default look:
| Lorem ipsum dolor sit [Print] |
| amet, consectetur adipiscing elit. |
Desired look on small screen (using media-queries):
| Lorem ipsum dolor sit |
| amet, consectetur |
| adipiscing elit. |
| [Print] |
Not desired:
| [Print] |
| Lorem ipsum dolor sit amet, |
| consectetur adipiscing elit. |
Not desired:
| Lorem ipsum dolor sit [Print] |
| amet, consectetur |
| adipiscing elit. |
Remarks:
Text may be any long.
Action element is constant height and variable width.
I see no way to do this using only CSS.
Right now I use JS (Harvey lib) to put floated DOM element before text on bigger screen.
Any ideas?
/// EDIT - moved to answer (sorry for mess)
There's no easy way to achieve both results using only CSS without modifying your markup. There are a few tricks you can use to try to emulate the behavior you want, though.
Trick 1: Use Absolute Positioning
Set the link to position:absolute;top:0;right:0; (and the container to position:relative; if needed). Then, use .text::before{display":block;content' ';float:right;} to place a gap where the print link will appear.
Trick 2: Double Links
You could place a link before/in the <h2> to float right for large displays, then hide it and show a (formerly-hidden) second link as a block element below the text on small displays.
Thanks to suggestions above, I finally decided to break pure semantics structure and put .action element before .text, so it floats easily with proper text wrapping. Adding the behavior desired for smaller screen was quite easy with the constrains about .action element size:
cssdeck.com/labs/epcoxk7o
HTML:
<div class="heading">
<a class="action" href="#">Print</a>
<h2 class="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</h2>
</div>
CSS:
.action {
float: right;
line-height: 20px;
padding: 5px 70px;
background: #eee;
}
.text {
line-height: 30px;
}
#media (max-width: 30em) {
.heading {
position: relative;
padding-bottom: 20px;
}
.action {
position: absolute;
bottom: 0;
left: 0;
}
}
Other solution would be what #cimmanon suggested: cssdeck.com/labs/9bntxaro