Note that yes there are similar questions here, but I tried what they have mentioned as solutions, and I still can't get my items centered. I have stuck text-align: center, display:inline-block; margin: 0 auto and width: 100% in multiple places and I still can't get the items to display centered.
O.k. With that out of the way, here is the code and then the explanation below:
<h4 style="margin-bottom:-15px"><strong>The Item Item Below:</strong></h4>
<img style="float:left;width:100%; max-width: 408px; height:100%; max-height: 134px; background-color:red; margin-top:40px; margin-right:20px"
src="http://s25.postimg.org/kbrcsousf/408x134.png"> <span style="float:left; margin:0 auto; max-width:100%">
<p style="text-align:center; text-decoration: underline"><strong> Works
with these Things:</strong></p>
<div style="float:left;max-width:100%; width:160px;text-align:center;margin-right:20px;margin-bottom:20px">
<div style="background-color: rgb(255, 0, 255);"><span style="text-decoration: underline;"><strong>Flowers
A<br>
</strong></span></div>
<div style="background-color: rgb(0, 255, 255);"><img src="http://s25.postimg.org/i8gxl0uzf/140x124.png"></div>
</div>
<div style="float:left;max-width:100%; width:230px;text-align:center;">
<div style="background-color: rgb(255, 0, 255);"><span style="text-decoration: underline;"><strong>Flowers
B<br>
</strong></span></div>
<div style="background-color: rgb(255, 255, 0);"><img src="http://s25.postimg.org/a48tga8kf/209x112.png"></div>
</div>
</span>
Now the explanation:
So, I first started out with the first image on a line by itself and then had the bottom two pairs of divs below it. I figured I was wasting a bit too much horizontal space, so I placed the two pairs of divs next to / to the right of the "408x134" image by placing float:left in that image and enclosing the two pairs of divs with a span tag and placing float:left in it.
I am designing this with responsiveness / mobile in mind and am trying to avoid using #media for now.
So, when one reduces the size of their screen on their browser horizontally, the two items to the right drop down as a pair below the "408x134" image, which is what I want. As the screen gets even smaller, the two items below get separated, with the "209 x 112" image going underneath the "140 x 124" image. (Well, that and the div label above each one of them).
This is what I want. However, the final part I can't get is that I want those two pairs of divs to be centered. How can I do that? If you can give an explanation of your solution also, that would be great, so that I can finally understand how to fix this problem in the future or avoid it in the first place. Now I realize that the first pair of divs -- Flowers A and "140 x 124" won't be exactly centered as I have placed a right margin, and that is so that there can be some space when they are next to one another horizontally and also under one another when they are stacked on a smaller screen. For that to be centered I think I will have to use #media tags to remove/resize the margin and add a line break or another div or something.
Try the code below:
<h4 style="margin-bottom:-15px"><strong>The Item Item Below:</strong></h4>
<img style="float:left;width:100%; max-width: 408px; height:100%; max-height: 134px; background-color:red; margin-top:40px; margin-right:20px"
src="http://s25.postimg.org/kbrcsousf/408x134.png"> <span style="float:left; margin:0 auto; max-width:100%">
<p style="text-align:center; text-decoration: underline"><strong> Works
with these Things:</strong></p>
<center>
<div style="margin: 0 auto;display:inline-block;max-width:100%; width:160px;text-align:center;margin-right:20px;">
<div style="background-color: rgb(255, 0, 255);"><span style="text-decoration: underline;"><strong>Flowers
A<br>
</strong></span></div>
<div style="background-color: rgb(0, 255, 255);"><img src="http://s25.postimg.org/i8gxl0uzf/140x124.png"></div>
</div>
<div style="margin: 0 auto; display:inline-block;max-width:100%; width:230px;text-align:center;">
<div style="background-color: rgb(255, 0, 255);"><span style="text-decoration: underline;"><strong>Flowers
B<br>
</strong></span></div>
<div style="background-color: rgb(255, 255, 0);"><img src="http://s25.postimg.org/a48tga8kf/209x112.png"></div>
</div>
</span>
</center>
I've tried looking at your HTML and I think you're missing a few tags. Additionally, best practice is to not have CSS styles inline with HTML; you typically want to have CSS in a separate stylesheet. Getting past all that, I think you need to know some things about the elements and properties you are working with.
A div is block-level element by default, which takes up 100% width of its container (parent) unless you add some other overriding style to it. One option (which you've selected) is float:left; which will get this element to act sort of like an inline element (like a span or img) except it will always try to align itself with the left of the container; no matter the type of alignment you've set to the container.
What I will recommend in your case is setting the image container div elements to display:inline-block; instead of float:left;, that way it will be affected by a parent's text-align:center; property and when there's no room for all of the div elements to be in a row, they will fall down underneath but be aligned in the middle. You don't currently seem to have a container element but your setup should probably look something like this:
#container{
text-align:center;
}
#container .imgwrap{
display:inline-block;
}
<div id="container">
<div class="imgwrap"><img src="#"/></div>
<div class="imgwrap"><img src="#"/></div>
<div class="imgwrap"><img src="#"/></div>
</div>
My off the cuff: divs in my limited experience defeat the point of css. Second, there is script to detect screen size and allow u to provide separate css based on that. Thats how I've played it.
Related
I have 2 divs side-by-side in a flexbox. The right hand one should always be the same width, and I want the left hand one to just grab the remaining space. But it won't unless I specifically set its width.
So at the moment, it's set to 96% which looks OK until you really squash the screen - then the right hand div gets a bit starved of the space it needs.
I guess I could leave it as it is but it feels wrong - like there has to be a way to say:
the right one is always the same; you on the left - you get everything that's left
.ar-course-nav {
cursor: pointer;
padding: 8px 12px 8px 12px;
border-radius: 8px;
}
.ar-course-nav:hover {
background-color: rgba(0, 0, 0, 0.1);
}
<br/>
<br/>
<div class="ar-course-nav" style="display:flex; justify-content:space-between;">
<div style="width:96%;">
<div style="overflow:hidden; white-space:nowrap; text-overflow:ellipsis;">
<strong title="Course Name Which is Really Quite Long And Does Go On a Bit But Then When You Think it's Stopped it Keeps on Going for even longer!">
Course Name Which is Really Quite Long And Does Go On a Bit But Then When You Think it's Stopped it Keeps on Going for even longer!
</strong>
</div>
<div style="width:100%; display:flex; justify-content:space-between;">
<div style="color:#555555; margin-right:8px; overflow:hidden; white-space:nowrap; text-overflow:ellipsis;" title="A really really really really really really really really really really really long department name">
A really really really really really really really really really really really long department name
</div>
<div style="color:#555555; text-align:right; white-space:nowrap;">
Created: 21 September 2016
</div>
</div>
</div>
<div style="margin-left:8px;">
<strong>></strong>
</div>
</div>
Use the flex-grow property to make a flex item consume free space on the main axis.
This property will expand the item as much as possible, adjusting the length to dynamic environments, such as screen re-sizing or the addition / removal of other items.
A common example is flex-grow: 1 or, using the shorthand property, flex: 1.
Hence, instead of width: 96% on your div, use flex: 1.
You wrote:
So at the moment, it's set to 96% which looks OK until you really squash the screen - then the right hand div gets a bit starved of the space it needs.
The squashing of the fixed-width div is related to another flex property: flex-shrink
By default, flex items are set to flex-shrink: 1 which enables them to shrink in order to prevent overflow of the container.
To disable this feature use flex-shrink: 0.
For more details see The flex-shrink factor section in the answer here:
What are the differences between flex-basis and width?
Learn more about flex alignment along the main axis here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
Learn more about flex alignment along the cross axis here:
How does flex-wrap work with align-self, align-items and align-content?
Basically I was trying to get my code to have a middle section on a 'row' to auto-adjust to the content on both sides (in my case, a dotted line separator). Like #Michael_B suggested, the key is using display:flex on the row container and at least making sure your middle container on the row has a flex-grow value of at least 1 higher than the outer containers (if outer containers don't have any flex-grow properties applied, middle container only needs 1 for flex-grow).
Here's a pic of what I was trying to do and sample code for how I solved it.
.row {
background: lightgray;
height: 30px;
width: 100%;
display: flex;
align-items:flex-end;
margin-top:5px;
}
.left {
background:lightblue;
}
.separator{
flex-grow:1;
border-bottom:dotted 2px black;
}
.right {
background:coral;
}
<div class="row">
<div class="left">Left</div>
<div class="separator"></div>
<div class="right">Right With Text</div>
</div>
<div class="row">
<div class="left">Left With More Text</div>
<div class="separator"></div>
<div class="right">Right</div>
</div>
<div class="row">
<div class="left">Left With Text</div>
<div class="separator"></div>
<div class="right">Right With More Text</div>
</div>
I'm going to put 4 divs in a 100% width container side-by-side.
I saw this question and it was useful.
But my problem is that while using that solution, divs are stick to each other, but I need a little margin/space among them.
For example support a child div like this:
<div class="introwrapper"
style="width:25%;height:100%; float:left; margin-left:5px;">
</div>
Actually, I want to make these 4 divs placed side-by-side and make a space of 5 pixels between them, for the rest of the space remaining, make their width equal and the make the sum of widths of all these 4 divs and all 3 5px margins 100%.
How can I make this type of alignment?
I'm sorry for the confusion. I though a technique would work, that I have used frequently for centring objects, however it didn't.
Unfortunately, the only thing I can think of right now is an additional layer of containers/wrappers.
Essentialy:
Container width: 100%
Inner container: width: 25%;
Your object div padding-right: 5px; no width. It will be set automatically through the display: blockproperty, that a div has by default.
Here is the solution:
(Please note that sx.png is a 10x10 px transparent placeholder).
<div style="height: 160px; width: 100%; box-sizing:border-box; display:table;">
<div class="introwrapper" style="height:100%; width:25%;display:table-cell;">a</div>
<img src="../_imgs/sx.png" />
<div class="introwrapper" style="height:100%; width:25%;display:table-cell;">b</div>
<img src="../_imgs/sx.png" />
<div class="introwrapper" style="height:100%; width:25%;display:table-cell;">c</div>
<img src="../_imgs/sx.png" />
<div class="introwrapper" style="height:100%; width:25%;display:table-cell;">d</div>
</div>
I am attempting to create a visual element using DIV elements and CSS which should display data in the format demonstrated below.
[-----50%-----|--25%--|--25%--]
When using the code and CSS I've specified below, my final element always spills onto the next line and the CSS percentage values I'm specifying don't seem to create the layout properly.
Could anybody suggest a better way to do this?
My HTML
<div class="visual-indicator-title">
All Items</div>
<div class="visual-indicator-holder">
<div class="vi-internal-element" style="width: 25%; background-color: #5E9BD1;">
25%</div>
<div class="vi-internal-element" style="width: 25%; background-color: #AB884D;">
25%</div>
<div class="vi-internal-element" style="width: 50%;">
50%</div>
</div>
<div class="visual-legend">
<ul class="inline-block">
<li>
<div class="legend-blue">
</div>
Sales</li>
<li><span class="legend-tan"></span>Processed</li>
<li><span class="legend-grey"></span>Pending Processing</li>
</ul>
My CSS
.visual-indicator-title{
font-size:12px;
font-weight:bold;
color:#777777;
}
.visual-indicator-holder
{
width:100%;
background-color:#666666;
height:28px;
border-radius: 8px;
}
.visual-indicator-holder .vi-internal-element
{
font-size:11px;
text-align:center;
color:#ffffff;
background-color:#777777;
border-radius: 6px;
display:inline-block;
}
The reason this happens is that with inline or inline-block, white space in the element will affect the rendering (adds space). Here is your demo working with white space removed, no changes to the CSS: http://jsfiddle.net/fZXnU/
Removing white space is not trivial though, so you'd be better off floating the elements (which triggers display:block). Working demo with plenty of white space: http://jsfiddle.net/fZXnU/1/
You can use float: left, position: relative, and then define width in percentage as you are.
I modified your code to use float here: http://jsfiddle.net/Z3kdP/.
If you remove the white-space between the divs then it works as intended.
http://jsfiddle.net/TeJuU/
EDIT: See this question: How to remove the space between inline-block elements?
You can make font-size: 0 on the parent element if you don't want to edit your html.
http://jsfiddle.net/TeJuU/1/
All of those elements have margin and padding with them as well as the percentages creating rounding errors during calculation. So you need to make sure you set, or take into consideration, what margin is doing to this. For rounding errors, it's typical to let the percentages add up to something less than 100% but then add margin: auto to center the whole thing.
I am trying to shift my style away from using tables to control formatting, but I haven't seen a simple css solution that does exactly the same thing as
<table><tr><td>aribitrary-html-A</td><td>aribitrary-html-B</td></tr><table>
All I want is to make sure aribitrary-html-A and aribitrary-html-B are aligned horizontally. I have tried various CSS concoctions using display: inline, clear: none, and float: left but they all have unwanted side-effects of moving my content around, while the table-tr solution just does what I want, regardless of what's in the arbitrary HTML, and regardless of what is in HTML that contains my table.
Am I missing something?
Why not use a grid system then like 960gs
You probably need this
<div id="wrap">
<div id="left">
content 1
</div>
<div id="right">
content 1
</div>
</div>
#wrap {
width: 50%; /* change this as you wish */
}
#left, #right {
display: block;
float: left;
}
You need to use <div>s and decent CSS. For people that aren't confident enough (or lazy, like me) Yahoos YUI CSS Grid Builder is invaluable! Enjoy!
Maybe this might help you?
<div style="width:400px;margin:0 auto;">
<div style="background-color:red;float:left;height:200px;width:200px;">
</div>
<div style="background-color:blue;float:right;height:200px;width:200px;">
</div>
</div>
Just don't put your css within style attributes like I did.
Just create two Divs and align the one you want to have on the left side.
Like so (update):
<div id="wrap" style=" width:300px;
margin:auto;
border: 1px solid black;
padding:1px">
<div id ="A" style="float:left;
border: 1px solid black;"> aribitrary-html-A </div>
<div id = "B" style="border: 1px solid black;"> aribitrary-html-A</div>
</div>
Update: I added a wrapper with a defined width. Also I used some borders to visualize the table-like layout. Hope that helps.
Wrap element in a div, set fixed width for the wrapper. Give each element in the div a width and set the margin to 0 auto.
Just set the CSS "float" property to left on every element you want to display horizontally, and make sure each of those elements have a set width.
I'm slicing a psd, and there is a part of the screen that will repeat with as many items as it needs, similar to the question list of stackoverflow.
It needs to have this structure:
Is it possible? How should the css be?
Thanks!
You could try the following:
<style type="text/css">
#container {
width:60%;
}
#content {
width:100%;
}
#user-content {
float:left;
}
</style>
<div id="container">
<div id="content">
<div id="user-content">
<p>This can change depending on what is in here.</p>
</div>
<!-- The rest of the page's content goes here. -->
</div>
</div>
This makes the "content" div fill the rest of the space that "user-content" doesn't fill. It will only be an issue when your content is taller than the user content... but that's a different problem :)
This is another possiblity:
<style type="text/css">
#container {
width:60%;
}
#content {
width:100%;
float:left;
}
#user-content {
float:left;
}
#page-content {
float:left;
}
</style>
<div id="container">
<div id="content">
<div id="user-content">
<p>This can change depending on what is in here.</p>
</div>
<div id="page-content">
<p>This should take up the rest of the space.</p>
</div>
</div>
</div>
The problem lies in your left div where you state "width can increase depending on the content". How is this width defined? The div to the right can expand to 100% of the remaining space but you must define the relationship between the left and the right divs by either providing a fixed width to the left div or providing a percentage to both that equals 100%.
Well, as you’ve probably seen, so.com used fixed width div’s to achieve your layout goal.
Obviously my first tries setting the width automatically failed, but maybe I’ve a useful workaround for you: use left and right floating of both boxes.
<div style="border: 1px solid #000000; width: 60%">
<div style="border: 1px solid #444444; float: left;">
some text
</div>
<div style="border: 1px solid #999999; float: right;">
foo
</div>
</div>
Of course this will only help if I understood your question correctly ;)
As far as I know the only way to give your variable width container a variable width and float it to the left is to give it {width:auto;float:left;}
But I don't know if you can do anything useful with this because if you have text or a lot of small fixed width items to put in this container, they will keep expanding out along the first line until they've filled the width of the outer div before going on to the second line. They won't fill up the whole height and then push outward gradually as the text gets too much to contain.
Just a thought - you might be able to do some nifty JavaScript (possibly using jQuery?) which sizes those divs like you need them.