Alright, so I'm trying to make div elements align down the middle (between two images) without using tables (because tables shouldn't be used for styling/layout).
I have the following individual elements:
img
img
div
And I want the final output, using CSS, to be:
Seems simple, right? Well, the trick is that the scores to the left and right of the images are variable width, and I want the center of the rounded rectangle to slice right between the two images, regardless of the widths of the score values. (Thus, I can't just wrap a div around the whole block and use text-align: center. Would do me no good.)
As you can see in my example pic, there is more space between the edge of the rectangle and the score on the right than there is on the left, because the left score itself is wider.
Also note that the images expand slightly above and below the rectangle div, which is another reason why using a table wouldn't be ideal.
I've tried to accomplish this layout using combinations of margin-left: auto, margin-right: auto, display: inline-block, etc., but I can't get the centered effect I'm looking for.
Here is a jsfiddle to play with.
Your help is greatly appreciated!
Here's the deal:
.team should have width: 50% and should be floated left. Or right. Whatever.
The images should be floated toward the center. The one on the left should be floated right, the one on the right should be floated left.
The images should also have position: relative and a negative top.
.team should also have text-align set. The one on left should have text-align: right
The outer container should have overflow set to visible (which is the default - I just wanted to mention it because other answers told you to use overflow: hidden. Which would break your "outside the box" stuff).
That should get you what you want. And here's proof (started before you posted your fiddle)
UI elements (things that are not content) should be CSS backgrounds. Make a composite image and make it the background for a wrapping DIV, then make two inside it - one floated left, the other floated right and with a bit of margin and padding everything will work just fine.
<div class="wrapper">
<div class="scoreLeft"></div>
<div class="scoreRight"></div>
</div>
All of the inner child elements of your div should have float: left. Then, the parent div should have overflow: hidden. From there, you can then add additional margin's to the div img elements.
Here is an example solution. The idea is that you have a wrapper do the grey background and size the bar. You than have a div half the size and align the text towards the center while putting enough padding to allow for background images.
HTML
<div class="wrapper">
<div class="scoreLeft">1231231</div>
<div class="scoreRight">123</div>
</div>
CSS
.wrapper {
background: grey;
background-image:url('http://i.stack.imgur.com/4fkZv.png');
background-size:400px 50px;
height: 50px;
width: 400px;
display: table; //Allow for vertical align
table-layout: fixed; //Allow for fixed widths of children
}
.scoreLeft, .scoreRight {
color: white;
display: table-cell; //Allow for vertical align
vertical-align: middle;
width: 50%;
-moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; //Allow for 50% width with padding sitting inside the 50%. This can be mathed out so the width + padding * 2 = wrapper width and then you can use the default box-sizing.
}
.scoreLeft {
background-image:url('http://i.stack.imgur.com/CmWiD.png');
background-size:50px 50px;
background-repeat: no-repeat;
background-position:right;
text-align: right;
padding-right: 55px;
}
.scoreRight {
background-image:url('http://i.stack.imgur.com/Gkll9.png');
background-size:50px 50px;
background-repeat: no-repeat;
padding-left: 55px;
}
Related
I was trying to center text in two adjacent divs and I can not understand what I am doing wrong.
Basically I have 2 divs each taken 50% of the window. The first div contains an image (which I successfully centered) and I am trying to center the text in the second div. So here is my Fiddle and I am using the following css:
.thumbnail-descr{
text-align: center;
min-height: 10px;
display: table-cell;
vertical-align: middle;
font-size: 26pt;
color: #bbb;
}
There is no point of having original DOM structure or CSS (the main thing is to have 2 divs taking all the width and one has a centered image another one has a text. How can I achieve it?
What I understand from the example is that you want to vertically center "Descr". There are several tricks to do that:
Adjust the padding and use box-sizing border-box to have better control of the height.
Use flexbox (still not broadly available): https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Flexible_boxes
If you know before hand that you'll have only one line of text you can use line-height.
(See the update for another option)
For example see this Fiddle:
.square{
width: 45%;
height: 200px;
border: 3px dotted #ddd;
display:inline-block;
margin: 0 10px 0 10px;
line-height: 200px;
}
But take a few things into account:
This will work only if you have one line of text, because on text wrap it will be broken.
This is not the normal use of line height, it's taking advantage of a side effect: text is vertically centered to the line-height.
This trick is used by some CSS frameworks (ie Bootstrap) to center the text on some components.
Update
I forgot another option, since you have one div inside the other you can use position: relative on the parent, and use absolute position for the child using top: 50% and a negative top margin. You'll need to setup the top margin to the half of the child height. (that's how modals are usually centered):
.square { position: relative; /*..*/ }
.thumbnail-descr{
position: absolute;
top: 50%;
margin-top: -10px;
/*...*/
}
See http://jsfiddle.net/diegof79/M4fKM/1/
Also you asked why your solution is not working... this can help to understand the reasons: http://phrogz.net/css/vertical-align/index.html
Before proceeding to giving you the solution, you could have he exact same result with a lot less code, giving so many classes for so little content can only lead to huge code.
The span class you are giving the text-align:center, doesn't fill up the whole width of the parent, so, where would it center the text since its width is equal to the text?
You can either put a 'text-align:center" to the span's parent, the square class, but I would use a different approach in general.
Not sure if you need a span.
If you remove span tag and use same css for the div styles, or simply ad a span class name to a your div class name- works perfectly.
i think the issue happen because the width in the description div
Try this suggestion:
warp the with div, the div will use thumbnail-descr class
<div class='square'>
<div class="thumbnail-descr"><span>Descr</span><div>
</div>
Update the thumbnail-descr
.thumbnail-descr{
text-align: center;
min-height: 10px;
background-color:red;
vertical-align: middle;
font-size: 26pt;
color: #bbb;
width:100%;
}
hope this help
I have to design a circle with two h3 in it. Both of them should line up at the left side but still be centered in the circle.
I already have positioned everything right but I dont know how to left-align both <h3>.
Here is the js fiddle.
Ok I guess I explained it wrong. I want the <h3> lined-up left. But both maybe wrapped up in another element should be centered in the circle referring to the <h3> with the greatest width. But the text in there is a sample text. I cannot give a wrapping element a fixed width and position it with margin: 0 auto.
Wrap the H3 elements in their own div and position it using margin: 0 auto.
Once that is done, you can simply left or right align the text within the div.
eg:
<div class="h3Wrapper">
<h3>Foo</h3>
<h3>Bar</h3>
</div>
CSS:
.h3Wrapper { margin: 0 auto; }
h3 { text-align: left;}
Remove the text-align:center and use margin:auto. Please check http://jsfiddle.net/B3vdz/7/. I hope this is how you want it.
Is this how yout want it?
I used a new div for the text.
#lefft{
width: 90px;
margin: 0 auto;
padding-top: 50px;
position: relative;
}
http://jsfiddle.net/B3vdz/1/
I was wondering how I can center this http://prntscr.com/hv2q7 It is hanging off and I want it to be centered like this http://prntscr.com/hv2ue so that the gray part is coming into the border. Here is the css code and html for it :
The css:
#banner{
height: 279px;
width: 998px;
margin-right: 0px;
margin-left: 0px;
background-image:
url(/template/default/images/layout/background/newlayout/test.png);
}
The html :
<div id="banner" ></div>
You want to set your left and right margins to auto, not 0px.
Try this, it's the shorthand for setting your top/bottom margin to 0 and your left/right to auto:
#banner {
margin:0 auto;
}
Centering with css normally revolves around the use of margin:auto;
In this case you're looking at left and right margins being auto, so something like margin:0 auto; As you try it out for your full page specifically you may find you have to set the elements' display to block or the float or even a position, depending on the browser. Though those are usually not necessary.
Also, if the div really only contains the background image, you might set the background-repeat to none and the background-position to center. That would only center in the div, so if the div is actually showing as the width and height of the image, it wouldn't change anything, but if the div is filling the width of it's containing block, then you'd get left and right centering.
put this arround your banner code:
<div align="center"> "your banner code" </div>
So what I'm trying to accomplish is to have a div centered on the page (margin: auto auto;) with a navigation div just to the left of it.
The idea being the navigation div can be switched on or off (so may or may not be there). If its there or not should not interferer with the centering of the main div.
Below is an example
I've tried a few things
Wrapping both divs with a main div. Setting the main div to margin: auto auto and then setting both child divs to float: left. The problem is that when the nav div dissapears the entire thing shifts left.
Keeping the middle div margin: auto auto; floating the nav div left and then using margin-left but this changes when the page gets bigger or smaller.
Any pointers would be appreciated in the best way to do this. I was hoping to avoid tables.
JSFiddle link
Try this:
In your html:
<body>
<div class="encasing">
<div class="leftmenu"></div>
</div>
</body>
In your css:
html, body
{
width: 100%;
height: 100%;
}
div.encasing
{
top: 50px;
margin-left: auto;
margin-right: auto;
width: 70%;
height: 500px;
background-color: green;
position: relative;
}
div.leftmenu
{
right: 100%;
width: 10%;
height: 300px;
background-color: red;
position: absolute;
}
The important parts are:
To put your block containing the menu inside your center block
Make the center block have margin-left: auto; margin-right: auto;
Make the center block have a relative positioning
Have the menu have a absolute positioning
Make the menu have right: 100%
The idea here is to make the left menu use the position of the center block and then adjust itself. Right: 100% will put the right edge of the menu on the left edge of the menu.
In the end, a really good trick in css is that absolute positioned elements adjust themselves relative the the nearest relative or absolute positioned parent. :)
A few solutions I can think of:
Use absolute positioning for the navigation div. You probably want to give the body element a min-width to avoid the navigation div overlapping the main div when the window is too small.
Three-column layout, e.g. two divs with fixed widths floated to the left and right, and the content div between them. Inside the left-floated div, display your navigation div (or not). Alternatively, try display: inline-block on the three columns. The difference is in how small windows are handled (try it out). Again, you can counter undesired effects by setting a min-width on the body.
Completely fixed layout. Decide on an ideal screen resolution, and hard-code everything to that. This way, you can absolute-position everything where you want it, but the downside is that it won't look good on anything that deviates too much from the intended resolution. Especially mobile devices will see devastating results; you counter these with #media queries to adjust your layout to other screen resolutions.
You should also try to find a site that does what you want to do, and see how they did it (inspect HTML, CSS, and maybe Javascript).
I am using the yui-grids css (irrelevant if you don't know what this is) with three columns. and I'm putting all the fancy design stuff on the left column and using z-index and relative psitioning bringing them in the center. and then putting all the important stuff like forms, inputs buttons, links and context in the center. Is this wrong. I've never seen this done so I was wondering maybe there is something I don't know! or am not considering. Should I just use one column?
I'm not totally sure what you're asking, so I'll give it a shot:
Columns
If you're going with a column layout, you should give just floating elements a go. Due to how floating works, a clearfix hack will be nessecary (link provided below). Clearfix allows child elements to be floated while maintaining the parent element's height and block nature. Clearfix can only be added to block elements.
For my example, we will be going with a 2 column layout -- one #content column and a #sidebar column -- you could do two, three or more.
For the parent div (that contains the #content and #sidebar elements), you'll need to add a class="clearfix".
For the content div, you'll want to float it to the left. For the sidebar div, you'll want to float it to the right.
Now, the CSS:
#parentDiv { width: 750px; margin: 0 auto; }
#parentDiv #content { float: left; width: 500px; }
#parentDiv #sidebar { float: right; width: 200px; }
This should produce a 750px box with a content element on the left and a sidebar on the right with 50px in between both elements (750-(500+200) = 50).
Floating Module
If this isn't what you wanted, and were looking to produce a module element (lightbox, popup window, etc) instead, this is easy too.
First, create a div called #module. Put in your content into it. Let's say you want to give it a width of 500px and you want the height to be static at 300px. So we'd do this CSS:
#module { width: 500px; height: 300px; border: 1px solid #000; position: absolute; top: 50%; left: 50%; margin: -150px 0 0 -250px; z-index: 100; }
What's going on here?
The #module element is being set to position: absolute. This means that it will be floating around the window, and is not constrained to it's parent element. We position it to be 50% from the left of the window and 50% from the top, so it gets in the middle of the window. Percent values are nessecary as they are adjusted when the window resizes. Without the margin, the element's top left corner will be 50% from the top and 50% from the left, so we need to use margin to move it back half of it's width and half it's height. This will allow us to have a box perfectly centered in the middle. The z-index is added to make sure that the element is on top of any other element, including , and other positioned elements.
I hope this helps.
Links
Clearfix: http://gist.github.com/550114
This kind of layout wouldn't be correct in my opinion.
The design of an element must be described in that particular element.