Using <fieldset> Tag within <div> Tag

I’m trying to format an area on a screen with a border and an identifier within the border. I think <fieldset> is the correct tag to use. If there is another tag that does this, I haven’t learned of it (yet?). I will have multiple such areas on a screen that have different size and positioning attributes; so I think I need to used <fieldset> within multiple <div> tags.

I searched online trying to find out if <div> and <fieldset can be used this way. I discovered that it’s OK to use <fieldset> tags within other fieldset tags but couldn’t find an answer about using <fieldset> within <div>. So far, I’ve been unable to do this; but there might be other errors I’ve made covering up my issue. CSS and HTML are shown below. While I show only one set of HTML (for birth information) there will be others for marriage, death, biography, children, etc. Those areas onscreen will be in different locations with different width/height attributes.

CSS:

fieldset {
  border: 2px solid #000;
  padding: 10px;
  margin: 10px;
}

/*   The following element is declared to define display as 'grid'   */
.personal-pages-container    
{
  display: grid;
  gap: 8px 50px;
  grid-template-columns: 12;
  grid-template-rows: 6; 
}

/*  The following element occupies row  column 2 of the grid   */
.birth-details-container
{ 
grid-area: birth-details; 
grid-row: 1;
grid-column: 2;
line-height: .95;
width: 360px;
height: 65px;
}

/*   Other containers for marriage, death, children, biography, etc follow.  */

HTML:

        <fieldset>  
          <legend>Birth</legend>
         Mother:<br>
	Father:<br>
	Place:<br>
	Siblings:<br>
        </fieldset>
</div>    <!--  END of .birth-details-container  -->
<!--   More HTML for other containers for marriage, death, children, biography, etc. to follow.  -->

Fieldsets are typically only used with input fields (and labels), but you can mimic the look relatively easily

I used a definition list, but I’m sure there are other semantic ways to approach it:

1 Like

Thanks for the information, Dave.

I’m afraid that I don’t know enough to understand the CSS you used. I will have to do some homework to figure out position: absolute and why the definition list elements weren’t preceded with a period, for example.

How did you get the title to appear in the border? Is that what the position element does?

FWIW: I have the result I wanted using div and fieldset but I’m still working on getting the containers positioned where i want them onscreen. I’m learning more about grids.

Thanks again.

Yes. Position:absolute gives you more “control” over the placement of an object via css. The reason for the position:relative on the .content class is to make the position:absolute be based on the positioning of the parent instead of the body of the document

Position: absolute does it’s positioning using the top/bottom/left/right instead of the margins (margin-left/margin-inline-start)

Glad you got what you wanted. I did a different way since I didn’t think the fieldset was semantically correct.

2 Likes

Don’t make the mistake of using html tags just for the way they look. Css doesn’t really care what they look like as in most cases you can style something to suit.

What is most important though is that html is a semantic language and the tags you use must be the right ones for the task in hand.

As Dave mentioned a fieldset is for grouping form elements and labels. It is not a fancy container for non form content. Therefore for your case it is incorrect to use. It is valid html but it is not semantically correct and will confuse screen readers and the like that will expect some sort of form action.

Also divs are just generic containers with no real semantics and should rarely hold solo content unless there is no more semantic element to use. For example a paragraph should use a p element and a heading use a heading element and so on. A div is usually a container for a group of semantic items or a wrapper that applies little semantic meaning.

An html document should make semantic sense on its own with logical headings and an html structure that reflects the content.

In the end you could use any old tag you want but there’s always a right way and a wrong way :slight_smile:

3 Likes

As before, I celebrate my ignorance and inexperience with every post!

I have taken Dave’s post to heart and continue to study it’s content and usage in order to learn more. When I have taken that information aboard, I’ll replace the fieldset with better code.

“A div is usually a container for a group of semantic items or a wrapper that applies little semantic meaning.”

This statement confuses me a bit. I don’t understand your use of the term ‘semantic’ in this context.

<div id='wrapper'>
	<header>
		<img src='sunset.jpg'>
		<nav>
			<ul>
				<li>Home</li>
				<li>About</li>
			</ul>
		</nav>
	</header>
	<p>Welcome to the site</p>
</div>

Without seeing any CSS, you can get a good idea of what’s going on inside this div, because the tags are semantic (header, navigation, etc). The div, even though it’s called “wrapper”, is just containing some stuff and providing a grouping.

2 Likes

