I've set up three divs as containers for my menu buttons. One will align buttons to the left side of the page, the other to the right, and the final one in the middle between both divs. This is what it looks like in CSS.
.actions {
#include clearfix();
.left {
float: left;
margin-left: $outside_margin;
height: $actions_height;
line-height: $actions_height;
}
.middle {
text-align: center;
margin-left: 0px auto;
margin-right: 0px auto;
height: $actions_height;
line-height: $actions_height;
}
.right {
float: right;
margin-right: $outside_margin;
height: $actions_height;
line-height: $actions_height;
}
}
This works quite well. Now I'm trying to write a .button class that will make a simple boxed button with a word of text in it and sometimes also an image. This is what my HTML looks like.
<div class="actions">
<div class="left">
<a class="button" href="#"><span class="text">Left</span><span class="image"></span></a>
</div>
<div class="right">
<a class="button" href="#"><span class="text">Left</span><span class="image"></span></a>
</div>
<div class="middle">
<a class="button" href="#"><span class="text">Left</span><span class="image"></span></a>
</div>
</div>
I have tried a bunch of things. I never got them to work properly with table-cells, display:block will require me to use floats again and I have no idea how to get a proper alignment on the middle one, and inline-blocks - aside from having that pesky space in between them when writing easily readable HTML - required a lot of tweaking to get right and that was only for one button size then.
I guess that I'm at the point where I am just lost because I've tried so many things but nothing worked right. So I thought I'd post my problem here.
What would be the best way to proceed with this project? Thanks in advance.
I created a prototype based on the following HTML:
<div class="actions">
<div class="left">
<a class="button" href="#">
<span class="text">Left</span><span class="image"><img src="http://placehold.it/30x30"></span></a>
</div>
<div class="right">
<a class="button" href="#">
<span class="text">Right</span><span class="image"><img src="http://placehold.it/30x30"></span></a>
</div>
<div class="middle">
<a class="button" href="#">
<span class="text">Middle</span><span class="image"><img src="http://placehold.it/30x30"></span>
</a>
</div>
</div>
and using the following CSS:
.actions {
outline: 1px dotted blue;
height: 30px;
line-height: 30px;
}
.actions a {
}
span.image img {
vertical-align: bottom;
}
span.text {
vertical-align: baseline;
}
.left {
float: left;
margin: 0 20px;
outline: inherit;
}
.middle {
display: block;
text-align: center;
margin-left: 0px auto;
margin-right: 0px auto;
outline: 1px dashed blue;;
overflow: hidden;
}
.right {
float: right;
margin: 0 20px;
outline: inherit;
}
I added some outline's so that we can visualize how far the blocks extend and how margins work and so on.
The left and right <div>'s are floated, nothing unusual and the the middle section is an inflow block with text-align: center and overflow: hidden, which is perhaps optional depending on other design considerations.
I put in some simple 30px by 30px images, but I am not sure what else you might need.
Hopefully this can get you started.
Fiddle Demo
It seems likely that the reason you couldn't get table-cell to "work" is because you didn't realize you needed to vertically align the contents of your cells as well as the cells themselves:
http://tinker.io/619bf/2
.actions {
display: table;
width: 100%;
}
.actions div {
display: table-cell;
border: 1px solid;
vertical-align: middle;
width: 33%
}
.actions div * {
vertical-align: middle;
}
Related
I need to center images inside of multiple divs. Everything I try breaks.
These are four boxes, alternating red & blue - horizontal. Looking to have them centered in the page and pushed to the top under another div block. Within each block is an image, which is centered to the same % margin on all sides to the relative red or blue box. You can see below I tried both placing the image directly in a redbox/bluebox div or even going one layer deeper with a box just for the image.
4 Box Example - HTML:
<div id="box-container">
<!-- Trying natively within a box -->
<div class="bluebox">
<img src="images/1.jpg">
</div>
<div class="redbox">
<!-- Trying one-layer deeper with its own div -->
<div class="thumbnail">
<img src="images/2.png">
</div>
</div>
<div class="bluebox">
<div class="thumbnail">
<img src="images/3.jpg">
</div>
</div>
<div class="redbox">
<div class="thumbnail">
<img src="images/4.png">
</div>
</div>
CSS:
box-container {
height: 900px;
width: 950px;
padding: 12px;
display: inline-block;
vertical-align: top;
margin-left: auto;
}
.bluebox {
height: 150px;
width: 170px;
background-color: cornflowerblue;
display: inline-block;
border: 3px solid black;
}
.redbox {
height: 150px;
width: 170px;
background-color: lightcoral;
display: inline-block;
border: 3px solid black;
}
.thumbnail img {
display: block;
margin: auto;
height: 130px;
width: 150px;
}
<!-- begin snippet: js hide: false console: true babel: false -->
<div id="box-container">
<!-- Trying natively within a box -->
<div class="bluebox">
<div class="thumbnail">
<img src="http://placehold.it/400x400.jpg">
</div>
</div>
<div class="redbox">
<!-- Trying one-layer deeper with its own div -->
<div class="thumbnail">
<img src="http://placehold.it/400x400.jpg">
</div>
</div>
<div class="bluebox">
<div class="thumbnail">
<img src="http://placehold.it/400x400.jpg">
</div>
</div>
<div class="redbox">
<div class="thumbnail">
<img src="http://placehold.it/400x400.jpg">
</div>
</div>
You need to add padding to the image based on the height of your thumbnail div.
.thumbnail img {
display: block;
height: 130px;
width: 150px;
padding: 10px;
}
.bluebox img, .redbox .thumbnail img, .bluebox .thumbnail img {
display: block;
margin: 0 auto;
}
or
.bluebox, .redbox .thumbnail, .bluebox .thumbnail {
text-align: center;
}
using flexbox
.bluebox, .redbox .thumbnail, .bluebox .thumbnail {
display: flex;
align-items: center;
justify-content: center;
}
I believe I have what you are looking for in this here JSFiddle I just wipped up: https://jsfiddle.net/9yLspwr6/5/
A few key points before the code...
In order to have all the div elements 'float' left you ahve to apply div.className{float:left;} This will ensure divs float left to right and wrap around if they run out of space (much like a paragraph of text). More on CSS float property here: https://www.w3schools.com/css/css_float.asp
Vertical margin does not support 'margin:auto;' like it does for horizontal. Margin can be defined by div.ClassName{margin: 0px 0px 0px 0px;} OR div.className{margin:0px auto;}. The first element this way is for top/bottom margin. The second is for left/right margin. I had to use a little math to vertically center your images, but it gets you what you need. Here is some good documentation on margin: https://www.w3schools.com/cssref/pr_margin.asp
Cleaned up the HTML and removed some CSS no longer needed. I did this to simplify the code while maintaining the solution. If you drop this code into a site you'll want to ensure you only target only the appropriate tags. For example - my code is targeting ALL img tags. You would want to put a class or ID on the IMG tags you want and then ensure that is reflected in the CSS.
I modified the HTML quite a bit. Removed much of the unnecessary elements that were in place for troubleshooting.
<div class="bluebox">
<img src="images/1.jpg">
</div>
<div class="redbox">
<img src="images/2.png">
</div>
<div class="bluebox">
<img src="images/3.jpg">
</div>
<div class="redbox">
<img src="images/4.png">
</div>
Modified CSS below:
.bluebox {
height: 150px;
width: 170px;
background-color: cornflowerblue;
display: inline-block;
border: 3px solid black;
float:left; // new. essentially left justifies the divs.
}
.redbox {
height: 150px;
width: 170px;
background-color: lightcoral;
display: inline-block;
border: 3px solid black;
float:left; // new
}
img { // simplified the target. wrap entire contents of the HTML with a different DIV id to target only images within that div
display: block;
margin: 10px auto; // added 10px. it will then apply 10px margin to top and bottom, auto on left/right
height: 130px;
width: 150px;
}
That should do it. Hope it helps!
I can't seem to make my images align side by side, they just keep stacking on top of each other. I have only enough knowledge to fumble my way through with instructions and I'm stuck here.
HTML:
<div class='sticky-bar'>
<div class='sticky-bar-inner'>
<div>
<a href='https://www.facebook.com/salvageinteriors' target='_blank'>
<img src='img.png' />
</a>
</div>
</div>
</div>
I cant seem to get the rest of the code in here, but it just keeps repeating the above.
CSS:
.sticky-bar {
background: none repeat scroll 0 0 #FFFFFF;
bottom: 0;
color: #000000;
font-weight: 700;
left: 10px;
margin: 9px;
opacity: 0.6;
position: fixed;
width: 45px;
z-index: 62;
}
.sticky-bar-inner {
display: inline-block;
margin-left:auto;
padding: 20px 0;
text-align: left;
width:90%;
}
Try this:
Use float:left property. It will use to align div's side by side and when the parent width is reached then , the images will align in next line.
HTML:
<div class='sticky-bar'>
<div class='sticky-bar-inner'>
<div class="inner-divs">
<a href='https://www.facebook.com/salvageinteriors' target='_blank'>
<img src='img.png' />
</a>
</div>
</div>
</div>
CSS:
Set width for .sticky-bar-inner class
.sticky-bar-inner {
padding: 20px 0;
width:500px;
}
and set float:left property to inner image divs.
.inner-divs{
float:left;
}
I'm a tables guy, but I'll need to drag and drop some divs, so I tried doing it tabeless (the right way).
This is what I want to do:
The space between all elements should be 24px. My main problem is having the divs (1,2,3) occupying 100% of available space. The width: 100% its sending them beyond the main container.
This is my code so far:
html
<div id="mainContainer">
<div id="topContainer">Just the top one
</div>
<div id="table">
<div id="Line1Container">
<div id="container1" class="container">1
</div>
<div id="container2" class="container">2
</div>
<div id="container3" class="container">3
</div>
</div>
<div id="Line2Container">
<div id="container4" class="container">4
</div>
<div id="container5" class="container">5
</div>
<div id="container6" class="container">6
</div>
</div>
</div>
</div>
And my css
#mainContainer {
border: 1px solid lightgray;
position:fixed;
top: 80px;
bottom:20px;
left:80px;
right:80px;
overflow-y: scroll;
overflow-x: hidden;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
}
#topContainer {
border: 1px solid lightgray;
border-radius: 10px;
margin-left: 24px;
margin-right: 24px;
margin-top: 24px;
}
#table {
display: table;
margin: 24px;
width: 95%;
}
#Line1Container, #Line2Container {
display: table-row;
}
.container {
border: 1px solid lightgray;
display: table-cell;
width: 33%;
border-radius: 10px;
}
As you see I tried the table-cell approach, but before I have tried the float: left approach.
Thanks
Fiddle
You can't properly use px values with % values together with dynamic sizes.
You should use x% instead of 24px.
And you can use float: left on the "cells"
How about using a table for separating the divs? that way with the td padding there will always be 24px between them
check out this fiddle:
http://jsfiddle.net/5zfEq/
added:
#Line1Container {
padding:12px;
}
#inner-table {
width: 100%;
}
#inner-table td {
padding: 12px;
}
based on #Edifice fiddle .... thanks ;)
how do i can stretch my div according text
I want to stretch height of a div , with the text user posted
look at the screen shot its clear.
CSS :
.cmnt{ width: 570px; margin-top: 10px; margin-bottom: 10px; float: right; margin-right: 15px; clear:both; }
.cmnt .body{ width: 570px; background: #333333; min-height: 90px; height: auto; }
.cmnt .text{ width: 513px; float: left; font-family: arial; color: #FFF; }
.cmnt .arrow{ width: 570px; height: 7px; background: url(../img/comment_arr.jpg) no-repeat 17px top; }
.cmnt .info{ width: 470px; height: 20px; font-family: arial; font-size: 14px; color: #FFF; float: left; text-align: left; }
HTML :
<div class="cmnt">
<a name="comment-id" />
<div class="body">
<div class="text">
text<br/>
text<br/>
text<br/>
text<br/>
text<br/>
text<br/>
text<br/>
</div>
<img class="avatar" src="assets/img/cmnt-u.jpg" align="absmiddle" />
</div>
<div class="arrow"></div>
<div class="info">
smith (date)
</div>
<div class="rp">
reply ↓
</div>
</div>
Image
Parent :
<div class="comment">
<div class="cmntHeader">Comments</div>
<div class="cmntBody">
<center>
....
</center>
</div>
</div>
You can try adding a float: left CSS property to your outer container.
.cmnt .body{ float: left; width: 570px; background: #333333; min-height: 90px; height: auto; }
Here's a fiddle for you
http://jsfiddle.net/znQa8/
Hope it helps.
The height of a container is automatically adjusted, if not specified, according to the child (not floated) elements.
Because the div (class=text) is floated, its height doesn't take into account. Whenever you used a float, systematically try to clear it after to resolve the height problem.
<div class="text">
...
</div>
<div style="clear:both"></div>
min-height would be your solution.
it could be a possibility to add:
div.body
{
height: auto;
}
but i'm not sure it isn't killing the whole layout
In reference to My Head Hurts you need to clear your floats:
.cmnt .body
{
overflow: hidden;
}
What is happening is that you have a floating container with floating children.
In this case, the floating container "ignores" the children dimensions. This is probably a side effect of the implementation of the real primary objective of float (that was to have floating images on wrapped text).
In this article you can see the floating purpose and a list of workarrounds:
http://www.ejeliot.com/blog/59
And this is what I think is the best solution so far, the micro clearfix (best about it is that you can put in you css and reuse it whenever you need it again):
http://nicolasgallagher.com/micro-clearfix-hack/
Just add this css block to your css file:
/*THE CLEARFIX HACK*/
.clearfix:before,
.clearfix:after {
content: " ";
display: table;
}
.clearfix:after { clear: both; }
.clearfix { *zoom: 1; }
And then add the clearfix class to the "body" div.
jsfiddle: XZRH5
I am building a bunch of list items in an un-ordered list. The list has a fixed size of 250px X 75px;These list items are generated dynamically so i do not know what text will be displayed, so my li looks like this.
#pages-content li{
float: left;
width: 250px;
height: 75px;
margin: 15px;
vertical-align: middle;
text-align: center;
}
I found one suggestion that said to make the line height 75px and that worked until there is more than one line.
CSS:
#pages-content ul li{
width: 250px;
height: 75px;
text-align: center;
}
HTML:
<div id="pages-content">
<ul>
<li>Matter here</li>
</ul>
</div>
Working fiddle
Do you have to use lists? Can you use divs instead?
<style>
.div {
width: 250px;
height: 75px;
position: relative;
}
.container {
position: absolute;
width: 250px;
height: 75px;
display: table;
}
.container p {
display: table-cell;
vertical-align: middle;
text-align: center;
}
</style>
<div class="div">
<div class="container">
<p>This text should look centered even if it's long.</p>
</div>
</div>
<div class="div">
<div class="container">
<p>This text should look centered even if it's long.</p>
</div>
</div>
<div class="div">
<div class="container">
<p>This text should look centered even if it's long.</p>
</div>
</div>
For a purely HTML/CSS solution, try using a table and vertical-align: middle in the table cell.
http://jsfiddle.net/3zLcT/
If you can only use css, I'm afraid you're out of luck.
adding display: table-cell might work, as vertical-align is meant, more or less, for table data