Why are elements in this CSS canceling each other out (in HTML without inheritance involved but with comments) that aren't related to each other?
Why are elements in this CSS canceling each other out (in HTML without inheritance involved but with comments) that aren't related to each other?
Using this class, some of the elements don't display. Which ones display depends on which order I put these elements into. The solution turned out to be that the wrong type of comment method was being used. Using HTML style comments within CSS style element, in an html file, creates all sorts of havoc.
I want all at the same time:
~ Text in a colored box,
~ Box centered on the page,
~ Text 120px from left side margin of the box
~ Tan colored border
~ Box 50% width of page
In the snippet, when the margin is positioned higher in the CSS list so that it works at centering the box, the border turns from tan to black!
The margin:auto is not working to center the box on the page in some of the orderings of CSS.
When the margin is placed later in css after the padding-left so that the border is tan, the 50% width turns to 100% width.
The padding-left stops working, when I have the margin working at centering and the width working at 50%.
I've moved them into so many orders, but I haven't spotted a pattern of what's cancelling out other things and why.
I can't figure out any keywords to search on to learn more of why this is happening, and 3www.school doesn't have an answer that I've found.
The CSS is
<style>
.highlightbox4
background-color:linen;
margin: auto;
font-weight:bold;
color:black;
padding:20px;
width:50%;
border-color:tan; <* use linen color instead? *>
border-style:solid; <* border didn't show up until style was added *>
border-width:5px;
padding-left:120px;
</style>
--
I also created it in the code-creator, but the size inside there doesn't give an idea of whether it's all working or not. (Turns out I didn't know you can run the snippet at the bottom of the question and see whether it's working.)
This snippet should have a brown border & a left margin of 120px. (Addition: The solution is that those are stopped by the html style in the snippet.)
.highlightbox4
background-color:linen;
margin: auto;
font-weight:bold;
color:black;
padding:20px;
width:50%; <* use linen color instead? *>
border-color:tan;
border-style:solid;
border-width:5px; <* another comment *>
padding-left:120px;
<!-- Not added yet box-shadow: 5px 10px; -->
<div class="highlightbox4">
Text that I'm trying to put into a linen colored box, <br> lined up 120px from it's left edge <br> and the box centered in the page <br> with a tan border that's a little thick.
</div>
padding-left: 120px
padding: 20px
padding-left: 20px
If padding-left is placed after padding in general, it would it be effective,? So last place should be fine for it? It's be wiped out only if it's placed before padding general?
– curls
Sep 12 '18 at 3:39
I updated the example. I had left off the comments since they seemed irrelevant and fluff. Turns out THEY were CAUSING the problem!! If you use html style comments in CSS, the CSS gets blocked, but only some lines of it, depending on where the comments are.
– curls
Sep 16 '18 at 15:47
2 Answers
2
Try adding:
display: table;
That will center
your div (because centering with margin: auto
only Works on fixed Width elements. display: table
eliminates this.
center
margin: auto
display: table
Edit:
You can find more examples on the great page:Css Centering Things
Thankyou! I thought center was downgraded and best to no longer use? The width is fixed at 50%. Is that not fixed enough (has to be in px)? I don't understand why a table is needed to center a box of text, and thought it was considered best practice to not use tables when it's not a data table?
– curls
Sep 12 '18 at 3:37
You're not using a table(html), you just specify that it should display as a table(CSS).
– Poul Bak
Sep 12 '18 at 3:39
It's just text in html, no tables. Then the CSS is creating the box of margins, padding & borders around the text.
– curls
Sep 12 '18 at 3:41
@curls the reason for not using the HTML
<table>
element for data that isn't tabular does not apply to using the table
value for the display
CSS property. CSS properties do not affect HTML semantics, so you can use any CSS on whatever element you like without breaking semantics.– ESR
Sep 12 '18 at 4:01
<table>
table
display
Oh, that display table may open up a lot of options for me. Specifically when displaying Form inputs. But in this case the CSS should work. I want to understand what's conflicting about it. This just text in a box. It should be do-able with a few straigthforward CSS attributes.
– curls
Sep 12 '18 at 4:53
The problem was use of an html comment, instead of the css comment method, inside the css style element.
So for instance the attributes here had this comment on them:
border-color:tan;
border-style:solid; <!-- this is a comment that should be /* */ style instead -->
border-width:5px;
-
The html-style comment was canceling out or confounding properties entered after the comment. Though not all attributes were being canceled out.
Changing those comments to /* which is css's comment style, caused the tags to work correctly.
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Your code works absolutely fine -- it creates a tan border and adds the right padding. The
padding-left: 120px
gets overwritten (when it's the last rule) becausepadding: 20px
is shorthand for four rules which includepadding-left: 20px
. Please ensure you have provided a a minimal, complete, and verifiable example of the problem, clearly explaining exactly what you're expecting to happen, and what is currently happening.– Obsidian Age
Sep 12 '18 at 3:20