Thanks. That helps. I have perhaps over-declared in my code so that the specifics of word choice define what the styles apply to. I think this is how ‘semantics’ applies here.

First, thanks to everyone who has responded to my posts, especially Dave Maxwell and PaulOB.

In trying to take aboard Dave Maxwell’s wisdom and guidance, I have incorporated what I think is the gist of it. I added his CSS and HTML to my code but tweaked it in a few places to suit my needs, allowing me to demonstrate to myself that I understand what he’s telling me.

In the screenshot below, the “Birth” container is all based on his lesson for me while the “Biography” container still uses div and fieldset and legend for now. I was able to suss out which statements affected the border, title, etc. and tweaked his code to make my definition list appear as I want it. I had not used definition lists before and find the information quite helpful in presenting the content. That’s a real bonus that I would not have know how to ask about.

I saw that Dave’s code gave the width as a percentage, but when I tried to follow his precept by declaring a height: 15%; attribute, nothing happened. I was able to make the container the width I wanted by changing Dave’s width: 40%; to 65% as shown in the screenshot. I made other tweaks to get the results shown; but a major question remains for me: How do I control the vertical height of the container for “Birth”?

I’m still struggling with position: xxxxx;. That might be why I haven’t got the container vertical dimension to my liking.

In the screen shot and looking ahead to results, the container at the left will ultimately be a square in which a photo will be placed. The middle of the display where “Birth” appears will be a stack of 3 similar containers to display birth, marriage, and death information. The container at the right, “Biography” will remain but will not be those dimensions. Finally there will be 2 containers below all that for names of children and a place for thumbnails linking to other pages. Those last 2 containers will be of equal width but with their own height and centered across the screen, spanning below the photo and other containers above them.

This might all be horrible aesthetic design, but having a design in mind drives the learning process for me.

Again, thank you to all who have responded.

Yes, that’s true. Keep in mind though, that while it may not be relevant to your particular web site, things like screen readers for blind people do specific things with specific html tags. So for them,

<nav>
... blah blah
</nav>

has a very meaning from

<div id='navigation'>
... blah blah
</div>
2 Likes

So, to see if I understand you, if I were to code my home page menu as

<div class="main-menu">
          <ul class="no-bullets">
            <li>
              <a href="index.html">Home</a><br>
              <a href="Phyllis-Scrapbook.html">Phyllis' Scrapbook</a><br>
              <a href="personal-pages.html">Personal Pages</a><br>
              <a href="hutchins-line.html">Hutchins Surname Line</a>  <br>
            </li>  
          </ul>
        </div>  <!--      END of .main-menu   -->

. . . it would not be clear that this is a menu to browsers but if I used:

<nav class="main-menu"> 
          <ul class="no-bullets">
            <li>
              <a href="index.html">Home</a><br>
              <a href="Phyllis-Scrapbook.html">Phyllis' Scrapbook</a><br>
              <a href="personal-pages.html">Personal Pages</a><br>
              <a href="hutchins-line.html">Hutchins Surname Line</a>  <br>
              <!--<a href="baker-line.html">Baker Surname Line</a>  <br>
              <a href="histories.html">Histories</a>  <br>
              <a href="anecdotes.html">Anecdotes</a>  <br>
              <a href="submissions.html">Submissions</a>  <br>
              <a href="search.html">Search</a>  <br>
              <a href="help.html">Help</a>  <br>-->
            </li>  
          </ul>
        </nav>  <!--      END of .main-menu  -->

. . . browsers would know that it is a menu?

I modified my HTML replacing <div> . . . </div> with <nav> . . .</nav> and the menu functions correctly.

So much to learn; so little time. :roll_eyes:

2 Likes

Correct, though not all browsers, just screen readers used by blind people. If it’s not relevant to your “customer base”, then this is just a hypothetical situation for you. I don’t believe there are any negative consequences for sighted-people if you made the swap you list here for navigation or any of the other “replace a div with a more semantic tag” situations.

2 Likes

Thanks, tracknut. I don’t think my audience includes anyone using screen readers but it won’t hurt; so I’ll incorporate the information in the final product.

1 Like

Yes I’m glad you spotted that. That’s the whole point of using semantic tags. You get a certain amount of baked in accessibility for little effort. It’s not just for screen readers either.

Semantic tags also enhance keyboard navigation, making it easier for users to navigate the page without using a mouse. This is especially true for old people like us with arthritis and struggle to hold a mouse.

You also get a benefit to SEO as search engines can understand your content more easily.

At some point in a persons life there is a good chance that they won’t be fully able and then accessibility features take on greater importance.

For professional designers accessibility is a priority these days as there are indeed laws and regulations that promote accessibility for all. Adhering to accessibility laws and guidelines ensures that websites are inclusive for all users, including those with disabilities, and is also considered an ethical responsibility. (I copied that last bit from somewhere else :))

Of course for small family sites like yours you can do what you like and no one is going to worry but with a few tweaks in structure you suddenly have something that works much better for all. :slight_smile:

Lots to take in i know:).

In my initial review of your code I mentioned to you that we rarely will set a height on elements that contain fluid content. Its just not a viable method unless it contains perhaps a fixed size image but even then there is no need as the image controls the height anyway. You should let content dictate the height and not your perception of a ‘design look’. If the reason the for the height is to match the height of something else then flex an grid can do that automaticallt when set up correctly without requiring any height dimensions at all.

Again this is a common beginners misunderstanding of the rules of css (there are always rules ;)). A percentage height on block elements refers to the height of the parent assuming that parent has a height defined other than auto. If the parent height is also a percentage then the same rules applies and every parent of that element must have a height defined other than auto all the way back to the root element. If there is no parent with a height that can be resolved then all become height:auto and nothing happens. That’s why height:15% doesn’t work unless all the criteria I just mentioned is in place. Even if it did work then it would blow up the design once the text wrapped to a new line in a responsive site and required more height than 15% or indeed if I increased text size in my browser as mentioned before. In most cases you will never need to use a percentage height barring the following caveats.

Flex and grid will allow the use of percentage heights on flex items because flex and grid have special properties and know how tall an element has to be to match other elements and also that it will allow the element to grow because all the elements in the row will increase so a 100% is never exceeded. However, you still will seldom need that as flex/ does match heights automatically if you set it up from the start.

So to recap (excluding grid and flex) if you need to use a percentage height then all the parents of that element also have to have a height defined other than auto. if a parent has a fixed height (px em etc) then that allows the direct child to have that height available as a percentage. If the parent is a percentage height then an unbroken chain of percentage heights must follow all the way up to the root element. That’s why you will see this rule in older sites.

html,body{
height:100%;
margin:0;
padding;0;
}

That will allow a direct child of the body to utilise a 100% percentage height (full viewport height). (Notice how the default margin are removed because if they were in place then 100% would be taller than the viewport). Once again there is a gotcha in that your page could never grow below the fold because you said height:100% only. Anything that is taller than the viewport will overflow. What you would need is min-height:100% instead. However once you use min-height on that parent you can no longer use height:100% on a child element because min-height does not allow any height to be passed to a child element as its height is really unknown because it is a minimum.

Suffice to say forget about percentage heights and do something else :slight_smile:

Because of the other requirements a new unit (actually a whole set of units but I won’t explain all) was developed just a few years ago to tackle these issues. The ‘vh’ unit allows developers to access the viewport height (vh) from whatever (block display) element they choose wherever that element may be. This avoids a lot of the problems mentioned above and you can use min-height:100vh also but remember that if an element is half way down the page and you give it a min-height: of 100vh (full viewport height) then that will stretch the element below the fold and not just to the bottom.

This is a complicated subject but your main take should be to avoid setting any fixed or percentage heights on content that is fluid like text.:wink:

1 Like

Thanks, again, Paul.

I’ve chewed on the information and made some changes. You were right about the problem being a parent container with a height declaration. I went back and wrote down the ‘hierarchy’. For now, I left the CSS for my screen background with a fixed height so as not to introduce too many variables. I then set height: auto; for the parent container (within my fixed height background) and removed all height declarations from child containers. I now see the containers wrapping around the text within them, for the most part. I still have some issues to track down but you set me on the right path. Be forewarned, though; I’m likely to be back with more questions.

Thank you.

Back with more questions.

Why don’t my layout elements align as intended? I used a grid for the parent screen and grid-row-start and grid-column-start for each child screen. That worked for the first 2 elements (.photo-container and .birth-details-container) but not the others. I have spent hours searching CSS and HTML and cannot find the answer. Have I misunderstood how grid coordinates work? If so, why do the first 2 work as intended but not the others.

The first screenshot shows the concept of my screen. The second one shows the results I have at present. It doesn’t show the whole result but you should be able to see the bits I have questions about.

Because some of the containers defined below share common attributes, I defined them as a group then added individual classes for each of them to define their grid coordinates and unique attributes. Is this acceptable? I wonder if it worked because I didn’t get the results intended.

I incorporated Dave’s suggestions to fit my needs. I think all the containers wrap around the text as intended and titles appear as intended but there are other problems.

  1. The .bio-container does not appear not at grid row, 1 column 3, as defined; its width (set at 30%) looks too wide. I’ve looked until I cannot see the code with fresh eyes. I looked for missing punctuation, etc in addition to details about placement. Can anyone point to my errors?

  2. The .marriage-container and .death-container do not appear at grid rows 2 and 3, column 2, as defined. Those 3 containers should be aligned one above the other as indicated in the layout screenshot. What have I done wrong?

  3. The background color is correct for the first 2 containers (photo and birth details) but disappeared on the other containers. I think I did something to interfere with cascading inheritance but I can’t spot the error. Anyone?

.personal-pages-container    
{
  display: grid;
  gap: 5px 50px;
  grid-template-columns: 3;
  grid-template-rows: 6; 
  font-size: 2.25rem;
  font-weight: 500;
}

.personal-pages-container > div
{
  background-color: rgba(255, 255, 255, 0.8);
  text-align: left;
  height: auto;
  font-size: 1rem;
  line-height: .95;
}

.photo-container
{ 
grid-row: 1;
grid-column: 1;
width: 100%;
margin-left: 30px;
border-style: ridge; 
border-width: 2.5px;
border-radius: 5px;
padding: 2px;  /*   all sides   */
object-fit: contain;
}

.birth-details-container,
.marriage-details-container,
.death-details-container,
.children-container,
.bio-container,
.thumbnails-container
{    
  position: relative;
  border: 1px solid black;
  border-radius: 5px;
  width: 50%;
  margin-inline-start: 2rem;
  padding: .1rem .5rem;  /*   .1 is top/bottom; .5 is right/left  */
  line-height: .95rem; 
}
.birth-details-container
{ 
  grid-row-start: 1;
  grid-column-start: 2;    
}

.bio-container  
{ 
  grid-row-start: 1;
  grid-column-start: 3; 
  width: 30%;
  margin-right: auto;
  margin-left: auto;
}
.marriage-details-container
{ 
  grid-row-start: 2;
  grid-column-start: 2;    
}

.death-details-container
{ 
  grid-row-start: 3;
  grid-column-start: 2; 
}

.children-container
{ 
  grid-row-start: 4;
  grid-column-start: 1;  
  width: 80%;
  margin-right: auto;
  margin-left: auto;
}

.thumbnails-container
{ 
  grid-row-start: 5;
  grid-column-start: 1;  
  width: 80%;
  margin-right: auto;
  margin-left: auto;
  line-height: 2rem;
}

If I understand the cascading effect and inheritance, those the parent element (.personal-pages-container) attributes are inherited by the child elements (.birth-details-container, .marriage-details-container, .death-details-container, .children-container, .bio-container, .thumbnails-container).

Here’s the HTML with content removed for brevity:

<div class="background">
    <h2 class="page-heading">The Hutchins Clan</h2>
      <h3 class="page-subheading">Charles Haddon Spurgeon Hutchins<br>"Daddy" - "Pa"</h3> 
    <div class="personal-pages-container">
      <div class="photo-container">
      </div>
      <div class="birth-details-container"> <!--  Dave's .container  -->
        <div class="title">Birth</div> <!-- from Dave -->
        <dl class="content">
         
        </dl> 
        </div>    <!--  END of .content -->
      </div>    <!--  END of .birth-details-container / Dave's .container  -->

      <div class="marriage-details-container">
        <div class="title">Marriage</div> <!-- from Dave -->
        <dl class="content">
          
        </dl> 
      </div>    <!--  END of .marriage-details-container / Dave's .container  -->

      <div class="death-details-container">
        <div class="title">Death</div> <!-- from Dave -->
         <dl class="content">
          
        </dl> 
      </div>    <!--  END of .death-details-container / Dave's .container  -->

      <div class="children-container">
        <div class="title">Children</div> <!-- from Dave -->
        <div class="content"> 
        
        </div>  <!--    END of .content   -->
      </div>    <!--  END of .children-container / Dave's .container  -->

       <div class="bio-container">
        <div class="title">Biography</div> <!-- from Dave -->
         <div class="content">
    
    </div>
</div>
   <div class="thumbnails-container">
        <div class="title">Thumbnails</div> <!-- from Dave -->
        <div class="content">
        </div>    <!--  END of .thumbnails-container  -->
    </div>  <!--    END of .personal-pages-container    -->
</div>  <!--    END of .background    -->
 <!--   PP 2-->


Your main mistake is here:

</div> <!-- END of .content -->

That is not the end of .content and so throws the whole structure out.

The second mistake is that you have invalid values for your grid-template-columns and rows. It need to be described properly. e.g. for 3 equal width columns and 5 rows you would do this:

  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(5, 1fr);

That says I want 3 across and the 1fr means they all take the same amount of space. No needs for width or heights on the children at all.

Your third mistake is that you have specified 6 rows when I can only see 5 but in actuality you only have 3 rows . The middle column of the first row should itself be divided into 3 rows and that keeps it automatically in track with the first and 4th columns.

Your 4th mistake is that you didn’t span the the 5th and 6th rows over all 3 columns and so they sit in the first column.

Your 5th mistake is that columns 5 and 6 don’t really need to be in the grid at all! Why make things complicated. They are just a normal width element and you don’t need to do anything to get them there.

Your 6th mistake is that you overstate the widths of everything and if you think logically how can an element that is width:100% wide fit in its parent when you the add a margin to it. .. and then you add padding and borders to it so nothing adds up or makes sense. You need to study the css box model (which really is lesson one) and in most cases you would set the default border-box model on everything to begin with so that padding and borders don’t increase the size but are contained within the element rather than added to it.

I always use this as the very start of my reset stylesheet.

html {
  box-sizing: border-box;
}
*,
*:before,
*:after {
  box-sizing: inherit;
}

I would recommend you think about the width of your content. At the moment on a large imac screen the content goes from edge to edge and any text is going to be so long on one line that it will be very difficult to scan and read. As I mentioned before only you have your setup. Everyone else has something different so don’t base things on what your screen looks like but think of all screens.

For your case I would contain the whole page in a wrapper and set a max–width of around 1480px (or thereabouts) and use auto margins to center the whole thing. Remember some people will be viewing on devices like the imac which is 2500px wide and also very tall.
Lastly, I assume that photo-container doesn’t have to match the height of anything but that you want columns 2 and 3 to always match each other in height.

Give me a few minutes and I’ll do a simplified demo of that structure with how I think you wanted it.:slight_smile:

1 Like

Here’s roughly what I think you were after.


(Click Edit on codepen to see full width effect)

I made a lot of assumptions but you should get the picture. No heights needed and no widths really needed.

(I did change Dave’s overlapping legend effect because it doesn’t allow for a transparent background but is a bit ‘magic numberish’ so changes will need to be matched to keep the effect.)

2 Likes

Again, thank you, Paul!!

You’ve given me much to chew on and learn about.

In my post above I asked about chaining class names together and defining attributes they share only once, like this;

.birth-details-container,
.marriage-details-container,
.death-details-container,
.children-container,
.bio-container,
.thumbnails-container
{    
  position: relative;
  border: 1px solid black;
  border-radius: 5px;
  width: 50%;
  margin-inline-start: 2rem;
  padding: .1rem .5rem;  /*   .1 is top/bottom; .5 is right/left  */
  line-height: .95rem; 
}

While my code may be elementary and better code probably exists, it’s the principle of stacking definintions that I want to know about. Is this good practice?

Thanks, again. As ever, be forewarned that I might have questions about this lesson. :roll_eyes:

That is starting to get unwieldly and as all those are the same you could do something like this instead.

.box {  /* common styles go here for these boxes */   
  position: relative;
  border: 1px solid black;
  border-radius: 5px;
  width: 50%;
  margin-inline-start: 2rem;
  padding: .1rem .5rem;  /*   .1 is top/bottom; .5 is right/left  */
  line-height: .95rem; 
}

Then in your html you would have:


<div class="box birth-details-container"> ...</div>

<div class="box marriage-details-container"> ...</div>

etc...

If you need modifications for each box then you just do this.

.birth-details-container {
  /* Modifications go here if needed */
}
.marriage-details-container {
  /* Modifications go here if needed */
}

If you look at my example there are no long chains of styles and if you find yourself comma separating more than a couple of items them most likely you are doing it wrong :wink:

1 Like